2 * More client-side support functions.
4 * Copyright (c) 1987-2017 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <sys/types.h>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
40 #include <libcitadel.h>
42 #include "citadel_ipc.h"
43 #include "citadel_decls.h"
44 #include "routines2.h"
49 /* work around solaris include files */
55 extern char tempdir[];
56 extern char *axdefs[8];
57 extern long highest_msg_read;
58 extern long maxmsgnum;
59 extern unsigned room_flags;
60 extern int screenwidth;
64 * return proper room prompt character
66 int room_prompt(unsigned int qrflags)
70 if (qrflags & QR_DIRECTORY)
77 * Register with name and address
79 void entregis(CtdlIPC *ipc)
96 int r; /* IPC response code */
101 strcpy(tmpstate, "");
103 strcpy(tmpphone, "");
104 strcpy(tmpemail, "");
105 strcpy(tmpcountry, "");
107 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
111 while (reg && !IsEmptyStr(reg)) {
113 extract_token(buf, reg, 0, '\n', sizeof buf);
114 remove_token(reg, 0, '\n');
117 safestrncpy(tmpname, buf, sizeof tmpname);
119 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
121 safestrncpy(tmpcity, buf, sizeof tmpcity);
123 safestrncpy(tmpstate, buf, sizeof tmpstate);
125 safestrncpy(tmpzip, buf, sizeof tmpzip);
127 safestrncpy(tmpphone, buf, sizeof tmpphone);
129 safestrncpy(tmpemail, buf, sizeof tmpemail);
131 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
135 strprompt("REAL name", tmpname, 29);
136 strprompt("Address", tmpaddr, 24);
137 strprompt("City/town", tmpcity, 14);
138 strprompt("State/province", tmpstate, 2);
139 strprompt("ZIP/Postal Code", tmpzip, 10);
140 strprompt("Country", tmpcountry, 31);
141 strprompt("Telephone number", tmpphone, 14);
145 safestrncpy(holdemail, tmpemail, sizeof holdemail);
146 strprompt("Email address", tmpemail, 31);
147 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
149 extract_token(diruser, buf, 0, '@', sizeof diruser);
150 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
153 if ((strcasecmp(diruser, fullname))
154 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
156 "\nYou can't use %s as your address.\n",
159 "It is already in use by %s @ %s.\n",
162 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
167 /* now send the registration info back to the server */
168 reg = (char *)realloc(reg, SIZ);
170 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
171 tmpname, tmpaddr, tmpcity, tmpstate,
172 tmpzip, tmpphone, tmpemail, tmpcountry);
173 r = CtdlIPCSetRegistration(ipc, reg, buf);
175 scr_printf("%s\n", buf);
182 * make all messages old in current room
184 void updatels(CtdlIPC *ipc)
187 int r; /* IPC response code */
189 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
192 scr_printf("%s\n", buf);
196 * only make messages old in this room that have been read
198 void updatelsa(CtdlIPC *ipc)
201 int r; /* IPC response code */
203 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
205 scr_printf("%s\n", &buf[4]);
210 * client-based uploads (for users with their own clientware)
212 void cli_upload(CtdlIPC *ipc)
218 int r; /* IPC response code */
222 if ((room_flags & QR_UPLOAD) == 0) {
223 scr_printf("*** You cannot upload to this room.\n");
226 newprompt("File to be uploaded: ", flnm, 55);
227 fd = open(flnm, O_RDONLY);
229 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
232 scr_printf("Enter a description of this file:\n");
233 newprompt(": ", desc, 75);
235 /* Keep generating filenames in hope of finding a unique one */
238 /* basename of filename */
240 if (haschar(tbuf, '/'))
241 extract_token(tbuf, flnm,
242 num_tokens(tbuf, '/') - 1,
245 /* filename.1, filename.2, etc */
247 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
250 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
251 if (r / 100 == 5 || r < 0)
252 scr_printf("%s\n", buf);
257 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
262 * Function used for various image upload commands
264 void cli_image_upload(CtdlIPC *ipc, char *keyname)
270 /* Can we upload this image? */
271 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
273 scr_printf("%s\n", buf);
276 newprompt("Image file to be uploaded: ", flnm, 55);
277 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
279 scr_printf("%s\n", buf);
281 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
283 /* else upload succeeded */
288 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
290 void upload(CtdlIPC *ipc, int c)
291 { /* c = upload mode */
301 if ((room_flags & QR_UPLOAD) == 0) {
302 scr_printf("*** You cannot upload to this room.\n");
305 /* we don't need a filename when receiving batch y/z modem */
306 if ((c == 2) || (c == 3))
309 newprompt("Enter filename: ", flnm, 15);
311 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
312 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
313 || (flnm[a] == '?') || (flnm[a] == '*')
314 || (flnm[a] == ';') || (flnm[a] == '&'))
317 /* create a temporary directory... */
318 if (mkdir(tempdir, 0700) != 0) {
319 scr_printf("*** Could not create temporary directory %s: %s\n",
320 tempdir, strerror(errno));
323 /* now do the transfer ... in a separate process */
328 scr_printf("failed to change into %s Reason %s\nAborting now.\n",
337 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
338 fp = fopen(flnm, "w");
353 execlp("rx", "rx", flnm, NULL);
357 execlp("rb", "rb", NULL);
361 execlp("rz", "rz", NULL);
367 } while ((b != xfer_pid) && (b != (-1)));
371 scr_printf("\r*** Transfer unsuccessful.\n");
375 scr_printf("\r*** Transfer successful.\n");
376 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
377 lsfp = popen(buf, "r");
379 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
380 flnm[strlen(flnm) - 1] = 0; /* chop newline */
381 snprintf(buf, sizeof buf,
382 "Enter a short description of '%s':\n: ",
384 newprompt(buf, desc, 150);
385 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
386 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
387 scr_printf("%s\n", tbuf);
395 * validate a user (returns 0 for successful validation, nonzero if quitting)
397 int val_user(CtdlIPC *ipc, char *user, int do_validate)
405 int r; /* IPC response code */
408 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
412 extract_token(buf, resp, 0, '\n', sizeof buf);
413 remove_token(resp, 0, '\n');
416 scr_printf("User #%s - %s ", buf, cmd);
418 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
420 scr_printf("%s\n", buf);
422 scr_printf("%s\n", buf);
424 scr_printf("%s, ", buf);
426 scr_printf("%s ", buf);
428 scr_printf("%s\n", buf);
430 scr_printf("%s\n", buf);
434 scr_printf("%s\n", buf);
436 scr_printf("%s\n", buf);
437 } while (!IsEmptyStr(resp));
439 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
440 * but most of it limits it to axdefs[6].
441 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
443 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
445 scr_printf("%s\n%s\n", user, &cmd[4]);
447 if (resp) free(resp);
450 /* now set the access level */
452 sprintf(answer, "%d", ax);
453 strprompt("New access level (? for help, q to quit)",
455 if ((answer[0] >= '0') && (answer[0] <= '6')) {
457 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
459 scr_printf("%s\n\n", cmd);
462 if (tolower(answer[0]) == 'q') {
463 scr_printf("*** Aborted.\n\n");
466 if (answer[0] == '?') {
467 scr_printf("Available access levels:\n");
468 for (a=0; a<7; ++a) {
469 scr_printf("%d - %s\n",
482 void validate(CtdlIPC *ipc)
487 int r; /* IPC response code */
490 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
494 scr_printf("%s\n", cmd);
496 extract_token(buf, cmd, 0, '|', sizeof buf);
497 if (val_user(ipc, buf, 1) != 0) finished = 1;
499 } while (finished == 0);
506 stty_ctdl(SB_RESTORE);
509 signal(SIGINT, SIG_DFL);
510 signal(SIGQUIT, SIG_DFL);
511 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
512 scr_printf("Could not open a shell: %s\n", strerror(errno));
517 } while ((a != b) && (a != (-1)));
522 * <.A>ide <F>ile <D>elete command
524 void deletefile(CtdlIPC *ipc)
529 newprompt("Filename: ", filename, 31);
530 if (IsEmptyStr(filename))
532 CtdlIPCDeleteFile(ipc, filename, buf);
533 scr_printf("%s\n", buf);
538 * <.A>ide <F>ile <M>ove command
540 void movefile(CtdlIPC *ipc)
543 char newroom[ROOMNAMELEN];
546 newprompt("Filename: ", filename, 63);
547 if (IsEmptyStr(filename))
549 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
550 CtdlIPCMoveFile(ipc, filename, newroom, buf);
551 scr_printf("%s\n", buf);
556 * list of users who have filled out a bio
558 void list_bio(CtdlIPC *ipc)
563 int r; /* IPC response code */
565 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
567 scr_printf("%s\n", buf);
570 while (resp && !IsEmptyStr(resp)) {
571 extract_token(buf, resp, 0, '\n', sizeof buf);
572 remove_token(resp, 0, '\n');
573 if ((pos + strlen(buf) + 5) > screenwidth) {
577 scr_printf("%s, ", buf);
578 pos = pos + strlen(buf) + 2;
580 scr_printf("%c%c \n\n", 8, 8);
581 if (resp) free(resp);
588 void read_bio(CtdlIPC *ipc)
593 int r; /* IPC response code */
596 newprompt("Read bio for who ('?' for list) : ", who, 25);
598 if (!strcmp(who, "?"))
600 } while (!strcmp(who, "?"));
602 r = CtdlIPCGetBio(ipc, who, &resp, buf);
604 scr_printf("%s\n", buf);
607 while (!IsEmptyStr(resp)) {
608 extract_token(buf, resp, 0, '\n', sizeof buf);
609 remove_token(resp, 0, '\n');
610 scr_printf("%s\n", buf);
612 if (resp) free(resp);