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