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) {
78 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
80 void edituser(CtdlIPC *ipc, int cmd)
83 char who[USERNAME_SIZE];
84 char newname[USERNAME_SIZE];
85 struct ctdluser *user = NULL;
87 int r; /* IPC response code */
92 newprompt("User name: ", who, 29);
93 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
94 scr_printf("%s\n", buf);
96 scr_printf("Do you want to create this user? ");
98 r = CtdlIPCCreateUser(ipc, who, 0, buf);
103 scr_printf("%s\n", buf);
111 val_user(ipc, user->fullname, 0); /* Display registration */
115 while (change_name == 1) {
116 if (boolprompt("Change name", 0)) {
117 strprompt("New name", newname, USERNAME_SIZE-1);
118 r = CtdlIPCRenameUser(ipc, user->fullname, newname, buf);
120 scr_printf("%s\n", buf);
123 strcpy(user->fullname, newname);
133 if (newnow || boolprompt("Change password", 0)) {
134 strprompt("Password", user->password, -19);
137 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
138 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET)))
139 user->flags |= US_INTERNET;
141 user->flags &= ~US_INTERNET;
142 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
143 user->flags &= ~US_REGIS;
145 user->flags |= US_REGIS;
146 user->timescalled = intprompt("Times called",
147 user->timescalled, 0, INT_MAX);
148 user->posted = intprompt("Messages posted",
149 user->posted, 0, INT_MAX);
150 user->lastcall = boolprompt("Set last call to now", 0) ?
151 time(NULL) : user->lastcall;
152 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
153 user->USuserpurge, 0, INT_MAX);
157 scr_printf("Do you want to delete this user? ");
162 user->axlevel = AxDeleted;
165 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
167 scr_printf("%s\n", buf);
173 /* Display a prompt and flip a bit based on whether the user answers
174 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
175 * in which case No=1 and Yes=0.
177 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
184 scr_printf("%50s ", prompt);
187 color(BRIGHT_MAGENTA);
190 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
193 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
201 if (backwards) a = 1 - a;
203 if (backwards) a = 1 - a;
206 if (!a) temp = (temp^sbit);
211 * modes are: 0 - .EC command, 1 - .EC for new user,
212 * 2 - toggle Xpert mode 3 - toggle floor mode
214 void enter_config(CtdlIPC *ipc, int mode)
217 struct ctdluser *user = NULL;
218 int r; /* IPC response code */
220 r = CtdlIPCGetConfig(ipc, &user, buf);
222 scr_printf("%s\n", buf);
227 if (mode == 0 || mode == 1) {
229 /* Does anyone still use dialup connections with manual
230 * screen dimensions setting anymore? For now we'll keep
231 * the system's ability to set these, but remove the prompts
232 * because they're spurious for nearly everyone.
234 user->USscreenwidth = intprompt("Enter your screen width",
235 user->USscreenwidth, 20, 255);
236 user->USscreenheight = intprompt("Enter your screen height",
237 user->USscreenheight, 3, 255);
240 user->flags = set_attr(ipc, user->flags,
241 "Are you an experienced Citadel user",
243 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
248 user->flags = set_attr(ipc, user->flags,
249 "Print last old message on New message request",
252 user->flags = set_attr(ipc, user->flags,
253 "Prompt after each message",
256 if ((user->flags & US_NOPROMPT) == 0)
257 user->flags = set_attr(ipc, user->flags,
258 "Use 'disappearing' prompts",
261 user->flags = set_attr(ipc, user->flags,
262 "Pause after each screenful of text",
265 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR))
266 user->flags = set_attr(ipc, user->flags,
267 "<N>ext and <S>top work at paginator prompt",
270 if (rc_floor_mode == RC_DEFAULT)
271 user->flags = set_attr(ipc, user->flags,
272 "View rooms by floor",
275 if (rc_ansi_color == 3)
276 user->flags = set_attr(ipc, user->flags,
277 "Enable color support",
280 if ((user->flags & US_EXPERT) == 0)
281 formout(ipc, "unlisted");
283 user->flags = set_attr(ipc, user->flags,
284 "Be unlisted in userlog",
287 if (!IsEmptyStr(editor_paths[0])) {
288 user->flags = set_attr(ipc, user->flags,
289 "Always enter messages with the full-screen editor",
296 if (user->flags & US_EXPERT) {
297 user->flags ^= US_EXPERT;
298 scr_printf("Expert mode now OFF\n");
300 user->flags |= US_EXPERT;
301 scr_printf("Expert mode now ON\n");
306 if (user->flags & US_FLOORS) {
307 user->flags ^= US_FLOORS;
308 scr_printf("Floor mode now OFF\n");
310 user->flags |= US_FLOORS;
311 scr_printf("Floor mode now ON\n");
315 r = CtdlIPCSetConfig(ipc, user, buf);
316 if (r / 100 != 2) scr_printf("%s\n", buf);
317 userflags = user->flags;
322 * getstring() - get a line of text from a file
323 * ignores lines beginning with "#"
325 int getstring(FILE *fp, char *string)
340 } while(string[0]=='#');
341 return(strlen(string));
345 /* Searches for patn in search string */
346 int pattern(char *search, char *patn) {
350 for (a=0; !IsEmptyStr(&search[a]); ++a) {
351 b=strncasecmp(&search[a],patn,len);
358 void strproc(char *string)
362 if (IsEmptyStr(string)) return;
364 /* Convert non-printable characters to blanks */
365 for (a=0; !IsEmptyStr(&string[a]); ++a) {
366 if (string[a]<32) string[a]=32;
367 if (string[a]>126) string[a]=32;
370 /* Remove leading and trailing blanks */
371 while(string[0]<33) strcpy(string,&string[1]);
372 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
374 /* Remove double blanks */
375 for (a=0; a<strlen(string); ++a) {
376 if ((string[a]==32)&&(string[a+1]==32)) {
377 strcpy(&string[a],&string[a+1]);
382 /* remove characters which would interfere with the network */
383 for (a=0; a<strlen(string); ++a) {
384 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
385 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
386 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
387 if (string[a]==',') strcpy(&string[a],&string[a+1]);
388 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
389 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
395 #ifndef HAVE_STRERROR
397 * replacement strerror() for systems that don't have it
399 char *strerror(int e)
401 static char buf[128];
403 snprintf(buf, sizeof buf, "errno = %d",e);
409 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
412 "**************************************************";
413 char dots_printed[51];
418 scr_printf("\r%79s\r","");
420 /* a will be range 0-50 rather than 0-100 */
421 a=(curr * 50) / cmax;
422 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
423 strncpy(dots_printed, dots, a);
425 scr_printf(fmt, dots_printed, "",
426 curr * 100 / cmax, curr, cmax);
433 * NOT the same locate_host() in locate_host.c. This one just does a
434 * 'who am i' to try to discover where the user is...
436 void locate_host(CtdlIPC* ipc, char *hbuf)
443 who = (FILE *)popen("who am i","r");
445 strcpy(hbuf, ipc->ServInfo.fqdn);
448 fgets(buf,sizeof buf,who);
452 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
453 if ((buf[a]=='(')||(buf[a]==')')) ++b;
456 strcpy(hbuf, ipc->ServInfo.fqdn);
460 for (a=0; a<strlen(buf); ++a) {
462 strcpy(buf,&buf[a+1]);
465 for (a=0; a<strlen(buf); ++a) {
466 if (buf[a]==')') buf[a] = 0;
469 if (IsEmptyStr(buf)) strcpy(hbuf, ipc->ServInfo.fqdn);
470 else strncpy(hbuf,buf,24);
472 char *tty = ttyname(0);
473 #ifdef HAVE_GETUTXLINE
474 struct utmpx ut, *put;
476 struct utmp ut, *put;
481 safestrncpy(hbuf, ipc->ServInfo.fqdn, 24);
485 if (strncmp(tty, "/dev/", 5))
488 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
490 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
491 if ((put = getutxline(&ut)) == NULL)
493 if ((put = getutline(&ut)) == NULL)
497 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
498 if (put->ut_type == USER_PROCESS) {
500 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
502 safestrncpy(hbuf, put->ut_host, 24);
505 safestrncpy(hbuf, put->ut_line, 24);
506 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
510 #endif /* HAVE_UTMP_H */
514 * miscellaneous server commands (testing, etc.)
516 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
519 CtdlIPC_chat_send(ipc, cmd);
520 CtdlIPC_chat_recv(ipc, buf);
521 scr_printf("%s\n",buf);
523 set_keepalives(KA_HALF);
524 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
525 scr_printf("%s\n",buf);
527 set_keepalives(KA_YES);
532 newprompt("> ",buf,255);
533 CtdlIPC_chat_send(ipc, buf);
534 } while(strcmp(buf,"000"));
541 * compute the checksum of a file
543 int file_checksum(char *filename)
549 fp = fopen(filename,"r");
550 if (fp == NULL) return(0);
552 /* yes, this algorithm may allow cksum to overflow, but that's ok
553 * as long as it overflows consistently, which it will.
555 while (ch=getc(fp), ch>=0) {
556 cksum = (cksum + ch);
564 * nuke a directory and its contents
566 int nukedir(char *dirname)
572 dp = opendir(dirname);
577 while (d = readdir(dp), d != NULL) {
578 snprintf(filename, sizeof filename, "%s/%s",
584 return(rmdir(dirname));