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;
30 * return proper room prompt character
32 int room_prompt(unsigned int qrflags)
36 if (qrflags & QR_DIRECTORY)
43 * Register with name and address
45 void entregis(CtdlIPC * ipc)
62 int r; /* IPC response code */
71 strcpy(tmpcountry, "");
73 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
77 while (reg && !IsEmptyStr(reg)) {
79 extract_token(buf, reg, 0, '\n', sizeof buf);
80 remove_token(reg, 0, '\n');
83 safestrncpy(tmpname, buf, sizeof tmpname);
85 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
87 safestrncpy(tmpcity, buf, sizeof tmpcity);
89 safestrncpy(tmpstate, buf, sizeof tmpstate);
91 safestrncpy(tmpzip, buf, sizeof tmpzip);
93 safestrncpy(tmpphone, buf, sizeof tmpphone);
95 safestrncpy(tmpemail, buf, sizeof tmpemail);
97 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
101 strprompt("REAL name", tmpname, 29);
102 strprompt("Address", tmpaddr, 24);
103 strprompt("City/town", tmpcity, 14);
104 strprompt("State/province", tmpstate, 2);
105 strprompt("ZIP/Postal Code", tmpzip, 10);
106 strprompt("Country", tmpcountry, 31);
107 strprompt("Telephone number", tmpphone, 14);
111 safestrncpy(holdemail, tmpemail, sizeof holdemail);
112 strprompt("Email address", tmpemail, 31);
113 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
115 extract_token(diruser, buf, 0, '@', sizeof diruser);
116 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
119 if ((strcasecmp(diruser, fullname))
120 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
121 scr_printf("\nYou can't use %s as your address.\n", tmpemail);
122 scr_printf("It is already in use by %s @ %s.\n", diruser, dirnode);
124 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
129 /* now send the registration info back to the server */
130 reg = (char *) realloc(reg, SIZ);
132 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
133 tmpname, tmpaddr, tmpcity, tmpstate, tmpzip, tmpphone, tmpemail, tmpcountry);
134 r = CtdlIPCSetRegistration(ipc, reg, buf);
136 scr_printf("%s\n", buf);
143 * make all messages old in current room
145 void updatels(CtdlIPC * ipc)
148 int r; /* IPC response code */
150 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
153 scr_printf("%s\n", buf);
157 * only make messages old in this room that have been read
159 void updatelsa(CtdlIPC * ipc)
162 int r; /* IPC response code */
164 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
166 scr_printf("%s\n", &buf[4]);
171 * client-based uploads (for users with their own clientware)
173 void cli_upload(CtdlIPC * ipc)
179 int r; /* IPC response code */
183 if ((room_flags & QR_UPLOAD) == 0) {
184 scr_printf("*** You cannot upload to this room.\n");
187 newprompt("File to be uploaded: ", flnm, 55);
188 fd = open(flnm, O_RDONLY);
190 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
193 scr_printf("Enter a description of this file:\n");
194 newprompt(": ", desc, 75);
196 /* Keep generating filenames in hope of finding a unique one */
199 /* basename of filename */
201 if (haschar(tbuf, '/'))
202 extract_token(tbuf, flnm, num_tokens(tbuf, '/') - 1, '/', sizeof tbuf);
203 /* filename.1, filename.2, etc */
205 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
208 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
209 if (r / 100 == 5 || r < 0)
210 scr_printf("%s\n", buf);
216 scr_printf("Saved as '%s'\n", tbuf);
221 * Function used for various image upload commands
223 void cli_image_upload(CtdlIPC * ipc, char *keyname)
229 /* Can we upload this image? */
230 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
232 scr_printf("%s\n", buf);
235 newprompt("Image file to be uploaded: ", flnm, 55);
236 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
238 scr_printf("%s\n", buf);
240 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
242 /* else upload succeeded */
247 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
249 void upload(CtdlIPC * ipc, int c)
250 { /* c = upload mode */
260 if ((room_flags & QR_UPLOAD) == 0) {
261 scr_printf("*** You cannot upload to this room.\n");
264 /* we don't need a filename when receiving batch y/z modem */
265 if ((c == 2) || (c == 3))
268 newprompt("Enter filename: ", flnm, 15);
270 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
271 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
272 || (flnm[a] == '?') || (flnm[a] == '*')
273 || (flnm[a] == ';') || (flnm[a] == '&'))
276 /* create a temporary directory... */
277 if (mkdir(tempdir, 0700) != 0) {
278 scr_printf("*** Could not create temporary directory %s: %s\n", tempdir, strerror(errno));
281 /* now do the transfer ... in a separate process */
286 scr_printf("failed to change into %s Reason %s\nAborting now.\n", tempdir, strerror(errno));
293 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
294 fp = fopen(flnm, "w");
309 execlp("rx", "rx", flnm, NULL);
313 execlp("rb", "rb", NULL);
317 execlp("rz", "rz", NULL);
323 } while ((b != xfer_pid) && (b != (-1)));
327 scr_printf("\r*** Transfer unsuccessful.\n");
331 scr_printf("\r*** Transfer successful.\n");
332 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
333 lsfp = popen(buf, "r");
335 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
336 flnm[strlen(flnm) - 1] = 0; /* chop newline */
337 snprintf(buf, sizeof buf, "Enter a short description of '%s':\n: ", flnm);
338 newprompt(buf, desc, 150);
339 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
340 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
341 scr_printf("%s\n", tbuf);
349 * validate a user (returns 0 for successful validation, nonzero if quitting)
351 int val_user(CtdlIPC * ipc, char *user, int do_validate)
359 int r; /* IPC response code */
362 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
366 extract_token(buf, resp, 0, '\n', sizeof buf);
367 remove_token(resp, 0, '\n');
370 scr_printf("User #%s - %s ", buf, cmd);
372 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>"));
374 scr_printf("%s\n", buf);
376 scr_printf("%s\n", buf);
378 scr_printf("%s, ", buf);
380 scr_printf("%s ", buf);
382 scr_printf("%s\n", buf);
384 scr_printf("%s\n", buf);
388 scr_printf("%s\n", buf);
390 scr_printf("%s\n", buf);
391 } while (!IsEmptyStr(resp));
393 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
394 * but most of it limits it to axdefs[6].
395 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
397 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
399 scr_printf("%s\n%s\n", user, &cmd[4]);
405 /* now set the access level */
407 sprintf(answer, "%d", ax);
408 strprompt("New access level (? for help, q to quit)", answer, 1);
409 if ((answer[0] >= '0') && (answer[0] <= '6')) {
411 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
413 scr_printf("%s\n\n", cmd);
416 if (tolower(answer[0]) == 'q') {
417 scr_printf("*** Aborted.\n\n");
420 if (answer[0] == '?') {
421 scr_printf("Available access levels:\n");
422 for (a = 0; a < 7; ++a) {
423 scr_printf("%d - %s\n", a, axdefs[a]);
435 void validate(CtdlIPC * ipc)
440 int r; /* IPC response code */
443 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
447 scr_printf("%s\n", cmd);
449 extract_token(buf, cmd, 0, '|', sizeof buf);
450 if (val_user(ipc, buf, 1) != 0)
453 } while (finished == 0);
460 stty_ctdl(SB_RESTORE);
463 signal(SIGINT, SIG_DFL);
464 signal(SIGQUIT, SIG_DFL);
465 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
466 scr_printf("Could not open a shell: %s\n", strerror(errno));
471 } while ((a != b) && (a != (-1)));
476 * <.A>ide <F>ile <D>elete command
478 void deletefile(CtdlIPC * ipc)
483 newprompt("Filename: ", filename, 31);
484 if (IsEmptyStr(filename))
486 CtdlIPCDeleteFile(ipc, filename, buf);
487 scr_printf("%s\n", buf);
492 * <.A>ide <F>ile <M>ove command
494 void movefile(CtdlIPC * ipc)
497 char newroom[ROOMNAMELEN];
500 newprompt("Filename: ", filename, 63);
501 if (IsEmptyStr(filename))
503 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
504 CtdlIPCMoveFile(ipc, filename, newroom, buf);
505 scr_printf("%s\n", buf);
510 * list of users who have filled out a bio
512 void list_bio(CtdlIPC * ipc)
517 int r; /* IPC response code */
519 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
521 scr_printf("%s\n", buf);
524 while (resp && !IsEmptyStr(resp)) {
525 extract_token(buf, resp, 0, '\n', sizeof buf);
526 remove_token(resp, 0, '\n');
527 if ((pos + strlen(buf) + 5) > screenwidth) {
531 scr_printf("%s, ", buf);
532 pos = pos + strlen(buf) + 2;
534 scr_printf("%c%c \n\n", 8, 8);
543 void read_bio(CtdlIPC * ipc)
548 int r; /* IPC response code */
551 newprompt("Read bio for who ('?' for list) : ", who, 25);
553 if (!strcmp(who, "?"))
555 } while (!strcmp(who, "?"));
557 r = CtdlIPCGetBio(ipc, who, &resp, buf);
559 scr_printf("%s\n", buf);
562 while (!IsEmptyStr(resp)) {
563 extract_token(buf, resp, 0, '\n', sizeof buf);
564 remove_token(resp, 0, '\n');
565 scr_printf("%s\n", buf);