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