2 * More client-side support functions.
3 * Unlike routines.c, some of these DO use global variables.
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
21 # include <sys/time.h>
31 #include <libcitadel.h>
34 #include "citadel_ipc.h"
35 #include "citadel_decls.h"
36 #include "routines2.h"
45 /* work around solaris include files */
51 extern char tempdir[];
52 extern char *axdefs[8];
53 extern long highest_msg_read;
54 extern long maxmsgnum;
55 extern unsigned room_flags;
56 extern int screenwidth;
60 int eopen(char *name, int mode)
63 ret = open(name, mode);
65 scr_printf("Cannot open file '%s', mode=%d, errno=%d\n",
74 int room_prompt(unsigned int qrflags)
75 { /* return proper room prompt character */
78 if (qrflags & QR_DIRECTORY)
80 if ((a == ']') && (qrflags & QR_NETWORK))
82 if ((a == '>') && (qrflags & QR_NETWORK))
87 void entregis(CtdlIPC *ipc)
88 { /* register with name and address */
104 int r; /* IPC response code */
109 strcpy(tmpstate, "");
111 strcpy(tmpphone, "");
112 strcpy(tmpemail, "");
113 strcpy(tmpcountry, "");
115 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
119 while (reg && !IsEmptyStr(reg)) {
121 extract_token(buf, reg, 0, '\n', sizeof buf);
122 remove_token(reg, 0, '\n');
125 safestrncpy(tmpname, buf, sizeof tmpname);
127 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
129 safestrncpy(tmpcity, buf, sizeof tmpcity);
131 safestrncpy(tmpstate, buf, sizeof tmpstate);
133 safestrncpy(tmpzip, buf, sizeof tmpzip);
135 safestrncpy(tmpphone, buf, sizeof tmpphone);
137 safestrncpy(tmpemail, buf, sizeof tmpemail);
139 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
143 strprompt("REAL name", tmpname, 29);
144 strprompt("Address", tmpaddr, 24);
145 strprompt("City/town", tmpcity, 14);
146 strprompt("State/province", tmpstate, 2);
147 strprompt("ZIP/Postal Code", tmpzip, 10);
148 strprompt("Country", tmpcountry, 31);
149 strprompt("Telephone number", tmpphone, 14);
153 safestrncpy(holdemail, tmpemail, sizeof holdemail);
154 strprompt("Email address", tmpemail, 31);
155 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
157 extract_token(diruser, buf, 0, '@', sizeof diruser);
158 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
161 if ((strcasecmp(diruser, fullname))
162 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
164 "\nYou can't use %s as your address.\n",
167 "It is already in use by %s @ %s.\n",
170 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
175 /* now send the registration info back to the server */
176 reg = (char *)realloc(reg, SIZ);
178 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
179 tmpname, tmpaddr, tmpcity, tmpstate,
180 tmpzip, tmpphone, tmpemail, tmpcountry);
181 r = CtdlIPCSetRegistration(ipc, reg, buf);
183 scr_printf("%s\n", buf);
189 void updatels(CtdlIPC *ipc)
190 { /* make all messages old in current room */
192 int r; /* IPC response code */
194 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ? maxmsgnum : highest_msg_read, buf);
197 scr_printf("%s\n", buf);
201 * only make messages old in this room that have been read
203 void updatelsa(CtdlIPC *ipc)
206 int r; /* IPC response code */
208 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
210 scr_printf("%s\n", &buf[4]);
215 * client-based uploads (for users with their own clientware)
217 void cli_upload(CtdlIPC *ipc)
223 int r; /* IPC response code */
227 if ((room_flags & QR_UPLOAD) == 0) {
228 scr_printf("*** You cannot upload to this room.\n");
231 newprompt("File to be uploaded: ", flnm, 55);
232 fd = open(flnm, O_RDONLY);
234 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
237 scr_printf("Enter a description of this file:\n");
238 newprompt(": ", desc, 75);
240 /* Keep generating filenames in hope of finding a unique one */
243 /* basename of filename */
245 if (haschar(tbuf, '/'))
246 extract_token(tbuf, flnm,
247 num_tokens(tbuf, '/') - 1,
250 /* filename.1, filename.2, etc */
252 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
255 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
256 if (r / 100 == 5 || r < 0)
257 scr_printf("%s\n", buf);
262 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
267 * Function used for various image upload commands
269 void cli_image_upload(CtdlIPC *ipc, char *keyname)
275 /* Can we upload this image? */
276 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
278 scr_printf("%s\n", buf);
281 newprompt("Image file to be uploaded: ", flnm, 55);
282 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
284 scr_printf("%s\n", buf);
286 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
288 /* else upload succeeded */
293 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
295 void upload(CtdlIPC *ipc, int c)
296 { /* c = upload mode */
306 if ((room_flags & QR_UPLOAD) == 0) {
307 scr_printf("*** You cannot upload to this room.\n");
310 /* we don't need a filename when receiving batch y/z modem */
311 if ((c == 2) || (c == 3))
314 newprompt("Enter filename: ", flnm, 15);
316 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
317 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
318 || (flnm[a] == '?') || (flnm[a] == '*')
319 || (flnm[a] == ';') || (flnm[a] == '&'))
322 /* create a temporary directory... */
323 if (mkdir(tempdir, 0700) != 0) {
324 scr_printf("*** Could not create temporary directory %s: %s\n",
325 tempdir, strerror(errno));
328 /* now do the transfer ... in a separate process */
333 scr_printf("failed to change into %s Reason %s\nAborting now.\n",
342 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
343 fp = fopen(flnm, "w");
358 execlp("rx", "rx", flnm, NULL);
362 execlp("rb", "rb", NULL);
366 execlp("rz", "rz", NULL);
372 } while ((b != xfer_pid) && (b != (-1)));
376 scr_printf("\r*** Transfer unsuccessful.\n");
380 scr_printf("\r*** Transfer successful.\n");
381 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
382 lsfp = popen(buf, "r");
384 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
385 flnm[strlen(flnm) - 1] = 0; /* chop newline */
386 snprintf(buf, sizeof buf,
387 "Enter a short description of '%s':\n: ",
389 newprompt(buf, desc, 150);
390 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
391 CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
392 scr_printf("%s\n", tbuf);
400 * validate a user (returns 0 for successful validation, nonzero if quitting)
402 int val_user(CtdlIPC *ipc, char *user, int do_validate)
410 int r; /* IPC response code */
413 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
417 extract_token(buf, resp, 0, '\n', sizeof buf);
418 remove_token(resp, 0, '\n');
421 scr_printf("User #%s - %s ", buf, cmd);
423 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
425 scr_printf("%s\n", buf);
427 scr_printf("%s\n", buf);
429 scr_printf("%s, ", buf);
431 scr_printf("%s ", buf);
433 scr_printf("%s\n", buf);
435 scr_printf("%s\n", buf);
439 scr_printf("%s\n", buf);
441 scr_printf("%s\n", buf);
442 } while (!IsEmptyStr(resp));
444 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
445 * but most of it limits it to axdefs[6].
446 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
448 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
450 scr_printf("%s\n%s\n", user, &cmd[4]);
452 if (resp) free(resp);
455 /* now set the access level */
457 sprintf(answer, "%d", ax);
458 strprompt("New access level (? for help, q to quit)",
460 if ((answer[0] >= '0') && (answer[0] <= '6')) {
462 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
464 scr_printf("%s\n\n", cmd);
467 if (tolower(answer[0]) == 'q') {
468 scr_printf("*** Aborted.\n\n");
471 if (answer[0] == '?') {
472 scr_printf("Available access levels:\n");
473 for (a=0; a<7; ++a) {
474 scr_printf("%d - %s\n",
484 void validate(CtdlIPC *ipc)
485 { /* validate new users */
489 int r; /* IPC response code */
492 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
496 scr_printf("%s\n", cmd);
498 extract_token(buf, cmd, 0, '|', sizeof buf);
499 if (val_user(ipc, buf, 1) != 0) finished = 1;
501 } while (finished == 0);
508 stty_ctdl(SB_RESTORE);
511 signal(SIGINT, SIG_DFL);
512 signal(SIGQUIT, SIG_DFL);
513 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
514 scr_printf("Could not open a shell: %s\n", strerror(errno));
519 } while ((a != b) && (a != (-1)));
524 * <.A>ide <F>ile <D>elete command
526 void deletefile(CtdlIPC *ipc)
531 newprompt("Filename: ", filename, 31);
532 if (IsEmptyStr(filename))
534 CtdlIPCDeleteFile(ipc, filename, buf);
535 scr_printf("%s\n", buf);
540 * <.A>ide <F>ile <M>ove command
542 void movefile(CtdlIPC *ipc)
545 char newroom[ROOMNAMELEN];
548 newprompt("Filename: ", filename, 63);
549 if (IsEmptyStr(filename))
551 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
552 CtdlIPCMoveFile(ipc, filename, newroom, buf);
553 scr_printf("%s\n", buf);
558 * list of users who have filled out a bio
560 void list_bio(CtdlIPC *ipc)
565 int r; /* IPC response code */
567 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
569 scr_printf("%s\n", buf);
572 while (resp && !IsEmptyStr(resp)) {
573 extract_token(buf, resp, 0, '\n', sizeof buf);
574 remove_token(resp, 0, '\n');
575 if ((pos + strlen(buf) + 5) > screenwidth) {
579 scr_printf("%s, ", buf);
580 pos = pos + strlen(buf) + 2;
582 scr_printf("%c%c \n\n", 8, 8);
583 if (resp) free(resp);
590 void read_bio(CtdlIPC *ipc)
595 int r; /* IPC response code */
598 newprompt("Read bio for who ('?' for list) : ", who, 25);
600 if (!strcmp(who, "?"))
602 } while (!strcmp(who, "?"));
604 r = CtdlIPCGetBio(ipc, who, &resp, buf);
606 scr_printf("%s\n", buf);
609 while (!IsEmptyStr(resp)) {
610 extract_token(buf, resp, 0, '\n', sizeof buf);
611 remove_token(resp, 0, '\n');
612 scr_printf("%s\n", buf);
614 if (resp) free(resp);