2 * Client-side support functions.
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
40 #include <libcitadel.h>
42 #include "citadel_ipc.h"
45 #ifndef HAVE_GETUTLINE
46 struct utmp *getutline(struct utmp *ut);
54 #include "citadel_decls.h"
55 #include "routines2.h"
57 #define IFAIDE if(axlevel>=AxAideU)
58 #define IFNAIDE if (axlevel<AxAideU)
60 extern unsigned userflags;
61 //extern char *axdefs[8];
62 extern char sigcaught;
63 extern char rc_floor_mode;
64 extern int rc_ansi_color;
65 extern int rc_prompt_control;
67 /* Destructive backspace */
68 void back(int spaces) {
70 for (a=0; a<spaces; ++a) {
77 void hit_any_key(CtdlIPC *ipc) { /* hit any key to continue */
82 scr_printf("%s\r", ipc->ServInfo.moreprompt);
86 for (a=0; !IsEmptyStr(&ipc->ServInfo.moreprompt[a]); ++a)
90 if ( (rc_prompt_control == 1)
91 || ((rc_prompt_control == 3) && (userflags & US_PROMPTCTL)) ) {
92 if (b == 'q' || b == 'Q' || b == 's' || b == 'S')
94 if (b == 'n' || b == 'N')
97 if (b==NEXT_KEY) sigcaught = SIGINT;
98 if (b==STOP_KEY) sigcaught = SIGQUIT;
102 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
104 void edituser(CtdlIPC *ipc, int cmd)
107 char who[USERNAME_SIZE];
108 char newname[USERNAME_SIZE];
109 struct ctdluser *user = NULL;
111 int r; /* IPC response code */
116 newprompt("User name: ", who, 29);
117 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
118 scr_printf("%s\n", buf);
120 scr_printf("Do you want to create this user? ");
122 r = CtdlIPCCreateUser(ipc, who, 0, buf);
127 scr_printf("%s\n", buf);
135 val_user(ipc, user->fullname, 0); /* Display registration */
139 while (change_name == 1) {
140 if (boolprompt("Change name", 0)) {
141 strprompt("New name", newname, USERNAME_SIZE-1);
142 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
144 scr_printf("%s\n", buf);
147 strcpy(user->fullname, newname);
157 if (newnow || boolprompt("Change password", 0)) {
158 strprompt("Password", user->password, -19);
161 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
162 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET)))
163 user->flags |= US_INTERNET;
165 user->flags &= ~US_INTERNET;
166 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
167 user->flags &= ~US_REGIS;
169 user->flags |= US_REGIS;
170 user->timescalled = intprompt("Times called",
171 user->timescalled, 0, INT_MAX);
172 user->posted = intprompt("Messages posted",
173 user->posted, 0, INT_MAX);
174 user->lastcall = boolprompt("Set last call to now", 0) ?
175 time(NULL) : user->lastcall;
176 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
177 user->USuserpurge, 0, INT_MAX);
181 scr_printf("Do you want to delete this user? ");
186 user->axlevel = AxDeleted;
189 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
191 scr_printf("%s\n", buf);
197 /* Display a prompt and flip a bit based on whether the user answers
198 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
199 * in which case No=1 and Yes=0.
201 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
208 scr_printf("%50s ", prompt);
211 color(BRIGHT_MAGENTA);
214 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
217 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
225 if (backwards) a = 1 - a;
227 if (backwards) a = 1 - a;
230 if (!a) temp = (temp^sbit);
235 * modes are: 0 - .EC command, 1 - .EC for new user,
236 * 2 - toggle Xpert mode 3 - toggle floor mode
238 void enter_config(CtdlIPC *ipc, int mode)
241 struct ctdluser *user = NULL;
242 int r; /* IPC response code */
244 r = CtdlIPCGetConfig(ipc, &user, buf);
246 scr_printf("%s\n", buf);
251 if (mode == 0 || mode == 1) {
253 /* Does anyone still use dialup connections with manual
254 * screen dimensions setting anymore? For now we'll keep
255 * the system's ability to set these, but remove the prompts
256 * because they're spurious for nearly everyone.
258 user->USscreenwidth = intprompt("Enter your screen width",
259 user->USscreenwidth, 20, 255);
260 user->USscreenheight = intprompt("Enter your screen height",
261 user->USscreenheight, 3, 255);
264 user->flags = set_attr(ipc, user->flags,
265 "Are you an experienced Citadel user",
267 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
272 user->flags = set_attr(ipc, user->flags,
273 "Print last old message on New message request",
276 user->flags = set_attr(ipc, user->flags,
277 "Prompt after each message",
280 if ((user->flags & US_NOPROMPT) == 0)
281 user->flags = set_attr(ipc, user->flags,
282 "Use 'disappearing' prompts",
285 user->flags = set_attr(ipc, user->flags,
286 "Pause after each screenful of text",
289 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR))
290 user->flags = set_attr(ipc, user->flags,
291 "<N>ext and <S>top work at paginator prompt",
294 if (rc_floor_mode == RC_DEFAULT)
295 user->flags = set_attr(ipc, user->flags,
296 "View rooms by floor",
299 if (rc_ansi_color == 3)
300 user->flags = set_attr(ipc, user->flags,
301 "Enable color support",
304 if ((user->flags & US_EXPERT) == 0)
305 formout(ipc, "unlisted");
307 user->flags = set_attr(ipc, user->flags,
308 "Be unlisted in userlog",
311 if (!IsEmptyStr(editor_paths[0])) {
312 user->flags = set_attr(ipc, user->flags,
313 "Always enter messages with the full-screen editor",
320 if (user->flags & US_EXPERT) {
321 user->flags ^= US_EXPERT;
322 scr_printf("Expert mode now OFF\n");
324 user->flags |= US_EXPERT;
325 scr_printf("Expert mode now ON\n");
330 if (user->flags & US_FLOORS) {
331 user->flags ^= US_FLOORS;
332 scr_printf("Floor mode now OFF\n");
334 user->flags |= US_FLOORS;
335 scr_printf("Floor mode now ON\n");
339 r = CtdlIPCSetConfig(ipc, user, buf);
340 if (r / 100 != 2) scr_printf("%s\n", buf);
341 userflags = user->flags;
346 * getstring() - get a line of text from a file
347 * ignores lines beginning with "#"
349 int getstring(FILE *fp, char *string)
364 } while(string[0]=='#');
365 return(strlen(string));
369 /* Searches for patn in search string */
370 int pattern(char *search, char *patn) {
374 for (a=0; !IsEmptyStr(&search[a]); ++a) {
375 b=strncasecmp(&search[a],patn,len);
382 void strproc(char *string)
386 if (IsEmptyStr(string)) return;
388 /* Convert non-printable characters to blanks */
389 for (a=0; !IsEmptyStr(&string[a]); ++a) {
390 if (string[a]<32) string[a]=32;
391 if (string[a]>126) string[a]=32;
394 /* Remove leading and trailing blanks */
395 while(string[0]<33) strcpy(string,&string[1]);
396 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
398 /* Remove double blanks */
399 for (a=0; a<strlen(string); ++a) {
400 if ((string[a]==32)&&(string[a+1]==32)) {
401 strcpy(&string[a],&string[a+1]);
406 /* remove characters which would interfere with the network */
407 for (a=0; a<strlen(string); ++a) {
408 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
409 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
410 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
411 if (string[a]==',') strcpy(&string[a],&string[a+1]);
412 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
413 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
419 #ifndef HAVE_STRERROR
421 * replacement strerror() for systems that don't have it
423 char *strerror(int e)
425 static char buf[128];
427 snprintf(buf, sizeof buf, "errno = %d",e);
433 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
436 "**************************************************";
437 char dots_printed[51];
442 scr_printf("\r%79s\r","");
444 /* a will be range 0-50 rather than 0-100 */
445 a=(curr * 50) / cmax;
446 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
447 strncpy(dots_printed, dots, a);
449 scr_printf(fmt, dots_printed, "",
450 curr * 100 / cmax, curr, cmax);
457 * NOT the same locate_host() in locate_host.c. This one just does a
458 * 'who am i' to try to discover where the user is...
460 void locate_host(CtdlIPC* ipc, char *hbuf)
467 who = (FILE *)popen("who am i","r");
469 strcpy(hbuf, ipc->ServInfo.fqdn);
472 fgets(buf,sizeof buf,who);
476 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
477 if ((buf[a]=='(')||(buf[a]==')')) ++b;
480 strcpy(hbuf, ipc->ServInfo.fqdn);
484 for (a=0; a<strlen(buf); ++a) {
486 strcpy(buf,&buf[a+1]);
489 for (a=0; a<strlen(buf); ++a) {
490 if (buf[a]==')') buf[a] = 0;
493 if (IsEmptyStr(buf)) strcpy(hbuf, ipc->ServInfo.fqdn);
494 else strncpy(hbuf,buf,24);
496 char *tty = ttyname(0);
497 #ifdef HAVE_GETUTXLINE
498 struct utmpx ut, *put;
500 struct utmp ut, *put;
505 safestrncpy(hbuf, ipc->ServInfo.fqdn, 24);
509 if (strncmp(tty, "/dev/", 5))
512 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
514 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
515 if ((put = getutxline(&ut)) == NULL)
517 if ((put = getutline(&ut)) == NULL)
521 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
522 if (put->ut_type == USER_PROCESS) {
524 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
526 safestrncpy(hbuf, put->ut_host, 24);
529 safestrncpy(hbuf, put->ut_line, 24);
530 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
534 #endif /* HAVE_UTMP_H */
538 * miscellaneous server commands (testing, etc.)
540 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
543 CtdlIPC_chat_send(ipc, cmd);
544 CtdlIPC_chat_recv(ipc, buf);
545 scr_printf("%s\n",buf);
547 set_keepalives(KA_HALF);
548 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
549 scr_printf("%s\n",buf);
551 set_keepalives(KA_YES);
556 newprompt("> ",buf,255);
557 CtdlIPC_chat_send(ipc, buf);
558 } while(strcmp(buf,"000"));
565 * compute the checksum of a file
567 int file_checksum(char *filename)
573 fp = fopen(filename,"r");
574 if (fp == NULL) return(0);
576 /* yes, this algorithm may allow cksum to overflow, but that's ok
577 * as long as it overflows consistently, which it will.
579 while (ch=getc(fp), ch>=0) {
580 cksum = (cksum + ch);
588 * nuke a directory and its contents
590 int nukedir(char *dirname)
596 dp = opendir(dirname);
601 while (d = readdir(dp), d != NULL) {
602 snprintf(filename, sizeof filename, "%s/%s",
608 return(rmdir(dirname));