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