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