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",
294 if ((flags&US_EXPERT)==0) formout("unlisted");
295 flags = set_attr(flags,
296 "Be unlisted in userlog",
301 if (flags & US_EXPERT) {
302 flags = (flags ^ US_EXPERT);
303 scr_printf("Expert mode now OFF\n");
306 flags = (flags | US_EXPERT);
307 scr_printf("Expert mode now ON\n");
312 if (flags & US_FLOORS) {
313 flags = (flags ^ US_FLOORS);
314 scr_printf("Floor mode now OFF\n");
317 flags = (flags | US_FLOORS);
318 scr_printf("Floor mode now ON\n");
322 snprintf(buf, sizeof buf, "SETU %d|%d|%d|%d",width,height,flags,filter);
325 if (buf[0]!='2') scr_printf("%s\n",&buf[4]);
330 * getstring() - get a line of text from a file
331 * ignores lines beginning with "#"
333 int getstring(FILE *fp, char *string)
348 } while(string[0]=='#');
349 return(strlen(string));
353 /* Searches for patn in search string */
354 int pattern(char *search, char *patn) {
357 for (a=0; a<strlen(search); ++a) {
358 b=strncasecmp(&search[a],patn,strlen(patn));
365 /* display internal error as defined in errmsgs */
366 void interr(int errnum) {
367 scr_printf("*** INTERNAL ERROR %d\n"
368 "(Press any key to continue)\n", errnum);
373 void strproc(char *string)
377 if (strlen(string)==0) return;
379 /* Convert non-printable characters to blanks */
380 for (a=0; a<strlen(string); ++a) {
381 if (string[a]<32) string[a]=32;
382 if (string[a]>126) string[a]=32;
385 /* Remove leading and trailing blanks */
386 while(string[0]<33) strcpy(string,&string[1]);
387 while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
389 /* Remove double blanks */
390 for (a=0; a<strlen(string); ++a) {
391 if ((string[a]==32)&&(string[a+1]==32)) {
392 strcpy(&string[a],&string[a+1]);
397 /* remove characters which would interfere with the network */
398 for (a=0; a<strlen(string); ++a) {
399 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
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]);
410 #ifndef HAVE_STRERROR
412 * replacement strerror() for systems that don't have it
414 char *strerror(int e)
416 static char buf[128];
418 snprintf(buf, sizeof buf, "errno = %d",e);
424 void progress(long int curr, long int cmax)
426 static long dots_printed;
430 scr_printf(".......................................");
431 scr_printf(".......................................\r");
435 else if (curr==cmax) {
436 scr_printf("\r%79s\n","");
439 a=(curr * 100) / cmax;
441 while (dots_printed < a) {
451 * NOT the same locate_host() in locate_host.c. This one just does a
452 * 'who am i' to try to discover where the user is...
454 void locate_host(char *hbuf)
461 who = (FILE *)popen("who am i","r");
463 strcpy(hbuf,serv_info.serv_fqdn);
466 fgets(buf,sizeof buf,who);
470 for (a=0; a<strlen(buf); ++a) {
471 if ((buf[a]=='(')||(buf[a]==')')) ++b;
474 strcpy(hbuf,serv_info.serv_fqdn);
478 for (a=0; a<strlen(buf); ++a) {
480 strcpy(buf,&buf[a+1]);
483 for (a=0; a<strlen(buf); ++a) {
484 if (buf[a]==')') buf[a] = 0;
487 if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
488 else strncpy(hbuf,buf,24);
490 char *tty = ttyname(0);
491 #ifdef HAVE_GETUTXLINE
492 struct utmpx ut, *put;
494 struct utmp ut, *put;
499 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
503 if (strncmp(tty, "/dev/", 5))
506 safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
508 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
509 if ((put = getutxline(&ut)) == NULL)
511 if ((put = getutline(&ut)) == NULL)
515 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
516 if (put->ut_type == USER_PROCESS) {
518 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
520 safestrncpy(hbuf, put->ut_host, 24);
523 safestrncpy(hbuf, put->ut_line, 24);
524 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
528 #endif /* HAVE_UTMP_H */
532 * miscellaneous server commands (testing, etc.)
534 void misc_server_cmd(char *cmd) {
539 scr_printf("%s\n",buf);
541 set_keepalives(KA_HALF);
542 while (serv_gets(buf), strcmp(buf,"000")) {
543 scr_printf("%s\n",buf);
545 set_keepalives(KA_YES);
550 newprompt("> ",buf,255);
552 } while(strcmp(buf,"000"));
559 * compute the checksum of a file
561 int file_checksum(char *filename)
567 fp = fopen(filename,"r");
568 if (fp == NULL) return(0);
570 /* yes, this algorithm may allow cksum to overflow, but that's ok
571 * as long as it overflows consistently, which it will.
573 while (ch=getc(fp), ch>=0) {
574 cksum = (cksum + ch);
582 * nuke a directory and its contents
584 int nukedir(char *dirname)
590 dp = opendir(dirname);
595 while (d = readdir(dp), d != NULL) {
596 snprintf(filename, sizeof filename, "%s/%s",
602 return(rmdir(dirname));