]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
fixes for BSDI. see ChangeLog.
[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
23 #ifndef HAVE_GETUTLINE
24 struct utmp *getutline(struct utmp *ut);
25 #endif
26
27 #define ROUTINES_C
28
29 #include "citadel.h"
30 #include "routines.h"
31 #include "commands.h"
32 #include "tools.h"
33
34 void sttybbs(int cmd);
35 void newprompt(char *prompt, char *str, int len);
36 void val_user(char *, int);
37 void formout(char *name);
38 void logoff(int code);
39 void set_keepalives(int s);
40 void strprompt(char *prompt, char *str, int len);
41 void newprompt(char *prompt, char *str, int len);
42 void color(int colornum);
43
44 #define IFAIDE if(axlevel>=6)
45 #define IFNAIDE if (axlevel<6)
46
47 extern unsigned userflags;
48 extern char *axdefs[7];
49 extern char sigcaught;
50 extern struct CtdlServInfo serv_info;
51 extern char rc_floor_mode;
52 extern int rc_ansi_color;
53
54 int struncmp(char *lstr, char *rstr, int len)
55 {
56         int pos = 0;
57         char lc,rc;
58         while (pos<len) {
59                 lc=tolower(lstr[pos]);
60                 rc=tolower(rstr[pos]);
61                 if ((lc==0)&&(rc==0)) return(0);
62                 if (lc<rc) return(-1);
63                 if (lc>rc) return(1);
64                 pos=pos+1;
65                 }
66         return(0);
67         }
68
69
70 /* 
71  * check for the presence of a character within a string (returns count)
72  */
73 int haschar(char *st, int ch)
74 {
75         int a,b;
76         b=0;
77         for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
78         return(b);
79         }
80
81
82 void back(int spaces) /* Destructive backspace */
83             {
84 int a;
85         for (a=1; a<=spaces; ++a) {
86                 putc(8,stdout); putc(32,stdout); putc(8,stdout);
87                 }
88         }
89
90 void hit_any_key(void) {                /* hit any key to continue */
91         int a,b;
92
93         printf("%s\r",serv_info.serv_moreprompt);
94         sttybbs(0);
95         b=inkey();
96         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
97                 putc(' ',stdout);
98         putc(13,stdout);
99         sttybbs(1);
100         if (b==NEXT_KEY) sigcaught = SIGINT;
101         if (b==STOP_KEY) sigcaught = SIGQUIT;
102         }
103
104 /*
105  * change a user's access level
106  */
107 void edituser(void)
108 {
109         char buf[256];
110         char who[256];
111         char pass[256];
112         int flags;
113         int timescalled;
114         int posted;
115         int axlevel;
116         long usernum;
117         time_t lastcall;
118         int userpurge;
119
120         newprompt("User name: ",who,25);
121         sprintf(buf,"AGUP %s",who);
122         serv_puts(buf);
123         serv_gets(buf);
124         if (buf[0]!='2') {
125                 printf("%s\n",&buf[4]);
126                 return;
127                 }
128         extract(who, &buf[4], 0);
129         extract(pass, &buf[4], 1);
130         flags = extract_int(&buf[4], 2);
131         timescalled = extract_int(&buf[4], 3);
132         posted = extract_int(&buf[4], 4);
133         axlevel = extract_int(&buf[4], 5);
134         usernum = extract_long(&buf[4], 6);
135         lastcall = extract_long(&buf[4], 7);
136         userpurge = extract_int(&buf[4], 8);
137
138         val_user(who, 0); /* Display registration */
139         strprompt("Password", pass, 19);
140         axlevel = intprompt("Access level", axlevel, 0, 6);
141         timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
142         posted = intprompt("Messages posted", posted, 0, INT_MAX);
143         lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
144         userpurge = intprompt("Purge time (in days, 0 for system default",
145                                 userpurge, 0, INT_MAX);
146
147         sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
148                 who, pass, flags, timescalled, posted, axlevel, usernum,
149                 lastcall, userpurge);
150         serv_puts(buf);
151         serv_gets(buf);
152         if (buf[0]!='2') {
153                 printf("%s\n",&buf[4]);
154                 }
155         }
156
157
158 int set_attr(int sval, char *prompt, unsigned int sbit)
159 {
160         int a;
161         int temp;
162
163         temp = sval;
164         color(3);
165         printf("%45s [", prompt);
166         color(1);
167         printf("%3s", ((temp&sbit) ? "Yes":"No"));
168         color(3);
169         printf("]? ");
170         color(2);
171         a=yesno_d(temp&sbit);
172         color(7);
173         temp=(temp|sbit);
174         if (!a) temp=(temp^sbit);
175         return(temp);
176         }
177
178 /*
179  * modes are:  0 - .EC command, 1 - .EC for new user,
180  *             2 - toggle Xpert mode  3 - toggle floor mode
181  */
182 void enter_config(int mode)
183 {
184         int width,height,flags;
185         char buf[128];
186
187         sprintf(buf,"GETU");
188         serv_puts(buf);
189         serv_gets(buf);
190         if (buf[0]!='2') {
191                 printf("%s\n",&buf[4]);
192                 return;
193                 }
194
195         width = extract_int(&buf[4],0);
196         height = extract_int(&buf[4],1);
197         flags = extract_int(&buf[4],2);
198
199         if ((mode==0)||(mode==1)) {
200
201          width = intprompt("Enter your screen width",width,20,255);
202          height = intprompt("Enter your screen height",height,3,255);
203  
204          flags = set_attr(flags,
205                 "Are you an experienced Citadel user",US_EXPERT);
206          if ( ((flags&US_EXPERT)==0) && (mode==1))
207                 return;
208          flags = set_attr(flags,
209                 "Print last old message on New message request",US_LASTOLD);
210          if ((flags&US_EXPERT)==0) formout("unlisted");
211          flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
212          flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
213          if ((flags & US_NOPROMPT)==0)
214             flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
215          flags = set_attr(flags,
216                 "Pause after each screenful of text",US_PAGINATOR);
217          if (rc_floor_mode == RC_DEFAULT) {
218           flags = set_attr(flags,
219                 "View rooms by floor",US_FLOORS);
220           }
221          if (rc_ansi_color == 3) {
222           flags = set_attr(flags,
223                 "Enable color support",US_COLOR);
224           }
225          }
226
227         if (mode==2) {
228          if (flags & US_EXPERT) {
229                 flags = (flags ^ US_EXPERT);
230                 printf("Expert mode now OFF\n");
231                 }
232          else {
233                 flags = (flags | US_EXPERT);
234                 printf("Expert mode now ON\n");
235                 }
236          }
237
238         if (mode==3) {
239          if (flags & US_FLOORS) {
240                 flags = (flags ^ US_FLOORS);
241                 printf("Floor mode now OFF\n");
242                 }
243          else {
244                 flags = (flags | US_FLOORS);
245                 printf("Floor mode now ON\n");
246                 }
247          }
248
249         sprintf(buf,"SETU %d|%d|%d",width,height,flags);
250         serv_puts(buf);
251         serv_gets(buf);
252         if (buf[0]!='2') printf("%s\n",&buf[4]);
253         userflags = flags;
254 }
255
256 /*
257  * getstring()  -  get a line of text from a file
258  *                 ignores lines beginning with "#"
259  */
260 int getstring(FILE *fp, char *string)
261 {
262         int a,c;
263         do {
264                 strcpy(string,"");
265                 a=0;
266                 do {
267                         c=getc(fp);
268                         if (c<0) {
269                                 string[a]=0;
270                                 return(-1);
271                                 }
272                         string[a++]=c;
273                         } while(c!=10);
274                         string[a-1]=0;
275                 } while(string[0]=='#');
276         return(strlen(string));
277         }
278
279 int pattern(char *search, char *patn)   /* Searches for patn in search string */
280               
281             
282 {
283         int a,b;
284         for (a=0; a<strlen(search); ++a)
285         {       b=struncmp(&search[a],patn,strlen(patn));
286                 if (b==0) return(b);
287                 }
288         return(-1);
289 }
290
291 void interr(int errnum) /* display internal error as defined in errmsgs */
292             {
293         printf("*** INTERNAL ERROR %d\n",errnum);
294         printf("(Press any key to continue)\n");
295         inkey();
296         logoff(errnum);
297 }
298
299
300
301 /*
302  * Check to see if we need to pause at the end of a screen.
303  * If we do, we have to disable server keepalives during the pause because
304  * we are probably in the middle of a server operation and the NOOP command
305  * would confuse everything.
306  */
307 int checkpagin(int lp, int pagin, int height)
308 {
309         if (pagin!=1) return(0);
310         if (lp>=(height-1)) {
311                 set_keepalives(KA_NO);
312                 hit_any_key();
313                 set_keepalives(KA_YES);
314                 return(0);
315                 }
316         return(lp);
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[32];
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                 printf(".......................................");
378                 printf(".......................................\r");
379                 fflush(stdout);
380                 dots_printed = 0;
381                 }
382         else if (curr==cmax) {
383                 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                         printf("*");
390                         ++dots_printed;
391                         fflush(stdout);
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[256];
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,256,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         struct utmp ut, *put;
439
440         if (tty == NULL) {
441             fail:
442                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
443                 return;
444                 }
445
446         if (strncmp(tty, "/dev/", 5))
447                 goto fail;
448
449         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
450
451         if ((put = getutline(&ut)) == NULL)
452                 goto fail;
453
454 #ifdef HAVE_UT_TYPE
455         if (put->ut_type == USER_PROCESS) {
456 #endif
457                 if (*put->ut_host)
458                         safestrncpy(hbuf, put->ut_host, 24);
459                 else
460                         safestrncpy(hbuf, put->ut_line, 24);
461 #ifdef HAVE_UT_TYPE
462                 }
463         else goto fail;
464 #endif
465 #endif /* HAVE_UTMP_H */
466         }
467
468 /*
469  * miscellaneous server commands (testing, etc.)
470  */
471 void misc_server_cmd(char *cmd) {
472         char buf[256];
473
474         serv_puts(cmd);
475         serv_gets(buf);
476         printf("%s\n",buf);
477         if (buf[0]=='1') {
478                 set_keepalives(KA_NO);
479                 while (serv_gets(buf), strcmp(buf,"000")) {
480                         printf("%s\n",buf);
481                         }
482                 set_keepalives(KA_YES);
483                 return;
484                 }
485         if (buf[0]=='4') {
486                 do {
487                         newprompt("> ",buf,255);
488                         serv_puts(buf);
489                         } while(strcmp(buf,"000"));
490                 return;
491                 }
492         }
493
494
495 /*
496  * compute the checksum of a file
497  */
498 int file_checksum(char *filename)
499 {
500         int cksum = 0;
501         int ch;
502         FILE *fp;
503
504         fp = fopen(filename,"r");
505         if (fp == NULL) return(0);
506
507         /* yes, this algorithm may allow cksum to overflow, but that's ok
508          * as long as it overflows consistently, which it will.
509          */
510         while (ch=getc(fp), ch>=0) {
511                 cksum = (cksum + ch);
512                 }
513
514         fclose(fp);
515         return(cksum);
516         }
517
518 /*
519  * nuke a directory and its contents
520  */
521 int nukedir(char *dirname)
522 {
523         DIR *dp;
524         struct dirent *d;
525         char filename[256];
526
527         dp = opendir(dirname);
528         if (dp == NULL) {
529                 return(errno);
530                 }
531
532         while (d = readdir(dp), d != NULL) {
533                 sprintf(filename, "%s/%s", dirname, d->d_name);
534                 unlink(filename);
535                 }
536
537         closedir(dp);
538         return(rmdir(dirname));
539         }