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 is 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 strncpy(tmpname, buf, sizeof tmpname);
80 strncpy(tmpaddr, buf, sizeof tmpaddr);
82 strncpy(tmpcity, buf, sizeof tmpcity);
84 strncpy(tmpstate, buf, sizeof tmpstate);
86 strncpy(tmpzip, buf, sizeof tmpzip);
88 strncpy(tmpphone, buf, sizeof tmpphone);
90 strncpy(tmpemail, buf, sizeof tmpemail);
92 strncpy(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 strncpy(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);
112 string_trim(diruser);
113 string_trim(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 strncpy(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) {
142 int r; /* IPC response code */
144 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
147 scr_printf("%s\n", buf);
151 * only make messages old in this room that have been read
153 void updatelsa(CtdlIPC * ipc) {
155 int r; /* IPC response code */
157 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
159 scr_printf("%s\n", &buf[4]);
164 * client-based uploads (for users with their own clientware)
166 void cli_upload(CtdlIPC * ipc) {
171 int r; /* IPC response code */
175 if ((room_flags & QR_UPLOAD) == 0) {
176 scr_printf("*** You cannot upload to this room.\n");
179 newprompt("File to be uploaded: ", flnm, 55);
180 fd = open(flnm, O_RDONLY);
182 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
185 scr_printf("Enter a description of this file:\n");
186 newprompt(": ", desc, 75);
188 /* Keep generating filenames in hope of finding a unique one */
191 /* basename of filename */
193 if (haschar(tbuf, '/'))
194 extract_token(tbuf, flnm, num_tokens(tbuf, '/') - 1, '/', sizeof tbuf);
195 /* filename.1, filename.2, etc */
197 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
200 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
201 if (r / 100 == 5 || r < 0)
202 scr_printf("%s\n", buf);
208 scr_printf("Saved as '%s'\n", tbuf);
213 * Function used for various image upload commands
215 void cli_image_upload(CtdlIPC * ipc, char *keyname) {
220 /* Can we upload this image? */
221 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
223 scr_printf("%s\n", buf);
226 newprompt("Image file to be uploaded: ", flnm, 55);
227 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
229 scr_printf("%s\n", buf);
232 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
234 /* else upload succeeded */
239 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
241 void upload(CtdlIPC * ipc, int c) { /* c = upload mode */
251 if ((room_flags & QR_UPLOAD) == 0) {
252 scr_printf("*** You cannot upload to this room.\n");
255 /* we don't need a filename when receiving batch y/z modem */
256 if ((c == 2) || (c == 3))
259 newprompt("Enter filename: ", flnm, 15);
261 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
262 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
263 || (flnm[a] == '?') || (flnm[a] == '*')
264 || (flnm[a] == ';') || (flnm[a] == '&'))
267 /* create a temporary directory... */
268 if (mkdir(tempdir, 0700) != 0) {
269 scr_printf("*** Could not create temporary directory %s: %s\n", tempdir, strerror(errno));
272 /* now do the transfer ... in a separate process */
277 scr_printf("failed to change into %s Reason %s\nAborting now.\n", tempdir, strerror(errno));
284 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
285 fp = fopen(flnm, "w");
300 execlp("rx", "rx", flnm, NULL);
304 execlp("rb", "rb", NULL);
308 execlp("rz", "rz", NULL);
315 } while ((b != xfer_pid) && (b != (-1)));
319 scr_printf("\r*** Transfer unsuccessful.\n");
323 scr_printf("\r*** Transfer successful.\n");
324 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
325 lsfp = popen(buf, "r");
327 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
328 flnm[strlen(flnm) - 1] = 0; /* chop newline */
329 snprintf(buf, sizeof buf, "Enter a short description of '%s':\n: ", flnm);
330 newprompt(buf, desc, 150);
331 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
332 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
333 scr_printf("%s\n", tbuf);
342 * validate a user (returns 0 for successful validation, nonzero if quitting)
344 int val_user(CtdlIPC * ipc, char *user, int do_validate) {
351 int r; /* IPC response code */
354 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
358 extract_token(buf, resp, 0, '\n', sizeof buf);
359 remove_token(resp, 0, '\n');
362 scr_printf("User #%s - %s ", buf, cmd);
364 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>"));
366 scr_printf("%s\n", buf);
368 scr_printf("%s\n", buf);
370 scr_printf("%s, ", buf);
372 scr_printf("%s ", buf);
374 scr_printf("%s\n", buf);
376 scr_printf("%s\n", buf);
380 scr_printf("%s\n", buf);
382 scr_printf("%s\n", buf);
383 } while (!IsEmptyStr(resp));
385 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
386 * but most of it limits it to axdefs[6].
387 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
389 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
392 scr_printf("%s\n%s\n", user, &cmd[4]);
398 /* now set the access level */
400 sprintf(answer, "%d", ax);
401 strprompt("New access level (? for help, q to quit)", answer, 1);
402 if ((answer[0] >= '0') && (answer[0] <= '6')) {
404 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
406 scr_printf("%s\n\n", cmd);
409 if (tolower(answer[0]) == 'q') {
410 scr_printf("*** Aborted.\n\n");
413 if (answer[0] == '?') {
414 scr_printf("Available access levels:\n");
415 for (a = 0; a < 7; ++a) {
416 scr_printf("%d - %s\n", a, axdefs[a]);
428 void validate(CtdlIPC * ipc) {
432 int r; /* IPC response code */
435 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
439 scr_printf("%s\n", cmd);
441 extract_token(buf, cmd, 0, '|', sizeof buf);
442 if (val_user(ipc, buf, 1) != 0)
445 } while (finished == 0);
449 void subshell(void) {
452 stty_ctdl(SB_RESTORE);
455 signal(SIGINT, SIG_DFL);
456 signal(SIGQUIT, SIG_DFL);
457 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
458 scr_printf("Could not open a shell: %s\n", strerror(errno));
463 } while ((a != b) && (a != (-1)));
468 * <.A>ide <F>ile <D>elete command
470 void deletefile(CtdlIPC * ipc) {
474 newprompt("Filename: ", filename, 31);
475 if (IsEmptyStr(filename))
477 CtdlIPCDeleteFile(ipc, filename, buf);
478 scr_printf("%s\n", buf);
483 * <.A>ide <F>ile <M>ove command
485 void movefile(CtdlIPC * ipc) {
487 char newroom[ROOMNAMELEN];
490 newprompt("Filename: ", filename, 63);
491 if (IsEmptyStr(filename))
493 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
494 CtdlIPCMoveFile(ipc, filename, newroom, buf);
495 scr_printf("%s\n", buf);
500 * list of users who have filled out a bio
502 void list_bio(CtdlIPC * ipc) {
506 int r; /* IPC response code */
508 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
510 scr_printf("%s\n", buf);
513 while (resp && !IsEmptyStr(resp)) {
514 extract_token(buf, resp, 0, '\n', sizeof buf);
515 remove_token(resp, 0, '\n');
516 if ((pos + strlen(buf) + 5) > screenwidth) {
520 scr_printf("%s, ", buf);
521 pos = pos + strlen(buf) + 2;
523 scr_printf("%c%c \n\n", 8, 8);
530 void read_bio(CtdlIPC * ipc) {
534 int r; /* IPC response code */
537 newprompt("Read bio for who ('?' for list) : ", who, 25);
539 if (!strcmp(who, "?"))
541 } while (!strcmp(who, "?"));
543 r = CtdlIPCGetBio(ipc, who, &resp, buf);
545 scr_printf("%s\n", buf);
548 while (!IsEmptyStr(resp)) {
549 extract_token(buf, resp, 0, '\n', sizeof buf);
550 remove_token(resp, 0, '\n');
551 scr_printf("%s\n", buf);