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