2 * More client-side support functions.
3 * Unlike routines.c, some of these DO use global variables.
5 * Copyright (c) 1987-2016 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 #include <sys/types.h>
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
31 # include <sys/time.h>
41 #include <libcitadel.h>
43 #include "citadel_ipc.h"
44 #include "citadel_decls.h"
45 #include "routines2.h"
50 /* work around solaris include files */
56 extern char tempdir[];
57 extern char *axdefs[8];
58 extern long highest_msg_read;
59 extern long maxmsgnum;
60 extern unsigned room_flags;
61 extern int screenwidth;
65 int eopen(char *name, int mode)
68 ret = open(name, mode);
70 scr_printf("Cannot open file '%s', mode=%d, errno=%d\n",
79 int room_prompt(unsigned int qrflags)
80 { /* return proper room prompt character */
83 if (qrflags & QR_DIRECTORY)
85 if ((a == ']') && (qrflags & QR_NETWORK))
87 if ((a == '>') && (qrflags & QR_NETWORK))
92 void entregis(CtdlIPC *ipc)
93 { /* register with name and address */
109 int r; /* IPC response code */
114 strcpy(tmpstate, "");
116 strcpy(tmpphone, "");
117 strcpy(tmpemail, "");
118 strcpy(tmpcountry, "");
120 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
124 while (reg && !IsEmptyStr(reg)) {
126 extract_token(buf, reg, 0, '\n', sizeof buf);
127 remove_token(reg, 0, '\n');
130 safestrncpy(tmpname, buf, sizeof tmpname);
132 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
134 safestrncpy(tmpcity, buf, sizeof tmpcity);
136 safestrncpy(tmpstate, buf, sizeof tmpstate);
138 safestrncpy(tmpzip, buf, sizeof tmpzip);
140 safestrncpy(tmpphone, buf, sizeof tmpphone);
142 safestrncpy(tmpemail, buf, sizeof tmpemail);
144 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
148 strprompt("REAL name", tmpname, 29);
149 strprompt("Address", tmpaddr, 24);
150 strprompt("City/town", tmpcity, 14);
151 strprompt("State/province", tmpstate, 2);
152 strprompt("ZIP/Postal Code", tmpzip, 10);
153 strprompt("Country", tmpcountry, 31);
154 strprompt("Telephone number", tmpphone, 14);
158 safestrncpy(holdemail, tmpemail, sizeof holdemail);
159 strprompt("Email address", tmpemail, 31);
160 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
162 extract_token(diruser, buf, 0, '@', sizeof diruser);
163 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
166 if ((strcasecmp(diruser, fullname))
167 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
169 "\nYou can't use %s as your address.\n",
172 "It is already in use by %s @ %s.\n",
175 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
180 /* now send the registration info back to the server */
181 reg = (char *)realloc(reg, SIZ);
183 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
184 tmpname, tmpaddr, tmpcity, tmpstate,
185 tmpzip, tmpphone, tmpemail, tmpcountry);
186 r = CtdlIPCSetRegistration(ipc, reg, buf);
188 scr_printf("%s\n", buf);
194 void updatels(CtdlIPC *ipc)
195 { /* make all messages old in current room */
197 int r; /* IPC response code */
199 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
202 scr_printf("%s\n", buf);
206 * only make messages old in this room that have been read
208 void updatelsa(CtdlIPC *ipc)
211 int r; /* IPC response code */
213 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
215 scr_printf("%s\n", &buf[4]);
220 * client-based uploads (for users with their own clientware)
222 void cli_upload(CtdlIPC *ipc)
228 int r; /* IPC response code */
232 if ((room_flags & QR_UPLOAD) == 0) {
233 scr_printf("*** You cannot upload to this room.\n");
236 newprompt("File to be uploaded: ", flnm, 55);
237 fd = open(flnm, O_RDONLY);
239 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
242 scr_printf("Enter a description of this file:\n");
243 newprompt(": ", desc, 75);
245 /* Keep generating filenames in hope of finding a unique one */
248 /* basename of filename */
250 if (haschar(tbuf, '/'))
251 extract_token(tbuf, flnm,
252 num_tokens(tbuf, '/') - 1,
255 /* filename.1, filename.2, etc */
257 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
260 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
261 if (r / 100 == 5 || r < 0)
262 scr_printf("%s\n", buf);
267 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
272 * Function used for various image upload commands
274 void cli_image_upload(CtdlIPC *ipc, char *keyname)
280 /* Can we upload this image? */
281 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
283 scr_printf("%s\n", buf);
286 newprompt("Image file to be uploaded: ", flnm, 55);
287 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
289 scr_printf("%s\n", buf);
291 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
293 /* else upload succeeded */
298 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
300 void upload(CtdlIPC *ipc, int c)
301 { /* c = upload mode */
311 if ((room_flags & QR_UPLOAD) == 0) {
312 scr_printf("*** You cannot upload to this room.\n");
315 /* we don't need a filename when receiving batch y/z modem */
316 if ((c == 2) || (c == 3))
319 newprompt("Enter filename: ", flnm, 15);
321 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
322 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
323 || (flnm[a] == '?') || (flnm[a] == '*')
324 || (flnm[a] == ';') || (flnm[a] == '&'))
327 /* create a temporary directory... */
328 if (mkdir(tempdir, 0700) != 0) {
329 scr_printf("*** Could not create temporary directory %s: %s\n",
330 tempdir, strerror(errno));
333 /* now do the transfer ... in a separate process */
338 scr_printf("failed to change into %s Reason %s\nAborting now.\n",
347 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
348 fp = fopen(flnm, "w");
363 execlp("rx", "rx", flnm, NULL);
367 execlp("rb", "rb", NULL);
371 execlp("rz", "rz", NULL);
377 } while ((b != xfer_pid) && (b != (-1)));
381 scr_printf("\r*** Transfer unsuccessful.\n");
385 scr_printf("\r*** Transfer successful.\n");
386 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
387 lsfp = popen(buf, "r");
389 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
390 flnm[strlen(flnm) - 1] = 0; /* chop newline */
391 snprintf(buf, sizeof buf,
392 "Enter a short description of '%s':\n: ",
394 newprompt(buf, desc, 150);
395 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
396 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
397 scr_printf("%s\n", tbuf);
405 * validate a user (returns 0 for successful validation, nonzero if quitting)
407 int val_user(CtdlIPC *ipc, char *user, int do_validate)
415 int r; /* IPC response code */
418 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
422 extract_token(buf, resp, 0, '\n', sizeof buf);
423 remove_token(resp, 0, '\n');
426 scr_printf("User #%s - %s ", buf, cmd);
428 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
430 scr_printf("%s\n", buf);
432 scr_printf("%s\n", buf);
434 scr_printf("%s, ", buf);
436 scr_printf("%s ", buf);
438 scr_printf("%s\n", buf);
440 scr_printf("%s\n", buf);
444 scr_printf("%s\n", buf);
446 scr_printf("%s\n", buf);
447 } while (!IsEmptyStr(resp));
449 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
450 * but most of it limits it to axdefs[6].
451 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
453 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
455 scr_printf("%s\n%s\n", user, &cmd[4]);
457 if (resp) free(resp);
460 /* now set the access level */
462 sprintf(answer, "%d", ax);
463 strprompt("New access level (? for help, q to quit)",
465 if ((answer[0] >= '0') && (answer[0] <= '6')) {
467 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
469 scr_printf("%s\n\n", cmd);
472 if (tolower(answer[0]) == 'q') {
473 scr_printf("*** Aborted.\n\n");
476 if (answer[0] == '?') {
477 scr_printf("Available access levels:\n");
478 for (a=0; a<7; ++a) {
479 scr_printf("%d - %s\n",
489 void validate(CtdlIPC *ipc)
490 { /* validate new users */
494 int r; /* IPC response code */
497 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
501 scr_printf("%s\n", cmd);
503 extract_token(buf, cmd, 0, '|', sizeof buf);
504 if (val_user(ipc, buf, 1) != 0) finished = 1;
506 } while (finished == 0);
513 stty_ctdl(SB_RESTORE);
516 signal(SIGINT, SIG_DFL);
517 signal(SIGQUIT, SIG_DFL);
518 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
519 scr_printf("Could not open a shell: %s\n", strerror(errno));
524 } while ((a != b) && (a != (-1)));
529 * <.A>ide <F>ile <D>elete command
531 void deletefile(CtdlIPC *ipc)
536 newprompt("Filename: ", filename, 31);
537 if (IsEmptyStr(filename))
539 CtdlIPCDeleteFile(ipc, filename, buf);
540 scr_printf("%s\n", buf);
545 * <.A>ide <F>ile <M>ove command
547 void movefile(CtdlIPC *ipc)
550 char newroom[ROOMNAMELEN];
553 newprompt("Filename: ", filename, 63);
554 if (IsEmptyStr(filename))
556 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
557 CtdlIPCMoveFile(ipc, filename, newroom, buf);
558 scr_printf("%s\n", buf);
563 * list of users who have filled out a bio
565 void list_bio(CtdlIPC *ipc)
570 int r; /* IPC response code */
572 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
574 scr_printf("%s\n", buf);
577 while (resp && !IsEmptyStr(resp)) {
578 extract_token(buf, resp, 0, '\n', sizeof buf);
579 remove_token(resp, 0, '\n');
580 if ((pos + strlen(buf) + 5) > screenwidth) {
584 scr_printf("%s, ", buf);
585 pos = pos + strlen(buf) + 2;
587 scr_printf("%c%c \n\n", 8, 8);
588 if (resp) free(resp);
595 void read_bio(CtdlIPC *ipc)
600 int r; /* IPC response code */
603 newprompt("Read bio for who ('?' for list) : ", who, 25);
605 if (!strcmp(who, "?"))
607 } while (!strcmp(who, "?"));
609 r = CtdlIPCGetBio(ipc, who, &resp, buf);
611 scr_printf("%s\n", buf);
614 while (!IsEmptyStr(resp)) {
615 extract_token(buf, resp, 0, '\n', sizeof buf);
616 remove_token(resp, 0, '\n');
617 scr_printf("%s\n", buf);
619 if (resp) free(resp);