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