* strlen holy war: loops. in loops it's very evil. the easy ones go away now.
[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; !IsEmptyStr(&ipc->ServInfo.moreprompt[0]); ++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 /*              user->flags = set_attr(ipc, user->flags,
140                         "Permission to send Internet mail",
141                         US_INTERNET, 0); */
142                 if (boolprompt("Permission to send Internet mail", (user->flags & US_INTERNET)))
143                         user->flags |= US_INTERNET;
144                 else
145                         user->flags &= ~US_INTERNET;
146                 if (boolprompt("Ask user to register again", !(user->flags & US_REGIS)))
147                         user->flags &= ~US_REGIS;
148                 else
149                         user->flags |= US_REGIS;
150                 user->timescalled = intprompt("Times called",
151                                 user->timescalled, 0, INT_MAX);
152                 user->posted = intprompt("Messages posted",
153                                         user->posted, 0, INT_MAX);
154                 user->lastcall = boolprompt("Set last call to now", 0) ?
155                                         time(NULL) : user->lastcall;
156                 user->USuserpurge = intprompt("Purge time (in days, 0 for system default",
157                                 user->USuserpurge, 0, INT_MAX);
158         }
159
160         if (cmd == 96) {
161                 scr_printf("Do you want to delete this user? ");
162                 if (!yesno()) return;
163                 user->axlevel = 0;
164         }
165         
166         r = CtdlIPCAideSetUserParameters(ipc, user, buf);
167         if (r / 100 != 2) {
168                 scr_printf("%s\n", buf);
169         }
170         free(user);
171 }
172
173
174 /* Display a prompt and flip a bit based on whether the user answers
175  * yes or no.  Yes=1 and No=0, unless 'backwards' is set to a nonzero value
176  * in which case No=1 and Yes=0.
177  */
178 int set_attr(CtdlIPC *ipc, unsigned int sval, char *prompt, unsigned int sbit, int backwards)
179 {
180         int a;
181         int temp;
182
183         temp = sval;
184         color(DIM_WHITE);
185         scr_printf("%50s ", prompt);
186         color(DIM_MAGENTA);
187         scr_printf("[");
188         color(BRIGHT_MAGENTA);
189
190         if (backwards) {
191                 scr_printf("%3s", ((temp&sbit) ? "No":"Yes"));
192         }
193         else {
194                 scr_printf("%3s", ((temp&sbit) ? "Yes":"No"));
195         }
196
197         color(DIM_MAGENTA);
198         scr_printf("]? ");
199         color(BRIGHT_CYAN);
200         a = (temp & sbit);
201         if (a != 0) a = 1;
202         if (backwards) a = 1 - a;
203         a = yesno_d(a);
204         if (backwards) a = 1 - a;
205         color(DIM_WHITE);
206         temp = (temp|sbit);
207         if (!a) temp = (temp^sbit);
208         return(temp);
209 }
210
211 /*
212  * modes are:  0 - .EC command, 1 - .EC for new user,
213  *             2 - toggle Xpert mode  3 - toggle floor mode
214  */
215 void enter_config(CtdlIPC *ipc, int mode)
216 {
217         char buf[SIZ];
218         struct ctdluser *user = NULL;
219         int r;                          /* IPC response code */
220
221         r = CtdlIPCGetConfig(ipc, &user, buf);
222         if (r / 100 != 2) {
223                 scr_printf("%s\n", buf);
224                 free(user);
225                 return;
226         }
227
228         if (mode == 0 || mode == 1) {
229
230                 /* Does anyone still use dialup connections with manual
231                  * screen dimensions setting anymore?  For now we'll keep
232                  * the system's ability to set these, but remove the prompts
233                  * because they're spurious for nearly everyone.
234                  * 
235                 user->USscreenwidth = intprompt("Enter your screen width",
236                                                 user->USscreenwidth, 20, 255);
237                 user->USscreenheight = intprompt("Enter your screen height",
238                                                  user->USscreenheight, 3, 255);
239                  */
240  
241                 user->flags = set_attr(ipc, user->flags,
242                                        "Are you an experienced Citadel user",
243                                        US_EXPERT, 0);
244                 if ((user->flags & US_EXPERT) == 0 && mode == 1) {
245                         free(user);
246                         return;
247                 }
248
249                 user->flags = set_attr(ipc, user->flags,
250                         "Print last old message on New message request",
251                         US_LASTOLD, 0);
252
253                 user->flags = set_attr(ipc, user->flags,
254                                        "Prompt after each message",
255                                        US_NOPROMPT, 1);
256
257                 if ((user->flags & US_NOPROMPT) == 0)
258                         user->flags = set_attr(ipc, user->flags,
259                                                "Use 'disappearing' prompts",
260                                                US_DISAPPEAR, 0);
261
262                 user->flags = set_attr(ipc, user->flags,
263                                        "Pause after each screenful of text",
264                                        US_PAGINATOR, 0);
265
266                 if (rc_prompt_control == 3 && (user->flags & US_PAGINATOR))
267                         user->flags = set_attr(ipc, user->flags,
268                                 "<N>ext and <S>top work at paginator prompt",
269                                 US_PROMPTCTL, 0);
270
271                 if (rc_floor_mode == RC_DEFAULT)
272                         user->flags = set_attr(ipc, user->flags,
273                                                "View rooms by floor",
274                                                US_FLOORS, 0);
275
276                 if (rc_ansi_color == 3)
277                         user->flags = set_attr(ipc, user->flags,
278                                                "Enable color support",
279                                                US_COLOR, 0);
280
281                 if ((user->flags & US_EXPERT) == 0)
282                         formout(ipc, "unlisted");
283
284                 user->flags = set_attr(ipc, user->flags,
285                                        "Be unlisted in userlog",
286                                        US_UNLISTED, 0);
287
288                 if (!IsEmptyStr(editor_paths[0])) {
289                         user->flags = set_attr(ipc, user->flags,
290                                 "Always enter messages with the full-screen editor",
291                                 US_EXTEDIT, 0);
292                 }
293
294         }
295
296         if (mode == 2) {
297                 if (user->flags & US_EXPERT) {
298                         user->flags ^= US_EXPERT;
299                         scr_printf("Expert mode now OFF\n");
300                 } else {
301                         user->flags |= US_EXPERT;
302                         scr_printf("Expert mode now ON\n");
303                 }
304         }
305
306         if (mode == 3) {
307                 if (user->flags & US_FLOORS) {
308                         user->flags ^= US_FLOORS;
309                         scr_printf("Floor mode now OFF\n");
310                 } else {
311                         user->flags |= US_FLOORS;
312                         scr_printf("Floor mode now ON\n");
313                 }
314         }
315
316         r = CtdlIPCSetConfig(ipc, user, buf);
317         if (r / 100 != 2) scr_printf("%s\n", buf);
318         userflags = user->flags;
319         free(user);
320 }
321
322 /*
323  * getstring()  -  get a line of text from a file
324  *                 ignores lines beginning with "#"
325  */
326 int getstring(FILE *fp, char *string)
327 {
328         int a,c;
329         do {
330                 strcpy(string,"");
331                 a=0;
332                 do {
333                         c=getc(fp);
334                         if (c<0) {
335                                 string[a]=0;
336                                 return(-1);
337                         }
338                         string[a++]=c;
339                 } while(c!=10);
340                         string[a-1]=0;
341         } while(string[0]=='#');
342         return(strlen(string));
343 }
344
345
346 /* Searches for patn in search string */
347 int pattern(char *search, char *patn) {
348         int a,b,len;
349         
350         len = strlen(patn);
351         for (a=0; !IsEmptyStr(&search[a]); ++a) {
352                 b=strncasecmp(&search[a],patn,len);
353                 if (b==0) return(b);
354         }
355         return(-1);
356 }
357
358
359 void strproc(char *string)
360 {
361         int a;
362
363         if (IsEmptyStr(string)) return;
364
365         /* Convert non-printable characters to blanks */
366         for (a=0; !IsEmptyStr(&string[a]); ++a) {
367                 if (string[a]<32) string[a]=32;
368                 if (string[a]>126) string[a]=32;
369         }
370
371         /* Remove leading and trailing blanks */
372         while(string[0]<33) strcpy(string,&string[1]);
373         while(string[strlen(string)-1]<33) string[strlen(string)-1]=0;
374
375         /* Remove double blanks */
376         for (a=0; a<strlen(string); ++a) {
377                 if ((string[a]==32)&&(string[a+1]==32)) {
378                         strcpy(&string[a],&string[a+1]);
379                         a=0;
380                 }
381         }
382
383         /* remove characters which would interfere with the network */
384         for (a=0; a<strlen(string); ++a) {
385                 if (string[a]=='!') strcpy(&string[a],&string[a+1]);
386                 if (string[a]=='@') strcpy(&string[a],&string[a+1]);
387                 if (string[a]=='_') strcpy(&string[a],&string[a+1]);
388                 if (string[a]==',') strcpy(&string[a],&string[a+1]);
389                 if (string[a]=='%') strcpy(&string[a],&string[a+1]);
390                 if (string[a]=='|') strcpy(&string[a],&string[a+1]);
391         }
392
393 }
394
395
396 #ifndef HAVE_STRERROR
397 /*
398  * replacement strerror() for systems that don't have it
399  */
400 char *strerror(int e)
401 {
402         static char buf[128];
403
404         snprintf(buf, sizeof buf, "errno = %d",e);
405         return(buf);
406 }
407 #endif
408
409
410 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax)
411 {
412         static char dots[] =
413                 "**************************************************";
414         char dots_printed[51];
415         char fmt[42];
416         unsigned long a;
417
418         if (curr >= cmax) {
419                 sln_printf("\r%79s\r","");
420                 status_line(ipc->ServInfo.humannode, ipc->ServInfo.site_location,
421                         room_name, secure, 0);
422         } else {
423                 /* a will be range 0-50 rather than 0-100 */
424                 a=(curr * 50) / cmax;
425                 sprintf(fmt, "[%%s%%%lds] %%3ld%%%% %%10ld/%%10ld\r", 50 - a);
426                 strncpy(dots_printed, dots, a);
427                 dots_printed[a] = 0;
428                 sln_printf(fmt, dots_printed, "",
429                                 curr * 100 / cmax, curr, cmax);
430                 sln_flush();
431         }
432 }
433
434
435 /*
436  * NOT the same locate_host() in locate_host.c.  This one just does a
437  * 'who am i' to try to discover where the user is...
438  */
439 void locate_host(CtdlIPC* ipc, char *hbuf)
440 {
441 #ifndef HAVE_UTMP_H
442         char buf[SIZ];
443         FILE *who;
444         int a,b;
445
446         who = (FILE *)popen("who am i","r");
447         if (who==NULL) {
448                 strcpy(hbuf, ipc->ServInfo.fqdn);
449                 return; 
450         }
451         fgets(buf,sizeof buf,who);
452         pclose(who);
453
454         b = 0;
455         for (a=0; !IsEmptyStr(&buf[a]); ++a) {
456                 if ((buf[a]=='(')||(buf[a]==')')) ++b;
457         }
458         if (b<2) {
459                 strcpy(hbuf, ipc->ServInfo.fqdn);
460                 return;
461         }
462
463         for (a=0; a<strlen(buf); ++a) {
464                 if (buf[a]=='(') {
465                         strcpy(buf,&buf[a+1]);
466                 }
467         }
468         for (a=0; a<strlen(buf); ++a) {
469                 if (buf[a]==')') buf[a] = 0;
470         }
471
472         if (IsEmptyStr(buf)) strcpy(hbuf, ipc->ServInfo.fqdn);
473         else strncpy(hbuf,buf,24);
474 #else
475         char *tty = ttyname(0);
476 #ifdef HAVE_GETUTXLINE
477         struct utmpx ut, *put;
478 #else
479         struct utmp ut, *put;
480 #endif
481
482         if (tty == NULL) {
483             fail:
484                 safestrncpy(hbuf, ipc->ServInfo.fqdn, 24);
485                 return;
486         }
487
488         if (strncmp(tty, "/dev/", 5))
489                 goto fail;
490
491         safestrncpy(ut.ut_line, &tty[5], sizeof ut.ut_line);
492
493 #ifdef HAVE_GETUTXLINE /* Solaris uses this */
494         if ((put = getutxline(&ut)) == NULL)
495 #else
496         if ((put = getutline(&ut)) == NULL)
497 #endif
498                 goto fail;
499
500 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
501         if (put->ut_type == USER_PROCESS) {
502 #endif
503 #if defined(HAVE_UT_HOST) || defined(HAVE_GETUTXLINE)
504                 if (*put->ut_host)
505                         safestrncpy(hbuf, put->ut_host, 24);
506                 else
507 #endif
508                         safestrncpy(hbuf, put->ut_line, 24);
509 #if defined(HAVE_UT_TYPE) || defined(HAVE_GETUTXLINE)
510         }
511         else goto fail;
512 #endif
513 #endif /* HAVE_UTMP_H */
514 }
515
516 /*
517  * miscellaneous server commands (testing, etc.)
518  */
519 void misc_server_cmd(CtdlIPC *ipc, char *cmd) {
520         char buf[SIZ];
521
522         CtdlIPC_chat_send(ipc, cmd);
523         CtdlIPC_chat_recv(ipc, buf);
524         scr_printf("%s\n",buf);
525         if (buf[0]=='1') {
526                 set_keepalives(KA_HALF);
527                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf,"000")) {
528                         scr_printf("%s\n",buf);
529                 }
530                 set_keepalives(KA_YES);
531                 return;
532         }
533         if (buf[0]=='4') {
534                 do {
535                         newprompt("> ",buf,255);
536                         CtdlIPC_chat_send(ipc, buf);
537                 } while(strcmp(buf,"000"));
538                 return;
539         }
540 }
541
542
543 /*
544  * compute the checksum of a file
545  */
546 int file_checksum(char *filename)
547 {
548         int cksum = 0;
549         int ch;
550         FILE *fp;
551
552         fp = fopen(filename,"r");
553         if (fp == NULL) return(0);
554
555         /* yes, this algorithm may allow cksum to overflow, but that's ok
556          * as long as it overflows consistently, which it will.
557          */
558         while (ch=getc(fp), ch>=0) {
559                 cksum = (cksum + ch);
560         }
561
562         fclose(fp);
563         return(cksum);
564 }
565
566 /*
567  * nuke a directory and its contents
568  */
569 int nukedir(char *dirname)
570 {
571         DIR *dp;
572         struct dirent *d;
573         char filename[SIZ];
574
575         dp = opendir(dirname);
576         if (dp == NULL) {
577                 return(errno);
578         }
579
580         while (d = readdir(dp), d != NULL) {
581                 snprintf(filename, sizeof filename, "%s/%s",
582                         dirname, d->d_name);
583                 unlink(filename);
584         }
585
586         closedir(dp);
587         return(rmdir(dirname));
588 }