]> code.citadel.org Git - citadel.git/blob - citadel/textclient/routines2.c
Removed obsolete features from client
[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         r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?  maxmsgnum : highest_msg_read, buf);
195
196         if (r / 100 != 2)
197                 scr_printf("%s\n", buf);
198 }
199
200 /*
201  * only make messages old in this room that have been read
202  */
203 void updatelsa(CtdlIPC *ipc)
204 {
205         char buf[256];
206         int r;                          /* IPC response code */
207
208         r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
209         if (r / 100 != 2)
210                 scr_printf("%s\n", &buf[4]);
211 }
212
213
214 /*
215  * client-based uploads (for users with their own clientware)
216  */
217 void cli_upload(CtdlIPC *ipc)
218 {
219         char flnm[PATH_MAX];
220         char desc[151];
221         char buf[256];
222         char tbuf[256];
223         int r;          /* IPC response code */
224         int a;
225         int fd;
226
227         if ((room_flags & QR_UPLOAD) == 0) {
228                 scr_printf("*** You cannot upload to this room.\n");
229                 return;
230         }
231         newprompt("File to be uploaded: ", flnm, 55);
232         fd = open(flnm, O_RDONLY);
233         if (fd < 0) {
234                 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
235                 return;
236         }
237         scr_printf("Enter a description of this file:\n");
238         newprompt(": ", desc, 75);
239
240         /* Keep generating filenames in hope of finding a unique one */
241         a = 0;
242         while (a < 10) {
243                 /* basename of filename */
244                 strcpy(tbuf, flnm);
245                 if (haschar(tbuf, '/'))
246                         extract_token(tbuf, flnm,
247                                 num_tokens(tbuf, '/') - 1,
248                                 '/', sizeof tbuf
249                         );
250                 /* filename.1, filename.2, etc */
251                 if (a > 0) {
252                         sprintf(&tbuf[strlen(tbuf)], ".%d", a);
253                 }
254                 /* Try upload */
255                 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
256                 if (r / 100 == 5 || r < 0)
257                         scr_printf("%s\n", buf);
258                 else
259                         break;
260                 ++a;
261         }
262         if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
263 }
264
265
266 /*
267  * Function used for various image upload commands
268  */
269 void cli_image_upload(CtdlIPC *ipc, char *keyname)
270 {
271         char flnm[PATH_MAX];
272         char buf[256];
273         int r;
274
275         /* Can we upload this image? */
276         r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
277         if (r / 100 != 2) {
278                 scr_printf("%s\n", buf);
279                 return;
280         }
281         newprompt("Image file to be uploaded: ", flnm, 55);
282         r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
283         if (r / 100 == 5) {
284                 scr_printf("%s\n", buf);
285         } else if (r < 0) {
286                 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
287         }
288         /* else upload succeeded */
289 }
290
291
292 /*
293  * protocol-based uploads (Xmodem, Ymodem, Zmodem)
294  */
295 void upload(CtdlIPC *ipc, int c)
296 {                               /* c = upload mode */
297         char flnm[PATH_MAX];
298         char desc[151];
299         char buf[256];
300         char tbuf[4096];
301         int xfer_pid;
302         int a, b;
303         FILE *fp, *lsfp;
304         int rv;
305
306         if ((room_flags & QR_UPLOAD) == 0) {
307                 scr_printf("*** You cannot upload to this room.\n");
308                 return;
309         }
310         /* we don't need a filename when receiving batch y/z modem */
311         if ((c == 2) || (c == 3))
312                 strcpy(flnm, "x");
313         else
314                 newprompt("Enter filename: ", flnm, 15);
315
316         for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
317                 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
318                     || (flnm[a] == '?') || (flnm[a] == '*')
319                     || (flnm[a] == ';') || (flnm[a] == '&'))
320                         flnm[a] = '_';
321
322         /* create a temporary directory... */
323         if (mkdir(tempdir, 0700) != 0) {
324                 scr_printf("*** Could not create temporary directory %s: %s\n",
325                        tempdir, strerror(errno));
326                 return;
327         }
328         /* now do the transfer ... in a separate process */
329         xfer_pid = fork();
330         if (xfer_pid == 0) {
331                 rv = chdir(tempdir);
332                 if (rv < 0) {
333                         scr_printf("failed to change into %s Reason %s\nAborting now.\n", 
334                                    tempdir, 
335                                    strerror(errno));
336                         nukedir(tempdir);
337                         return;
338                 }
339                 switch (c) {
340                 case 0:
341                         stty_ctdl(0);
342                         scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
343                         fp = fopen(flnm, "w");
344                         do {
345                                 b = inkey();
346                                 if (b == 13) {
347                                         b = 10;
348                                 }
349                                 if (b != 4) {
350                                         scr_printf("%c", b);
351                                         putc(b, fp);
352                                 }
353                         } while (b != 4);
354                         fclose(fp);
355                         exit(0);
356                 case 1:
357                         stty_ctdl(3);
358                         execlp("rx", "rx", flnm, NULL);
359                         exit(1);
360                 case 2:
361                         stty_ctdl(3);
362                         execlp("rb", "rb", NULL);
363                         exit(1);
364                 case 3:
365                         stty_ctdl(3);
366                         execlp("rz", "rz", NULL);
367                         exit(1);
368                 }
369         } else
370                 do {
371                         b = ka_wait(&a);
372                 } while ((b != xfer_pid) && (b != (-1)));
373         stty_ctdl(0);
374
375         if (a != 0) {
376                 scr_printf("\r*** Transfer unsuccessful.\n");
377                 nukedir(tempdir);
378                 return;
379         }
380         scr_printf("\r*** Transfer successful.\n");
381         snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
382         lsfp = popen(buf, "r");
383         if (lsfp != NULL) {
384                 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
385                         flnm[strlen(flnm) - 1] = 0;     /* chop newline */
386                         snprintf(buf, sizeof buf,
387                                  "Enter a short description of '%s':\n: ",
388                                  flnm);
389                         newprompt(buf, desc, 150);
390                         snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
391                         CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
392                         scr_printf("%s\n", tbuf);
393                 }
394                 pclose(lsfp);
395         }
396         nukedir(tempdir);
397 }
398
399 /* 
400  * validate a user (returns 0 for successful validation, nonzero if quitting)
401  */
402 int val_user(CtdlIPC *ipc, char *user, int do_validate)
403 {
404         int a;
405         char cmd[256];
406         char buf[256];
407         char *resp = NULL;
408         int ax = 0;
409         char answer[2];
410         int r;                          /* IPC response code */
411
412         scr_printf("\n");
413         r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
414         if (r / 100 == 1) {
415                 a = 0;
416                 do {
417                         extract_token(buf, resp, 0, '\n', sizeof buf);
418                         remove_token(resp, 0, '\n');
419                         ++a;
420                         if (a == 1)
421                                 scr_printf("User #%s - %s  ", buf, cmd);
422                         if (a == 2)
423                                 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
424                         if (a == 3)
425                                 scr_printf("%s\n", buf);
426                         if (a == 4)
427                                 scr_printf("%s\n", buf);
428                         if (a == 5)
429                                 scr_printf("%s, ", buf);
430                         if (a == 6)
431                                 scr_printf("%s ", buf);
432                         if (a == 7)
433                                 scr_printf("%s\n", buf);
434                         if (a == 8)
435                                 scr_printf("%s\n", buf);
436                         if (a == 9)
437                                 ax = atoi(buf);
438                         if (a == 10)
439                                 scr_printf("%s\n", buf);
440                         if (a == 11)
441                                 scr_printf("%s\n", buf);
442                 } while (!IsEmptyStr(resp));
443
444 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
445  * but most of it limits it to axdefs[6].
446  * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
447  */
448                 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
449         } else {
450                 scr_printf("%s\n%s\n", user, &cmd[4]);
451         }
452         if (resp) free(resp);
453
454         if (do_validate) {
455                 /* now set the access level */
456                 while(1) {
457                         sprintf(answer, "%d", ax);
458                         strprompt("New access level (? for help, q to quit)",
459                                 answer, 1);
460                         if ((answer[0] >= '0') && (answer[0] <= '6')) {
461                                 ax = atoi(answer);
462                                 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
463                                 if (r / 100 != 2)
464                                 scr_printf("%s\n\n", cmd);
465                                 return(0);
466                         }
467                         if (tolower(answer[0]) == 'q') {
468                                 scr_printf("*** Aborted.\n\n");
469                                 return(1);
470                         }
471                         if (answer[0] == '?') {
472                                 scr_printf("Available access levels:\n");
473                                 for (a=0; a<7; ++a) {
474                                         scr_printf("%d - %s\n",
475                                                 a, axdefs[a]);
476                                 }
477                         }
478                 }
479         }
480         return(0);
481 }
482
483
484 void validate(CtdlIPC *ipc)
485 {                               /* validate new users */
486         char cmd[256];
487         char buf[256];
488         int finished = 0;
489         int r;                          /* IPC response code */
490
491         do {
492                 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
493                 if (r / 100 != 3)
494                         finished = 1;
495                 if (r / 100 == 2)
496                         scr_printf("%s\n", cmd);
497                 if (r / 100 == 3) {
498                         extract_token(buf, cmd, 0, '|', sizeof buf);
499                         if (val_user(ipc, buf, 1) != 0) finished = 1;
500                 }
501         } while (finished == 0);
502 }
503
504 void subshell(void)
505 {
506         int a, b;
507
508         stty_ctdl(SB_RESTORE);
509         a = fork();
510         if (a == 0) {
511                 signal(SIGINT, SIG_DFL);
512                 signal(SIGQUIT, SIG_DFL);
513                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
514                 scr_printf("Could not open a shell: %s\n", strerror(errno));
515                 exit(errno);
516         }
517         do {
518                 b = ka_wait(NULL);
519         } while ((a != b) && (a != (-1)));
520         stty_ctdl(0);
521 }
522
523 /*
524  * <.A>ide <F>ile <D>elete command
525  */
526 void deletefile(CtdlIPC *ipc)
527 {
528         char filename[32];
529         char buf[256];
530
531         newprompt("Filename: ", filename, 31);
532         if (IsEmptyStr(filename))
533                 return;
534         CtdlIPCDeleteFile(ipc, filename, buf);
535         scr_printf("%s\n", buf);
536 }
537
538
539 /*
540  * <.A>ide <F>ile <M>ove command
541  */
542 void movefile(CtdlIPC *ipc)
543 {
544         char filename[64];
545         char newroom[ROOMNAMELEN];
546         char buf[256];
547
548         newprompt("Filename: ", filename, 63);
549         if (IsEmptyStr(filename))
550                 return;
551         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
552         CtdlIPCMoveFile(ipc, filename, newroom, buf);
553         scr_printf("%s\n", buf);
554 }
555
556
557 /* 
558  * list of users who have filled out a bio
559  */
560 void list_bio(CtdlIPC *ipc)
561 {
562         char buf[256];
563         char *resp = NULL;
564         int pos = 1;
565         int r;                  /* IPC response code */
566
567         r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
568         if (r / 100 != 1) {
569                 scr_printf("%s\n", buf);
570                 return;
571         }
572         while (resp && !IsEmptyStr(resp)) {
573                 extract_token(buf, resp, 0, '\n', sizeof buf);
574                 remove_token(resp, 0, '\n');
575                 if ((pos + strlen(buf) + 5) > screenwidth) {
576                         scr_printf("\n");
577                         pos = 1;
578                 }
579                 scr_printf("%s, ", buf);
580                 pos = pos + strlen(buf) + 2;
581         }
582         scr_printf("%c%c  \n\n", 8, 8);
583         if (resp) free(resp);
584 }
585
586
587 /*
588  * read bio
589  */
590 void read_bio(CtdlIPC *ipc)
591 {
592         char who[256];
593         char buf[256];
594         char *resp = NULL;
595         int r;                  /* IPC response code */
596
597         do {
598                 newprompt("Read bio for who ('?' for list) : ", who, 25);
599                 scr_printf("\n");
600                 if (!strcmp(who, "?"))
601                         list_bio(ipc);
602         } while (!strcmp(who, "?"));
603
604         r = CtdlIPCGetBio(ipc, who, &resp, buf);
605         if (r / 100 != 1) {
606                 scr_printf("%s\n", buf);
607                 return;
608         }
609         while (!IsEmptyStr(resp)) {
610                 extract_token(buf, resp, 0, '\n', sizeof buf);
611                 remove_token(resp, 0, '\n');
612                 scr_printf("%s\n", buf);
613         }
614         if (resp) free(resp);
615 }
616
617
618