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.
15 #include "textclient.h"
17 /* work around solaris include files */
23 extern char tempdir[];
24 extern char *axdefs[8];
25 extern long highest_msg_read;
26 extern long maxmsgnum;
27 extern unsigned room_flags;
28 extern int screenwidth;
32 * return proper room prompt character
34 int room_prompt(unsigned int qrflags)
38 if (qrflags & QR_DIRECTORY)
45 * Register with name and address
47 void entregis(CtdlIPC * ipc)
64 int r; /* IPC response code */
73 strcpy(tmpcountry, "");
75 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
79 while (reg && !IsEmptyStr(reg)) {
81 extract_token(buf, reg, 0, '\n', sizeof buf);
82 remove_token(reg, 0, '\n');
85 safestrncpy(tmpname, buf, sizeof tmpname);
87 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
89 safestrncpy(tmpcity, buf, sizeof tmpcity);
91 safestrncpy(tmpstate, buf, sizeof tmpstate);
93 safestrncpy(tmpzip, buf, sizeof tmpzip);
95 safestrncpy(tmpphone, buf, sizeof tmpphone);
97 safestrncpy(tmpemail, buf, sizeof tmpemail);
99 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
103 strprompt("REAL name", tmpname, 29);
104 strprompt("Address", tmpaddr, 24);
105 strprompt("City/town", tmpcity, 14);
106 strprompt("State/province", tmpstate, 2);
107 strprompt("ZIP/Postal Code", tmpzip, 10);
108 strprompt("Country", tmpcountry, 31);
109 strprompt("Telephone number", tmpphone, 14);
113 safestrncpy(holdemail, tmpemail, sizeof holdemail);
114 strprompt("Email address", tmpemail, 31);
115 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
117 extract_token(diruser, buf, 0, '@', sizeof diruser);
118 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
121 if ((strcasecmp(diruser, fullname))
122 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
123 scr_printf("\nYou can't use %s as your address.\n", tmpemail);
124 scr_printf("It is already in use by %s @ %s.\n", diruser, dirnode);
126 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
131 /* now send the registration info back to the server */
132 reg = (char *) realloc(reg, SIZ);
134 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
135 tmpname, tmpaddr, tmpcity, tmpstate, tmpzip, tmpphone, tmpemail, tmpcountry);
136 r = CtdlIPCSetRegistration(ipc, reg, buf);
138 scr_printf("%s\n", buf);
145 * make all messages old in current room
147 void updatels(CtdlIPC * ipc)
150 int r; /* IPC response code */
152 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
155 scr_printf("%s\n", buf);
159 * only make messages old in this room that have been read
161 void updatelsa(CtdlIPC * ipc)
164 int r; /* IPC response code */
166 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
168 scr_printf("%s\n", &buf[4]);
173 * client-based uploads (for users with their own clientware)
175 void cli_upload(CtdlIPC * ipc)
181 int r; /* IPC response code */
185 if ((room_flags & QR_UPLOAD) == 0) {
186 scr_printf("*** You cannot upload to this room.\n");
189 newprompt("File to be uploaded: ", flnm, 55);
190 fd = open(flnm, O_RDONLY);
192 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
195 scr_printf("Enter a description of this file:\n");
196 newprompt(": ", desc, 75);
198 /* Keep generating filenames in hope of finding a unique one */
201 /* basename of filename */
203 if (haschar(tbuf, '/'))
204 extract_token(tbuf, flnm, num_tokens(tbuf, '/') - 1, '/', sizeof tbuf);
205 /* filename.1, filename.2, etc */
207 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
210 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
211 if (r / 100 == 5 || r < 0)
212 scr_printf("%s\n", buf);
218 scr_printf("Saved as '%s'\n", tbuf);
223 * Function used for various image upload commands
225 void cli_image_upload(CtdlIPC * ipc, char *keyname)
231 /* Can we upload this image? */
232 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
234 scr_printf("%s\n", buf);
237 newprompt("Image file to be uploaded: ", flnm, 55);
238 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
240 scr_printf("%s\n", buf);
242 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
244 /* else upload succeeded */
249 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
251 void upload(CtdlIPC * ipc, int c)
252 { /* c = upload mode */
262 if ((room_flags & QR_UPLOAD) == 0) {
263 scr_printf("*** You cannot upload to this room.\n");
266 /* we don't need a filename when receiving batch y/z modem */
267 if ((c == 2) || (c == 3))
270 newprompt("Enter filename: ", flnm, 15);
272 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
273 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
274 || (flnm[a] == '?') || (flnm[a] == '*')
275 || (flnm[a] == ';') || (flnm[a] == '&'))
278 /* create a temporary directory... */
279 if (mkdir(tempdir, 0700) != 0) {
280 scr_printf("*** Could not create temporary directory %s: %s\n", tempdir, strerror(errno));
283 /* now do the transfer ... in a separate process */
288 scr_printf("failed to change into %s Reason %s\nAborting now.\n", tempdir, strerror(errno));
295 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
296 fp = fopen(flnm, "w");
311 execlp("rx", "rx", flnm, NULL);
315 execlp("rb", "rb", NULL);
319 execlp("rz", "rz", NULL);
325 } while ((b != xfer_pid) && (b != (-1)));
329 scr_printf("\r*** Transfer unsuccessful.\n");
333 scr_printf("\r*** Transfer successful.\n");
334 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
335 lsfp = popen(buf, "r");
337 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
338 flnm[strlen(flnm) - 1] = 0; /* chop newline */
339 snprintf(buf, sizeof buf, "Enter a short description of '%s':\n: ", flnm);
340 newprompt(buf, desc, 150);
341 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
342 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
343 scr_printf("%s\n", tbuf);
351 * validate a user (returns 0 for successful validation, nonzero if quitting)
353 int val_user(CtdlIPC * ipc, char *user, int do_validate)
361 int r; /* IPC response code */
364 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
368 extract_token(buf, resp, 0, '\n', sizeof buf);
369 remove_token(resp, 0, '\n');
372 scr_printf("User #%s - %s ", buf, cmd);
374 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>"));
376 scr_printf("%s\n", buf);
378 scr_printf("%s\n", buf);
380 scr_printf("%s, ", buf);
382 scr_printf("%s ", buf);
384 scr_printf("%s\n", buf);
386 scr_printf("%s\n", buf);
390 scr_printf("%s\n", buf);
392 scr_printf("%s\n", buf);
393 } while (!IsEmptyStr(resp));
395 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
396 * but most of it limits it to axdefs[6].
397 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
399 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
401 scr_printf("%s\n%s\n", user, &cmd[4]);
407 /* now set the access level */
409 sprintf(answer, "%d", ax);
410 strprompt("New access level (? for help, q to quit)", answer, 1);
411 if ((answer[0] >= '0') && (answer[0] <= '6')) {
413 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
415 scr_printf("%s\n\n", cmd);
418 if (tolower(answer[0]) == 'q') {
419 scr_printf("*** Aborted.\n\n");
422 if (answer[0] == '?') {
423 scr_printf("Available access levels:\n");
424 for (a = 0; a < 7; ++a) {
425 scr_printf("%d - %s\n", a, axdefs[a]);
437 void validate(CtdlIPC * ipc)
442 int r; /* IPC response code */
445 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
449 scr_printf("%s\n", cmd);
451 extract_token(buf, cmd, 0, '|', sizeof buf);
452 if (val_user(ipc, buf, 1) != 0)
455 } while (finished == 0);
462 stty_ctdl(SB_RESTORE);
465 signal(SIGINT, SIG_DFL);
466 signal(SIGQUIT, SIG_DFL);
467 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
468 scr_printf("Could not open a shell: %s\n", strerror(errno));
473 } while ((a != b) && (a != (-1)));
478 * <.A>ide <F>ile <D>elete command
480 void deletefile(CtdlIPC * ipc)
485 newprompt("Filename: ", filename, 31);
486 if (IsEmptyStr(filename))
488 CtdlIPCDeleteFile(ipc, filename, buf);
489 scr_printf("%s\n", buf);
494 * <.A>ide <F>ile <M>ove command
496 void movefile(CtdlIPC * ipc)
499 char newroom[ROOMNAMELEN];
502 newprompt("Filename: ", filename, 63);
503 if (IsEmptyStr(filename))
505 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
506 CtdlIPCMoveFile(ipc, filename, newroom, buf);
507 scr_printf("%s\n", buf);
512 * list of users who have filled out a bio
514 void list_bio(CtdlIPC * ipc)
519 int r; /* IPC response code */
521 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
523 scr_printf("%s\n", buf);
526 while (resp && !IsEmptyStr(resp)) {
527 extract_token(buf, resp, 0, '\n', sizeof buf);
528 remove_token(resp, 0, '\n');
529 if ((pos + strlen(buf) + 5) > screenwidth) {
533 scr_printf("%s, ", buf);
534 pos = pos + strlen(buf) + 2;
536 scr_printf("%c%c \n\n", 8, 8);
545 void read_bio(CtdlIPC * ipc)
550 int r; /* IPC response code */
553 newprompt("Read bio for who ('?' for list) : ", who, 25);
555 if (!strcmp(who, "?"))
557 } while (!strcmp(who, "?"));
559 r = CtdlIPCGetBio(ipc, who, &resp, buf);
561 scr_printf("%s\n", buf);
564 while (!IsEmptyStr(resp)) {
565 extract_token(buf, resp, 0, '\n', sizeof buf);
566 remove_token(resp, 0, '\n');
567 scr_printf("%s\n", buf);