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"
56 #include "tuiconfig.h"
58 #define IFAIDE if(axlevel>=AxAideU)
59 #define IFNAIDE if (axlevel<AxAideU)
61 extern unsigned userflags;
62 //extern char *axdefs[8];
63 extern char sigcaught;
64 extern char rc_floor_mode;
65 extern int rc_ansi_color;
66 extern int rc_prompt_control;
68 /* Destructive backspace */
69 void back(int spaces) {
71 for (a=0; a<spaces; ++a) {
79 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
81 void edituser(CtdlIPC *ipc, int cmd)
84 char who[USERNAME_SIZE];
85 char newname[USERNAME_SIZE];
86 struct ctdluser *user = NULL;
88 int r; /* IPC response code */
93 newprompt("User name: ", who, 29);
94 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
95 scr_printf("%s\n", buf);
97 scr_printf("Do you want to create this user? ");
99 r = CtdlIPCCreateUser(ipc, who, 0, buf);
104 scr_printf("%s\n", buf);
112 val_user(ipc, user->fullname, 0); /* Display registration */
116 while (change_name == 1) {
117 if (boolprompt("Change name", 0)) {
118 strprompt("New name", newname, USERNAME_SIZE-1);
119 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
121 scr_printf("%s\n", buf);
124 strcpy(user->fullname, newname);
134 if (newnow || boolprompt("Change password", 0)) {
135 strprompt("Password", user->password, -19);
138 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
139 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET)))
140 user->flags |= US_INTERNET;
142 user->flags &= ~US_INTERNET;
143 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
144 user->flags &= ~US_REGIS;
146 user->flags |= US_REGIS;
147 user->timescalled = intprompt("Times called",
148 user->timescalled, 0, INT_MAX);
149 user->posted = intprompt("Messages posted",
150 user->posted, 0, INT_MAX);
151 user->lastcall = boolprompt("Set last call to now", 0) ?
152 time(NULL) : user->lastcall;
153 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
154 user->USuserpurge, 0, INT_MAX);
158 scr_printf("Do you want to delete this user? ");
163 user->axlevel = AxDeleted;
166 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
168 scr_printf("%s\n", buf);
174 /* Display a prompt and flip a bit based on whether the user answers
175 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
176 * in which case No=1 and Yes=0.
178 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
185 scr_printf("%50s ", prompt);
188 color(BRIGHT_MAGENTA);
191 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
194 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
202 if (backwards) a = 1 - a;
204 if (backwards) a = 1 - a;
207 if (!a) temp = (temp^sbit);
212 * modes are: 0 - .EC command, 1 - .EC for new user,
213 * 2 - toggle Xpert mode 3 - toggle floor mode
215 void enter_config(CtdlIPC *ipc, int mode)
218 struct ctdluser *user = NULL;
219 int r; /* IPC response code */
221 r = CtdlIPCGetConfig(ipc, &user, buf);
223 scr_printf("%s\n", buf);
228 if (mode == 0 || mode == 1) {
230 user->flags = set_attr(ipc, user->flags,
231 "Are you an experienced Citadel user",
233 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
238 user->flags = set_attr(
241 "Print last old message on New message request",
246 user->flags = set_attr(
249 "Prompt after each message",
254 if ((user->flags & US_NOPROMPT) == 0) {
255 user->flags = set_attr(
258 "Use 'disappearing' prompts",
264 user->flags = set_attr(
267 "Pause after each screenful of text",
272 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR)) {
273 user->flags = set_attr(
276 "<N>ext and <S>top work at paginator prompt",
282 if (rc_floor_mode == RC_DEFAULT) {
283 user->flags = set_attr(
286 "View rooms by floor",
292 if (rc_ansi_color == 3) {
293 user->flags = set_attr(
296 "Enable color support",
302 if ((user->flags & US_EXPERT) == 0) {
303 formout(ipc, "unlisted");
306 user->flags = set_attr(
309 "Be unlisted in userlog",
314 if (!IsEmptyStr(editor_path)) {
315 user->flags = set_attr(
318 "Always enter messages with the full-screen editor",
327 if (user->flags & US_EXPERT) {
328 user->flags ^= US_EXPERT;
329 scr_printf("Expert mode now OFF\n");
331 user->flags |= US_EXPERT;
332 scr_printf("Expert mode now ON\n");
337 if (user->flags & US_FLOORS) {
338 user->flags ^= US_FLOORS;
339 scr_printf("Floor mode now OFF\n");
341 user->flags |= US_FLOORS;
342 scr_printf("Floor mode now ON\n");
346 r = CtdlIPCSetConfig(ipc, user, buf);
347 if (r / 100 != 2) scr_printf("%s\n", buf);
348 userflags = user->flags;
353 * getstring() - get a line of text from a file
354 * ignores lines beginning with "#"
356 int getstring(FILE *fp, char *string)
371 } while(string[0]=='#');
372 return(strlen(string));
376 /* Searches for patn in search string */
377 int pattern(char *search, char *patn) {
381 for (a=0; !IsEmptyStr(&search[a]); ++a) {
382 b=strncasecmp(&search[a],patn,len);
389 void strproc(char *string)
393 if (IsEmptyStr(string)) return;
395 /* Convert non-printable characters to blanks */
396 for (a=0; !IsEmptyStr(&string[a]); ++a) {
397 if (string[a]<32) string[a]=32;
398 if (string[a]>126) string[a]=32;
401 /* Remove leading and trailing blanks */
402 while(string[0]<33) strcpy(string,&string[1]);
403 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
405 /* Remove double blanks */
406 for (a=0; a<strlen(string); ++a) {
407 if ((string[a]==32)&&(string[a+1]==32)) {
408 strcpy(&string[a],&string[a+1]);
413 /* remove characters which would interfere with the network */
414 for (a=0; a<strlen(string); ++a) {
415 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
416 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
417 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
418 if (string[a]==',') strcpy(&string[a],&string[a+1]);
419 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
420 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
426 #ifndef HAVE_STRERROR
428 * replacement strerror() for systems that don't have it
430 char *strerror(int e)
432 static char buf[128];
434 snprintf(buf, sizeof buf, "errno = %d",e);
440 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
443 "**************************************************";
444 char dots_printed[51];
449 scr_printf("\r%79s\r","");
451 /* a will be range 0-50 rather than 0-100 */
452 a=(curr * 50) / cmax;
453 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
454 strncpy(dots_printed, dots, a);
456 scr_printf(fmt, dots_printed, "",
457 curr * 100 / cmax, curr, cmax);
464 * NOT the same locate_host() in locate_host.c. This one just does a
465 * 'who am i' to try to discover where the user is...
467 void locate_host(CtdlIPC* ipc, char *hbuf)
474 who = (FILE *)popen("who am i","r");
476 strcpy(hbuf, ipc->ServInfo.fqdn);
479 fgets(buf,sizeof buf,who);
483 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
484 if ((buf[a]=='(')||(buf[a]==')')) ++b;
487 strcpy(hbuf, ipc->ServInfo.fqdn);
491 for (a=0; a<strlen(buf); ++a) {
493 strcpy(buf,&buf[a+1]);
496 for (a=0; a<strlen(buf); ++a) {
497 if (buf[a]==')') buf[a] = 0;
500 if (IsEmptyStr(buf)) strcpy(hbuf, ipc->ServInfo.fqdn);
501 else strncpy(hbuf,buf,24);
503 char *tty = ttyname(0);
504 #ifdef HAVE_GETUTXLINE
505 struct utmpx ut, *put;
507 struct utmp ut, *put;
512 safestrncpy(hbuf, ipc->ServInfo.fqdn, 24);
516 if (strncmp(tty, "/dev/", 5))
519 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
521 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
522 if ((put = getutxline(&ut)) == NULL)
524 if ((put = getutline(&ut)) == NULL)
528 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
529 if (put->ut_type == USER_PROCESS) {
531 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
533 safestrncpy(hbuf, put->ut_host, 24);
536 safestrncpy(hbuf, put->ut_line, 24);
537 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
541 #endif /* HAVE_UTMP_H */
545 * miscellaneous server commands (testing, etc.)
547 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
550 CtdlIPC_chat_send(ipc, cmd);
551 CtdlIPC_chat_recv(ipc, buf);
552 scr_printf("%s\n",buf);
554 set_keepalives(KA_HALF);
555 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
556 scr_printf("%s\n",buf);
558 set_keepalives(KA_YES);
563 newprompt("> ",buf,255);
564 CtdlIPC_chat_send(ipc, buf);
565 } while(strcmp(buf,"000"));
572 * compute the checksum of a file
574 int file_checksum(char *filename)
580 fp = fopen(filename,"r");
581 if (fp == NULL) return(0);
583 /* yes, this algorithm may allow cksum to overflow, but that's ok
584 * as long as it overflows consistently, which it will.
586 while (ch=getc(fp), ch>=0) {
587 cksum = (cksum + ch);
595 * nuke a directory and its contents
597 int nukedir(char *dirname)
603 dp = opendir(dirname);
608 while (d = readdir(dp), d != NULL) {
609 snprintf(filename, sizeof filename, "%s/%s",
615 return(rmdir(dirname));