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