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>
43 #ifndef HAVE_GETUTLINE
44 struct utmp *getutline(struct utmp *ut);
53 #include "citadel_decls.h"
54 #include "routines2.h"
56 #define IFAIDE if(axlevel>=6)
57 #define IFNAIDE if (axlevel<6)
59 extern unsigned userflags;
60 extern char *axdefs[7];
61 extern char sigcaught;
62 extern struct CtdlServInfo serv_info;
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(void) { /* hit any key to continue */
82 scr_printf("%s\r",serv_info.serv_moreprompt);
86 for (a=0; a<strlen(serv_info.serv_moreprompt); ++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 * change a user's access level
118 newprompt("User name: ",who,25);
119 AGUP: snprintf(buf, sizeof buf, "AGUP %s",who);
123 scr_printf("%s\n",&buf[4]);
124 scr_printf("Do you want to create this user? ");
126 snprintf(buf, sizeof buf, "CREU %s", who);
133 scr_printf("%s\n",&buf[4]);
138 extract(who, &buf[4], 0);
139 extract(pass, &buf[4], 1);
140 flags = extract_int(&buf[4], 2);
141 timescalled = extract_int(&buf[4], 3);
142 posted = extract_int(&buf[4], 4);
143 axlevel = extract_int(&buf[4], 5);
144 usernum = extract_long(&buf[4], 6);
145 lastcall = extract_long(&buf[4], 7);
146 userpurge = extract_int(&buf[4], 8);
148 val_user(who, 0); /* Display registration */
151 newprompt("Password: ", pass, -19);
153 else if (boolprompt("Change password", 0)) {
154 strprompt("Password", pass, -19);
157 axlevel = intprompt("Access level", axlevel, 0, 6);
158 if (boolprompt("Ask user to register again", !(flags & US_REGIS)))
162 timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
163 posted = intprompt("Messages posted", posted, 0, INT_MAX);
164 lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
165 userpurge = intprompt("Purge time (in days, 0 for system default",
166 userpurge, 0, INT_MAX);
168 snprintf(buf, sizeof buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
169 who, pass, flags, timescalled, posted, axlevel, usernum,
170 (long)lastcall, userpurge);
174 scr_printf("%s\n",&buf[4]);
179 /* Display a prompt and flip a bit based on whether the user answers
180 * yes or no. Yes=1 and No=0, unless 'backwards' is set to a nonzero value
181 * in which case No=1 and Yes=0.
183 int set_attr(int sval, char *prompt, unsigned int sbit, int backwards)
190 scr_printf("%45s ", prompt);
193 color(BRIGHT_MAGENTA);
196 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
199 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
207 if (backwards) a = 1 - a;
209 if (backwards) a = 1 - a;
212 if (!a) temp = (temp^sbit);
217 * modes are: 0 - .EC command, 1 - .EC for new user,
218 * 2 - toggle Xpert mode 3 - toggle floor mode
220 void enter_config(int mode)
222 int width, height, flags, filter;
225 snprintf(buf, sizeof buf, "GETU");
229 scr_printf("%s\n",&buf[4]);
233 width = extract_int(&buf[4],0);
234 height = extract_int(&buf[4],1);
235 flags = extract_int(&buf[4],2);
236 filter = extract_int(&buf[4],3);
238 if ((mode==0)||(mode==1)) {
240 width = intprompt("Enter your screen width",width,20,255);
241 height = intprompt("Enter your screen height",height,3,255);
243 flags = set_attr(flags,
244 "Are you an experienced Citadel user",
247 if ( ((flags&US_EXPERT)==0) && (mode==1)) {
250 flags = set_attr(flags,
251 "Print last old message on New message request",
255 flags = set_attr(flags,
256 "Prompt after each message",
260 if ((flags & US_NOPROMPT)==0)
261 flags = set_attr(flags,
262 "Use 'disappearing' prompts",
266 flags = set_attr(flags,
267 "Pause after each screenful of text",
271 if ( (rc_prompt_control == 3) && (flags & US_PAGINATOR) ) {
272 flags = set_attr(flags,
273 "<N>ext and <S>top work at paginator prompt",
278 if (rc_floor_mode == RC_DEFAULT) {
279 flags = set_attr(flags,
280 "View rooms by floor",
285 if (rc_ansi_color == 3) {
286 flags = set_attr(flags,
287 "Enable color support",
293 if ((flags&US_EXPERT)==0) formout("unlisted");
294 flags = set_attr(flags,
295 "Be unlisted in userlog",
302 if (flags & US_EXPERT) {
303 flags = (flags ^ US_EXPERT);
304 scr_printf("Expert mode now OFF\n");
307 flags = (flags | US_EXPERT);
308 scr_printf("Expert mode now ON\n");
313 if (flags & US_FLOORS) {
314 flags = (flags ^ US_FLOORS);
315 scr_printf("Floor mode now OFF\n");
318 flags = (flags | US_FLOORS);
319 scr_printf("Floor mode now ON\n");
323 snprintf(buf, sizeof buf, "SETU %d|%d|%d|%d",width,height,flags,filter);
326 if (buf[0]!='2') scr_printf("%s\n",&buf[4]);
331 * getstring() - get a line of text from a file
332 * ignores lines beginning with "#"
334 int getstring(FILE *fp, char *string)
349 } while(string[0]=='#');
350 return(strlen(string));
354 /* Searches for patn in search string */
355 int pattern(char *search, char *patn) {
358 for (a=0; a<strlen(search); ++a) {
359 b=strncasecmp(&search[a],patn,strlen(patn));
366 /* display internal error as defined in errmsgs */
367 void interr(int errnum) {
368 scr_printf("*** INTERNAL ERROR %d\n"
369 "(Press any key to continue)\n", errnum);
374 void strproc(char *string)
378 if (strlen(string)==0) return;
380 /* Convert non-printable characters to blanks */
381 for (a=0; a<strlen(string); ++a) {
382 if (string[a]<32) string[a]=32;
383 if (string[a]>126) string[a]=32;
386 /* Remove leading and trailing blanks */
387 while(string[0]<33) strcpy(string,&string[1]);
388 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
390 /* Remove double blanks */
391 for (a=0; a<strlen(string); ++a) {
392 if ((string[a]==32)&&(string[a+1]==32)) {
393 strcpy(&string[a],&string[a+1]);
398 /* remove characters which would interfere with the network */
399 for (a=0; a<strlen(string); ++a) {
400 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
401 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
402 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
403 if (string[a]==',') strcpy(&string[a],&string[a+1]);
404 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
405 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
411 #ifndef HAVE_STRERROR
413 * replacement strerror() for systems that don't have it
415 char *strerror(int e)
417 static char buf[128];
419 snprintf(buf, sizeof buf, "errno = %d",e);
425 void progress(long int curr, long int cmax)
427 static long dots_printed;
431 scr_printf(".......................................");
432 scr_printf(".......................................\r");
436 else if (curr==cmax) {
437 scr_printf("\r%79s\n","");
440 a=(curr * 100) / cmax;
442 while (dots_printed < a) {
452 * NOT the same locate_host() in locate_host.c. This one just does a
453 * 'who am i' to try to discover where the user is...
455 void locate_host(char *hbuf)
462 who = (FILE *)popen("who am i","r");
464 strcpy(hbuf,serv_info.serv_fqdn);
467 fgets(buf,sizeof buf,who);
471 for (a=0; a<strlen(buf); ++a) {
472 if ((buf[a]=='(')||(buf[a]==')')) ++b;
475 strcpy(hbuf,serv_info.serv_fqdn);
479 for (a=0; a<strlen(buf); ++a) {
481 strcpy(buf,&buf[a+1]);
484 for (a=0; a<strlen(buf); ++a) {
485 if (buf[a]==')') buf[a] = 0;
488 if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
489 else strncpy(hbuf,buf,24);
491 char *tty = ttyname(0);
492 #ifdef HAVE_GETUTXLINE
493 struct utmpx ut, *put;
495 struct utmp ut, *put;
500 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
504 if (strncmp(tty, "/dev/", 5))
507 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
509 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
510 if ((put = getutxline(&ut)) == NULL)
512 if ((put = getutline(&ut)) == NULL)
516 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
517 if (put->ut_type == USER_PROCESS) {
519 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
521 safestrncpy(hbuf, put->ut_host, 24);
524 safestrncpy(hbuf, put->ut_line, 24);
525 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
529 #endif /* HAVE_UTMP_H */
533 * miscellaneous server commands (testing, etc.)
535 void misc_server_cmd(char *cmd) {
540 scr_printf("%s\n",buf);
542 set_keepalives(KA_HALF);
543 while (serv_gets(buf), strcmp(buf,"000")) {
544 scr_printf("%s\n",buf);
546 set_keepalives(KA_YES);
551 newprompt("> ",buf,255);
553 } while(strcmp(buf,"000"));
560 * compute the checksum of a file
562 int file_checksum(char *filename)
568 fp = fopen(filename,"r");
569 if (fp == NULL) return(0);
571 /* yes, this algorithm may allow cksum to overflow, but that's ok
572 * as long as it overflows consistently, which it will.
574 while (ch=getc(fp), ch>=0) {
575 cksum = (cksum + ch);
583 * nuke a directory and its contents
585 int nukedir(char *dirname)
591 dp = opendir(dirname);
596 while (d = readdir(dp), d != NULL) {
597 snprintf(filename, sizeof filename, "%s/%s",
603 return(rmdir(dirname));