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