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