]> code.citadel.org Git - citadel.git/blob - citadel/routines.c
* "Suppress message prompts" has been changed to "Prompt after each message"
[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 #ifdef HAVE_LIMITS_H
33 #include <limits.h>
34 #endif
35 #ifdef HAVE_UTMP_H
36 #include <utmp.h>
37 #endif
38 #ifdef HAVE_UTMPX_H
39 #include <utmpx.h>
40 #endif
41 #include "screen.h"
42
43 #ifndef HAVE_GETUTLINE
44 struct utmp *getutline(struct utmp *ut);
45 #endif
46
47 #define ROUTINES_C
48
49 #include "citadel.h"
50 #include "routines.h"
51 #include "commands.h"
52 #include "tools.h"
53 #include "citadel_decls.h"
54 #include "routines2.h"
55
56 #define IFAIDE if(axlevel>=6)
57 #define IFNAIDE if (axlevel<6)
58
59 extern unsigned userflags;
60 extern char *axdefs[7];
61 extern char sigcaught;
62 extern struct CtdlServInfo serv_info;
63 extern char rc_floor_mode;
64 extern int rc_ansi_color;
65 extern int rc_prompt_control;
66
67 /* Destructive backspace */
68 void back(int spaces) {
69         int a;
70         for (a=0; a<spaces; ++a) {
71                 scr_putc(8);
72                 scr_putc(32);
73                 scr_putc(8);
74         }
75 }
76
77 void hit_any_key(void) {                /* hit any key to continue */
78         int a,b;
79
80         color(COLOR_PUSH);
81         color(DIM_RED);
82         scr_printf("%s\r",serv_info.serv_moreprompt);
83         color(COLOR_POP);
84         sttybbs(0);
85         b=inkey();
86         for (a=0; a<strlen(serv_info.serv_moreprompt); ++a)
87                 scr_putc(' ');
88         scr_putc(13);
89         sttybbs(1);
90         if ( (rc_prompt_control == 1)
91            || ((rc_prompt_control == 3) && (userflags & US_PROMPTCTL)) ) {
92                 if (b == 'q' || b == 'Q' || b == 's' || b == 'S')
93                         b = STOP_KEY;
94                 if (b == 'n' || b == 'N')
95                         b = NEXT_KEY;
96         }
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         int newnow = 0;
117
118         newprompt("User name: ",who,25);
119 AGUP:   snprintf(buf, sizeof buf, "AGUP %s",who);
120         serv_puts(buf);
121         serv_gets(buf);
122         if (buf[0]!='2') {
123                 scr_printf("%s\n",&buf[4]);
124                 scr_printf("Do you want to create this user? ");
125                 if (yesno()) {
126                         snprintf(buf, sizeof buf, "CREU %s", who);
127                         serv_puts(buf);
128                         serv_gets(buf);
129                         if (buf[0] == '2') {
130                                 newnow = 1;
131                                 goto AGUP;
132                         }
133                         scr_printf("%s\n",&buf[4]);
134                         return;
135                 }
136                 return;
137         }
138         extract(who, &buf[4], 0);
139         extract(pass, &buf[4], 1);
140         flags = extract_int(&buf[4], 2);
141         timescalled = extract_int(&buf[4], 3);
142         posted = extract_int(&buf[4], 4);
143         axlevel = extract_int(&buf[4], 5);
144         usernum = extract_long(&buf[4], 6);
145         lastcall = extract_long(&buf[4], 7);
146         userpurge = extract_int(&buf[4], 8);
147
148         val_user(who, 0); /* Display registration */
149
150         if (newnow) {
151                 newprompt("Password: ", pass, -19);
152         }
153         else if (boolprompt("Change password", 0)) {
154                 strprompt("Password", pass, -19);
155         }
156
157         axlevel = intprompt("Access level", axlevel, 0, 6);
158         if (boolprompt("Ask user to register again", !(flags & US_REGIS)))
159                 flags &= ~US_REGIS;
160         else
161                 flags |= US_REGIS;
162         timescalled = intprompt("Times called", timescalled, 0, INT_MAX);
163         posted = intprompt("Messages posted", posted, 0, INT_MAX);
164         lastcall = (boolprompt("Set last call to now", 0)?time(NULL):lastcall);
165         userpurge = intprompt("Purge time (in days, 0 for system default",
166                                 userpurge, 0, INT_MAX);
167
168         snprintf(buf, sizeof buf, "ASUP %s|%s|%d|%d|%d|%d|%ld|%ld|%d",
169                 who, pass, flags, timescalled, posted, axlevel, usernum,
170                 (long)lastcall, userpurge);
171         serv_puts(buf);
172         serv_gets(buf);
173         if (buf[0]!='2') {
174                 scr_printf("%s\n",&buf[4]);
175         }
176 }
177
178
179 /* Display a prompt and flip a bit based on whether the user answers
180  * yes or no.  Yes=1 and No=0, unless 'backwards' is set to a nonzero value
181  * in which case No=1 and Yes=0.
182  */
183 int set_attr(int sval, char *prompt, unsigned int sbit, int backwards)
184 {
185         int a;
186         int temp;
187
188         temp = sval;
189         color(DIM_WHITE);
190         scr_printf("%45s ", prompt);
191         color(DIM_MAGENTA);
192         scr_printf("[");
193         color(BRIGHT_MAGENTA);
194
195         if (backwards) {
196                 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
197         }
198         else {
199                 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
200         }
201
202         color(DIM_MAGENTA);
203         scr_printf("]? ");
204         color(BRIGHT_CYAN);
205         a = (temp & sbit);
206         if (a != 0) a = 1;
207         if (backwards) a = 1 - a;
208         a = yesno_d(a);
209         if (backwards) a = 1 - a;
210         color(DIM_WHITE);
211         temp = (temp|sbit);
212         if (!a) temp = (temp^sbit);
213         return(temp);
214 }
215
216 /*
217  * modes are:  0 - .EC command, 1 - .EC for new user,
218  *             2 - toggle Xpert mode  3 - toggle floor mode
219  */
220 void enter_config(int mode)
221 {
222         int width, height, flags, filter;
223         char buf[128];
224
225         snprintf(buf, sizeof buf, "GETU");
226         serv_puts(buf);
227         serv_gets(buf);
228         if (buf[0]!='2') {
229                 scr_printf("%s\n",&buf[4]);
230                 return;
231         }
232
233         width = extract_int(&buf[4],0);
234         height = extract_int(&buf[4],1);
235         flags = extract_int(&buf[4],2);
236         filter = extract_int(&buf[4],3);
237
238         if ((mode==0)||(mode==1)) {
239
240          width = intprompt("Enter your screen width",width,20,255);
241          height = intprompt("Enter your screen height",height,3,255);
242  
243          flags = set_attr(flags,
244                 "Are you an experienced Citadel user",
245                 US_EXPERT,
246                 0);
247          if ( ((flags&US_EXPERT)==0) && (mode==1)) {
248                 return;
249          }
250          flags = set_attr(flags,
251                 "Print last old message on New message request",
252                 US_LASTOLD,
253                 0);
254
255          flags = set_attr(flags,
256                 "Prompt after each message",
257                 US_NOPROMPT,
258                 1);
259
260          if ((flags & US_NOPROMPT)==0)
261             flags = set_attr(flags,
262                 "Use 'disappearing' prompts",
263                 US_DISAPPEAR,
264                 0);
265
266          flags = set_attr(flags,
267                 "Pause after each screenful of text",
268                 US_PAGINATOR,
269                 0);
270
271          if ( (rc_prompt_control == 3) && (flags & US_PAGINATOR) ) {
272                 flags = set_attr(flags,
273                         "<N>ext and <S>top work at paginator prompt",
274                         US_PROMPTCTL,
275                         0);
276          }
277
278          if (rc_floor_mode == RC_DEFAULT) {
279           flags = set_attr(flags,
280                 "View rooms by floor",
281                 US_FLOORS,
282                 0);
283           }
284
285          if (rc_ansi_color == 3) {
286           flags = set_attr(flags,
287                 "Enable color support",
288                 US_COLOR,
289                 0);
290           }
291         
292          }
293
294          if ((flags&US_EXPERT)==0) formout("unlisted");
295          flags = set_attr(flags,
296                 "Be unlisted in userlog",
297                 US_UNLISTED,
298                 0);
299
300         if (mode==2) {
301          if (flags & US_EXPERT) {
302                 flags = (flags ^ US_EXPERT);
303                 scr_printf("Expert mode now OFF\n");
304                 }
305          else {
306                 flags = (flags | US_EXPERT);
307                 scr_printf("Expert mode now ON\n");
308                 }
309          }
310
311         if (mode==3) {
312          if (flags & US_FLOORS) {
313                 flags = (flags ^ US_FLOORS);
314                 scr_printf("Floor mode now OFF\n");
315                 }
316          else {
317                 flags = (flags | US_FLOORS);
318                 scr_printf("Floor mode now ON\n");
319                 }
320          }
321
322         snprintf(buf, sizeof buf, "SETU %d|%d|%d|%d",width,height,flags,filter);
323         serv_puts(buf);
324         serv_gets(buf);
325         if (buf[0]!='2') scr_printf("%s\n",&buf[4]);
326         userflags = flags;
327 }
328
329 /*
330  * getstring()  -  get a line of text from a file
331  *                 ignores lines beginning with "#"
332  */
333 int getstring(FILE *fp, char *string)
334 {
335         int a,c;
336         do {
337                 strcpy(string,"");
338                 a=0;
339                 do {
340                         c=getc(fp);
341                         if (c<0) {
342                                 string[a]=0;
343                                 return(-1);
344                         }
345                         string[a++]=c;
346                 } while(c!=10);
347                         string[a-1]=0;
348         } while(string[0]=='#');
349         return(strlen(string));
350 }
351
352
353 /* Searches for patn in search string */
354 int pattern(char *search, char *patn) {
355         int a,b;
356
357         for (a=0; a<strlen(search); ++a) {
358                 b=strncasecmp(&search[a],patn,strlen(patn));
359                 if (b==0) return(b);
360         }
361         return(-1);
362 }
363
364
365 /* display internal error as defined in errmsgs */
366 void interr(int errnum) {
367         scr_printf("*** INTERNAL ERROR %d\n"
368                 "(Press any key to continue)\n", errnum);
369         inkey();
370         logoff(errnum);
371 }
372
373 void strproc(char *string)
374 {
375         int a;
376
377         if (strlen(string)==0) return;
378
379         /* Convert non-printable characters to blanks */
380         for (a=0; a<strlen(string); ++a) {
381                 if (string[a]<32) string[a]=32;
382                 if (string[a]>126) string[a]=32;
383         }
384
385         /* Remove leading and trailing blanks */
386         while(string[0]<33) strcpy(string,&string[1]);
387         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
388
389         /* Remove double blanks */
390         for (a=0; a<strlen(string); ++a) {
391                 if ((string[a]==32)&&(string[a+1]==32)) {
392                         strcpy(&string[a],&string[a+1]);
393                         a=0;
394                 }
395         }
396
397         /* remove characters which would interfere with the network */
398         for (a=0; a<strlen(string); ++a) {
399                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
400                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
401                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
402                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
403                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
404                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
405         }
406
407 }
408
409
410 #ifndef HAVE_STRERROR
411 /*
412  * replacement strerror() for systems that don't have it
413  */
414 char *strerror(int e)
415 {
416         static char buf[128];
417
418         snprintf(buf, sizeof buf, "errno = %d",e);
419         return(buf);
420 }
421 #endif
422
423
424 void progress(long int curr, long int cmax)
425 {
426         static long dots_printed;
427         long a;
428
429         if (curr==0) {
430                 scr_printf(".......................................");
431                 scr_printf(".......................................\r");
432                 scr_flush();
433                 dots_printed = 0;
434         }
435         else if (curr==cmax) {
436                 scr_printf("\r%79s\n","");
437         }
438         else {
439                 a=(curr * 100) / cmax;
440                 a=a*78; a=a/100;
441                 while (dots_printed < a) {
442                         scr_printf("*");
443                         ++dots_printed;
444                         scr_flush();
445                 }
446         }
447 }
448
449
450 /*
451  * NOT the same locate_host() in locate_host.c.  This one just does a
452  * 'who am i' to try to discover where the user is...
453  */
454 void locate_host(char *hbuf)
455 {
456 #ifndef HAVE_UTMP_H
457         char buf[SIZ];
458         FILE *who;
459         int a,b;
460
461         who = (FILE *)popen("who am i","r");
462         if (who==NULL) {
463                 strcpy(hbuf,serv_info.serv_fqdn);
464                 return; 
465         }
466         fgets(buf,sizeof buf,who);
467         pclose(who);
468
469         b = 0;
470         for (a=0; a<strlen(buf); ++a) {
471                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
472         }
473         if (b<2) {
474                 strcpy(hbuf,serv_info.serv_fqdn);
475                 return;
476         }
477
478         for (a=0; a<strlen(buf); ++a) {
479                 if (buf[a]=='(') {
480                         strcpy(buf,&buf[a+1]);
481                 }
482         }
483         for (a=0; a<strlen(buf); ++a) {
484                 if (buf[a]==')') buf[a] = 0;
485         }
486
487         if (strlen(buf)==0) strcpy(hbuf,serv_info.serv_fqdn);
488         else strncpy(hbuf,buf,24);
489 #else
490         char *tty = ttyname(0);
491 #ifdef HAVE_GETUTXLINE
492         struct utmpx ut, *put;
493 #else
494         struct utmp ut, *put;
495 #endif
496
497         if (tty == NULL) {
498             fail:
499                 safestrncpy(hbuf, serv_info.serv_fqdn, 24);
500                 return;
501         }
502
503         if (strncmp(tty, "/dev/", 5))
504                 goto fail;
505
506         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
507
508 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
509         if ((put = getutxline(&ut)) == NULL)
510 #else
511         if ((put = getutline(&ut)) == NULL)
512 #endif
513                 goto fail;
514
515 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
516         if (put->ut_type == USER_PROCESS) {
517 #endif
518 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
519                 if (*put->ut_host)
520                         safestrncpy(hbuf, put->ut_host, 24);
521                 else
522 #endif
523                         safestrncpy(hbuf, put->ut_line, 24);
524 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
525         }
526         else goto fail;
527 #endif
528 #endif /* HAVE_UTMP_H */
529 }
530
531 /*
532  * miscellaneous server commands (testing, etc.)
533  */
534 void misc_server_cmd(char *cmd) {
535         char buf[SIZ];
536
537         serv_puts(cmd);
538         serv_gets(buf);
539         scr_printf("%s\n",buf);
540         if (buf[0]=='1') {
541                 set_keepalives(KA_HALF);
542                 while (serv_gets(buf), strcmp(buf,"000")) {
543                         scr_printf("%s\n",buf);
544                 }
545                 set_keepalives(KA_YES);
546                 return;
547         }
548         if (buf[0]=='4') {
549                 do {
550                         newprompt("> ",buf,255);
551                         serv_puts(buf);
552                 } while(strcmp(buf,"000"));
553                 return;
554         }
555 }
556
557
558 /*
559  * compute the checksum of a file
560  */
561 int file_checksum(char *filename)
562 {
563         int cksum = 0;
564         int ch;
565         FILE *fp;
566
567         fp = fopen(filename,"r");
568         if (fp == NULL) return(0);
569
570         /* yes, this algorithm may allow cksum to overflow, but that's ok
571          * as long as it overflows consistently, which it will.
572          */
573         while (ch=getc(fp), ch>=0) {
574                 cksum = (cksum + ch);
575         }
576
577         fclose(fp);
578         return(cksum);
579 }
580
581 /*
582  * nuke a directory and its contents
583  */
584 int nukedir(char *dirname)
585 {
586         DIR *dp;
587         struct dirent *d;
588         char filename[SIZ];
589
590         dp = opendir(dirname);
591         if (dp == NULL) {
592                 return(errno);
593         }
594
595         while (d = readdir(dp), d != NULL) {
596                 snprintf(filename, sizeof filename, "%s/%s",
597                         dirname, d->d_name);
598                 unlink(filename);
599         }
600
601         closedir(dp);
602         return(rmdir(dirname));
603 }