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 if (rc_alt_semantics) {
195 if (maxmsgnum == 0 && highest_msg_read == 0) {
198 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
199 maxmsgnum : highest_msg_read, buf);
201 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
202 maxmsgnum : highest_msg_read, buf);
203 /* r = CtdlIPCSetLastRead(ipc, maxmsgnum, buf); */
204 /* This is a quick-and-dirty fix to all msgs becoming new in Mail>.
205 * It will need to be rethought when messages.c is rewritten.
209 scr_printf("%s\n", buf);
213 * only make messages old in this room that have been read
215 void updatelsa(CtdlIPC *ipc)
218 int r; /* IPC response code */
220 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
222 scr_printf("%s\n", &buf[4]);
227 * client-based uploads (for users with their own clientware)
229 void cli_upload(CtdlIPC *ipc)
235 int r; /* IPC response code */
239 if ((room_flags & QR_UPLOAD) == 0) {
240 scr_printf("*** You cannot upload to this room.\n");
243 newprompt("File to be uploaded: ", flnm, 55);
244 fd = open(flnm, O_RDONLY);
246 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
249 scr_printf("Enter a description of this file:\n");
250 newprompt(": ", desc, 75);
252 /* Keep generating filenames in hope of finding a unique one */
255 /* basename of filename */
257 if (haschar(tbuf, '/'))
258 extract_token(tbuf, flnm,
259 num_tokens(tbuf, '/') - 1,
262 /* filename.1, filename.2, etc */
264 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
267 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
268 if (r / 100 == 5 || r < 0)
269 scr_printf("%s\n", buf);
274 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
279 * Function used for various image upload commands
281 void cli_image_upload(CtdlIPC *ipc, char *keyname)
287 /* Can we upload this image? */
288 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
290 scr_printf("%s\n", buf);
293 newprompt("Image file to be uploaded: ", flnm, 55);
294 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
296 scr_printf("%s\n", buf);
298 scr_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
300 /* else upload succeeded */
305 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
307 void upload(CtdlIPC *ipc, int c)
308 { /* c = upload mode */
319 if ((room_flags & QR_UPLOAD) == 0) {
320 scr_printf("*** You cannot upload to this room.\n");
323 /* we don't need a filename when receiving batch y/z modem */
324 if ((c == 2) || (c == 3))
327 newprompt("Enter filename: ", flnm, 15);
329 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
330 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
331 || (flnm[a] == '?') || (flnm[a] == '*')
332 || (flnm[a] == ';') || (flnm[a] == '&'))
335 /* create a temporary directory... */
336 if (mkdir(tempdir, 0700) != 0) {
337 scr_printf("*** Could not create temporary directory %s: %s\n",
338 tempdir, strerror(errno));
341 /* now do the transfer ... in a separate process */
348 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
349 fp = fopen(flnm, "w");
364 execlp("rx", "rx", flnm, NULL);
368 execlp("rb", "rb", NULL);
372 execlp("rz", "rz", NULL);
378 } while ((b != xfer_pid) && (b != (-1)));
382 scr_printf("\r*** Transfer unsuccessful.\n");
386 scr_printf("\r*** Transfer successful.\n");
387 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
388 lsfp = popen(buf, "r");
390 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
391 flnm[strlen(flnm) - 1] = 0; /* chop newline */
392 snprintf(buf, sizeof buf,
393 "Enter a short description of '%s':\n: ",
395 newprompt(buf, desc, 150);
396 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
397 r = CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
398 scr_printf("%s\n", tbuf);
406 * validate a user (returns 0 for successful validation, nonzero if quitting)
408 int val_user(CtdlIPC *ipc, char *user, int do_validate)
416 int r; /* IPC response code */
419 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
423 extract_token(buf, resp, 0, '\n', sizeof buf);
424 remove_token(resp, 0, '\n');
427 scr_printf("User #%s - %s ", buf, cmd);
429 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
431 scr_printf("%s\n", buf);
433 scr_printf("%s\n", buf);
435 scr_printf("%s, ", buf);
437 scr_printf("%s ", buf);
439 scr_printf("%s\n", buf);
441 scr_printf("%s\n", buf);
445 scr_printf("%s\n", buf);
447 scr_printf("%s\n", buf);
448 } while (!IsEmptyStr(resp));
450 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
451 * but most of it limits it to axdefs[6].
452 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
454 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
456 scr_printf("%s\n%s\n", user, &cmd[4]);
458 if (resp) free(resp);
461 /* now set the access level */
463 sprintf(answer, "%d", ax);
464 strprompt("New access level (? for help, q to quit)",
466 if ((answer[0] >= '0') && (answer[0] <= '6')) {
468 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
470 scr_printf("%s\n\n", cmd);
473 if (tolower(answer[0]) == 'q') {
474 scr_printf("*** Aborted.\n\n");
477 if (answer[0] == '?') {
478 scr_printf("Available access levels:\n");
479 for (a=0; a<7; ++a) {
480 scr_printf("%d - %s\n",
490 void validate(CtdlIPC *ipc)
491 { /* validate new users */
495 int r; /* IPC response code */
498 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
502 scr_printf("%s\n", cmd);
504 extract_token(buf, cmd, 0, '|', sizeof buf);
505 if (val_user(ipc, buf, 1) != 0) finished = 1;
507 } while (finished == 0);
514 stty_ctdl(SB_RESTORE);
517 signal(SIGINT, SIG_DFL);
518 signal(SIGQUIT, SIG_DFL);
519 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
520 scr_printf("Could not open a shell: %s\n", strerror(errno));
525 } while ((a != b) && (a != (-1)));
530 * <.A>ide <F>ile <D>elete command
532 void deletefile(CtdlIPC *ipc)
537 newprompt("Filename: ", filename, 31);
538 if (IsEmptyStr(filename))
540 CtdlIPCDeleteFile(ipc, filename, buf);
541 scr_printf("%s\n", buf);
546 * <.A>ide <F>ile <M>ove command
548 void movefile(CtdlIPC *ipc)
551 char newroom[ROOMNAMELEN];
554 newprompt("Filename: ", filename, 63);
555 if (IsEmptyStr(filename))
557 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
558 CtdlIPCMoveFile(ipc, filename, newroom, buf);
559 scr_printf("%s\n", buf);
564 * list of users who have filled out a bio
566 void list_bio(CtdlIPC *ipc)
571 int r; /* IPC response code */
573 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
575 scr_printf("%s\n", buf);
578 while (resp && !IsEmptyStr(resp)) {
579 extract_token(buf, resp, 0, '\n', sizeof buf);
580 remove_token(resp, 0, '\n');
581 if ((pos + strlen(buf) + 5) > screenwidth) {
585 scr_printf("%s, ", buf);
586 pos = pos + strlen(buf) + 2;
588 scr_printf("%c%c \n\n", 8, 8);
589 if (resp) free(resp);
596 void read_bio(CtdlIPC *ipc)
601 int r; /* IPC response code */
604 newprompt("Read bio for who ('?' for list) : ", who, 25);
606 if (!strcmp(who, "?"))
608 } while (!strcmp(who, "?"));
610 r = CtdlIPCGetBio(ipc, who, &resp, buf);
612 scr_printf("%s\n", buf);
615 while (!IsEmptyStr(resp)) {
616 extract_token(buf, resp, 0, '\n', sizeof buf);
617 remove_token(resp, 0, '\n');
618 scr_printf("%s\n", buf);
620 if (resp) free(resp);