4 * Client-side support functions.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
42 #include "citadel_ipc.h"
45 #ifndef HAVE_GETUTLINE
46 struct utmp *getutline(struct utmp *ut);
55 #include "citadel_decls.h"
56 #include "routines2.h"
58 #define IFAIDE if(axlevel>=6)
59 #define IFNAIDE if (axlevel<6)
61 extern unsigned userflags;
62 extern char *axdefs[7];
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) {
78 void hit_any_key(CtdlIPC *ipc) { /* hit any key to continue */
83 scr_printf("%s\r", ipc->ServInfo.moreprompt);
87 for (a=0; a<strlen(ipc->ServInfo.moreprompt); ++a)
91 if ( (rc_prompt_control == 1)
92 || ((rc_prompt_control == 3) && (userflags & US_PROMPTCTL)) ) {
93 if (b == 'q' || b == 'Q' || b == 's' || b == 'S')
95 if (b == 'n' || b == 'N')
98 if (b==NEXT_KEY) sigcaught = SIGINT;
99 if (b==STOP_KEY) sigcaught = SIGQUIT;
103 * Edit or delete a user (cmd=25 to edit/create, 96 to delete)
105 void edituser(CtdlIPC *ipc, int cmd)
108 char who[USERNAME_SIZE];
109 struct ctdluser *user = NULL;
111 int r; /* IPC response code */
113 newprompt("User name: ", who, 29);
114 while ((r = CtdlIPCAideGetUserParameters(ipc, who, &user, buf)) / 100 != 2) {
115 scr_printf("%s\n", buf);
117 scr_printf("Do you want to create this user? ");
119 r = CtdlIPCCreateUser(ipc, who, 0, buf);
124 scr_printf("%s\n",&buf[4]);
132 val_user(ipc, user->fullname, 0); /* Display registration */
134 if (newnow || boolprompt("Change password", 0)) {
135 strprompt("Password", user->password, -19);
138 user->axlevel = intprompt("Access level", user->axlevel, 0, 6);
140 user->flags = set_attr(ipc, user->flags,
141 "Permission to send Internet mail",
144 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
145 user->flags &= ~US_REGIS;
147 user->flags |= US_REGIS;
148 user->timescalled = intprompt("Times called",
149 user->timescalled, 0, INT_MAX);
150 user->posted = intprompt("Messages posted",
151 user->posted, 0, INT_MAX);
152 user->lastcall = boolprompt("Set last call to now", 0) ?
153 time(NULL) : user->lastcall;
154 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
155 user->USuserpurge, 0, INT_MAX);
159 scr_printf("Do you want to delete this user? ");
160 if (!yesno()) return;
164 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
166 scr_printf("%s\n", buf);
172 /* Display a prompt and flip a bit based on whether the user answers
173 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
174 * in which case No=1 and Yes=0.
176 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
183 scr_printf("%50s ", prompt);
186 color(BRIGHT_MAGENTA);
189 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
192 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
200 if (backwards) a = 1 - a;
202 if (backwards) a = 1 - a;
205 if (!a) temp = (temp^sbit);
210 * modes are: 0 - .EC command, 1 - .EC for new user,
211 * 2 - toggle Xpert mode 3 - toggle floor mode
213 void enter_config(CtdlIPC *ipc, int mode)
216 struct ctdluser *user = NULL;
217 int r; /* IPC response code */
219 r = CtdlIPCGetConfig(ipc, &user, buf);
221 scr_printf("%s\n", buf);
226 if (mode == 0 || mode == 1) {
228 /* Does anyone still use dialup connections with manual
229 * screen dimensions setting anymore? For now we'll keep
230 * the system's ability to set these, but remove the prompts
231 * because they're spurious for nearly everyone.
233 user->USscreenwidth = intprompt("Enter your screen width",
234 user->USscreenwidth, 20, 255);
235 user->USscreenheight = intprompt("Enter your screen height",
236 user->USscreenheight, 3, 255);
239 user->flags = set_attr(ipc, user->flags,
240 "Are you an experienced Citadel user",
242 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
247 user->flags = set_attr(ipc, user->flags,
248 "Print last old message on New message request",
251 user->flags = set_attr(ipc, user->flags,
252 "Prompt after each message",
255 if ((user->flags & US_NOPROMPT) == 0)
256 user->flags = set_attr(ipc, user->flags,
257 "Use 'disappearing' prompts",
260 user->flags = set_attr(ipc, user->flags,
261 "Pause after each screenful of text",
264 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR))
265 user->flags = set_attr(ipc, user->flags,
266 "<N>ext and <S>top work at paginator prompt",
269 if (rc_floor_mode == RC_DEFAULT)
270 user->flags = set_attr(ipc, user->flags,
271 "View rooms by floor",
274 if (rc_ansi_color == 3)
275 user->flags = set_attr(ipc, user->flags,
276 "Enable color support",
279 if ((user->flags & US_EXPERT) == 0)
280 formout(ipc, "unlisted");
282 user->flags = set_attr(ipc, user->flags,
283 "Be unlisted in userlog",
286 if (strlen(editor_paths[0]) > 0) {
287 user->flags = set_attr(ipc, user->flags,
288 "Always enter messages with the full-screen editor",
295 if (user->flags & US_EXPERT) {
296 user->flags ^= US_EXPERT;
297 scr_printf("Expert mode now OFF\n");
299 user->flags |= US_EXPERT;
300 scr_printf("Expert mode now ON\n");
305 if (user->flags & US_FLOORS) {
306 user->flags ^= US_FLOORS;
307 scr_printf("Floor mode now OFF\n");
309 user->flags |= US_FLOORS;
310 scr_printf("Floor mode now ON\n");
314 r = CtdlIPCSetConfig(ipc, user, buf);
315 if (r / 100 != 2) scr_printf("%s\n", buf);
316 userflags = user->flags;
321 * getstring() - get a line of text from a file
322 * ignores lines beginning with "#"
324 int getstring(FILE *fp, char *string)
339 } while(string[0]=='#');
340 return(strlen(string));
344 /* Searches for patn in search string */
345 int pattern(char *search, char *patn) {
348 for (a=0; a<strlen(search); ++a) {
349 b=strncasecmp(&search[a],patn,strlen(patn));
356 void strproc(char *string)
360 if (strlen(string)==0) return;
362 /* Convert non-printable characters to blanks */
363 for (a=0; a<strlen(string); ++a) {
364 if (string[a]<32) string[a]=32;
365 if (string[a]>126) string[a]=32;
368 /* Remove leading and trailing blanks */
369 while(string[0]<33) strcpy(string,&string[1]);
370 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
372 /* Remove double blanks */
373 for (a=0; a<strlen(string); ++a) {
374 if ((string[a]==32)&&(string[a+1]==32)) {
375 strcpy(&string[a],&string[a+1]);
380 /* remove characters which would interfere with the network */
381 for (a=0; a<strlen(string); ++a) {
382 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
383 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
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]);
393 #ifndef HAVE_STRERROR
395 * replacement strerror() for systems that don't have it
397 char *strerror(int e)
399 static char buf[128];
401 snprintf(buf, sizeof buf, "errno = %d",e);
407 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
410 "**************************************************";
411 char dots_printed[51];
416 sln_printf("\r%79s\r","");
417 status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location,
418 room_name, secure, 0);
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 sln_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; a<strlen(buf); ++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 (strlen(buf)==0) 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));