fix all the <time.h> vs. <sys/time.h> issues, hopefully
[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
33 #include <limits.h>
34 #ifdef HAVE_UTMP_H
35 #include <utmp.h>
36 #endif
37 #ifdef HAVE_UTMPX_H
38 #include <utmpx.h>
39 #endif
40
41 #ifndef HAVE_GETUTLINE
42 struct utmp *getutline(struct utmp *ut);
43 #endif
44
45 #define ROUTINES_C
46
47 #include "citadel.h"
48 #include "routines.h"
49 #include "commands.h"
50 #include "tools.h"
51
52 void sttybbs(int cmd);
53 void newprompt(char *prompt, char *str, int len);
54 void val_user(char *, int);
55 void formout(char *name);
56 void logoff(int code);
57 void set_keepalives(int s);
58 void strprompt(char *prompt, char *str, int len);
59 void newprompt(char *prompt, char *str, int len);
60 void color(int colornum);
61
62 #define IFAIDE if(axlevel>=6)
63 #define IFNAIDE if (axlevel<6)
64
65 extern unsigned userflags;
66 extern char *axdefs[7];
67 extern char sigcaught;
68 extern struct CtdlServInfo serv_info;
69 extern char rc_floor_mode;
70 extern int rc_ansi_color;
71
72 void back(int spaces) /* Destructive backspace */
73             {
74         int a;
75         for (a=1; a<=spaces; ++a) {
76                 putc(8,stdout); putc(32,stdout); putc(8,stdout);
77                 }
78         }
79
80 void hit_any_key(void) {                /* hit any key to continue */
81         int a,b;
82
83         color(COLOR_PUSH);
84         color(DIM_RED);
85         printf("%s\r",serv_info.serv_moreprompt);
86         color(COLOR_POP);
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[SIZ];
103         char who[SIZ];
104         char pass[SIZ];
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, 0); /* Display registration */
132         if (boolprompt("Change password", 0)) {
133                 strprompt("Password", pass, 19);
134         }
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, filter;
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         filter = extract_int(&buf[4],3);
196
197         if ((mode==0)||(mode==1)) {
198
199          width = intprompt("Enter your screen width",width,20,255);
200          height = intprompt("Enter your screen height",height,3,255);
201  
202          flags = set_attr(flags,
203                 "Are you an experienced Citadel user",US_EXPERT);
204          if ( ((flags&US_EXPERT)==0) && (mode==1))
205                 return;
206          flags = set_attr(flags,
207                 "Print last old message on New message request",US_LASTOLD);
208          if ((flags&US_EXPERT)==0) formout("unlisted");
209          flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
210          flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
211          if ((flags & US_NOPROMPT)==0)
212             flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
213          flags = set_attr(flags,
214                 "Pause after each screenful of text",US_PAGINATOR);
215          if (rc_floor_mode == RC_DEFAULT) {
216           flags = set_attr(flags,
217                 "View rooms by floor",US_FLOORS);
218           }
219          if (rc_ansi_color == 3) {
220           flags = set_attr(flags,
221                 "Enable color support",US_COLOR);
222           }
223         
224          /* filter = intprompt("Moderation filter level", filter, -63, 63); */
225
226          }
227
228         if (mode==2) {
229          if (flags & US_EXPERT) {
230                 flags = (flags ^ US_EXPERT);
231                 printf("Expert mode now OFF\n");
232                 }
233          else {
234                 flags = (flags | US_EXPERT);
235                 printf("Expert mode now ON\n");
236                 }
237          }
238
239         if (mode==3) {
240          if (flags & US_FLOORS) {
241                 flags = (flags ^ US_FLOORS);
242                 printf("Floor mode now OFF\n");
243                 }
244          else {
245                 flags = (flags | US_FLOORS);
246                 printf("Floor mode now ON\n");
247                 }
248          }
249
250         sprintf(buf,"SETU %d|%d|%d|%d",width,height,flags,filter);
251         serv_puts(buf);
252         serv_gets(buf);
253         if (buf[0]!='2') printf("%s\n",&buf[4]);
254         userflags = flags;
255 }
256
257 /*
258  * getstring()  -  get a line of text from a file
259  *                 ignores lines beginning with "#"
260  */
261 int getstring(FILE *fp, char *string)
262 {
263         int a,c;
264         do {
265                 strcpy(string,"");
266                 a=0;
267                 do {
268                         c=getc(fp);
269                         if (c<0) {
270                                 string[a]=0;
271                                 return(-1);
272                                 }
273                         string[a++]=c;
274                         } while(c!=10);
275                         string[a-1]=0;
276                 } while(string[0]=='#');
277         return(strlen(string));
278         }
279
280 int pattern(char *search, char *patn)   /* Searches for patn in search string */
281               
282             
283 {
284         int a,b;
285         for (a=0; a<strlen(search); ++a)
286         {       b=strncasecmp(&search[a],patn,strlen(patn));
287                 if (b==0) return(b);
288                 }
289         return(-1);
290 }
291
292 void interr(int errnum) /* display internal error as defined in errmsgs */
293             {
294         printf("*** INTERNAL ERROR %d\n",errnum);
295         printf("(Press any key to continue)\n");
296         inkey();
297         logoff(errnum);
298 }
299
300
301
302 void strproc(char *string)
303 {
304         int a;
305
306         if (strlen(string)==0) return;
307
308         /* Convert non-printable characters to blanks */
309         for (a=0; a<strlen(string); ++a) {
310                 if (string[a]<32) string[a]=32;
311                 if (string[a]>126) string[a]=32;
312                 }
313
314         /* Remove leading and trailing blanks */
315         while(string[0]<33) strcpy(string,&string[1]);
316         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
317
318         /* Remove double blanks */
319         for (a=0; a<strlen(string); ++a) {
320                 if ((string[a]==32)&&(string[a+1]==32)) {
321                         strcpy(&string[a],&string[a+1]);
322                         a=0;
323                         }
324                 }
325
326         /* remove characters which would interfere with the network */
327         for (a=0; a<strlen(string); ++a) {
328                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
329                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
330                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
331                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
332                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
333                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
334                 }
335
336         }
337
338
339 #ifndef HAVE_STRERROR
340 /*
341  * replacement strerror() for systems that don't have it
342  */
343 char *strerror(int e)
344 {
345         static char buf[32];
346
347         sprintf(buf,"errno = %d",e);
348         return(buf);
349         }
350 #endif
351
352
353 void progress(long int curr, long int cmax)
354 {
355         static long dots_printed;
356         long a;
357
358         if (curr==0) {
359                 printf(".......................................");
360                 printf(".......................................\r");
361                 fflush(stdout);
362                 dots_printed = 0;
363                 }
364         else if (curr==cmax) {
365                 printf("\r%79s\n","");
366                 }
367         else {
368                 a=(curr * 100) / cmax;
369                 a=a*78; a=a/100;
370                 while (dots_printed < a) {
371                         printf("*");
372                         ++dots_printed;
373                         fflush(stdout);
374                         }
375                 }
376         }
377
378
379 /*
380  * NOT the same locate_host() in locate_host.c.  This one just does a
381  * 'who am i' to try to discover where the user is...
382  */
383 void locate_host(char *hbuf)
384 {
385 #ifndef HAVE_UTMP_H
386         char buf[SIZ];
387         FILE *who;
388         int a,b;
389
390         who = (FILE *)popen("who am i","r");
391         if (who==NULL) {
392                 strcpy(hbuf,serv_info.serv_fqdn);
393                 return; 
394                 }
395         fgets(buf,sizeof buf,who);
396         pclose(who);
397
398         b = 0;
399         for (a=0; a<strlen(buf); ++a) {
400                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
401                 }
402         if (b<2) {
403                 strcpy(hbuf,serv_info.serv_fqdn);
404                 return;
405                 }
406
407         for (a=0; a<strlen(buf); ++a) {
408                 if (buf[a]=='(') {
409                         strcpy(buf,&buf[a+1]);
410                         }
411                 }
412         for (a=0; a<strlen(buf); ++a) {
413                 if (buf[a]==')') buf[a] = 0;
414                 }
415
416         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
417         else strncpy(hbuf,buf,24);
418 #else
419         char *tty = ttyname(0);
420 #ifdef HAVE_GETUTXLINE
421         struct utmpx ut, *put;
422 #else
423         struct utmp ut, *put;
424 #endif
425
426         if (tty == NULL) {
427             fail:
428                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
429                 return;
430                 }
431
432         if (strncmp(tty, "/dev/", 5))
433                 goto fail;
434
435         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
436
437 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
438         if ((put = getutxline(&ut)) == NULL)
439 #else
440         if ((put = getutline(&ut)) == NULL)
441 #endif
442                 goto fail;
443
444 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
445         if (put->ut_type == USER_PROCESS) {
446 #endif
447 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
448                 if (*put->ut_host)
449                         safestrncpy(hbuf, put->ut_host, 24);
450                 else
451 #endif
452                         safestrncpy(hbuf, put->ut_line, 24);
453 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
454                 }
455         else goto fail;
456 #endif
457 #endif /* HAVE_UTMP_H */
458         }
459
460 /*
461  * miscellaneous server commands (testing, etc.)
462  */
463 void misc_server_cmd(char *cmd) {
464         char buf[SIZ];
465
466         serv_puts(cmd);
467         serv_gets(buf);
468         printf("%s\n",buf);
469         if (buf[0]=='1') {
470                 set_keepalives(KA_NO);
471                 while (serv_gets(buf), strcmp(buf,"000")) {
472                         printf("%s\n",buf);
473                         }
474                 set_keepalives(KA_YES);
475                 return;
476                 }
477         if (buf[0]=='4') {
478                 do {
479                         newprompt("> ",buf,255);
480                         serv_puts(buf);
481                         } while(strcmp(buf,"000"));
482                 return;
483                 }
484         }
485
486
487 /*
488  * compute the checksum of a file
489  */
490 int file_checksum(char *filename)
491 {
492         int cksum = 0;
493         int ch;
494         FILE *fp;
495
496         fp = fopen(filename,"r");
497         if (fp == NULL) return(0);
498
499         /* yes, this algorithm may allow cksum to overflow, but that's ok
500          * as long as it overflows consistently, which it will.
501          */
502         while (ch=getc(fp), ch>=0) {
503                 cksum = (cksum + ch);
504                 }
505
506         fclose(fp);
507         return(cksum);
508         }
509
510 /*
511  * nuke a directory and its contents
512  */
513 int nukedir(char *dirname)
514 {
515         DIR *dp;
516         struct dirent *d;
517         char filename[SIZ];
518
519         dp = opendir(dirname);
520         if (dp == NULL) {
521                 return(errno);
522                 }
523
524         while (d = readdir(dp), d != NULL) {
525                 sprintf(filename, "%s/%s", dirname, d->d_name);
526                 unlink(filename);
527                 }
528
529         closedir(dp);
530         return(rmdir(dirname));
531         }