]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
* Fixed small bug in the e<X>pert mode toggle
[citadel.git] / citadel / routines.c
1 /*
2  * $Id$
3  *
4  * Client-side support functions.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <pwd.h>
18 #include <signal.h>
19 #include <dirent.h>
20 #include <errno.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32 #ifdef HAVE_LIMITS_H
33 #include <limits.h>
34 #endif
35 #ifdef HAVE_UTMP_H
36 #include <utmp.h>
37 #endif
38 #ifdef HAVE_UTMPX_H
39 #include <utmpx.h>
40 #endif
41 #include "screen.h"
42
43 #ifndef HAVE_GETUTLINE
44 struct utmp *getutline(struct utmp *ut);
45 #endif
46
47 #define ROUTINES_C
48
49 #include "citadel.h"
50 #include "routines.h"
51 #include "commands.h"
52 #include "tools.h"
53 #include "citadel_decls.h"
54 #include "routines2.h"
55
56 #define IFAIDE if(axlevel>=6)
57 #define IFNAIDE if (axlevel<6)
58
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;
66
67 /* Destructive backspace */
68 void back(int spaces) {
69         int a;
70         for (a=0; a<spaces; ++a) {
71                 scr_putc(8);
72                 scr_putc(32);
73                 scr_putc(8);
74         }
75 }
76
77 void hit_any_key(void) {                /* hit any key to continue */
78         int a,b;
79
80         color(COLOR_PUSH);
81         color(DIM_RED);
82         scr_printf("%s\r",serv_info.serv_moreprompt);
83         color(COLOR_POP);
84         sttybbs(0);
85         b=inkey();
86         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
87                 scr_putc(' ');
88         scr_putc(13);
89         sttybbs(1);
90         if ( (rc_prompt_control == 1)
91            || ((rc_prompt_control == 3) && (userflags & US_PROMPTCTL)) ) {
92                 if (b == 'q' || b == 'Q' || b == 's' || b == 'S')
93                         b = STOP_KEY;
94                 if (b == 'n' || b == 'N')
95                         b = NEXT_KEY;
96         }
97         if (b==NEXT_KEY) sigcaught = SIGINT;
98         if (b==STOP_KEY) sigcaught = SIGQUIT;
99 }
100
101 /*
102  * change a user's access level
103  */
104 void edituser(void)
105 {
106         char buf[SIZ];
107         char who[SIZ];
108         char pass[SIZ];
109         int flags;
110         int timescalled;
111         int posted;
112         int axlevel;
113         long usernum;
114         time_t lastcall;
115         int userpurge;
116         int newnow = 0;
117
118         newprompt("User name: ",who,25);
119 AGUP:   snprintf(buf, sizeof buf, "AGUP %s",who);
120         serv_puts(buf);
121         serv_gets(buf);
122         if (buf[0]!='2') {
123                 scr_printf("%s\n",&buf[4]);
124                 scr_printf("Do you want to create this user? ");
125                 if (yesno()) {
126                         snprintf(buf, sizeof buf, "CREU %s", who);
127                         serv_puts(buf);
128                         serv_gets(buf);
129                         if (buf[0] == '2') {
130                                 newnow = 1;
131                                 goto AGUP;
132                         }
133                         scr_printf("%s\n",&buf[4]);
134                         return;
135                 }
136                 return;
137         }
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);
147
148         val_user(who, 0); /* Display registration */
149
150         if (newnow) {
151                 newprompt("Password: ", pass, -19);
152         }
153         else if (boolprompt("Change password", 0)) {
154                 strprompt("Password", pass, -19);
155         }
156
157         axlevel = intprompt("Access level", axlevel, 0, 6);
158         if (boolprompt("Ask user to register again", !(flags & US_REGIS)))
159                 flags &= ~US_REGIS;
160         else
161                 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);
167
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);
171         serv_puts(buf);
172         serv_gets(buf);
173         if (buf[0]!='2') {
174                 scr_printf("%s\n",&buf[4]);
175         }
176 }
177
178
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.
182  */
183 int set_attr(int sval, char *prompt, unsigned int sbit, int backwards)
184 {
185         int a;
186         int temp;
187
188         temp = sval;
189         color(DIM_WHITE);
190         scr_printf("%45s ", prompt);
191         color(DIM_MAGENTA);
192         scr_printf("[");
193         color(BRIGHT_MAGENTA);
194
195         if (backwards) {
196                 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
197         }
198         else {
199                 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
200         }
201
202         color(DIM_MAGENTA);
203         scr_printf("]? ");
204         color(BRIGHT_CYAN);
205         a = (temp & sbit);
206         if (a != 0) a = 1;
207         if (backwards) a = 1 - a;
208         a = yesno_d(a);
209         if (backwards) a = 1 - a;
210         color(DIM_WHITE);
211         temp = (temp|sbit);
212         if (!a) temp = (temp^sbit);
213         return(temp);
214 }
215
216 /*
217  * modes are:  0 - .EC command, 1 - .EC for new user,
218  *             2 - toggle Xpert mode  3 - toggle floor mode
219  */
220 void enter_config(int mode)
221 {
222         int width, height, flags, filter;
223         char buf[128];
224
225         snprintf(buf, sizeof buf, "GETU");
226         serv_puts(buf);
227         serv_gets(buf);
228         if (buf[0]!='2') {
229                 scr_printf("%s\n",&buf[4]);
230                 return;
231         }
232
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);
237
238         if ((mode==0)||(mode==1)) {
239
240          width = intprompt("Enter your screen width",width,20,255);
241          height = intprompt("Enter your screen height",height,3,255);
242  
243          flags = set_attr(flags,
244                 "Are you an experienced Citadel user",
245                 US_EXPERT,
246                 0);
247          if ( ((flags&US_EXPERT)==0) && (mode==1)) {
248                 return;
249          }
250          flags = set_attr(flags,
251                 "Print last old message on New message request",
252                 US_LASTOLD,
253                 0);
254
255          flags = set_attr(flags,
256                 "Prompt after each message",
257                 US_NOPROMPT,
258                 1);
259
260          if ((flags & US_NOPROMPT)==0)
261             flags = set_attr(flags,
262                 "Use 'disappearing' prompts",
263                 US_DISAPPEAR,
264                 0);
265
266          flags = set_attr(flags,
267                 "Pause after each screenful of text",
268                 US_PAGINATOR,
269                 0);
270
271          if ( (rc_prompt_control == 3) && (flags & US_PAGINATOR) ) {
272                 flags = set_attr(flags,
273                         "<N>ext and <S>top work at paginator prompt",
274                         US_PROMPTCTL,
275                         0);
276          }
277
278          if (rc_floor_mode == RC_DEFAULT) {
279           flags = set_attr(flags,
280                 "View rooms by floor",
281                 US_FLOORS,
282                 0);
283           }
284
285          if (rc_ansi_color == 3) {
286           flags = set_attr(flags,
287                 "Enable color support",
288                 US_COLOR,
289                 0);
290           }
291         
292
293           if ((flags&US_EXPERT)==0) formout("unlisted");
294           flags = set_attr(flags,
295                 "Be unlisted in userlog",
296                 US_UNLISTED,
297                 0);
298
299          }
300
301         if (mode==2) {
302          if (flags & US_EXPERT) {
303                 flags = (flags ^ US_EXPERT);
304                 scr_printf("Expert mode now OFF\n");
305                 }
306          else {
307                 flags = (flags | US_EXPERT);
308                 scr_printf("Expert mode now ON\n");
309                 }
310          }
311
312         if (mode==3) {
313          if (flags & US_FLOORS) {
314                 flags = (flags ^ US_FLOORS);
315                 scr_printf("Floor mode now OFF\n");
316                 }
317          else {
318                 flags = (flags | US_FLOORS);
319                 scr_printf("Floor mode now ON\n");
320                 }
321          }
322
323         snprintf(buf, sizeof buf, "SETU %d|%d|%d|%d",width,height,flags,filter);
324         serv_puts(buf);
325         serv_gets(buf);
326         if (buf[0]!='2') scr_printf("%s\n",&buf[4]);
327         userflags = flags;
328 }
329
330 /*
331  * getstring()  -  get a line of text from a file
332  *                 ignores lines beginning with "#"
333  */
334 int getstring(FILE *fp, char *string)
335 {
336         int a,c;
337         do {
338                 strcpy(string,"");
339                 a=0;
340                 do {
341                         c=getc(fp);
342                         if (c<0) {
343                                 string[a]=0;
344                                 return(-1);
345                         }
346                         string[a++]=c;
347                 } while(c!=10);
348                         string[a-1]=0;
349         } while(string[0]=='#');
350         return(strlen(string));
351 }
352
353
354 /* Searches for patn in search string */
355 int pattern(char *search, char *patn) {
356         int a,b;
357
358         for (a=0; a<strlen(search); ++a) {
359                 b=strncasecmp(&search[a],patn,strlen(patn));
360                 if (b==0) return(b);
361         }
362         return(-1);
363 }
364
365
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);
370         inkey();
371         logoff(errnum);
372 }
373
374 void strproc(char *string)
375 {
376         int a;
377
378         if (strlen(string)==0) return;
379
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;
384         }
385
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;
389
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]);
394                         a=0;
395                 }
396         }
397
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]);
406         }
407
408 }
409
410
411 #ifndef HAVE_STRERROR
412 /*
413  * replacement strerror() for systems that don't have it
414  */
415 char *strerror(int e)
416 {
417         static char buf[128];
418
419         snprintf(buf, sizeof buf, "errno = %d",e);
420         return(buf);
421 }
422 #endif
423
424
425 void progress(long int curr, long int cmax)
426 {
427         static long dots_printed;
428         long a;
429
430         if (curr==0) {
431                 scr_printf(".......................................");
432                 scr_printf(".......................................\r");
433                 scr_flush();
434                 dots_printed = 0;
435         }
436         else if (curr==cmax) {
437                 scr_printf("\r%79s\n","");
438         }
439         else {
440                 a=(curr * 100) / cmax;
441                 a=a*78; a=a/100;
442                 while (dots_printed < a) {
443                         scr_printf("*");
444                         ++dots_printed;
445                         scr_flush();
446                 }
447         }
448 }
449
450
451 /*
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...
454  */
455 void locate_host(char *hbuf)
456 {
457 #ifndef HAVE_UTMP_H
458         char buf[SIZ];
459         FILE *who;
460         int a,b;
461
462         who = (FILE *)popen("who am i","r");
463         if (who==NULL) {
464                 strcpy(hbuf,serv_info.serv_fqdn);
465                 return; 
466         }
467         fgets(buf,sizeof buf,who);
468         pclose(who);
469
470         b = 0;
471         for (a=0; a<strlen(buf); ++a) {
472                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
473         }
474         if (b<2) {
475                 strcpy(hbuf,serv_info.serv_fqdn);
476                 return;
477         }
478
479         for (a=0; a<strlen(buf); ++a) {
480                 if (buf[a]=='(') {
481                         strcpy(buf,&buf[a+1]);
482                 }
483         }
484         for (a=0; a<strlen(buf); ++a) {
485                 if (buf[a]==')') buf[a] = 0;
486         }
487
488         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
489         else strncpy(hbuf,buf,24);
490 #else
491         char *tty = ttyname(0);
492 #ifdef HAVE_GETUTXLINE
493         struct utmpx ut, *put;
494 #else
495         struct utmp ut, *put;
496 #endif
497
498         if (tty == NULL) {
499             fail:
500                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
501                 return;
502         }
503
504         if (strncmp(tty, "/dev/", 5))
505                 goto fail;
506
507         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
508
509 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
510         if ((put = getutxline(&ut)) == NULL)
511 #else
512         if ((put = getutline(&ut)) == NULL)
513 #endif
514                 goto fail;
515
516 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
517         if (put->ut_type == USER_PROCESS) {
518 #endif
519 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
520                 if (*put->ut_host)
521                         safestrncpy(hbuf, put->ut_host, 24);
522                 else
523 #endif
524                         safestrncpy(hbuf, put->ut_line, 24);
525 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
526         }
527         else goto fail;
528 #endif
529 #endif /* HAVE_UTMP_H */
530 }
531
532 /*
533  * miscellaneous server commands (testing, etc.)
534  */
535 void misc_server_cmd(char *cmd) {
536         char buf[SIZ];
537
538         serv_puts(cmd);
539         serv_gets(buf);
540         scr_printf("%s\n",buf);
541         if (buf[0]=='1') {
542                 set_keepalives(KA_HALF);
543                 while (serv_gets(buf), strcmp(buf,"000")) {
544                         scr_printf("%s\n",buf);
545                 }
546                 set_keepalives(KA_YES);
547                 return;
548         }
549         if (buf[0]=='4') {
550                 do {
551                         newprompt("> ",buf,255);
552                         serv_puts(buf);
553                 } while(strcmp(buf,"000"));
554                 return;
555         }
556 }
557
558
559 /*
560  * compute the checksum of a file
561  */
562 int file_checksum(char *filename)
563 {
564         int cksum = 0;
565         int ch;
566         FILE *fp;
567
568         fp = fopen(filename,"r");
569         if (fp == NULL) return(0);
570
571         /* yes, this algorithm may allow cksum to overflow, but that's ok
572          * as long as it overflows consistently, which it will.
573          */
574         while (ch=getc(fp), ch>=0) {
575                 cksum = (cksum + ch);
576         }
577
578         fclose(fp);
579         return(cksum);
580 }
581
582 /*
583  * nuke a directory and its contents
584  */
585 int nukedir(char *dirname)
586 {
587         DIR *dp;
588         struct dirent *d;
589         char filename[SIZ];
590
591         dp = opendir(dirname);
592         if (dp == NULL) {
593                 return(errno);
594         }
595
596         while (d = readdir(dp), d != NULL) {
597                 snprintf(filename, sizeof filename, "%s/%s",
598                         dirname, d->d_name);
599                 unlink(filename);
600         }
601
602         closedir(dp);
603         return(rmdir(dirname));
604 }