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