* Moved num_parms() and all the extract() type functions into tools.c
[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
20 #define ROUTINES_C
21
22 #include "citadel.h"
23 #include "routines.h"
24 #include "commands.h"
25 #include "tools.h"
26
27 void sttybbs(int cmd);
28 void newprompt(char *prompt, char *str, int len);
29 void val_user(char *user);
30 void formout(char *name);
31 void logoff(int code);
32 void set_keepalives(int s);
33 void strprompt(char *prompt, char *str, int len);
34 void newprompt(char *prompt, char *str, int len);
35 void color(int colornum);
36
37 #define IFAIDE if(axlevel>=6)
38 #define IFNAIDE if (axlevel<6)
39
40 extern unsigned userflags;
41 extern char *axdefs[7];
42 extern char sigcaught;
43 extern struct CtdlServInfo serv_info;
44 extern char rc_floor_mode;
45 extern int rc_ansi_color;
46
47 int struncmp(char *lstr, char *rstr, int len)
48 {
49         int pos = 0;
50         char lc,rc;
51         while (pos<len) {
52                 lc=tolower(lstr[pos]);
53                 rc=tolower(rstr[pos]);
54                 if ((lc==0)&&(rc==0)) return(0);
55                 if (lc<rc) return(-1);
56                 if (lc>rc) return(1);
57                 pos=pos+1;
58                 }
59         return(0);
60         }
61
62
63 /* 
64  * check for the presence of a character within a string (returns count)
65  */
66 int haschar(char *st, int ch)
67 {
68         int a,b;
69         b=0;
70         for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
71         return(b);
72         }
73
74
75 void back(int spaces) /* Destructive backspace */
76             {
77 int a;
78         for (a=1; a<=spaces; ++a) {
79                 putc(8,stdout); putc(32,stdout); putc(8,stdout);
80                 }
81         }
82
83 void hit_any_key(void) {                /* hit any key to continue */
84         int a,b;
85
86         printf("%s\r",serv_info.serv_moreprompt);
87         sttybbs(0);
88         b=inkey();
89         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
90                 putc(' ',stdout);
91         putc(13,stdout);
92         sttybbs(1);
93         if (b==NEXT_KEY) sigcaught = SIGINT;
94         if (b==STOP_KEY) sigcaught = SIGQUIT;
95         }
96
97 /*
98  * change a user's access level
99  */
100 void edituser(void)
101 {
102         char buf[256];
103         char who[256];
104         char pass[256];
105         int flags;
106         int timescalled;
107         int posted;
108         int axlevel;
109         long usernum;
110         time_t lastcall;
111         int userpurge;
112
113         newprompt("User name: ",who,25);
114         sprintf(buf,"AGUP %s",who);
115         serv_puts(buf);
116         serv_gets(buf);
117         if (buf[0]!='2') {
118                 printf("%s\n",&buf[4]);
119                 return;
120                 }
121         extract(who, &buf[4], 0);
122         extract(pass, &buf[4], 1);
123         flags = extract_int(&buf[4], 2);
124         timescalled = extract_int(&buf[4], 3);
125         posted = extract_int(&buf[4], 4);
126         axlevel = extract_int(&buf[4], 5);
127         usernum = extract_long(&buf[4], 6);
128         lastcall = extract_long(&buf[4], 7);
129         userpurge = extract_int(&buf[4], 8);
130
131         val_user(who); /* Display registration, and set access level */
132         strprompt("Password", pass, 19);
133         timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
134         posted = intprompt("Messages posted", posted, 0, INT_MAX);
135         lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
136         userpurge = intprompt("Purge time (in days, 0 for system default",
137                                 userpurge, 0, INT_MAX);
138
139         sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
140                 who, pass, flags, timescalled, posted, axlevel, usernum,
141                 lastcall, userpurge);
142         serv_puts(buf);
143         serv_gets(buf);
144         if (buf[0]!='2') {
145                 printf("%s\n",&buf[4]);
146                 }
147         }
148
149
150 int set_attr(int sval, char *prompt, unsigned int sbit)
151 {
152         int a;
153         int temp;
154
155         temp = sval;
156         color(3);
157         printf("%45s [", prompt);
158         color(1);
159         printf("%3s", ((temp&sbit) ? "Yes":"No"));
160         color(3);
161         printf("]? ");
162         color(2);
163         a=yesno_d(temp&sbit);
164         color(7);
165         temp=(temp|sbit);
166         if (!a) temp=(temp^sbit);
167         return(temp);
168         }
169
170 /*
171  * modes are:  0 - .EC command, 1 - .EC for new user,
172  *             2 - toggle Xpert mode  3 - toggle floor mode
173  */
174 void enter_config(int mode)
175 {
176         int width,height,flags;
177         char buf[128];
178
179         sprintf(buf,"GETU");
180         serv_puts(buf);
181         serv_gets(buf);
182         if (buf[0]!='2') {
183                 printf("%s\n",&buf[4]);
184                 return;
185                 }
186
187         width = extract_int(&buf[4],0);
188         height = extract_int(&buf[4],1);
189         flags = extract_int(&buf[4],2);
190
191         if ((mode==0)||(mode==1)) {
192
193          width = intprompt("Enter your screen width",width,20,255);
194          height = intprompt("Enter your screen height",height,3,255);
195  
196          flags = set_attr(flags,
197                 "Are you an experienced Citadel user",US_EXPERT);
198          if ( ((flags&US_EXPERT)==0) && (mode==1))
199                 return;
200          flags = set_attr(flags,
201                 "Print last old message on New message request",US_LASTOLD);
202          if ((flags&US_EXPERT)==0) formout("unlisted");
203          flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
204          flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
205          if ((flags & US_NOPROMPT)==0)
206             flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
207          flags = set_attr(flags,
208                 "Pause after each screenful of text",US_PAGINATOR);
209          if (rc_floor_mode == RC_DEFAULT) {
210           flags = set_attr(flags,
211                 "View rooms by floor",US_FLOORS);
212           }
213          if (rc_ansi_color == 3) {
214           flags = set_attr(flags,
215                 "Enable color support",US_COLOR);
216           }
217          }
218
219         if (mode==2) {
220          if (flags & US_EXPERT) {
221                 flags = (flags ^ US_EXPERT);
222                 printf("Expert mode now OFF\n");
223                 }
224          else {
225                 flags = (flags | US_EXPERT);
226                 printf("Expert mode now ON\n");
227                 }
228          }
229
230         if (mode==3) {
231          if (flags & US_FLOORS) {
232                 flags = (flags ^ US_FLOORS);
233                 printf("Floor mode now OFF\n");
234                 }
235          else {
236                 flags = (flags | US_FLOORS);
237                 printf("Floor mode now ON\n");
238                 }
239          }
240
241         sprintf(buf,"SETU %d|%d|%d",width,height,flags);
242         serv_puts(buf);
243         serv_gets(buf);
244         if (buf[0]!='2') printf("%s\n",&buf[4]);
245         userflags = flags;
246 }
247
248 /*
249  * getstring()  -  get a line of text from a file
250  *                 ignores lines beginning with "#"
251  */
252 int getstring(FILE *fp, char *string)
253 {
254         int a,c;
255         do {
256                 strcpy(string,"");
257                 a=0;
258                 do {
259                         c=getc(fp);
260                         if (c<0) {
261                                 string[a]=0;
262                                 return(-1);
263                                 }
264                         string[a++]=c;
265                         } while(c!=10);
266                         string[a-1]=0;
267                 } while(string[0]=='#');
268         return(strlen(string));
269         }
270
271 int pattern(char *search, char *patn)   /* Searches for patn in search string */
272               
273             
274 {
275         int a,b;
276         for (a=0; a<strlen(search); ++a)
277         {       b=struncmp(&search[a],patn,strlen(patn));
278                 if (b==0) return(b);
279                 }
280         return(-1);
281 }
282
283 void interr(int errnum) /* display internal error as defined in errmsgs */
284             {
285         printf("*** INTERNAL ERROR %d\n",errnum);
286         printf("(Press any key to continue)\n");
287         inkey();
288         logoff(errnum);
289 }
290
291
292
293 /*
294  * Check to see if we need to pause at the end of a screen.
295  * If we do, we have to disable server keepalives during the pause because
296  * we are probably in the middle of a server operation and the NOOP command
297  * would confuse everything.
298  */
299 int checkpagin(int lp, int pagin, int height)
300 {
301         if (pagin!=1) return(0);
302         if (lp>=(height-1)) {
303                 set_keepalives(KA_NO);
304                 hit_any_key();
305                 set_keepalives(KA_YES);
306                 return(0);
307                 }
308         return(lp);
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[32];
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                 printf(".......................................");
370                 printf(".......................................\r");
371                 fflush(stdout);
372                 dots_printed = 0;
373                 }
374         else if (curr==cmax) {
375                 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                         printf("*");
382                         ++dots_printed;
383                         fflush(stdout);
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         char buf[256];
396         FILE *who;
397         int a,b;
398
399         who = (FILE *)popen("who am i","r");
400         if (who==NULL) {
401                 strcpy(hbuf,serv_info.serv_fqdn);
402                 return; 
403                 }
404         fgets(buf,256,who);
405         pclose(who);
406
407         b = 0;
408         for (a=0; a<strlen(buf); ++a) {
409                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
410                 }
411         if (b<2) {
412                 strcpy(hbuf,serv_info.serv_fqdn);
413                 return;
414                 }
415
416         for (a=0; a<strlen(buf); ++a) {
417                 if (buf[a]=='(') {
418                         strcpy(buf,&buf[a+1]);
419                         }
420                 }
421         for (a=0; a<strlen(buf); ++a) {
422                 if (buf[a]==')') buf[a] = 0;
423                 }
424
425         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
426         else strncpy(hbuf,buf,24);
427         }
428
429 /*
430  * miscellaneous server commands (testing, etc.)
431  */
432 void misc_server_cmd(char *cmd) {
433         char buf[256];
434
435         serv_puts(cmd);
436         serv_gets(buf);
437         printf("%s\n",buf);
438         if (buf[0]=='1') {
439                 while (serv_gets(buf), strcmp(buf,"000")) {
440                         printf("%s\n",buf);
441                         }
442                 return;
443                 }
444         if (buf[0]=='4') {
445                 do {
446                         newprompt("> ",buf,255);
447                         serv_puts(buf);
448                         } while(strcmp(buf,"000"));
449                 return;
450                 }
451         }
452
453
454 /*
455  * compute the checksum of a file
456  */
457 int file_checksum(char *filename)
458 {
459         int cksum = 0;
460         int ch;
461         FILE *fp;
462
463         fp = fopen(filename,"r");
464         if (fp == NULL) return(0);
465
466         /* yes, this algorithm may allow cksum to overflow, but that's ok
467          * as long as it overflows consistently, which it will.
468          */
469         while (ch=getc(fp), ch>=0) {
470                 cksum = (cksum + ch);
471                 }
472
473         fclose(fp);
474         return(cksum);
475         }
476
477 /*
478  * nuke a directory and its contents
479  */
480 int nukedir(char *dirname)
481 {
482         DIR *dp;
483         struct dirent *d;
484         char filename[256];
485
486         dp = opendir(dirname);
487         if (dp == NULL) {
488                 return(errno);
489                 }
490
491         while (d = readdir(dp), d != NULL) {
492                 sprintf(filename, "%s/%s", dirname, d->d_name);
493                 unlink(filename);
494                 }
495
496         closedir(dp);
497         return(rmdir(dirname));
498         }