1 // More client-side support functions.
3 // Copyright (c) 1987-2017 by the citadel.org team
5 // This program is open source software. Use, duplication, and/or
6 // disclosure are subject to the GNU General Purpose License version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 #include "textclient.h"
15 // work around solaris include files
21 extern char tempdir[];
22 extern char *axdefs[8];
23 extern long highest_msg_read;
24 extern long maxmsgnum;
25 extern unsigned room_flags;
26 extern int screenwidth;
29 // return proper room prompt character
30 int room_prompt(unsigned int qrflags) {
33 if (qrflags & QR_DIRECTORY) {
40 // Register with name and address
41 void entregis(CtdlIPC * ipc) {
57 int r; /* IPC response code */
66 strcpy(tmpcountry, "");
68 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
72 while (reg && !IsEmptyStr(reg)) {
74 extract_token(buf, reg, 0, '\n', sizeof buf);
75 remove_token(reg, 0, '\n');
78 safestrncpy(tmpname, buf, sizeof tmpname);
80 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
82 safestrncpy(tmpcity, buf, sizeof tmpcity);
84 safestrncpy(tmpstate, buf, sizeof tmpstate);
86 safestrncpy(tmpzip, buf, sizeof tmpzip);
88 safestrncpy(tmpphone, buf, sizeof tmpphone);
90 safestrncpy(tmpemail, buf, sizeof tmpemail);
92 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
96 strprompt("REAL name", tmpname, 29);
97 strprompt("Address", tmpaddr, 24);
98 strprompt("City/town", tmpcity, 14);
99 strprompt("State/province", tmpstate, 2);
100 strprompt("ZIP/Postal Code", tmpzip, 10);
101 strprompt("Country", tmpcountry, 31);
102 strprompt("Telephone number", tmpphone, 14);
106 safestrncpy(holdemail, tmpemail, sizeof holdemail);
107 strprompt("Email address", tmpemail, 31);
108 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
110 extract_token(diruser, buf, 0, '@', sizeof diruser);
111 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
114 if ((strcasecmp(diruser, fullname))
115 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
116 scr_printf("\nYou can't use %s as your address.\n", tmpemail);
117 scr_printf("It is already in use by %s @ %s.\n", diruser, dirnode);
119 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
124 /* now send the registration info back to the server */
125 reg = (char *) realloc(reg, SIZ);
127 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
128 tmpname, tmpaddr, tmpcity, tmpstate, tmpzip, tmpphone, tmpemail, tmpcountry);
129 r = CtdlIPCSetRegistration(ipc, reg, buf);
131 scr_printf("%s\n", buf);
138 * make all messages old in current room
140 void updatels(CtdlIPC * ipc)
143 int r; /* IPC response code */
145 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
148 scr_printf("%s\n", buf);
152 * only make messages old in this room that have been read
154 void updatelsa(CtdlIPC * ipc)
157 int r; /* IPC response code */
159 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
161 scr_printf("%s\n", &buf[4]);
166 * client-based uploads (for users with their own clientware)
168 void cli_upload(CtdlIPC * ipc)
174 int r; /* IPC response code */
178 if ((room_flags & QR_UPLOAD) == 0) {
179 scr_printf("*** You cannot upload to this room.\n");
182 newprompt("File to be uploaded: ", flnm, 55);
183 fd = open(flnm, O_RDONLY);
185 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
188 scr_printf("Enter a description of this file:\n");
189 newprompt(": ", desc, 75);
191 /* Keep generating filenames in hope of finding a unique one */
194 /* basename of filename */
196 if (haschar(tbuf, '/'))
197 extract_token(tbuf, flnm, num_tokens(tbuf, '/') - 1, '/', sizeof tbuf);
198 /* filename.1, filename.2, etc */
200 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
203 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
204 if (r / 100 == 5 || r < 0)
205 scr_printf("%s\n", buf);
211 scr_printf("Saved as '%s'\n", tbuf);
216 * Function used for various image upload commands
218 void cli_image_upload(CtdlIPC * ipc, char *keyname)
224 /* Can we upload this image? */
225 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
227 scr_printf("%s\n", buf);
230 newprompt("Image file to be uploaded: ", flnm, 55);
231 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
233 scr_printf("%s\n", buf);
236 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
238 /* else upload succeeded */
243 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
245 void upload(CtdlIPC * ipc, int c)
246 { /* c = upload mode */
256 if ((room_flags & QR_UPLOAD) == 0) {
257 scr_printf("*** You cannot upload to this room.\n");
260 /* we don't need a filename when receiving batch y/z modem */
261 if ((c == 2) || (c == 3))
264 newprompt("Enter filename: ", flnm, 15);
266 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
267 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
268 || (flnm[a] == '?') || (flnm[a] == '*')
269 || (flnm[a] == ';') || (flnm[a] == '&'))
272 /* create a temporary directory... */
273 if (mkdir(tempdir, 0700) != 0) {
274 scr_printf("*** Could not create temporary directory %s: %s\n", tempdir, strerror(errno));
277 /* now do the transfer ... in a separate process */
282 scr_printf("failed to change into %s Reason %s\nAborting now.\n", tempdir, strerror(errno));
289 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
290 fp = fopen(flnm, "w");
305 execlp("rx", "rx", flnm, NULL);
309 execlp("rb", "rb", NULL);
313 execlp("rz", "rz", NULL);
319 } while ((b != xfer_pid) && (b != (-1)));
323 scr_printf("\r*** Transfer unsuccessful.\n");
327 scr_printf("\r*** Transfer successful.\n");
328 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
329 lsfp = popen(buf, "r");
331 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
332 flnm[strlen(flnm) - 1] = 0; /* chop newline */
333 snprintf(buf, sizeof buf, "Enter a short description of '%s':\n: ", flnm);
334 newprompt(buf, desc, 150);
335 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
336 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
337 scr_printf("%s\n", tbuf);
346 * validate a user (returns 0 for successful validation, nonzero if quitting)
348 int val_user(CtdlIPC * ipc, char *user, int do_validate)
356 int r; /* IPC response code */
359 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
363 extract_token(buf, resp, 0, '\n', sizeof buf);
364 remove_token(resp, 0, '\n');
367 scr_printf("User #%s - %s ", buf, cmd);
369 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>"));
371 scr_printf("%s\n", buf);
373 scr_printf("%s\n", buf);
375 scr_printf("%s, ", buf);
377 scr_printf("%s ", buf);
379 scr_printf("%s\n", buf);
381 scr_printf("%s\n", buf);
385 scr_printf("%s\n", buf);
387 scr_printf("%s\n", buf);
388 } while (!IsEmptyStr(resp));
390 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
391 * but most of it limits it to axdefs[6].
392 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
394 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
396 scr_printf("%s\n%s\n", user, &cmd[4]);
402 /* now set the access level */
404 sprintf(answer, "%d", ax);
405 strprompt("New access level (? for help, q to quit)", answer, 1);
406 if ((answer[0] >= '0') && (answer[0] <= '6')) {
408 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
410 scr_printf("%s\n\n", cmd);
413 if (tolower(answer[0]) == 'q') {
414 scr_printf("*** Aborted.\n\n");
417 if (answer[0] == '?') {
418 scr_printf("Available access levels:\n");
419 for (a = 0; a < 7; ++a) {
420 scr_printf("%d - %s\n", a, axdefs[a]);
432 void validate(CtdlIPC * ipc)
437 int r; /* IPC response code */
440 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
444 scr_printf("%s\n", cmd);
446 extract_token(buf, cmd, 0, '|', sizeof buf);
447 if (val_user(ipc, buf, 1) != 0)
450 } while (finished == 0);
458 stty_ctdl(SB_RESTORE);
461 signal(SIGINT, SIG_DFL);
462 signal(SIGQUIT, SIG_DFL);
463 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
464 scr_printf("Could not open a shell: %s\n", strerror(errno));
469 } while ((a != b) && (a != (-1)));
474 * <.A>ide <F>ile <D>elete command
476 void deletefile(CtdlIPC * ipc)
481 newprompt("Filename: ", filename, 31);
482 if (IsEmptyStr(filename))
484 CtdlIPCDeleteFile(ipc, filename, buf);
485 scr_printf("%s\n", buf);
490 * <.A>ide <F>ile <M>ove command
492 void movefile(CtdlIPC * ipc)
495 char newroom[ROOMNAMELEN];
498 newprompt("Filename: ", filename, 63);
499 if (IsEmptyStr(filename))
501 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
502 CtdlIPCMoveFile(ipc, filename, newroom, buf);
503 scr_printf("%s\n", buf);
508 * list of users who have filled out a bio
510 void list_bio(CtdlIPC * ipc)
515 int r; /* IPC response code */
517 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
519 scr_printf("%s\n", buf);
522 while (resp && !IsEmptyStr(resp)) {
523 extract_token(buf, resp, 0, '\n', sizeof buf);
524 remove_token(resp, 0, '\n');
525 if ((pos + strlen(buf) + 5) > screenwidth) {
529 scr_printf("%s, ", buf);
530 pos = pos + strlen(buf) + 2;
532 scr_printf("%c%c \n\n", 8, 8);
539 void read_bio(CtdlIPC * ipc) {
543 int r; /* IPC response code */
546 newprompt("Read bio for who ('?' for list) : ", who, 25);
548 if (!strcmp(who, "?"))
550 } while (!strcmp(who, "?"));
552 r = CtdlIPCGetBio(ipc, who, &resp, buf);
554 scr_printf("%s\n", buf);
557 while (!IsEmptyStr(resp)) {
558 extract_token(buf, resp, 0, '\n', sizeof buf);
559 remove_token(resp, 0, '\n');
560 scr_printf("%s\n", buf);