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