]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
* configure.in, Makefile.in, getutline.c: replace getutline() on
[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 #define ROUTINES_C
24
25 #include "citadel.h"
26 #include "routines.h"
27 #include "commands.h"
28 #include "tools.h"
29
30 void sttybbs(int cmd);
31 void newprompt(char *prompt, char *str, int len);
32 void val_user(char *, int);
33 void formout(char *name);
34 void logoff(int code);
35 void set_keepalives(int s);
36 void strprompt(char *prompt, char *str, int len);
37 void newprompt(char *prompt, char *str, int len);
38 void color(int colornum);
39
40 #define IFAIDE if(axlevel>=6)
41 #define IFNAIDE if (axlevel<6)
42
43 extern unsigned userflags;
44 extern char *axdefs[7];
45 extern char sigcaught;
46 extern struct CtdlServInfo serv_info;
47 extern char rc_floor_mode;
48 extern int rc_ansi_color;
49
50 int struncmp(char *lstr, char *rstr, int len)
51 {
52         int pos = 0;
53         char lc,rc;
54         while (pos<len) {
55                 lc=tolower(lstr[pos]);
56                 rc=tolower(rstr[pos]);
57                 if ((lc==0)&&(rc==0)) return(0);
58                 if (lc<rc) return(-1);
59                 if (lc>rc) return(1);
60                 pos=pos+1;
61                 }
62         return(0);
63         }
64
65
66 /* 
67  * check for the presence of a character within a string (returns count)
68  */
69 int haschar(char *st, int ch)
70 {
71         int a,b;
72         b=0;
73         for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
74         return(b);
75         }
76
77
78 void back(int spaces) /* Destructive backspace */
79             {
80 int a;
81         for (a=1; a<=spaces; ++a) {
82                 putc(8,stdout); putc(32,stdout); putc(8,stdout);
83                 }
84         }
85
86 void hit_any_key(void) {                /* hit any key to continue */
87         int a,b;
88
89         printf("%s\r",serv_info.serv_moreprompt);
90         sttybbs(0);
91         b=inkey();
92         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
93                 putc(' ',stdout);
94         putc(13,stdout);
95         sttybbs(1);
96         if (b==NEXT_KEY) sigcaught = SIGINT;
97         if (b==STOP_KEY) sigcaught = SIGQUIT;
98         }
99
100 /*
101  * change a user's access level
102  */
103 void edituser(void)
104 {
105         char buf[256];
106         char who[256];
107         char pass[256];
108         int flags;
109         int timescalled;
110         int posted;
111         int axlevel;
112         long usernum;
113         time_t lastcall;
114         int userpurge;
115
116         newprompt("User name: ",who,25);
117         sprintf(buf,"AGUP %s",who);
118         serv_puts(buf);
119         serv_gets(buf);
120         if (buf[0]!='2') {
121                 printf("%s\n",&buf[4]);
122                 return;
123                 }
124         extract(who, &buf[4], 0);
125         extract(pass, &buf[4], 1);
126         flags = extract_int(&buf[4], 2);
127         timescalled = extract_int(&buf[4], 3);
128         posted = extract_int(&buf[4], 4);
129         axlevel = extract_int(&buf[4], 5);
130         usernum = extract_long(&buf[4], 6);
131         lastcall = extract_long(&buf[4], 7);
132         userpurge = extract_int(&buf[4], 8);
133
134         val_user(who, 0); /* Display registration */
135         strprompt("Password", pass, 19);
136         axlevel = intprompt("Access level", axlevel, 0, 6);
137         timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
138         posted = intprompt("Messages posted", posted, 0, INT_MAX);
139         lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
140         userpurge = intprompt("Purge time (in days, 0 for system default",
141                                 userpurge, 0, INT_MAX);
142
143         sprintf(buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
144                 who, pass, flags, timescalled, posted, axlevel, usernum,
145                 lastcall, userpurge);
146         serv_puts(buf);
147         serv_gets(buf);
148         if (buf[0]!='2') {
149                 printf("%s\n",&buf[4]);
150                 }
151         }
152
153
154 int set_attr(int sval, char *prompt, unsigned int sbit)
155 {
156         int a;
157         int temp;
158
159         temp = sval;
160         color(3);
161         printf("%45s [", prompt);
162         color(1);
163         printf("%3s", ((temp&sbit) ? "Yes":"No"));
164         color(3);
165         printf("]? ");
166         color(2);
167         a=yesno_d(temp&sbit);
168         color(7);
169         temp=(temp|sbit);
170         if (!a) temp=(temp^sbit);
171         return(temp);
172         }
173
174 /*
175  * modes are:  0 - .EC command, 1 - .EC for new user,
176  *             2 - toggle Xpert mode  3 - toggle floor mode
177  */
178 void enter_config(int mode)
179 {
180         int width,height,flags;
181         char buf[128];
182
183         sprintf(buf,"GETU");
184         serv_puts(buf);
185         serv_gets(buf);
186         if (buf[0]!='2') {
187                 printf("%s\n",&buf[4]);
188                 return;
189                 }
190
191         width = extract_int(&buf[4],0);
192         height = extract_int(&buf[4],1);
193         flags = extract_int(&buf[4],2);
194
195         if ((mode==0)||(mode==1)) {
196
197          width = intprompt("Enter your screen width",width,20,255);
198          height = intprompt("Enter your screen height",height,3,255);
199  
200          flags = set_attr(flags,
201                 "Are you an experienced Citadel user",US_EXPERT);
202          if ( ((flags&US_EXPERT)==0) && (mode==1))
203                 return;
204          flags = set_attr(flags,
205                 "Print last old message on New message request",US_LASTOLD);
206          if ((flags&US_EXPERT)==0) formout("unlisted");
207          flags = set_attr(flags,"Be unlisted in userlog",US_UNLISTED);
208          flags = set_attr(flags,"Suppress message prompts",US_NOPROMPT);
209          if ((flags & US_NOPROMPT)==0)
210             flags = set_attr(flags,"Use 'disappearing' prompts",US_DISAPPEAR);
211          flags = set_attr(flags,
212                 "Pause after each screenful of text",US_PAGINATOR);
213          if (rc_floor_mode == RC_DEFAULT) {
214           flags = set_attr(flags,
215                 "View rooms by floor",US_FLOORS);
216           }
217          if (rc_ansi_color == 3) {
218           flags = set_attr(flags,
219                 "Enable color support",US_COLOR);
220           }
221          }
222
223         if (mode==2) {
224          if (flags & US_EXPERT) {
225                 flags = (flags ^ US_EXPERT);
226                 printf("Expert mode now OFF\n");
227                 }
228          else {
229                 flags = (flags | US_EXPERT);
230                 printf("Expert mode now ON\n");
231                 }
232          }
233
234         if (mode==3) {
235          if (flags & US_FLOORS) {
236                 flags = (flags ^ US_FLOORS);
237                 printf("Floor mode now OFF\n");
238                 }
239          else {
240                 flags = (flags | US_FLOORS);
241                 printf("Floor mode now ON\n");
242                 }
243          }
244
245         sprintf(buf,"SETU %d|%d|%d",width,height,flags);
246         serv_puts(buf);
247         serv_gets(buf);
248         if (buf[0]!='2') printf("%s\n",&buf[4]);
249         userflags = flags;
250 }
251
252 /*
253  * getstring()  -  get a line of text from a file
254  *                 ignores lines beginning with "#"
255  */
256 int getstring(FILE *fp, char *string)
257 {
258         int a,c;
259         do {
260                 strcpy(string,"");
261                 a=0;
262                 do {
263                         c=getc(fp);
264                         if (c<0) {
265                                 string[a]=0;
266                                 return(-1);
267                                 }
268                         string[a++]=c;
269                         } while(c!=10);
270                         string[a-1]=0;
271                 } while(string[0]=='#');
272         return(strlen(string));
273         }
274
275 int pattern(char *search, char *patn)   /* Searches for patn in search string */
276               
277             
278 {
279         int a,b;
280         for (a=0; a<strlen(search); ++a)
281         {       b=struncmp(&search[a],patn,strlen(patn));
282                 if (b==0) return(b);
283                 }
284         return(-1);
285 }
286
287 void interr(int errnum) /* display internal error as defined in errmsgs */
288             {
289         printf("*** INTERNAL ERROR %d\n",errnum);
290         printf("(Press any key to continue)\n");
291         inkey();
292         logoff(errnum);
293 }
294
295
296
297 /*
298  * Check to see if we need to pause at the end of a screen.
299  * If we do, we have to disable server keepalives during the pause because
300  * we are probably in the middle of a server operation and the NOOP command
301  * would confuse everything.
302  */
303 int checkpagin(int lp, int pagin, int height)
304 {
305         if (pagin!=1) return(0);
306         if (lp>=(height-1)) {
307                 set_keepalives(KA_NO);
308                 hit_any_key();
309                 set_keepalives(KA_YES);
310                 return(0);
311                 }
312         return(lp);
313         }
314
315
316 void strproc(char *string)
317 {
318         int a;
319
320         if (strlen(string)==0) return;
321
322         /* Convert non-printable characters to blanks */
323         for (a=0; a<strlen(string); ++a) {
324                 if (string[a]<32) string[a]=32;
325                 if (string[a]>126) string[a]=32;
326                 }
327
328         /* Remove leading and trailing blanks */
329         while(string[0]<33) strcpy(string,&string[1]);
330         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
331
332         /* Remove double blanks */
333         for (a=0; a<strlen(string); ++a) {
334                 if ((string[a]==32)&&(string[a+1]==32)) {
335                         strcpy(&string[a],&string[a+1]);
336                         a=0;
337                         }
338                 }
339
340         /* remove characters which would interfere with the network */
341         for (a=0; a<strlen(string); ++a) {
342                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
343                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
344                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
345                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
346                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
347                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
348                 }
349
350         }
351
352
353 #ifndef HAVE_STRERROR
354 /*
355  * replacement strerror() for systems that don't have it
356  */
357 char *strerror(int e)
358 {
359         static char buf[32];
360
361         sprintf(buf,"errno = %d",e);
362         return(buf);
363         }
364 #endif
365
366
367 void progress(long int curr, long int cmax)
368 {
369         static long dots_printed;
370         long a;
371
372         if (curr==0) {
373                 printf(".......................................");
374                 printf(".......................................\r");
375                 fflush(stdout);
376                 dots_printed = 0;
377                 }
378         else if (curr==cmax) {
379                 printf("\r%79s\n","");
380                 }
381         else {
382                 a=(curr * 100) / cmax;
383                 a=a*78; a=a/100;
384                 while (dots_printed < a) {
385                         printf("*");
386                         ++dots_printed;
387                         fflush(stdout);
388                         }
389                 }
390         }
391
392
393 /*
394  * NOT the same locate_host() in locate_host.c.  This one just does a
395  * 'who am i' to try to discover where the user is...
396  */
397 void locate_host(char *hbuf)
398 {
399 #ifndef HAVE_UTMP_H
400         char buf[256];
401         FILE *who;
402         int a,b;
403
404         who = (FILE *)popen("who am i","r");
405         if (who==NULL) {
406                 strcpy(hbuf,serv_info.serv_fqdn);
407                 return; 
408                 }
409         fgets(buf,256,who);
410         pclose(who);
411
412         b = 0;
413         for (a=0; a<strlen(buf); ++a) {
414                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
415                 }
416         if (b<2) {
417                 strcpy(hbuf,serv_info.serv_fqdn);
418                 return;
419                 }
420
421         for (a=0; a<strlen(buf); ++a) {
422                 if (buf[a]=='(') {
423                         strcpy(buf,&buf[a+1]);
424                         }
425                 }
426         for (a=0; a<strlen(buf); ++a) {
427                 if (buf[a]==')') buf[a] = 0;
428                 }
429
430         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
431         else strncpy(hbuf,buf,24);
432 #else
433         char *tty = ttyname(0);
434         struct utmp ut, *put;
435
436         if (tty == NULL) {
437             fail:
438                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
439                 return;
440                 }
441
442         if (strncmp(tty, "/dev/", 5))
443                 goto fail;
444
445         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
446
447         if ((put = getutline(&ut)) == NULL)
448                 goto fail;
449
450         if (put->ut_type == USER_PROCESS) {
451                 if (*put->ut_host)
452                         safestrncpy(hbuf, put->ut_host, 24);
453                 else
454                         safestrncpy(hbuf, put->ut_line, 24);
455                 }
456         else goto fail;
457 #endif
458         }
459
460 /*
461  * miscellaneous server commands (testing, etc.)
462  */
463 void misc_server_cmd(char *cmd) {
464         char buf[256];
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[256];
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         }