3fbff5f3d136d85b242d81f9fef7c5eec820564f
[citadel.git] / citadel / textclient / routines2.c
1 /* $Id$
2  *
3  * More client-side support functions.
4  * Unlike routines.c, some of these DO use global variables.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <limits.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <libcitadel.h>
34 #include "sysdep.h"
35 #include "citadel.h"
36 #include "citadel_ipc.h"
37 #include "citadel_decls.h"
38 #include "routines2.h"
39 #include "routines.h"
40 #include "commands.h"
41 #include "messages.h"
42 #ifndef HAVE_SNPRINTF
43 #include "snprintf.h"
44 #endif
45 #include "screen.h"
46
47 /* work around solaris include files */
48 #ifdef reg
49 #undef reg
50 #endif
51
52 extern char temp[];
53 extern char tempdir[];
54 extern char *axdefs[8];
55 extern long highest_msg_read;
56 extern long maxmsgnum;
57 extern unsigned room_flags;
58 extern int screenwidth;
59
60
61 /*
62 int eopen(char *name, int mode)
63 {
64         int ret;
65         ret = open(name, mode);
66         if (ret < 0) {
67                 err_printf("Cannot open file '%s', mode=%d, errno=%d\n",
68                         name, mode, errno);
69                 interr(errno);
70         }
71         return (ret);
72 }
73 */
74
75
76 int room_prompt(unsigned int qrflags)
77 {                               /* return proper room prompt character */
78         int a;
79         a = '>';
80         if (qrflags & QR_DIRECTORY)
81                 a = ']';
82         if ((a == ']') && (qrflags & QR_NETWORK))
83                 a = '}';
84         if ((a == '>') && (qrflags & QR_NETWORK))
85                 a = ')';
86         return (a);
87 }
88
89 void entregis(CtdlIPC *ipc)
90 {                               /* register with name and address */
91
92         char buf[SIZ];
93         char tmpname[30];
94         char tmpaddr[25];
95         char tmpcity[15];
96         char tmpstate[3];
97         char tmpzip[11];
98         char tmpphone[15];
99         char tmpemail[SIZ];
100         char tmpcountry[32];
101         char diruser[256];
102         char dirnode[256];
103         char holdemail[SIZ];
104         char *reg = NULL;
105         int ok = 0;
106         int r;                          /* IPC response code */
107
108         strcpy(tmpname, "");
109         strcpy(tmpaddr, "");
110         strcpy(tmpcity, "");
111         strcpy(tmpstate, "");
112         strcpy(tmpzip, "");
113         strcpy(tmpphone, "");
114         strcpy(tmpemail, "");
115         strcpy(tmpcountry, "");
116
117         r = CtdlIPCGetUserRegistration(ipc, NULL, &reg, buf);
118         if (r / 100 == 1) {
119                 int a = 0;
120
121                 while (reg && !IsEmptyStr(reg)) {
122
123                         extract_token(buf, reg, 0, '\n', sizeof buf);
124                         remove_token(reg, 0, '\n');
125
126                         if (a == 2)
127                                 safestrncpy(tmpname, buf, sizeof tmpname);
128                         else if (a == 3)
129                                 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
130                         else if (a == 4)
131                                 safestrncpy(tmpcity, buf, sizeof tmpcity);
132                         else if (a == 5)
133                                 safestrncpy(tmpstate, buf, sizeof tmpstate);
134                         else if (a == 6)
135                                 safestrncpy(tmpzip, buf, sizeof tmpzip);
136                         else if (a == 7)
137                                 safestrncpy(tmpphone, buf, sizeof tmpphone);
138                         else if (a == 9)
139                                 safestrncpy(tmpemail, buf, sizeof tmpemail);
140                         else if (a == 10)
141                                 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
142                         ++a;
143                 }
144         }
145         strprompt("REAL name", tmpname, 29);
146         strprompt("Address", tmpaddr, 24);
147         strprompt("City/town", tmpcity, 14);
148         strprompt("State/province", tmpstate, 2);
149         strprompt("ZIP/Postal Code", tmpzip, 10);
150         strprompt("Country", tmpcountry, 31);
151         strprompt("Telephone number", tmpphone, 14);
152
153         do {
154                 ok = 1;
155                 safestrncpy(holdemail, tmpemail, sizeof holdemail);
156                 strprompt("Email address", tmpemail, 31);
157                 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
158                 if (r / 100 == 2) {
159                         extract_token(diruser, buf, 0, '@', sizeof diruser);
160                         extract_token(dirnode, buf, 1, '@', sizeof dirnode);
161                         striplt(diruser);
162                         striplt(dirnode);
163                         if ((strcasecmp(diruser, fullname))
164                            || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
165                                 scr_printf(
166                                         "\nYou can't use %s as your address.\n",
167                                         tmpemail);
168                                 scr_printf(
169                                         "It is already in use by %s @ %s.\n",
170                                         diruser, dirnode);
171                                 ok = 0;
172                                 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
173                         }
174                 }
175         } while (ok == 0);
176
177         /* now send the registration info back to the server */
178         reg = (char *)realloc(reg, SIZ);
179         if (reg) {
180                 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
181                         tmpname, tmpaddr, tmpcity, tmpstate,
182                         tmpzip, tmpphone, tmpemail, tmpcountry);
183                 r = CtdlIPCSetRegistration(ipc, reg, buf);
184                 if (r / 100 != 4)
185                         scr_printf("%s\n", buf);
186                 free(reg);
187         }
188         scr_printf("\n");
189 }
190
191 void updatels(CtdlIPC *ipc)
192 {                               /* make all messages old in current room */
193         char buf[256];
194         int r;                          /* IPC response code */
195
196         if (rc_alt_semantics) {
197                 if (maxmsgnum == 0 && highest_msg_read == 0) {
198                         return;
199                 }
200                 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
201                                  maxmsgnum : highest_msg_read, buf);
202         } else {
203                 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
204                                  maxmsgnum : highest_msg_read, buf);
205 /*              r = CtdlIPCSetLastRead(ipc, maxmsgnum, buf); */
206 /* This is a quick-and-dirty fix to all msgs becoming new in Mail>.
207  * It will need to be rethought when messages.c is rewritten.
208  */
209         }
210         if (r / 100 != 2)
211                 scr_printf("%s\n", buf);
212 }
213
214 /*
215  * only make messages old in this room that have been read
216  */
217 void updatelsa(CtdlIPC *ipc)
218 {
219         char buf[256];
220         int r;                          /* IPC response code */
221
222         r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
223         if (r / 100 != 2)
224                 scr_printf("%s\n", &buf[4]);
225 }
226
227
228 /*
229  * client-based uploads (for users with their own clientware)
230  */
231 void cli_upload(CtdlIPC *ipc)
232 {
233         char flnm[PATH_MAX];
234         char desc[151];
235         char buf[256];
236         char tbuf[256];
237         int r;          /* IPC response code */
238         int a;
239         int fd;
240
241         if ((room_flags & QR_UPLOAD) == 0) {
242                 scr_printf("*** You cannot upload to this room.\n");
243                 return;
244         }
245         newprompt("File to be uploaded: ", flnm, 55);
246         fd = open(flnm, O_RDONLY);
247         if (fd < 0) {
248                 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
249                 return;
250         }
251         scr_printf("Enter a description of this file:\n");
252         newprompt(": ", desc, 75);
253
254         /* Keep generating filenames in hope of finding a unique one */
255         a = 0;
256         while (a < 10) {
257                 /* basename of filename */
258                 strcpy(tbuf, flnm);
259                 if (haschar(tbuf, '/'))
260                         extract_token(tbuf, flnm,
261                                 num_tokens(tbuf, '/') - 1,
262                                 '/', sizeof tbuf
263                         );
264                 /* filename.1, filename.2, etc */
265                 if (a > 0) {
266                         sprintf(&tbuf[strlen(tbuf)], ".%d", a);
267                 }
268                 /* Try upload */
269                 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
270                 if (r / 100 == 5 || r < 0)
271                         scr_printf("%s\n", buf);
272                 else
273                         break;
274                 ++a;
275         }
276         if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
277 }
278
279
280 /*
281  * Function used for various image upload commands
282  */
283 void cli_image_upload(CtdlIPC *ipc, char *keyname)
284 {
285         char flnm[PATH_MAX];
286         char buf[256];
287         int r;
288
289         /* Can we upload this image? */
290         r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
291         if (r / 100 != 2) {
292                 err_printf("%s\n", buf);
293                 return;
294         }
295         newprompt("Image file to be uploaded: ", flnm, 55);
296         r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
297         if (r / 100 == 5) {
298                 err_printf("%s\n", buf);
299         } else if (r < 0) {
300                 err_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
301         }
302         /* else upload succeeded */
303 }
304
305
306 /*
307  * protocol-based uploads (Xmodem, Ymodem, Zmodem)
308  */
309 void upload(CtdlIPC *ipc, int c)
310 {                               /* c = upload mode */
311         char flnm[PATH_MAX];
312         char desc[151];
313         char buf[256];
314         char tbuf[4096];
315         int xfer_pid;
316         int a, b;
317         FILE *fp, *lsfp;
318         int r;
319         int rv;
320
321         if ((room_flags & QR_UPLOAD) == 0) {
322                 scr_printf("*** You cannot upload to this room.\n");
323                 return;
324         }
325         /* we don't need a filename when receiving batch y/z modem */
326         if ((c == 2) || (c == 3))
327                 strcpy(flnm, "x");
328         else
329                 newprompt("Enter filename: ", flnm, 15);
330
331         for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
332                 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
333                     || (flnm[a] == '?') || (flnm[a] == '*')
334                     || (flnm[a] == ';') || (flnm[a] == '&'))
335                         flnm[a] = '_';
336
337         /* create a temporary directory... */
338         if (mkdir(tempdir, 0700) != 0) {
339                 scr_printf("*** Could not create temporary directory %s: %s\n",
340                        tempdir, strerror(errno));
341                 return;
342         }
343         /* now do the transfer ... in a separate process */
344         xfer_pid = fork();
345         if (xfer_pid == 0) {
346                 rv = chdir(tempdir);
347                 switch (c) {
348                 case 0:
349                         stty_ctdl(0);
350                         scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
351                         fp = fopen(flnm, "w");
352                         do {
353                                 b = inkey();
354                                 if (b == 13) {
355                                         b = 10;
356                                 }
357                                 if (b != 4) {
358                                         scr_printf("%c", b);
359                                         putc(b, fp);
360                                 }
361                         } while (b != 4);
362                         fclose(fp);
363                         exit(0);
364                 case 1:
365                         screen_reset();
366                         stty_ctdl(3);
367                         execlp("rx", "rx", flnm, NULL);
368                         exit(1);
369                 case 2:
370                         screen_reset();
371                         stty_ctdl(3);
372                         execlp("rb", "rb", NULL);
373                         exit(1);
374                 case 3:
375                         screen_reset();
376                         stty_ctdl(3);
377                         execlp("rz", "rz", NULL);
378                         exit(1);
379                 }
380         } else
381                 do {
382                         b = ka_wait(&a);
383                 } while ((b != xfer_pid) && (b != (-1)));
384         stty_ctdl(0);
385         screen_set();
386
387         if (a != 0) {
388                 scr_printf("\r*** Transfer unsuccessful.\n");
389                 nukedir(tempdir);
390                 return;
391         }
392         scr_printf("\r*** Transfer successful.\n");
393         snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
394         lsfp = popen(buf, "r");
395         if (lsfp != NULL) {
396                 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
397                         flnm[strlen(flnm) - 1] = 0;     /* chop newline */
398                         snprintf(buf, sizeof buf,
399                                  "Enter a short description of '%s':\n: ",
400                                  flnm);
401                         newprompt(buf, desc, 150);
402                         snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
403                         r = CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
404                         scr_printf("%s\n", tbuf);
405                 }
406                 pclose(lsfp);
407         }
408         nukedir(tempdir);
409 }
410
411 /* 
412  * validate a user (returns 0 for successful validation, nonzero if quitting)
413  */
414 int val_user(CtdlIPC *ipc, char *user, int do_validate)
415 {
416         int a;
417         char cmd[256];
418         char buf[256];
419         char *resp = NULL;
420         int ax = 0;
421         char answer[2];
422         int r;                          /* IPC response code */
423
424         scr_printf("\n");
425         r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
426         if (r / 100 == 1) {
427                 a = 0;
428                 do {
429                         extract_token(buf, resp, 0, '\n', sizeof buf);
430                         remove_token(resp, 0, '\n');
431                         ++a;
432                         if (a == 1)
433                                 scr_printf("User #%s - %s  ", buf, cmd);
434                         if (a == 2)
435                                 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
436                         if (a == 3)
437                                 scr_printf("%s\n", buf);
438                         if (a == 4)
439                                 scr_printf("%s\n", buf);
440                         if (a == 5)
441                                 scr_printf("%s, ", buf);
442                         if (a == 6)
443                                 scr_printf("%s ", buf);
444                         if (a == 7)
445                                 scr_printf("%s\n", buf);
446                         if (a == 8)
447                                 scr_printf("%s\n", buf);
448                         if (a == 9)
449                                 ax = atoi(buf);
450                         if (a == 10)
451                                 scr_printf("%s\n", buf);
452                         if (a == 11)
453                                 scr_printf("%s\n", buf);
454                 } while (!IsEmptyStr(resp));
455
456 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
457  * but most of it limits it to axdefs[6].
458  * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
459  */
460                 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
461         } else {
462                 scr_printf("%s\n%s\n", user, &cmd[4]);
463         }
464         if (resp) free(resp);
465
466         if (do_validate) {
467                 /* now set the access level */
468                 while(1) {
469                         sprintf(answer, "%d", ax);
470                         strprompt("New access level (? for help, q to quit)",
471                                 answer, 1);
472                         if ((answer[0] >= '0') && (answer[0] <= '6')) {
473                                 ax = atoi(answer);
474                                 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
475                                 if (r / 100 != 2)
476                                 scr_printf("%s\n\n", cmd);
477                                 return(0);
478                         }
479                         if (tolower(answer[0]) == 'q') {
480                                 scr_printf("*** Aborted.\n\n");
481                                 return(1);
482                         }
483                         if (answer[0] == '?') {
484                                 scr_printf("Available access levels:\n");
485                                 for (a=0; a<7; ++a) {
486                                         scr_printf("%d - %s\n",
487                                                 a, axdefs[a]);
488                                 }
489                         }
490                 }
491         }
492         return(0);
493 }
494
495
496 void validate(CtdlIPC *ipc)
497 {                               /* validate new users */
498         char cmd[256];
499         char buf[256];
500         int finished = 0;
501         int r;                          /* IPC response code */
502
503         do {
504                 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
505                 if (r / 100 != 3)
506                         finished = 1;
507                 if (r / 100 == 2)
508                         scr_printf("%s\n", cmd);
509                 if (r / 100 == 3) {
510                         extract_token(buf, cmd, 0, '|', sizeof buf);
511                         if (val_user(ipc, buf, 1) != 0) finished = 1;
512                 }
513         } while (finished == 0);
514 }
515
516 void subshell(void)
517 {
518         int a, b;
519
520         screen_reset();
521         stty_ctdl(SB_RESTORE);
522         a = fork();
523         if (a == 0) {
524                 signal(SIGINT, SIG_DFL);
525                 signal(SIGQUIT, SIG_DFL);
526                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
527                 err_printf("Could not open a shell: %s\n", strerror(errno));
528                 exit(errno);
529         }
530         do {
531                 b = ka_wait(NULL);
532         } while ((a != b) && (a != (-1)));
533         stty_ctdl(0);
534         screen_set();
535 }
536
537 /*
538  * <.A>ide <F>ile <D>elete command
539  */
540 void deletefile(CtdlIPC *ipc)
541 {
542         char filename[32];
543         char buf[256];
544
545         newprompt("Filename: ", filename, 31);
546         if (IsEmptyStr(filename))
547                 return;
548         CtdlIPCDeleteFile(ipc, filename, buf);
549         err_printf("%s\n", buf);
550 }
551
552
553 /*
554  * <.A>ide <F>ile <M>ove command
555  */
556 void movefile(CtdlIPC *ipc)
557 {
558         char filename[64];
559         char newroom[ROOMNAMELEN];
560         char buf[256];
561
562         newprompt("Filename: ", filename, 63);
563         if (IsEmptyStr(filename))
564                 return;
565         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
566         CtdlIPCMoveFile(ipc, filename, newroom, buf);
567         err_printf("%s\n", buf);
568 }
569
570
571 /* 
572  * list of users who have filled out a bio
573  */
574 void list_bio(CtdlIPC *ipc)
575 {
576         char buf[256];
577         char *resp = NULL;
578         int pos = 1;
579         int r;                  /* IPC response code */
580
581         r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
582         if (r / 100 != 1) {
583                 pprintf("%s\n", buf);
584                 return;
585         }
586         while (resp && !IsEmptyStr(resp)) {
587                 extract_token(buf, resp, 0, '\n', sizeof buf);
588                 remove_token(resp, 0, '\n');
589                 if ((pos + strlen(buf) + 5) > screenwidth) {
590                         pprintf("\n");
591                         pos = 1;
592                 }
593                 pprintf("%s, ", buf);
594                 pos = pos + strlen(buf) + 2;
595         }
596         pprintf("%c%c  \n\n", 8, 8);
597         if (resp) free(resp);
598 }
599
600
601 /*
602  * read bio
603  */
604 void read_bio(CtdlIPC *ipc)
605 {
606         char who[256];
607         char buf[256];
608         char *resp = NULL;
609         int r;                  /* IPC response code */
610
611         do {
612                 newprompt("Read bio for who ('?' for list) : ", who, 25);
613                 pprintf("\n");
614                 if (!strcmp(who, "?"))
615                         list_bio(ipc);
616         } while (!strcmp(who, "?"));
617
618         r = CtdlIPCGetBio(ipc, who, &resp, buf);
619         if (r / 100 != 1) {
620                 pprintf("%s\n", buf);
621                 return;
622         }
623         while (!IsEmptyStr(resp)) {
624                 extract_token(buf, resp, 0, '\n', sizeof buf);
625                 remove_token(resp, 0, '\n');
626                 pprintf("%s\n", buf);
627         }
628         if (resp) free(resp);
629 }
630
631
632