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