]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
* Aides can now ask a user to re-register with .Aide User
[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
54 void sttybbs(int cmd);
55 void newprompt(char *prompt, char *str, int len);
56 void val_user(char *, int);
57 void formout(char *name);
58 void logoff(int code);
59 void set_keepalives(int s);
60 void strprompt(char *prompt, char *str, int len);
61 void newprompt(char *prompt, char *str, int len);
62 void color(int colornum);
63
64 #define IFAIDE if(axlevel>=6)
65 #define IFNAIDE if (axlevel<6)
66
67 extern unsigned userflags;
68 extern char *axdefs[7];
69 extern char sigcaught;
70 extern struct CtdlServInfo serv_info;
71 extern char rc_floor_mode;
72 extern int rc_ansi_color;
73
74 void back(int spaces) /* Destructive backspace */
75             {
76         int a;
77         for (a=1; a<=spaces; ++a) {
78                 scr_putc(8); scr_putc(32); scr_putc(8);
79                 }
80         }
81
82 void hit_any_key(void) {                /* hit any key to continue */
83         int a,b;
84
85         color(COLOR_PUSH);
86         color(DIM_RED);
87         scr_printf("%s\r",serv_info.serv_moreprompt);
88         color(COLOR_POP);
89         sttybbs(0);
90         b=inkey();
91         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
92                 scr_putc(' ');
93         scr_putc(13);
94         sttybbs(1);
95         if (b == 'q' || b == 'Q' || b == 's' || b == 'S')
96                 b = STOP_KEY;
97         if (b == 'n' || b == 'N')
98                 b = NEXT_KEY;
99         if (b==NEXT_KEY) sigcaught = SIGINT;
100         if (b==STOP_KEY) sigcaught = SIGQUIT;
101 }
102
103 /*
104  * change a user's access level
105  */
106 void edituser(void)
107 {
108         char buf[SIZ];
109         char who[SIZ];
110         char pass[SIZ];
111         int flags;
112         int timescalled;
113         int posted;
114         int axlevel;
115         long usernum;
116         time_t lastcall;
117         int userpurge;
118
119         newprompt("User name: ",who,25);
120         sprintf(buf,"AGUP %s",who);
121         serv_puts(buf);
122         serv_gets(buf);
123         if (buf[0]!='2') {
124                 scr_printf("%s\n",&buf[4]);
125                 return;
126                 }
127         extract(who, &buf[4], 0);
128         extract(pass, &buf[4], 1);
129         flags = extract_int(&buf[4], 2);
130         timescalled = extract_int(&buf[4], 3);
131         posted = extract_int(&buf[4], 4);
132         axlevel = extract_int(&buf[4], 5);
133         usernum = extract_long(&buf[4], 6);
134         lastcall = extract_long(&buf[4], 7);
135         userpurge = extract_int(&buf[4], 8);
136
137         val_user(who, 0); /* Display registration */
138         if (boolprompt("Change password", 0)) {
139                 strprompt("Password", pass, 19);
140         }
141         axlevel = intprompt("Access level", axlevel, 0, 6);
142         if (boolprompt("Ask user to register again", !(flags & US_REGIS)))
143                 flags &= ~US_REGIS;
144         else
145                 flags |= US_REGIS;
146         timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
147         posted = intprompt("Messages posted", posted, 0, INT_MAX);
148         lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
149         userpurge = intprompt("Purge time (in days, 0 for system default",
150                                 userpurge, 0, INT_MAX);
151
152         sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
153                 who, pass, flags, timescalled, posted, axlevel, usernum,
154                 (long)lastcall, userpurge);
155         serv_puts(buf);
156         serv_gets(buf);
157         if (buf[0]!='2') {
158                 scr_printf("%s\n",&buf[4]);
159                 }
160         }
161
162
163 int set_attr(int sval, char *prompt, unsigned int sbit)
164 {
165         int a;
166         int temp;
167
168         temp = sval;
169         color(DIM_WHITE);
170         scr_printf("%45s ", prompt);
171         color(DIM_MAGENTA);
172         scr_printf("[");
173         color(BRIGHT_MAGENTA);
174         scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
175         color(DIM_MAGENTA);
176         scr_printf("]? ");
177         color(BRIGHT_CYAN);
178         a=yesno_d(temp&sbit);
179         color(DIM_WHITE);
180         temp=(temp|sbit);
181         if (!a) temp=(temp^sbit);
182         return(temp);
183         }
184
185 /*
186  * modes are:  0 - .EC command, 1 - .EC for new user,
187  *             2 - toggle Xpert mode  3 - toggle floor mode
188  */
189 void enter_config(int mode)
190 {
191         int width, height, flags, filter;
192         char buf[128];
193
194         sprintf(buf,"GETU");
195         serv_puts(buf);
196         serv_gets(buf);
197         if (buf[0]!='2') {
198                 scr_printf("%s\n",&buf[4]);
199                 return;
200                 }
201
202         width = extract_int(&buf[4],0);
203         height = extract_int(&buf[4],1);
204         flags = extract_int(&buf[4],2);
205         filter = extract_int(&buf[4],3);
206
207         if ((mode==0)||(mode==1)) {
208
209          width = intprompt("Enter your screen width",width,20,255);
210          height = intprompt("Enter your screen height",height,3,255);
211  
212          flags = set_attr(flags,
213                 "Are you an experienced Citadel user",US_EXPERT);
214          if ( ((flags&US_EXPERT)==0) && (mode==1))
215                 return;
216          flags = set_attr(flags,
217                 "Print last old message on New message request",US_LASTOLD);
218          if ((flags&US_EXPERT)==0) formout("unlisted");
219          flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
220          flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
221          if ((flags & US_NOPROMPT)==0)
222             flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
223          flags = set_attr(flags,
224                 "Pause after each screenful of text",US_PAGINATOR);
225          if (rc_floor_mode == RC_DEFAULT) {
226           flags = set_attr(flags,
227                 "View rooms by floor",US_FLOORS);
228           }
229          if (rc_ansi_color == 3) {
230           flags = set_attr(flags,
231                 "Enable color support",US_COLOR);
232           }
233         
234          /* filter = intprompt("Moderation filter level", filter, -63, 63); */
235
236          }
237
238         if (mode==2) {
239          if (flags & US_EXPERT) {
240                 flags = (flags ^ US_EXPERT);
241                 scr_printf("Expert mode now OFF\n");
242                 }
243          else {
244                 flags = (flags | US_EXPERT);
245                 scr_printf("Expert mode now ON\n");
246                 }
247          }
248
249         if (mode==3) {
250          if (flags & US_FLOORS) {
251                 flags = (flags ^ US_FLOORS);
252                 scr_printf("Floor mode now OFF\n");
253                 }
254          else {
255                 flags = (flags | US_FLOORS);
256                 scr_printf("Floor mode now ON\n");
257                 }
258          }
259
260         sprintf(buf,"SETU %d|%d|%d|%d",width,height,flags,filter);
261         serv_puts(buf);
262         serv_gets(buf);
263         if (buf[0]!='2') scr_printf("%s\n",&buf[4]);
264         userflags = flags;
265 }
266
267 /*
268  * getstring()  -  get a line of text from a file
269  *                 ignores lines beginning with "#"
270  */
271 int getstring(FILE *fp, char *string)
272 {
273         int a,c;
274         do {
275                 strcpy(string,"");
276                 a=0;
277                 do {
278                         c=getc(fp);
279                         if (c<0) {
280                                 string[a]=0;
281                                 return(-1);
282                                 }
283                         string[a++]=c;
284                         } while(c!=10);
285                         string[a-1]=0;
286                 } while(string[0]=='#');
287         return(strlen(string));
288         }
289
290 int pattern(char *search, char *patn)   /* Searches for patn in search string */
291               
292             
293 {
294         int a,b;
295         for (a=0; a<strlen(search); ++a)
296         {       b=strncasecmp(&search[a],patn,strlen(patn));
297                 if (b==0) return(b);
298                 }
299         return(-1);
300 }
301
302 void interr(int errnum) /* display internal error as defined in errmsgs */
303             {
304         scr_printf("*** INTERNAL ERROR %d\n"
305                 "(Press any key to continue)\n", errnum);
306         inkey();
307         logoff(errnum);
308 }
309
310
311
312 void strproc(char *string)
313 {
314         int a;
315
316         if (strlen(string)==0) return;
317
318         /* Convert non-printable characters to blanks */
319         for (a=0; a<strlen(string); ++a) {
320                 if (string[a]<32) string[a]=32;
321                 if (string[a]>126) string[a]=32;
322                 }
323
324         /* Remove leading and trailing blanks */
325         while(string[0]<33) strcpy(string,&string[1]);
326         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
327
328         /* Remove double blanks */
329         for (a=0; a<strlen(string); ++a) {
330                 if ((string[a]==32)&&(string[a+1]==32)) {
331                         strcpy(&string[a],&string[a+1]);
332                         a=0;
333                         }
334                 }
335
336         /* remove characters which would interfere with the network */
337         for (a=0; a<strlen(string); ++a) {
338                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
339                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
340                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
341                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
342                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
343                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
344                 }
345
346         }
347
348
349 #ifndef HAVE_STRERROR
350 /*
351  * replacement strerror() for systems that don't have it
352  */
353 char *strerror(int e)
354 {
355         static char buf[128];
356
357         sprintf(buf,"errno = %d",e);
358         return(buf);
359         }
360 #endif
361
362
363 void progress(long int curr, long int cmax)
364 {
365         static long dots_printed;
366         long a;
367
368         if (curr==0) {
369                 scr_printf(".......................................");
370                 scr_printf(".......................................\r");
371                 scr_flush();
372                 dots_printed = 0;
373                 }
374         else if (curr==cmax) {
375                 scr_printf("\r%79s\n","");
376                 }
377         else {
378                 a=(curr * 100) / cmax;
379                 a=a*78; a=a/100;
380                 while (dots_printed < a) {
381                         scr_printf("*");
382                         ++dots_printed;
383                         scr_flush();
384                         }
385                 }
386         }
387
388
389 /*
390  * NOT the same locate_host() in locate_host.c.  This one just does a
391  * 'who am i' to try to discover where the user is...
392  */
393 void locate_host(char *hbuf)
394 {
395 #ifndef HAVE_UTMP_H
396         char buf[SIZ];
397         FILE *who;
398         int a,b;
399
400         who = (FILE *)popen("who am i","r");
401         if (who==NULL) {
402                 strcpy(hbuf,serv_info.serv_fqdn);
403                 return; 
404                 }
405         fgets(buf,sizeof buf,who);
406         pclose(who);
407
408         b = 0;
409         for (a=0; a<strlen(buf); ++a) {
410                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
411                 }
412         if (b<2) {
413                 strcpy(hbuf,serv_info.serv_fqdn);
414                 return;
415                 }
416
417         for (a=0; a<strlen(buf); ++a) {
418                 if (buf[a]=='(') {
419                         strcpy(buf,&buf[a+1]);
420                         }
421                 }
422         for (a=0; a<strlen(buf); ++a) {
423                 if (buf[a]==')') buf[a] = 0;
424                 }
425
426         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
427         else strncpy(hbuf,buf,24);
428 #else
429         char *tty = ttyname(0);
430 #ifdef HAVE_GETUTXLINE
431         struct utmpx ut, *put;
432 #else
433         struct utmp ut, *put;
434 #endif
435
436         if (tty == NULL) {
437             fail:
438                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
439                 return;
440                 }
441
442         if (strncmp(tty, "/dev/", 5))
443                 goto fail;
444
445         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
446
447 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
448         if ((put = getutxline(&ut)) == NULL)
449 #else
450         if ((put = getutline(&ut)) == NULL)
451 #endif
452                 goto fail;
453
454 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
455         if (put->ut_type == USER_PROCESS) {
456 #endif
457 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
458                 if (*put->ut_host)
459                         safestrncpy(hbuf, put->ut_host, 24);
460                 else
461 #endif
462                         safestrncpy(hbuf, put->ut_line, 24);
463 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
464                 }
465         else goto fail;
466 #endif
467 #endif /* HAVE_UTMP_H */
468         }
469
470 /*
471  * miscellaneous server commands (testing, etc.)
472  */
473 void misc_server_cmd(char *cmd) {
474         char buf[SIZ];
475
476         serv_puts(cmd);
477         serv_gets(buf);
478         scr_printf("%s\n",buf);
479         if (buf[0]=='1') {
480                 set_keepalives(KA_NO);
481                 while (serv_gets(buf), strcmp(buf,"000")) {
482                         scr_printf("%s\n",buf);
483                         }
484                 set_keepalives(KA_YES);
485                 return;
486                 }
487         if (buf[0]=='4') {
488                 do {
489                         newprompt("> ",buf,255);
490                         serv_puts(buf);
491                         } while(strcmp(buf,"000"));
492                 return;
493                 }
494         }
495
496
497 /*
498  * compute the checksum of a file
499  */
500 int file_checksum(char *filename)
501 {
502         int cksum = 0;
503         int ch;
504         FILE *fp;
505
506         fp = fopen(filename,"r");
507         if (fp == NULL) return(0);
508
509         /* yes, this algorithm may allow cksum to overflow, but that's ok
510          * as long as it overflows consistently, which it will.
511          */
512         while (ch=getc(fp), ch>=0) {
513                 cksum = (cksum + ch);
514                 }
515
516         fclose(fp);
517         return(cksum);
518         }
519
520 /*
521  * nuke a directory and its contents
522  */
523 int nukedir(char *dirname)
524 {
525         DIR *dp;
526         struct dirent *d;
527         char filename[SIZ];
528
529         dp = opendir(dirname);
530         if (dp == NULL) {
531                 return(errno);
532                 }
533
534         while (d = readdir(dp), d != NULL) {
535                 sprintf(filename, "%s/%s", dirname, d->d_name);
536                 unlink(filename);
537                 }
538
539         closedir(dp);
540         return(rmdir(dirname));
541         }