]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
* More paginator changes. Shuffled code around, added pagination to a bunch
[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 void strproc(char *string)
299 {
300         int a;
301
302         if (strlen(string)==0) return;
303
304         /* Convert non-printable characters to blanks */
305         for (a=0; a<strlen(string); ++a) {
306                 if (string[a]<32) string[a]=32;
307                 if (string[a]>126) string[a]=32;
308                 }
309
310         /* Remove leading and trailing blanks */
311         while(string[0]<33) strcpy(string,&string[1]);
312         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
313
314         /* Remove double blanks */
315         for (a=0; a<strlen(string); ++a) {
316                 if ((string[a]==32)&&(string[a+1]==32)) {
317                         strcpy(&string[a],&string[a+1]);
318                         a=0;
319                         }
320                 }
321
322         /* remove characters which would interfere with the network */
323         for (a=0; a<strlen(string); ++a) {
324                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
325                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
326                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
327                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
328                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
329                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
330                 }
331
332         }
333
334
335 #ifndef HAVE_STRERROR
336 /*
337  * replacement strerror() for systems that don't have it
338  */
339 char *strerror(int e)
340 {
341         static char buf[32];
342
343         sprintf(buf,"errno = %d",e);
344         return(buf);
345         }
346 #endif
347
348
349 void progress(long int curr, long int cmax)
350 {
351         static long dots_printed;
352         long a;
353
354         if (curr==0) {
355                 printf(".......................................");
356                 printf(".......................................\r");
357                 fflush(stdout);
358                 dots_printed = 0;
359                 }
360         else if (curr==cmax) {
361                 printf("\r%79s\n","");
362                 }
363         else {
364                 a=(curr * 100) / cmax;
365                 a=a*78; a=a/100;
366                 while (dots_printed < a) {
367                         printf("*");
368                         ++dots_printed;
369                         fflush(stdout);
370                         }
371                 }
372         }
373
374
375 /*
376  * NOT the same locate_host() in locate_host.c.  This one just does a
377  * 'who am i' to try to discover where the user is...
378  */
379 void locate_host(char *hbuf)
380 {
381 #ifndef HAVE_UTMP_H
382         char buf[256];
383         FILE *who;
384         int a,b;
385
386         who = (FILE *)popen("who am i","r");
387         if (who==NULL) {
388                 strcpy(hbuf,serv_info.serv_fqdn);
389                 return; 
390                 }
391         fgets(buf,256,who);
392         pclose(who);
393
394         b = 0;
395         for (a=0; a<strlen(buf); ++a) {
396                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
397                 }
398         if (b<2) {
399                 strcpy(hbuf,serv_info.serv_fqdn);
400                 return;
401                 }
402
403         for (a=0; a<strlen(buf); ++a) {
404                 if (buf[a]=='(') {
405                         strcpy(buf,&buf[a+1]);
406                         }
407                 }
408         for (a=0; a<strlen(buf); ++a) {
409                 if (buf[a]==')') buf[a] = 0;
410                 }
411
412         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
413         else strncpy(hbuf,buf,24);
414 #else
415         char *tty = ttyname(0);
416 #ifdef HAVE_GETUTXLINE
417         struct utmpx ut, *put;
418 #else
419         struct utmp ut, *put;
420 #endif
421
422         if (tty == NULL) {
423             fail:
424                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
425                 return;
426                 }
427
428         if (strncmp(tty, "/dev/", 5))
429                 goto fail;
430
431         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
432
433 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
434         if ((put = getutxline(&ut)) == NULL)
435 #else
436         if ((put = getutline(&ut)) == NULL)
437 #endif
438                 goto fail;
439
440 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
441         if (put->ut_type == USER_PROCESS) {
442 #endif
443 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
444                 if (*put->ut_host)
445                         safestrncpy(hbuf, put->ut_host, 24);
446                 else
447 #endif
448                         safestrncpy(hbuf, put->ut_line, 24);
449 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
450                 }
451         else goto fail;
452 #endif
453 #endif /* HAVE_UTMP_H */
454         }
455
456 /*
457  * miscellaneous server commands (testing, etc.)
458  */
459 void misc_server_cmd(char *cmd) {
460         char buf[256];
461
462         serv_puts(cmd);
463         serv_gets(buf);
464         printf("%s\n",buf);
465         if (buf[0]=='1') {
466                 set_keepalives(KA_NO);
467                 while (serv_gets(buf), strcmp(buf,"000")) {
468                         printf("%s\n",buf);
469                         }
470                 set_keepalives(KA_YES);
471                 return;
472                 }
473         if (buf[0]=='4') {
474                 do {
475                         newprompt("> ",buf,255);
476                         serv_puts(buf);
477                         } while(strcmp(buf,"000"));
478                 return;
479                 }
480         }
481
482
483 /*
484  * compute the checksum of a file
485  */
486 int file_checksum(char *filename)
487 {
488         int cksum = 0;
489         int ch;
490         FILE *fp;
491
492         fp = fopen(filename,"r");
493         if (fp == NULL) return(0);
494
495         /* yes, this algorithm may allow cksum to overflow, but that's ok
496          * as long as it overflows consistently, which it will.
497          */
498         while (ch=getc(fp), ch>=0) {
499                 cksum = (cksum + ch);
500                 }
501
502         fclose(fp);
503         return(cksum);
504         }
505
506 /*
507  * nuke a directory and its contents
508  */
509 int nukedir(char *dirname)
510 {
511         DIR *dp;
512         struct dirent *d;
513         char filename[256];
514
515         dp = opendir(dirname);
516         if (dp == NULL) {
517                 return(errno);
518                 }
519
520         while (d = readdir(dp), d != NULL) {
521                 sprintf(filename, "%s/%s", dirname, d->d_name);
522                 unlink(filename);
523                 }
524
525         closedir(dp);
526         return(rmdir(dirname));
527         }