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)
72 if ((a == ']') && (qrflags & QR_NETWORK))
74 if ((a == '>') && (qrflags & QR_NETWORK))
81 * Register with name and address
83 void entregis(CtdlIPC *ipc)
100 int r; /* IPC response code */
105 strcpy(tmpstate, "");
107 strcpy(tmpphone, "");
108 strcpy(tmpemail, "");
109 strcpy(tmpcountry, "");
111 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
115 while (reg && !IsEmptyStr(reg)) {
117 extract_token(buf, reg, 0, '\n', sizeof buf);
118 remove_token(reg, 0, '\n');
121 safestrncpy(tmpname, buf, sizeof tmpname);
123 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
125 safestrncpy(tmpcity, buf, sizeof tmpcity);
127 safestrncpy(tmpstate, buf, sizeof tmpstate);
129 safestrncpy(tmpzip, buf, sizeof tmpzip);
131 safestrncpy(tmpphone, buf, sizeof tmpphone);
133 safestrncpy(tmpemail, buf, sizeof tmpemail);
135 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
139 strprompt("REAL name", tmpname, 29);
140 strprompt("Address", tmpaddr, 24);
141 strprompt("City/town", tmpcity, 14);
142 strprompt("State/province", tmpstate, 2);
143 strprompt("ZIP/Postal Code", tmpzip, 10);
144 strprompt("Country", tmpcountry, 31);
145 strprompt("Telephone number", tmpphone, 14);
149 safestrncpy(holdemail, tmpemail, sizeof holdemail);
150 strprompt("Email address", tmpemail, 31);
151 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
153 extract_token(diruser, buf, 0, '@', sizeof diruser);
154 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
157 if ((strcasecmp(diruser, fullname))
158 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
160 "\nYou can't use %s as your address.\n",
163 "It is already in use by %s @ %s.\n",
166 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
171 /* now send the registration info back to the server */
172 reg = (char *)realloc(reg, SIZ);
174 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
175 tmpname, tmpaddr, tmpcity, tmpstate,
176 tmpzip, tmpphone, tmpemail, tmpcountry);
177 r = CtdlIPCSetRegistration(ipc, reg, buf);
179 scr_printf("%s\n", buf);
186 * make all messages old in current room
188 void updatels(CtdlIPC *ipc)
191 int r; /* IPC response code */
193 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
196 scr_printf("%s\n", buf);
200 * only make messages old in this room that have been read
202 void updatelsa(CtdlIPC *ipc)
205 int r; /* IPC response code */
207 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
209 scr_printf("%s\n", &buf[4]);
214 * client-based uploads (for users with their own clientware)
216 void cli_upload(CtdlIPC *ipc)
222 int r; /* IPC response code */
226 if ((room_flags & QR_UPLOAD) == 0) {
227 scr_printf("*** You cannot upload to this room.\n");
230 newprompt("File to be uploaded: ", flnm, 55);
231 fd = open(flnm, O_RDONLY);
233 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
236 scr_printf("Enter a description of this file:\n");
237 newprompt(": ", desc, 75);
239 /* Keep generating filenames in hope of finding a unique one */
242 /* basename of filename */
244 if (haschar(tbuf, '/'))
245 extract_token(tbuf, flnm,
246 num_tokens(tbuf, '/') - 1,
249 /* filename.1, filename.2, etc */
251 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
254 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
255 if (r / 100 == 5 || r < 0)
256 scr_printf("%s\n", buf);
261 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
266 * Function used for various image upload commands
268 void cli_image_upload(CtdlIPC *ipc, char *keyname)
274 /* Can we upload this image? */
275 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
277 scr_printf("%s\n", buf);
280 newprompt("Image file to be uploaded: ", flnm, 55);
281 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
283 scr_printf("%s\n", buf);
285 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
287 /* else upload succeeded */
292 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
294 void upload(CtdlIPC *ipc, int c)
295 { /* c = upload mode */
305 if ((room_flags & QR_UPLOAD) == 0) {
306 scr_printf("*** You cannot upload to this room.\n");
309 /* we don't need a filename when receiving batch y/z modem */
310 if ((c == 2) || (c == 3))
313 newprompt("Enter filename: ", flnm, 15);
315 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
316 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
317 || (flnm[a] == '?') || (flnm[a] == '*')
318 || (flnm[a] == ';') || (flnm[a] == '&'))
321 /* create a temporary directory... */
322 if (mkdir(tempdir, 0700) != 0) {
323 scr_printf("*** Could not create temporary directory %s: %s\n",
324 tempdir, strerror(errno));
327 /* now do the transfer ... in a separate process */
332 scr_printf("failed to change into %s Reason %s\nAborting now.\n",
341 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
342 fp = fopen(flnm, "w");
357 execlp("rx", "rx", flnm, NULL);
361 execlp("rb", "rb", NULL);
365 execlp("rz", "rz", NULL);
371 } while ((b != xfer_pid) && (b != (-1)));
375 scr_printf("\r*** Transfer unsuccessful.\n");
379 scr_printf("\r*** Transfer successful.\n");
380 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
381 lsfp = popen(buf, "r");
383 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
384 flnm[strlen(flnm) - 1] = 0; /* chop newline */
385 snprintf(buf, sizeof buf,
386 "Enter a short description of '%s':\n: ",
388 newprompt(buf, desc, 150);
389 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
390 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
391 scr_printf("%s\n", tbuf);
399 * validate a user (returns 0 for successful validation, nonzero if quitting)
401 int val_user(CtdlIPC *ipc, char *user, int do_validate)
409 int r; /* IPC response code */
412 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
416 extract_token(buf, resp, 0, '\n', sizeof buf);
417 remove_token(resp, 0, '\n');
420 scr_printf("User #%s - %s ", buf, cmd);
422 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
424 scr_printf("%s\n", buf);
426 scr_printf("%s\n", buf);
428 scr_printf("%s, ", buf);
430 scr_printf("%s ", buf);
432 scr_printf("%s\n", buf);
434 scr_printf("%s\n", buf);
438 scr_printf("%s\n", buf);
440 scr_printf("%s\n", buf);
441 } while (!IsEmptyStr(resp));
443 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
444 * but most of it limits it to axdefs[6].
445 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
447 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
449 scr_printf("%s\n%s\n", user, &cmd[4]);
451 if (resp) free(resp);
454 /* now set the access level */
456 sprintf(answer, "%d", ax);
457 strprompt("New access level (? for help, q to quit)",
459 if ((answer[0] >= '0') && (answer[0] <= '6')) {
461 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
463 scr_printf("%s\n\n", cmd);
466 if (tolower(answer[0]) == 'q') {
467 scr_printf("*** Aborted.\n\n");
470 if (answer[0] == '?') {
471 scr_printf("Available access levels:\n");
472 for (a=0; a<7; ++a) {
473 scr_printf("%d - %s\n",
486 void validate(CtdlIPC *ipc)
491 int r; /* IPC response code */
494 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
498 scr_printf("%s\n", cmd);
500 extract_token(buf, cmd, 0, '|', sizeof buf);
501 if (val_user(ipc, buf, 1) != 0) finished = 1;
503 } while (finished == 0);
510 stty_ctdl(SB_RESTORE);
513 signal(SIGINT, SIG_DFL);
514 signal(SIGQUIT, SIG_DFL);
515 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
516 scr_printf("Could not open a shell: %s\n", strerror(errno));
521 } while ((a != b) && (a != (-1)));
526 * <.A>ide <F>ile <D>elete command
528 void deletefile(CtdlIPC *ipc)
533 newprompt("Filename: ", filename, 31);
534 if (IsEmptyStr(filename))
536 CtdlIPCDeleteFile(ipc, filename, buf);
537 scr_printf("%s\n", buf);
542 * <.A>ide <F>ile <M>ove command
544 void movefile(CtdlIPC *ipc)
547 char newroom[ROOMNAMELEN];
550 newprompt("Filename: ", filename, 63);
551 if (IsEmptyStr(filename))
553 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
554 CtdlIPCMoveFile(ipc, filename, newroom, buf);
555 scr_printf("%s\n", buf);
560 * list of users who have filled out a bio
562 void list_bio(CtdlIPC *ipc)
567 int r; /* IPC response code */
569 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
571 scr_printf("%s\n", buf);
574 while (resp && !IsEmptyStr(resp)) {
575 extract_token(buf, resp, 0, '\n', sizeof buf);
576 remove_token(resp, 0, '\n');
577 if ((pos + strlen(buf) + 5) > screenwidth) {
581 scr_printf("%s, ", buf);
582 pos = pos + strlen(buf) + 2;
584 scr_printf("%c%c \n\n", 8, 8);
585 if (resp) free(resp);
592 void read_bio(CtdlIPC *ipc)
597 int r; /* IPC response code */
600 newprompt("Read bio for who ('?' for list) : ", who, 25);
602 if (!strcmp(who, "?"))
604 } while (!strcmp(who, "?"));
606 r = CtdlIPCGetBio(ipc, who, &resp, buf);
608 scr_printf("%s\n", buf);
611 while (!IsEmptyStr(resp)) {
612 extract_token(buf, resp, 0, '\n', sizeof buf);
613 remove_token(resp, 0, '\n');
614 scr_printf("%s\n", buf);
616 if (resp) free(resp);