3 * More client-side support functions.
4 * Unlike routines.c, some of these DO use global variables.
10 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
33 #include <libcitadel.h>
36 #include "citadel_ipc.h"
37 #include "citadel_decls.h"
38 #include "routines2.h"
47 /* work around solaris include files */
53 extern char tempdir[];
54 extern char *axdefs[8];
55 extern long highest_msg_read;
56 extern long maxmsgnum;
57 extern unsigned room_flags;
58 extern int screenwidth;
62 int eopen(char *name, int mode)
65 ret = open(name, mode);
67 err_printf("Cannot open file '%s', mode=%d, errno=%d\n",
76 int room_prompt(unsigned int qrflags)
77 { /* return proper room prompt character */
80 if (qrflags & QR_DIRECTORY)
82 if ((a == ']') && (qrflags & QR_NETWORK))
84 if ((a == '>') && (qrflags & QR_NETWORK))
89 void entregis(CtdlIPC *ipc)
90 { /* register with name and address */
106 int r; /* IPC response code */
111 strcpy(tmpstate, "");
113 strcpy(tmpphone, "");
114 strcpy(tmpemail, "");
115 strcpy(tmpcountry, "");
117 r = CtdlIPCGetUserRegistration(ipc, NULL, ®, buf);
121 while (reg && !IsEmptyStr(reg)) {
123 extract_token(buf, reg, 0, '\n', sizeof buf);
124 remove_token(reg, 0, '\n');
127 safestrncpy(tmpname, buf, sizeof tmpname);
129 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
131 safestrncpy(tmpcity, buf, sizeof tmpcity);
133 safestrncpy(tmpstate, buf, sizeof tmpstate);
135 safestrncpy(tmpzip, buf, sizeof tmpzip);
137 safestrncpy(tmpphone, buf, sizeof tmpphone);
139 safestrncpy(tmpemail, buf, sizeof tmpemail);
141 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
145 strprompt("REAL name", tmpname, 29);
146 strprompt("Address", tmpaddr, 24);
147 strprompt("City/town", tmpcity, 14);
148 strprompt("State/province", tmpstate, 2);
149 strprompt("ZIP/Postal Code", tmpzip, 10);
150 strprompt("Country", tmpcountry, 31);
151 strprompt("Telephone number", tmpphone, 14);
155 safestrncpy(holdemail, tmpemail, sizeof holdemail);
156 strprompt("Email address", tmpemail, 31);
157 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
159 extract_token(diruser, buf, 0, '@', sizeof diruser);
160 extract_token(dirnode, buf, 1, '@', sizeof dirnode);
163 if ((strcasecmp(diruser, fullname))
164 || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
166 "\nYou can't use %s as your address.\n",
169 "It is already in use by %s @ %s.\n",
172 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
177 /* now send the registration info back to the server */
178 reg = (char *)realloc(reg, SIZ);
180 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
181 tmpname, tmpaddr, tmpcity, tmpstate,
182 tmpzip, tmpphone, tmpemail, tmpcountry);
183 r = CtdlIPCSetRegistration(ipc, reg, buf);
185 scr_printf("%s\n", buf);
191 void updatels(CtdlIPC *ipc)
192 { /* make all messages old in current room */
194 int r; /* IPC response code */
196 if (rc_alt_semantics) {
197 if (maxmsgnum == 0 && highest_msg_read == 0) {
200 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
201 maxmsgnum : highest_msg_read, buf);
203 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
204 maxmsgnum : highest_msg_read, buf);
205 /* r = CtdlIPCSetLastRead(ipc, maxmsgnum, buf); */
206 /* This is a quick-and-dirty fix to all msgs becoming new in Mail>.
207 * It will need to be rethought when messages.c is rewritten.
211 scr_printf("%s\n", buf);
215 * only make messages old in this room that have been read
217 void updatelsa(CtdlIPC *ipc)
220 int r; /* IPC response code */
222 r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
224 scr_printf("%s\n", &buf[4]);
229 * client-based uploads (for users with their own clientware)
231 void cli_upload(CtdlIPC *ipc)
237 int r; /* IPC response code */
241 if ((room_flags & QR_UPLOAD) == 0) {
242 scr_printf("*** You cannot upload to this room.\n");
245 newprompt("File to be uploaded: ", flnm, 55);
246 fd = open(flnm, O_RDONLY);
248 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
251 scr_printf("Enter a description of this file:\n");
252 newprompt(": ", desc, 75);
254 /* Keep generating filenames in hope of finding a unique one */
257 /* basename of filename */
259 if (haschar(tbuf, '/'))
260 extract_token(tbuf, flnm,
261 num_tokens(tbuf, '/') - 1,
264 /* filename.1, filename.2, etc */
266 sprintf(&tbuf[strlen(tbuf)], ".%d", a);
269 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
270 if (r / 100 == 5 || r < 0)
271 scr_printf("%s\n", buf);
276 if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
281 * Function used for various image upload commands
283 void cli_image_upload(CtdlIPC *ipc, char *keyname)
289 /* Can we upload this image? */
290 r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
292 err_printf("%s\n", buf);
295 newprompt("Image file to be uploaded: ", flnm, 55);
296 r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
298 err_printf("%s\n", buf);
300 err_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
302 /* else upload succeeded */
307 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
309 void upload(CtdlIPC *ipc, int c)
310 { /* c = upload mode */
321 if ((room_flags & QR_UPLOAD) == 0) {
322 scr_printf("*** You cannot upload to this room.\n");
325 /* we don't need a filename when receiving batch y/z modem */
326 if ((c == 2) || (c == 3))
329 newprompt("Enter filename: ", flnm, 15);
331 for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
332 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
333 || (flnm[a] == '?') || (flnm[a] == '*')
334 || (flnm[a] == ';') || (flnm[a] == '&'))
337 /* create a temporary directory... */
338 if (mkdir(tempdir, 0700) != 0) {
339 scr_printf("*** Could not create temporary directory %s: %s\n",
340 tempdir, strerror(errno));
343 /* now do the transfer ... in a separate process */
350 scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
351 fp = fopen(flnm, "w");
367 execlp("rx", "rx", flnm, NULL);
372 execlp("rb", "rb", NULL);
377 execlp("rz", "rz", NULL);
383 } while ((b != xfer_pid) && (b != (-1)));
388 scr_printf("\r*** Transfer unsuccessful.\n");
392 scr_printf("\r*** Transfer successful.\n");
393 snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
394 lsfp = popen(buf, "r");
396 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
397 flnm[strlen(flnm) - 1] = 0; /* chop newline */
398 snprintf(buf, sizeof buf,
399 "Enter a short description of '%s':\n: ",
401 newprompt(buf, desc, 150);
402 snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
403 r = CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
404 scr_printf("%s\n", tbuf);
412 * validate a user (returns 0 for successful validation, nonzero if quitting)
414 int val_user(CtdlIPC *ipc, char *user, int do_validate)
422 int r; /* IPC response code */
425 r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
429 extract_token(buf, resp, 0, '\n', sizeof buf);
430 remove_token(resp, 0, '\n');
433 scr_printf("User #%s - %s ", buf, cmd);
435 scr_printf("PW: %s\n", (IsEmptyStr(buf) ? "<NOT SET>" : "<SET>") );
437 scr_printf("%s\n", buf);
439 scr_printf("%s\n", buf);
441 scr_printf("%s, ", buf);
443 scr_printf("%s ", buf);
445 scr_printf("%s\n", buf);
447 scr_printf("%s\n", buf);
451 scr_printf("%s\n", buf);
453 scr_printf("%s\n", buf);
454 } while (!IsEmptyStr(resp));
456 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
457 * but most of it limits it to axdefs[6].
458 * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
460 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
462 scr_printf("%s\n%s\n", user, &cmd[4]);
464 if (resp) free(resp);
467 /* now set the access level */
469 sprintf(answer, "%d", ax);
470 strprompt("New access level (? for help, q to quit)",
472 if ((answer[0] >= '0') && (answer[0] <= '6')) {
474 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
476 scr_printf("%s\n\n", cmd);
479 if (tolower(answer[0]) == 'q') {
480 scr_printf("*** Aborted.\n\n");
483 if (answer[0] == '?') {
484 scr_printf("Available access levels:\n");
485 for (a=0; a<7; ++a) {
486 scr_printf("%d - %s\n",
496 void validate(CtdlIPC *ipc)
497 { /* validate new users */
501 int r; /* IPC response code */
504 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
508 scr_printf("%s\n", cmd);
510 extract_token(buf, cmd, 0, '|', sizeof buf);
511 if (val_user(ipc, buf, 1) != 0) finished = 1;
513 } while (finished == 0);
521 stty_ctdl(SB_RESTORE);
524 signal(SIGINT, SIG_DFL);
525 signal(SIGQUIT, SIG_DFL);
526 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
527 err_printf("Could not open a shell: %s\n", strerror(errno));
532 } while ((a != b) && (a != (-1)));
538 * <.A>ide <F>ile <D>elete command
540 void deletefile(CtdlIPC *ipc)
545 newprompt("Filename: ", filename, 31);
546 if (IsEmptyStr(filename))
548 CtdlIPCDeleteFile(ipc, filename, buf);
549 err_printf("%s\n", buf);
554 * <.A>ide <F>ile <M>ove command
556 void movefile(CtdlIPC *ipc)
559 char newroom[ROOMNAMELEN];
562 newprompt("Filename: ", filename, 63);
563 if (IsEmptyStr(filename))
565 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
566 CtdlIPCMoveFile(ipc, filename, newroom, buf);
567 err_printf("%s\n", buf);
572 * list of users who have filled out a bio
574 void list_bio(CtdlIPC *ipc)
579 int r; /* IPC response code */
581 r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
583 pprintf("%s\n", buf);
586 while (resp && !IsEmptyStr(resp)) {
587 extract_token(buf, resp, 0, '\n', sizeof buf);
588 remove_token(resp, 0, '\n');
589 if ((pos + strlen(buf) + 5) > screenwidth) {
593 pprintf("%s, ", buf);
594 pos = pos + strlen(buf) + 2;
596 pprintf("%c%c \n\n", 8, 8);
597 if (resp) free(resp);
604 void read_bio(CtdlIPC *ipc)
609 int r; /* IPC response code */
612 newprompt("Read bio for who ('?' for list) : ", who, 25);
614 if (!strcmp(who, "?"))
616 } while (!strcmp(who, "?"));
618 r = CtdlIPCGetBio(ipc, who, &resp, buf);
620 pprintf("%s\n", buf);
623 while (!IsEmptyStr(resp)) {
624 extract_token(buf, resp, 0, '\n', sizeof buf);
625 remove_token(resp, 0, '\n');
626 pprintf("%s\n", buf);
628 if (resp) free(resp);