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