2 * More client-side support functions.
3 * Unlike routines.c, some of these DO use global variables.
5 * Copyright (c) 1987-2012 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.h"
44 #include "citadel_ipc.h"
45 #include "citadel_decls.h"
46 #include "routines2.h"
49 //#include "messages.h"
50 //#ifndef HAVE_SNPRINTF
51 //#include "snprintf.h"
55 /* work around solaris include files */
61 extern char tempdir[];
62 extern char *axdefs[8];
63 extern long highest_msg_read;
64 extern long maxmsgnum;
65 extern unsigned room_flags;
66 extern int screenwidth;
70 int eopen(char *name, int mode)
73 ret = open(name, mode);
75 scr_printf("Cannot open file '%s', mode=%d, errno=%d\n",
84 int room_prompt(unsigned int qrflags)
85 { /* return proper room prompt character */
88 if (qrflags & QR_DIRECTORY)
90 if ((a == ']') && (qrflags & QR_NETWORK))
92 if ((a == '>') && (qrflags & QR_NETWORK))
97 void entregis(CtdlIPC *ipc)
98 { /* register with name and address */
114 int r; /* IPC response code */
119 strcpy(tmpstate, "");
121 strcpy(tmpphone, "");
122 strcpy(tmpemail, "");
123 strcpy(tmpcountry, "");
125 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
129 while (reg && !IsEmptyStr(reg)) {
131 extract_token(buf, reg, 0, '\n', sizeof buf);
132 remove_token(reg, 0, '\n');
135 safestrncpy(tmpname, buf, sizeof tmpname);
137 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
139 safestrncpy(tmpcity, buf, sizeof tmpcity);
141 safestrncpy(tmpstate, buf, sizeof tmpstate);
143 safestrncpy(tmpzip, buf, sizeof tmpzip);
145 safestrncpy(tmpphone, buf, sizeof tmpphone);
147 safestrncpy(tmpemail, buf, sizeof tmpemail);
149 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
153 strprompt("REAL name", tmpname, 29);
154 strprompt("Address", tmpaddr, 24);
155 strprompt("City/town", tmpcity, 14);
156 strprompt("State/province", tmpstate, 2);
157 strprompt("ZIP/Postal Code", tmpzip, 10);
158 strprompt("Country", tmpcountry, 31);
159 strprompt("Telephone number", tmpphone, 14);
163 safestrncpy(holdemail, tmpemail, sizeof holdemail);
164 strprompt("Email address", tmpemail, 31);
165 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
167 extract_token(diruser, buf, 0, '@', sizeof diruser);
168 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
171 if ((strcasecmp(diruser, fullname))
172 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
174 "\nYou can't use %s as your address.\n",
177 "It is already in use by %s @ %s.\n",
180 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
185 /* now send the registration info back to the server */
186 reg = (char *)realloc(reg, SIZ);
188 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
189 tmpname, tmpaddr, tmpcity, tmpstate,
190 tmpzip, tmpphone, tmpemail, tmpcountry);
191 r = CtdlIPCSetRegistration(ipc, reg, buf);
193 scr_printf("%s\n", buf);
199 void updatels(CtdlIPC *ipc)
200 { /* make all messages old in current room */
202 int r; /* IPC response code */
204 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
207 scr_printf("%s\n", buf);
211 * only make messages old in this room that have been read
213 void updatelsa(CtdlIPC *ipc)
216 int r; /* IPC response code */
218 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
220 scr_printf("%s\n", &buf[4]);
225 * client-based uploads (for users with their own clientware)
227 void cli_upload(CtdlIPC *ipc)
233 int r; /* IPC response code */
237 if ((room_flags & QR_UPLOAD) == 0) {
238 scr_printf("*** You cannot upload to this room.\n");
241 newprompt("File to be uploaded: ", flnm, 55);
242 fd = open(flnm, O_RDONLY);
244 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
247 scr_printf("Enter a description of this file:\n");
248 newprompt(": ", desc, 75);
250 /* Keep generating filenames in hope of finding a unique one */
253 /* basename of filename */
255 if (haschar(tbuf, '/'))
256 extract_token(tbuf, flnm,
257 num_tokens(tbuf, '/') - 1,
260 /* filename.1, filename.2, etc */
262 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
265 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
266 if (r / 100 == 5 || r < 0)
267 scr_printf("%s\n", buf);
272 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
277 * Function used for various image upload commands
279 void cli_image_upload(CtdlIPC *ipc, char *keyname)
285 /* Can we upload this image? */
286 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
288 scr_printf("%s\n", buf);
291 newprompt("Image file to be uploaded: ", flnm, 55);
292 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
294 scr_printf("%s\n", buf);
296 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
298 /* else upload succeeded */
303 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
305 void upload(CtdlIPC *ipc, int c)
306 { /* c = upload mode */
316 if ((room_flags & QR_UPLOAD) == 0) {
317 scr_printf("*** You cannot upload to this room.\n");
320 /* we don't need a filename when receiving batch y/z modem */
321 if ((c == 2) || (c == 3))
324 newprompt("Enter filename: ", flnm, 15);
326 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
327 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
328 || (flnm[a] == '?') || (flnm[a] == '*')
329 || (flnm[a] == ';') || (flnm[a] == '&'))
332 /* create a temporary directory... */
333 if (mkdir(tempdir, 0700) != 0) {
334 scr_printf("*** Could not create temporary directory %s: %s\n",
335 tempdir, strerror(errno));
338 /* now do the transfer ... in a separate process */
343 scr_printf("failed to change into %s Reason %s\nAborting now.\n",
352 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
353 fp = fopen(flnm, "w");
368 execlp("rx", "rx", flnm, NULL);
372 execlp("rb", "rb", NULL);
376 execlp("rz", "rz", NULL);
382 } while ((b != xfer_pid) && (b != (-1)));
386 scr_printf("\r*** Transfer unsuccessful.\n");
390 scr_printf("\r*** Transfer successful.\n");
391 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
392 lsfp = popen(buf, "r");
394 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
395 flnm[strlen(flnm) - 1] = 0; /* chop newline */
396 snprintf(buf, sizeof buf,
397 "Enter a short description of '%s':\n: ",
399 newprompt(buf, desc, 150);
400 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
401 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
402 scr_printf("%s\n", tbuf);
410 * validate a user (returns 0 for successful validation, nonzero if quitting)
412 int val_user(CtdlIPC *ipc, char *user, int do_validate)
420 int r; /* IPC response code */
423 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
427 extract_token(buf, resp, 0, '\n', sizeof buf);
428 remove_token(resp, 0, '\n');
431 scr_printf("User #%s - %s ", buf, cmd);
433 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
435 scr_printf("%s\n", buf);
437 scr_printf("%s\n", buf);
439 scr_printf("%s, ", buf);
441 scr_printf("%s ", buf);
443 scr_printf("%s\n", buf);
445 scr_printf("%s\n", buf);
449 scr_printf("%s\n", buf);
451 scr_printf("%s\n", buf);
452 } while (!IsEmptyStr(resp));
454 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
455 * but most of it limits it to axdefs[6].
456 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
458 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
460 scr_printf("%s\n%s\n", user, &cmd[4]);
462 if (resp) free(resp);
465 /* now set the access level */
467 sprintf(answer, "%d", ax);
468 strprompt("New access level (? for help, q to quit)",
470 if ((answer[0] >= '0') && (answer[0] <= '6')) {
472 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
474 scr_printf("%s\n\n", cmd);
477 if (tolower(answer[0]) == 'q') {
478 scr_printf("*** Aborted.\n\n");
481 if (answer[0] == '?') {
482 scr_printf("Available access levels:\n");
483 for (a=0; a<7; ++a) {
484 scr_printf("%d - %s\n",
494 void validate(CtdlIPC *ipc)
495 { /* validate new users */
499 int r; /* IPC response code */
502 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
506 scr_printf("%s\n", cmd);
508 extract_token(buf, cmd, 0, '|', sizeof buf);
509 if (val_user(ipc, buf, 1) != 0) finished = 1;
511 } while (finished == 0);
518 stty_ctdl(SB_RESTORE);
521 signal(SIGINT, SIG_DFL);
522 signal(SIGQUIT, SIG_DFL);
523 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
524 scr_printf("Could not open a shell: %s\n", strerror(errno));
529 } while ((a != b) && (a != (-1)));
534 * <.A>ide <F>ile <D>elete command
536 void deletefile(CtdlIPC *ipc)
541 newprompt("Filename: ", filename, 31);
542 if (IsEmptyStr(filename))
544 CtdlIPCDeleteFile(ipc, filename, buf);
545 scr_printf("%s\n", buf);
550 * <.A>ide <F>ile <M>ove command
552 void movefile(CtdlIPC *ipc)
555 char newroom[ROOMNAMELEN];
558 newprompt("Filename: ", filename, 63);
559 if (IsEmptyStr(filename))
561 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
562 CtdlIPCMoveFile(ipc, filename, newroom, buf);
563 scr_printf("%s\n", buf);
568 * list of users who have filled out a bio
570 void list_bio(CtdlIPC *ipc)
575 int r; /* IPC response code */
577 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
579 scr_printf("%s\n", buf);
582 while (resp && !IsEmptyStr(resp)) {
583 extract_token(buf, resp, 0, '\n', sizeof buf);
584 remove_token(resp, 0, '\n');
585 if ((pos + strlen(buf) + 5) > screenwidth) {
589 scr_printf("%s, ", buf);
590 pos = pos + strlen(buf) + 2;
592 scr_printf("%c%c \n\n", 8, 8);
593 if (resp) free(resp);
600 void read_bio(CtdlIPC *ipc)
605 int r; /* IPC response code */
608 newprompt("Read bio for who ('?' for list) : ", who, 25);
610 if (!strcmp(who, "?"))
612 } while (!strcmp(who, "?"));
614 r = CtdlIPCGetBio(ipc, who, &resp, buf);
616 scr_printf("%s\n", buf);
619 while (!IsEmptyStr(resp)) {
620 extract_token(buf, resp, 0, '\n', sizeof buf);
621 remove_token(resp, 0, '\n');
622 scr_printf("%s\n", buf);
624 if (resp) free(resp);