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