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[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) {
78 void hit_any_key(CtdlIPC *ipc) { /* hit any key to continue */
83 scr_printf("%s\r", ipc->ServInfo.moreprompt);
87 for (a=0; !IsEmptyStr(&ipc->ServInfo.moreprompt[a]); ++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);
139 /* user->flags = set_attr(ipc, user->flags,
140 "Permission to send Internet mail",
142 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET)))
143 user->flags |= US_INTERNET;
145 user->flags &= ~US_INTERNET;
146 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
147 user->flags &= ~US_REGIS;
149 user->flags |= US_REGIS;
150 user->timescalled = intprompt("Times called",
151 user->timescalled, 0, INT_MAX);
152 user->posted = intprompt("Messages posted",
153 user->posted, 0, INT_MAX);
154 user->lastcall = boolprompt("Set last call to now", 0) ?
155 time(NULL) : user->lastcall;
156 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
157 user->USuserpurge, 0, INT_MAX);
161 scr_printf("Do you want to delete this user? ");
169 r = CtdlIPCAideSetUserParameters(ipc, user, buf);
171 scr_printf("%s\n", buf);
177 /* Display a prompt and flip a bit based on whether the user answers
178 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
179 * in which case No=1 and Yes=0.
181 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
188 scr_printf("%50s ", prompt);
191 color(BRIGHT_MAGENTA);
194 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
197 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
205 if (backwards) a = 1 - a;
207 if (backwards) a = 1 - a;
210 if (!a) temp = (temp^sbit);
215 * modes are: 0 - .EC command, 1 - .EC for new user,
216 * 2 - toggle Xpert mode 3 - toggle floor mode
218 void enter_config(CtdlIPC *ipc, int mode)
221 struct ctdluser *user = NULL;
222 int r; /* IPC response code */
224 r = CtdlIPCGetConfig(ipc, &user, buf);
226 scr_printf("%s\n", buf);
231 if (mode == 0 || mode == 1) {
233 /* Does anyone still use dialup connections with manual
234 * screen dimensions setting anymore? For now we'll keep
235 * the system's ability to set these, but remove the prompts
236 * because they're spurious for nearly everyone.
238 user->USscreenwidth = intprompt("Enter your screen width",
239 user->USscreenwidth, 20, 255);
240 user->USscreenheight = intprompt("Enter your screen height",
241 user->USscreenheight, 3, 255);
244 user->flags = set_attr(ipc, user->flags,
245 "Are you an experienced Citadel user",
247 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
252 user->flags = set_attr(ipc, user->flags,
253 "Print last old message on New message request",
256 user->flags = set_attr(ipc, user->flags,
257 "Prompt after each message",
260 if ((user->flags & US_NOPROMPT) == 0)
261 user->flags = set_attr(ipc, user->flags,
262 "Use 'disappearing' prompts",
265 user->flags = set_attr(ipc, user->flags,
266 "Pause after each screenful of text",
269 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR))
270 user->flags = set_attr(ipc, user->flags,
271 "<N>ext and <S>top work at paginator prompt",
274 if (rc_floor_mode == RC_DEFAULT)
275 user->flags = set_attr(ipc, user->flags,
276 "View rooms by floor",
279 if (rc_ansi_color == 3)
280 user->flags = set_attr(ipc, user->flags,
281 "Enable color support",
284 if ((user->flags & US_EXPERT) == 0)
285 formout(ipc, "unlisted");
287 user->flags = set_attr(ipc, user->flags,
288 "Be unlisted in userlog",
291 if (!IsEmptyStr(editor_paths[0])) {
292 user->flags = set_attr(ipc, user->flags,
293 "Always enter messages with the full-screen editor",
300 if (user->flags & US_EXPERT) {
301 user->flags ^= US_EXPERT;
302 scr_printf("Expert mode now OFF\n");
304 user->flags |= US_EXPERT;
305 scr_printf("Expert mode now ON\n");
310 if (user->flags & US_FLOORS) {
311 user->flags ^= US_FLOORS;
312 scr_printf("Floor mode now OFF\n");
314 user->flags |= US_FLOORS;
315 scr_printf("Floor mode now ON\n");
319 r = CtdlIPCSetConfig(ipc, user, buf);
320 if (r / 100 != 2) scr_printf("%s\n", buf);
321 userflags = user->flags;
326 * getstring() - get a line of text from a file
327 * ignores lines beginning with "#"
329 int getstring(FILE *fp, char *string)
344 } while(string[0]=='#');
345 return(strlen(string));
349 /* Searches for patn in search string */
350 int pattern(char *search, char *patn) {
354 for (a=0; !IsEmptyStr(&search[a]); ++a) {
355 b=strncasecmp(&search[a],patn,len);
362 void strproc(char *string)
366 if (IsEmptyStr(string)) return;
368 /* Convert non-printable characters to blanks */
369 for (a=0; !IsEmptyStr(&string[a]); ++a) {
370 if (string[a]<32) string[a]=32;
371 if (string[a]>126) string[a]=32;
374 /* Remove leading and trailing blanks */
375 while(string[0]<33) strcpy(string,&string[1]);
376 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
378 /* Remove double blanks */
379 for (a=0; a<strlen(string); ++a) {
380 if ((string[a]==32)&&(string[a+1]==32)) {
381 strcpy(&string[a],&string[a+1]);
386 /* remove characters which would interfere with the network */
387 for (a=0; a<strlen(string); ++a) {
388 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
389 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
390 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
391 if (string[a]==',') strcpy(&string[a],&string[a+1]);
392 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
393 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
399 #ifndef HAVE_STRERROR
401 * replacement strerror() for systems that don't have it
403 char *strerror(int e)
405 static char buf[128];
407 snprintf(buf, sizeof buf, "errno = %d",e);
413 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
416 "**************************************************";
417 char dots_printed[51];
422 sln_printf("\r%79s\r","");
423 status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location,
424 room_name, secure, 0);
426 /* a will be range 0-50 rather than 0-100 */
427 a=(curr * 50) / cmax;
428 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
429 strncpy(dots_printed, dots, a);
431 sln_printf(fmt, dots_printed, "",
432 curr * 100 / cmax, curr, cmax);
439 * NOT the same locate_host() in locate_host.c. This one just does a
440 * 'who am i' to try to discover where the user is...
442 void locate_host(CtdlIPC* ipc, char *hbuf)
449 who = (FILE *)popen("who am i","r");
451 strcpy(hbuf, ipc->ServInfo.fqdn);
454 fgets(buf,sizeof buf,who);
458 for (a=0; !IsEmptyStr(&buf[a]); ++a) {
459 if ((buf[a]=='(')||(buf[a]==')')) ++b;
462 strcpy(hbuf, ipc->ServInfo.fqdn);
466 for (a=0; a<strlen(buf); ++a) {
468 strcpy(buf,&buf[a+1]);
471 for (a=0; a<strlen(buf); ++a) {
472 if (buf[a]==')') buf[a] = 0;
475 if (IsEmptyStr(buf)) strcpy(hbuf, ipc->ServInfo.fqdn);
476 else strncpy(hbuf,buf,24);
478 char *tty = ttyname(0);
479 #ifdef HAVE_GETUTXLINE
480 struct utmpx ut, *put;
482 struct utmp ut, *put;
487 safestrncpy(hbuf, ipc->ServInfo.fqdn, 24);
491 if (strncmp(tty, "/dev/", 5))
494 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
496 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
497 if ((put = getutxline(&ut)) == NULL)
499 if ((put = getutline(&ut)) == NULL)
503 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
504 if (put->ut_type == USER_PROCESS) {
506 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
508 safestrncpy(hbuf, put->ut_host, 24);
511 safestrncpy(hbuf, put->ut_line, 24);
512 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
516 #endif /* HAVE_UTMP_H */
520 * miscellaneous server commands (testing, etc.)
522 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
525 CtdlIPC_chat_send(ipc, cmd);
526 CtdlIPC_chat_recv(ipc, buf);
527 scr_printf("%s\n",buf);
529 set_keepalives(KA_HALF);
530 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
531 scr_printf("%s\n",buf);
533 set_keepalives(KA_YES);
538 newprompt("> ",buf,255);
539 CtdlIPC_chat_send(ipc, buf);
540 } while(strcmp(buf,"000"));
547 * compute the checksum of a file
549 int file_checksum(char *filename)
555 fp = fopen(filename,"r");
556 if (fp == NULL) return(0);
558 /* yes, this algorithm may allow cksum to overflow, but that's ok
559 * as long as it overflows consistently, which it will.
561 while (ch=getc(fp), ch>=0) {
562 cksum = (cksum + ch);
570 * nuke a directory and its contents
572 int nukedir(char *dirname)
578 dp = opendir(dirname);
583 while (d = readdir(dp), d != NULL) {
584 snprintf(filename, sizeof filename, "%s/%s",
590 return(rmdir(dirname));