Removed err_* and sln_* functions; replaced all calls with scr_* functions.
[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 r;
317         int rv;
318
319         if ((room_flags & QR_UPLOAD) == 0) {
320                 scr_printf("*** You cannot upload to this room.\n");
321                 return;
322         }
323         /* we don't need a filename when receiving batch y/z modem */
324         if ((c == 2) || (c == 3))
325                 strcpy(flnm, "x");
326         else
327                 newprompt("Enter filename: ", flnm, 15);
328
329         for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
330                 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
331                     || (flnm[a] == '?') || (flnm[a] == '*')
332                     || (flnm[a] == ';') || (flnm[a] == '&'))
333                         flnm[a] = '_';
334
335         /* create a temporary directory... */
336         if (mkdir(tempdir, 0700) != 0) {
337                 scr_printf("*** Could not create temporary directory %s: %s\n",
338                        tempdir, strerror(errno));
339                 return;
340         }
341         /* now do the transfer ... in a separate process */
342         xfer_pid = fork();
343         if (xfer_pid == 0) {
344                 rv = chdir(tempdir);
345                 switch (c) {
346                 case 0:
347                         stty_ctdl(0);
348                         scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
349                         fp = fopen(flnm, "w");
350                         do {
351                                 b = inkey();
352                                 if (b == 13) {
353                                         b = 10;
354                                 }
355                                 if (b != 4) {
356                                         scr_printf("%c", b);
357                                         putc(b, fp);
358                                 }
359                         } while (b != 4);
360                         fclose(fp);
361                         exit(0);
362                 case 1:
363                         screen_reset();
364                         stty_ctdl(3);
365                         execlp("rx", "rx", flnm, NULL);
366                         exit(1);
367                 case 2:
368                         screen_reset();
369                         stty_ctdl(3);
370                         execlp("rb", "rb", NULL);
371                         exit(1);
372                 case 3:
373                         screen_reset();
374                         stty_ctdl(3);
375                         execlp("rz", "rz", NULL);
376                         exit(1);
377                 }
378         } else
379                 do {
380                         b = ka_wait(&a);
381                 } while ((b != xfer_pid) && (b != (-1)));
382         stty_ctdl(0);
383         screen_set();
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                         r = 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         screen_reset();
519         stty_ctdl(SB_RESTORE);
520         a = fork();
521         if (a == 0) {
522                 signal(SIGINT, SIG_DFL);
523                 signal(SIGQUIT, SIG_DFL);
524                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
525                 scr_printf("Could not open a shell: %s\n", strerror(errno));
526                 exit(errno);
527         }
528         do {
529                 b = ka_wait(NULL);
530         } while ((a != b) && (a != (-1)));
531         stty_ctdl(0);
532         screen_set();
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                 pprintf("%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                         pprintf("\n");
589                         pos = 1;
590                 }
591                 pprintf("%s, ", buf);
592                 pos = pos + strlen(buf) + 2;
593         }
594         pprintf("%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                 pprintf("\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                 pprintf("%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                 pprintf("%s\n", buf);
625         }
626         if (resp) free(resp);
627 }
628
629
630