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