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))) {
124 "\nYou can't use %s as your address.\n",
127 "It is already in use by %s @ %s.\n",
130 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
135 /* now send the registration info back to the server */
136 reg = (char *)realloc(reg, SIZ);
138 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
139 tmpname, tmpaddr, tmpcity, tmpstate,
140 tmpzip, tmpphone, tmpemail, tmpcountry);
141 r = CtdlIPCSetRegistration(ipc, reg, buf);
143 scr_printf("%s\n", buf);
150 * make all messages old in current room
152 void updatels(CtdlIPC *ipc)
155 int r; /* IPC response code */
157 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
160 scr_printf("%s\n", buf);
164 * only make messages old in this room that have been read
166 void updatelsa(CtdlIPC *ipc)
169 int r; /* IPC response code */
171 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
173 scr_printf("%s\n", &buf[4]);
178 * client-based uploads (for users with their own clientware)
180 void cli_upload(CtdlIPC *ipc)
186 int r; /* IPC response code */
190 if ((room_flags & QR_UPLOAD) == 0) {
191 scr_printf("*** You cannot upload to this room.\n");
194 newprompt("File to be uploaded: ", flnm, 55);
195 fd = open(flnm, O_RDONLY);
197 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
200 scr_printf("Enter a description of this file:\n");
201 newprompt(": ", desc, 75);
203 /* Keep generating filenames in hope of finding a unique one */
206 /* basename of filename */
208 if (haschar(tbuf, '/'))
209 extract_token(tbuf, flnm,
210 num_tokens(tbuf, '/') - 1,
213 /* filename.1, filename.2, etc */
215 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
218 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
219 if (r / 100 == 5 || r < 0)
220 scr_printf("%s\n", buf);
225 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
230 * Function used for various image upload commands
232 void cli_image_upload(CtdlIPC *ipc, char *keyname)
238 /* Can we upload this image? */
239 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
241 scr_printf("%s\n", buf);
244 newprompt("Image file to be uploaded: ", flnm, 55);
245 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
247 scr_printf("%s\n", buf);
249 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
251 /* else upload succeeded */
256 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
258 void upload(CtdlIPC *ipc, int c)
259 { /* c = upload mode */
269 if ((room_flags & QR_UPLOAD) == 0) {
270 scr_printf("*** You cannot upload to this room.\n");
273 /* we don't need a filename when receiving batch y/z modem */
274 if ((c == 2) || (c == 3))
277 newprompt("Enter filename: ", flnm, 15);
279 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
280 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
281 || (flnm[a] == '?') || (flnm[a] == '*')
282 || (flnm[a] == ';') || (flnm[a] == '&'))
285 /* create a temporary directory... */
286 if (mkdir(tempdir, 0700) != 0) {
287 scr_printf("*** Could not create temporary directory %s: %s\n",
288 tempdir, strerror(errno));
291 /* now do the transfer ... in a separate process */
296 scr_printf("failed to change into %s Reason %s\nAborting now.\n",
305 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
306 fp = fopen(flnm, "w");
321 execlp("rx", "rx", flnm, NULL);
325 execlp("rb", "rb", NULL);
329 execlp("rz", "rz", NULL);
335 } while ((b != xfer_pid) && (b != (-1)));
339 scr_printf("\r*** Transfer unsuccessful.\n");
343 scr_printf("\r*** Transfer successful.\n");
344 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
345 lsfp = popen(buf, "r");
347 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
348 flnm[strlen(flnm) - 1] = 0; /* chop newline */
349 snprintf(buf, sizeof buf,
350 "Enter a short description of '%s':\n: ",
352 newprompt(buf, desc, 150);
353 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
354 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
355 scr_printf("%s\n", tbuf);
363 * validate a user (returns 0 for successful validation, nonzero if quitting)
365 int val_user(CtdlIPC *ipc, char *user, int do_validate)
373 int r; /* IPC response code */
376 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
380 extract_token(buf, resp, 0, '\n', sizeof buf);
381 remove_token(resp, 0, '\n');
384 scr_printf("User #%s - %s ", buf, cmd);
386 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
388 scr_printf("%s\n", buf);
390 scr_printf("%s\n", buf);
392 scr_printf("%s, ", buf);
394 scr_printf("%s ", buf);
396 scr_printf("%s\n", buf);
398 scr_printf("%s\n", buf);
402 scr_printf("%s\n", buf);
404 scr_printf("%s\n", buf);
405 } while (!IsEmptyStr(resp));
407 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
408 * but most of it limits it to axdefs[6].
409 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
411 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
413 scr_printf("%s\n%s\n", user, &cmd[4]);
415 if (resp) free(resp);
418 /* now set the access level */
420 sprintf(answer, "%d", ax);
421 strprompt("New access level (? for help, q to quit)",
423 if ((answer[0] >= '0') && (answer[0] <= '6')) {
425 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
427 scr_printf("%s\n\n", cmd);
430 if (tolower(answer[0]) == 'q') {
431 scr_printf("*** Aborted.\n\n");
434 if (answer[0] == '?') {
435 scr_printf("Available access levels:\n");
436 for (a=0; a<7; ++a) {
437 scr_printf("%d - %s\n",
450 void validate(CtdlIPC *ipc)
455 int r; /* IPC response code */
458 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
462 scr_printf("%s\n", cmd);
464 extract_token(buf, cmd, 0, '|', sizeof buf);
465 if (val_user(ipc, buf, 1) != 0) finished = 1;
467 } while (finished == 0);
474 stty_ctdl(SB_RESTORE);
477 signal(SIGINT, SIG_DFL);
478 signal(SIGQUIT, SIG_DFL);
479 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
480 scr_printf("Could not open a shell: %s\n", strerror(errno));
485 } while ((a != b) && (a != (-1)));
490 * <.A>ide <F>ile <D>elete command
492 void deletefile(CtdlIPC *ipc)
497 newprompt("Filename: ", filename, 31);
498 if (IsEmptyStr(filename))
500 CtdlIPCDeleteFile(ipc, filename, buf);
501 scr_printf("%s\n", buf);
506 * <.A>ide <F>ile <M>ove command
508 void movefile(CtdlIPC *ipc)
511 char newroom[ROOMNAMELEN];
514 newprompt("Filename: ", filename, 63);
515 if (IsEmptyStr(filename))
517 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
518 CtdlIPCMoveFile(ipc, filename, newroom, buf);
519 scr_printf("%s\n", buf);
524 * list of users who have filled out a bio
526 void list_bio(CtdlIPC *ipc)
531 int r; /* IPC response code */
533 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
535 scr_printf("%s\n", buf);
538 while (resp && !IsEmptyStr(resp)) {
539 extract_token(buf, resp, 0, '\n', sizeof buf);
540 remove_token(resp, 0, '\n');
541 if ((pos + strlen(buf) + 5) > screenwidth) {
545 scr_printf("%s, ", buf);
546 pos = pos + strlen(buf) + 2;
548 scr_printf("%c%c \n\n", 8, 8);
549 if (resp) free(resp);
556 void read_bio(CtdlIPC *ipc)
561 int r; /* IPC response code */
564 newprompt("Read bio for who ('?' for list) : ", who, 25);
566 if (!strcmp(who, "?"))
568 } while (!strcmp(who, "?"));
570 r = CtdlIPCGetBio(ipc, who, &resp, buf);
572 scr_printf("%s\n", buf);
575 while (!IsEmptyStr(resp)) {
576 extract_token(buf, resp, 0, '\n', sizeof buf);
577 remove_token(resp, 0, '\n');
578 scr_printf("%s\n", buf);
580 if (resp) free(resp);