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