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, or disclosure is subject to the GNU General Public License version 3.
7 #include "textclient.h"
9 // work around solaris include files
15 extern char tempdir[];
16 extern char *axdefs[8];
17 extern long highest_msg_read;
18 extern long maxmsgnum;
19 extern unsigned room_flags;
20 extern int screenwidth;
23 // return proper room prompt character
24 int room_prompt(unsigned int qrflags) {
27 if (qrflags & QR_DIRECTORY) {
34 // Register with name and address
35 void entregis(CtdlIPC * ipc) {
51 int r; /* IPC response code */
60 strcpy(tmpcountry, "");
62 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
66 while (reg && !IsEmptyStr(reg)) {
68 extract_token(buf, reg, 0, '\n', sizeof buf);
69 remove_token(reg, 0, '\n');
72 strncpy(tmpname, buf, sizeof tmpname);
74 strncpy(tmpaddr, buf, sizeof tmpaddr);
76 strncpy(tmpcity, buf, sizeof tmpcity);
78 strncpy(tmpstate, buf, sizeof tmpstate);
80 strncpy(tmpzip, buf, sizeof tmpzip);
82 strncpy(tmpphone, buf, sizeof tmpphone);
84 strncpy(tmpemail, buf, sizeof tmpemail);
86 strncpy(tmpcountry, buf, sizeof tmpcountry);
90 strprompt("REAL name", tmpname, 29);
91 strprompt("Address", tmpaddr, 24);
92 strprompt("City/town", tmpcity, 14);
93 strprompt("State/province", tmpstate, 2);
94 strprompt("ZIP/Postal Code", tmpzip, 10);
95 strprompt("Country", tmpcountry, 31);
96 strprompt("Telephone number", tmpphone, 14);
100 strncpy(holdemail, tmpemail, sizeof holdemail);
101 strprompt("Email address", tmpemail, 31);
102 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
104 extract_token(diruser, buf, 0, '@', sizeof diruser);
105 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
106 string_trim(diruser);
107 string_trim(dirnode);
108 if ((strcasecmp(diruser, fullname))
109 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
110 scr_printf("\nYou can't use %s as your address.\n", tmpemail);
111 scr_printf("It is already in use by %s @ %s.\n", diruser, dirnode);
113 strncpy(tmpemail, holdemail, sizeof tmpemail);
118 /* now send the registration info back to the server */
119 reg = (char *) realloc(reg, SIZ);
121 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
122 tmpname, tmpaddr, tmpcity, tmpstate, tmpzip, tmpphone, tmpemail, tmpcountry);
123 r = CtdlIPCSetRegistration(ipc, reg, buf);
125 scr_printf("%s\n", buf);
132 * make all messages old in current room
134 void updatels(CtdlIPC * ipc) {
136 int r; /* IPC response code */
138 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
141 scr_printf("%s\n", buf);
145 * only make messages old in this room that have been read
147 void updatelsa(CtdlIPC * ipc) {
149 int r; /* IPC response code */
151 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
153 scr_printf("%s\n", &buf[4]);
158 * client-based uploads (for users with their own clientware)
160 void cli_upload(CtdlIPC * ipc) {
165 int r; /* IPC response code */
169 if ((room_flags & QR_UPLOAD) == 0) {
170 scr_printf("*** You cannot upload to this room.\n");
173 newprompt("File to be uploaded: ", flnm, 55);
174 fd = open(flnm, O_RDONLY);
176 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
179 scr_printf("Enter a description of this file:\n");
180 newprompt(": ", desc, 75);
182 /* Keep generating filenames in hope of finding a unique one */
185 /* basename of filename */
187 if (haschar(tbuf, '/'))
188 extract_token(tbuf, flnm, num_tokens(tbuf, '/') - 1, '/', sizeof tbuf);
189 /* filename.1, filename.2, etc */
191 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
194 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
195 if (r / 100 == 5 || r < 0)
196 scr_printf("%s\n", buf);
202 scr_printf("Saved as '%s'\n", tbuf);
207 * Function used for various image upload commands
209 void cli_image_upload(CtdlIPC * ipc, char *keyname) {
214 /* Can we upload this image? */
215 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
217 scr_printf("%s\n", buf);
220 newprompt("Image file to be uploaded: ", flnm, 55);
221 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
223 scr_printf("%s\n", buf);
226 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
228 /* else upload succeeded */
233 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
235 void upload(CtdlIPC * ipc, int c) { /* c = upload mode */
245 if ((room_flags & QR_UPLOAD) == 0) {
246 scr_printf("*** You cannot upload to this room.\n");
249 /* we don't need a filename when receiving batch y/z modem */
250 if ((c == 2) || (c == 3))
253 newprompt("Enter filename: ", flnm, 15);
255 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
256 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
257 || (flnm[a] == '?') || (flnm[a] == '*')
258 || (flnm[a] == ';') || (flnm[a] == '&'))
261 /* create a temporary directory... */
262 if (mkdir(tempdir, 0700) != 0) {
263 scr_printf("*** Could not create temporary directory %s: %s\n", tempdir, strerror(errno));
266 /* now do the transfer ... in a separate process */
271 scr_printf("failed to change into %s Reason %s\nAborting now.\n", tempdir, strerror(errno));
278 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
279 fp = fopen(flnm, "w");
294 execlp("rx", "rx", flnm, NULL);
298 execlp("rb", "rb", NULL);
302 execlp("rz", "rz", NULL);
309 } while ((b != xfer_pid) && (b != (-1)));
313 scr_printf("\r*** Transfer unsuccessful.\n");
317 scr_printf("\r*** Transfer successful.\n");
318 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
319 lsfp = popen(buf, "r");
321 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
322 flnm[strlen(flnm) - 1] = 0; /* chop newline */
323 snprintf(buf, sizeof buf, "Enter a short description of '%s':\n: ", flnm);
324 newprompt(buf, desc, 150);
325 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
326 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
327 scr_printf("%s\n", tbuf);
336 * validate a user (returns 0 for successful validation, nonzero if quitting)
338 int val_user(CtdlIPC * ipc, char *user, int do_validate) {
345 int r; /* IPC response code */
348 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
352 extract_token(buf, resp, 0, '\n', sizeof buf);
353 remove_token(resp, 0, '\n');
356 scr_printf("User #%s - %s ", buf, cmd);
358 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>"));
360 scr_printf("%s\n", buf);
362 scr_printf("%s\n", buf);
364 scr_printf("%s, ", buf);
366 scr_printf("%s ", buf);
368 scr_printf("%s\n", buf);
370 scr_printf("%s\n", buf);
374 scr_printf("%s\n", buf);
376 scr_printf("%s\n", buf);
377 } while (!IsEmptyStr(resp));
379 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
380 * but most of it limits it to axdefs[6].
381 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
383 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
386 scr_printf("%s\n%s\n", user, &cmd[4]);
392 /* now set the access level */
394 sprintf(answer, "%d", ax);
395 strprompt("New access level (? for help, q to quit)", answer, 1);
396 if ((answer[0] >= '0') && (answer[0] <= '6')) {
398 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
400 scr_printf("%s\n\n", cmd);
403 if (tolower(answer[0]) == 'q') {
404 scr_printf("*** Aborted.\n\n");
407 if (answer[0] == '?') {
408 scr_printf("Available access levels:\n");
409 for (a = 0; a < 7; ++a) {
410 scr_printf("%d - %s\n", a, axdefs[a]);
422 void validate(CtdlIPC * ipc) {
426 int r; /* IPC response code */
429 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
433 scr_printf("%s\n", cmd);
435 extract_token(buf, cmd, 0, '|', sizeof buf);
436 if (val_user(ipc, buf, 1) != 0)
439 } while (finished == 0);
443 void subshell(void) {
446 stty_ctdl(SB_RESTORE);
449 signal(SIGINT, SIG_DFL);
450 signal(SIGQUIT, SIG_DFL);
451 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
452 scr_printf("Could not open a shell: %s\n", strerror(errno));
457 } while ((a != b) && (a != (-1)));
462 * <.A>ide <F>ile <D>elete command
464 void deletefile(CtdlIPC * ipc) {
468 newprompt("Filename: ", filename, 31);
469 if (IsEmptyStr(filename))
471 CtdlIPCDeleteFile(ipc, filename, buf);
472 scr_printf("%s\n", buf);
477 * <.A>ide <F>ile <M>ove command
479 void movefile(CtdlIPC * ipc) {
481 char newroom[ROOMNAMELEN];
484 newprompt("Filename: ", filename, 63);
485 if (IsEmptyStr(filename))
487 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
488 CtdlIPCMoveFile(ipc, filename, newroom, buf);
489 scr_printf("%s\n", buf);
494 * list of users who have filled out a bio
496 void list_bio(CtdlIPC * ipc) {
500 int r; /* IPC response code */
502 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
504 scr_printf("%s\n", buf);
507 while (resp && !IsEmptyStr(resp)) {
508 extract_token(buf, resp, 0, '\n', sizeof buf);
509 remove_token(resp, 0, '\n');
510 if ((pos + strlen(buf) + 5) > screenwidth) {
514 scr_printf("%s, ", buf);
515 pos = pos + strlen(buf) + 2;
517 scr_printf("%c%c \n\n", 8, 8);
524 void read_bio(CtdlIPC * ipc) {
528 int r; /* IPC response code */
531 newprompt("Read bio for who ('?' for list) : ", who, 25);
533 if (!strcmp(who, "?"))
535 } while (!strcmp(who, "?"));
537 r = CtdlIPCGetBio(ipc, who, &resp, buf);
539 scr_printf("%s\n", buf);
542 while (!IsEmptyStr(resp)) {
543 extract_token(buf, resp, 0, '\n', sizeof buf);
544 remove_token(resp, 0, '\n');
545 scr_printf("%s\n", buf);