3 * More client-side support functions.
4 * Unlike routines.c, some of these DO use global variables.
11 #include <sys/types.h>
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
24 # include <sys/time.h>
36 #include "citadel_decls.h"
37 #include "routines2.h"
46 void interr(int errnum);
47 void strprompt(char *prompt, char *str, int len);
48 void newprompt(char *prompt, char *str, int len);
49 void sttybbs(int cmd);
51 void serv_write(char *buf, int nbytes);
52 int haschar(char *st, int ch);
53 void progress(long int curr, long int cmax);
57 extern char tempdir[];
58 extern char *axdefs[7];
59 extern long highest_msg_read;
60 extern long maxmsgnum;
61 extern unsigned room_flags;
62 extern int screenwidth;
65 int eopen(char *name, int mode)
68 ret = open(name, mode);
70 fprintf(stderr, "Cannot open file '%s', mode=%d, errno=%d\n",
78 int room_prompt(int qrflags)
79 { /* return proper room prompt character */
82 if (qrflags & QR_DIRECTORY)
84 if ((a == ']') && (qrflags & QR_NETWORK))
86 if ((a == '>') && (qrflags & QR_NETWORK))
92 { /* register with name and address */
102 char tmpcountry[SIZ];
108 strcpy(tmpstate, "");
110 strcpy(tmpphone, "");
111 strcpy(tmpemail, "");
112 strcpy(tmpcountry, "");
114 serv_puts("GREG _SELF_");
118 while (serv_gets(buf), strcmp(buf, "000")) {
120 strcpy(tmpname, buf);
122 strcpy(tmpaddr, buf);
124 strcpy(tmpcity, buf);
126 strcpy(tmpstate, buf);
130 strcpy(tmpphone, buf);
132 strcpy(tmpemail, buf);
134 strcpy(tmpcountry, buf);
138 strprompt("REAL name", tmpname, 29);
139 strprompt("Address", tmpaddr, 24);
140 strprompt("City/town", tmpcity, 14);
141 strprompt("State/province", tmpstate, 2);
142 strprompt("ZIP/Postal Code", tmpzip, 10);
143 strprompt("Country", tmpcountry, 31);
144 strprompt("Telephone number", tmpphone, 14);
145 strprompt("Email address", tmpemail, 31);
147 /* now send the registration info back to the server */
151 printf("%s\n", &buf[4]);
161 serv_puts(tmpcountry);
167 { /* make all messages old in current room */
169 serv_puts("SLRP HIGHEST");
172 printf("%s\n", &buf[4]);
176 * only make messages old in this room that have been read
181 sprintf(buf, "SLRP %ld", highest_msg_read);
185 printf("%s\n", &buf[4]);
190 * This routine completes a client upload
192 void do_upload(int fd)
196 long transmitted_bytes, total_bytes;
200 /* learn the size of the file */
201 total_bytes = lseek(fd, 0L, 2);
204 transmitted_bytes = 0L;
205 progress(transmitted_bytes, total_bytes);
207 bytes_to_send = read(fd, tbuf, 4096);
208 if (bytes_to_send > 0) {
209 sprintf(buf, "WRIT %d", bytes_to_send);
213 bytes_expected = atoi(&buf[4]);
214 serv_write(tbuf, bytes_expected);
216 printf("%s\n", &buf[4]);
219 transmitted_bytes = transmitted_bytes + (long) bytes_to_send;
220 progress(transmitted_bytes, total_bytes);
221 } while (bytes_to_send > 0);
223 /* close the upload file, locally and at the server */
227 printf("%s\n", &buf[4]);
232 * client-based uploads (for users with their own clientware)
234 void cli_upload(void)
243 if ((room_flags & QR_UPLOAD) == 0) {
244 printf("*** You cannot upload to this room.\n");
247 newprompt("File to be uploaded: ", flnm, 55);
248 fd = open(flnm, O_RDONLY);
250 printf("Cannot open '%s': %s\n", flnm, strerror(errno));
253 printf("Enter a description of this file:\n");
254 newprompt(": ", desc, 75);
256 /* keep generating filenames in hope of finding a unique one */
260 return; /* fail if tried 10 times */
262 while ((strlen(buf) > 0) && (haschar(buf, '/')))
263 strcpy(buf, &buf[1]);
265 sprintf(&buf[strlen(buf)], "%d", a);
266 sprintf(tbuf, "UOPN %s|%s", buf, desc);
270 printf("%s\n", &buf[4]);
272 } while (buf[0] != '2');
274 /* at this point we have an open upload file at the server */
280 * Function used for various image upload commands
282 void cli_image_upload(char *keyname)
288 sprintf(buf, "UIMG 0|%s", keyname);
292 printf("%s\n", &buf[4]);
295 newprompt("Image file to be uploaded: ", flnm, 55);
296 fd = open(flnm, O_RDONLY);
298 printf("Cannot open '%s': %s\n", flnm, strerror(errno));
301 sprintf(buf, "UIMG 1|%s", keyname);
305 printf("%s\n", &buf[4]);
313 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
316 { /* c = upload mode */
326 if ((room_flags & QR_UPLOAD) == 0) {
327 printf("*** You cannot upload to this room.\n");
330 /* we don't need a filename when receiving batch y/z modem */
331 if ((c == 2) || (c == 3))
334 newprompt("Enter filename: ", flnm, 15);
336 for (a = 0; a < strlen(flnm); ++a)
337 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
338 || (flnm[a] == '?') || (flnm[a] == '*')
339 || (flnm[a] == ';') || (flnm[a] == '&'))
342 newprompt("Enter a short description of the file:\n: ", desc, 150);
344 /* create a temporary directory... */
345 if (mkdir(tempdir, 0700) != 0) {
346 printf("*** Could not create temporary directory %s: %s\n",
347 tempdir, strerror(errno));
350 /* now do the transfer ... in a separate process */
357 printf("Receiving %s - press Ctrl-D to end.\n", flnm);
358 fp = fopen(flnm, "w");
374 execlp("rx", "rx", flnm, NULL);
378 execlp("rb", "rb", NULL);
382 execlp("rz", "rz", NULL);
388 } while ((b != xfer_pid) && (b != (-1)));
392 printf("\r*** Transfer unsuccessful.\n");
396 printf("\r*** Transfer successful. Sending file(s) to server...\n");
397 sprintf(buf, "cd %s; ls", tempdir);
398 lsfp = popen(buf, "r");
400 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
401 flnm[strlen(flnm) - 1] = 0;
402 sprintf(buf, "%s/%s", tempdir, flnm);
403 fd = open(buf, O_RDONLY);
407 sprintf(buf, "UOPN %s|%s", flnm, desc);
409 sprintf(&buf[strlen(buf)],
414 } while ((buf[0] != '2') && (a < 100));
417 a = read(fd, tbuf, 4096);
419 sprintf(buf, "WRIT %d", a);
429 printf("%s\n", &buf[4]);
440 void val_user(char *user, int do_validate)
447 sprintf(cmd, "GREG %s", user);
456 printf("User #%s - %s ", buf, &cmd[4]);
458 printf("PW: %s\n", buf);
477 } while (strcmp(buf, "000"));
478 printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
480 printf("%-30s\n%s\n", user, &cmd[4]);
484 /* now set the access level */
485 ax = intprompt("Access level", ax, 0, 6);
486 sprintf(cmd, "VALI %s|%d", user, ax);
490 printf("%s\n", &cmd[4]);
497 { /* validate new users */
508 printf("%s\n", &cmd[4]);
510 extract(buf, cmd, 0);
511 val_user(&buf[4], 1);
513 } while (finished == 0);
522 signal(SIGINT, SIG_DFL);
523 signal(SIGQUIT, SIG_DFL);
524 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
525 printf("Could not open a shell: %s\n", strerror(errno));
530 } while ((a != b) && (a != (-1)));
535 * <.A>ide <F>ile <D>elete command
537 void deletefile(void)
542 newprompt("Filename: ", filename, 31);
543 if (strlen(filename) == 0)
545 sprintf(cmd, "DELF %s", filename);
548 printf("%s\n", &cmd[4]);
552 * <.A>ide <F>ile <S>end command
554 void netsendfile(void)
556 char filename[32], destsys[20], cmd[SIZ];
558 newprompt("Filename: ", filename, 31);
559 if (strlen(filename) == 0)
561 newprompt("System to send to: ", destsys, 19);
562 sprintf(cmd, "NETF %s|%s", filename, destsys);
565 printf("%s\n", &cmd[4]);
570 * <.A>ide <F>ile <M>ove command
575 char newroom[ROOMNAMELEN];
578 newprompt("Filename: ", filename, 63);
579 if (strlen(filename) == 0)
581 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
583 sprintf(cmd, "MOVF %s|%s", filename, newroom);
586 printf("%s\n", &cmd[4]);
591 * list of users who have filled out a bio
601 pprintf("%s\n", &buf[4]);
604 while (serv_gets(buf), strcmp(buf, "000")) {
605 if ((pos + strlen(buf) + 5) > screenwidth) {
609 pprintf("%s, ", buf);
610 pos = pos + strlen(buf) + 2;
612 pprintf("%c%c \n\n", 8, 8);
625 newprompt("Read bio for who ('?' for list) : ", who, 25);
627 if (!strcmp(who, "?"))
629 } while (!strcmp(who, "?"));
630 sprintf(buf, "RBIO %s", who);
634 pprintf("%s\n", &buf[4]);
637 while (serv_gets(buf), strcmp(buf, "000")) {
638 pprintf("%s\n", buf);
644 * General system configuration command
646 void do_system_configuration(void)
651 int expire_value = 0;
655 /* Clear out the config buffers */
656 memset(&sc[0][0], 0, sizeof(sc));
658 /* Fetch the current config */
659 serv_puts("CONF get");
663 while (serv_gets(buf), strcmp(buf, "000")) {
665 strcpy(&sc[a][0], buf);
669 /* Fetch the expire policy (this will silently fail on old servers,
670 * resulting in "default" policy)
672 serv_puts("GPEX site");
675 expire_mode = extract_int(&buf[4], 0);
676 expire_value = extract_int(&buf[4], 1);
680 /* Identification parameters */
682 strprompt("Node name", &sc[0][0], 15);
683 strprompt("Fully qualified domain name", &sc[1][0], 63);
684 strprompt("Human readable node name", &sc[2][0], 20);
685 strprompt("Modem dialup number", &sc[3][0], 15);
686 strprompt("Geographic location of this system", &sc[12][0], 31);
687 strprompt("Name of system administrator", &sc[13][0], 25);
688 strprompt("Paginator prompt", &sc[10][0], 79);
689 /* strprompt("Default moderation filter for new users", &sc[25][0], 4); */
691 /* Security parameters */
693 sprintf(&sc[7][0], "%d", (boolprompt(
694 "Require registration for new users",
696 strprompt("Initial access level for new users", &sc[6][0], 1);
697 strprompt("Access level required to create rooms", &sc[19][0], 1);
698 sprintf(&sc[4][0], "%d", (boolprompt(
699 "Automatically give room aide privs to a user who creates a private room",
702 sprintf(&sc[8][0], "%d", (boolprompt(
703 "Automatically move problem user messages to twit room",
706 strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
707 sprintf(&sc[11][0], "%d", (boolprompt(
708 "Restrict Internet mail to only those with that privilege",
710 sprintf(&sc[26][0], "%d", (boolprompt(
711 "Allow Aides to Zap (forget) rooms",
714 if (strlen(&sc[18][0]) > 0) logpages = 1;
716 logpages = boolprompt("Log all pages", logpages);
718 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
727 strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
728 strprompt("Maximum concurrent sessions", &sc[14][0], 4);
729 strprompt("Maximum message length", &sc[20][0], 20);
730 strprompt("Minimum number of worker threads", &sc[21][0], 3);
731 strprompt("Maximum number of worker threads", &sc[22][0], 3);
732 strprompt("Server-to-server networking password", &sc[15][0], 19);
733 strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
734 strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
735 strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
738 /* Expiry settings */
739 strprompt("Default user purge time (days)", &sc[16][0], 5);
740 strprompt("Default room purge time (days)", &sc[17][0], 5);
742 /* Angels and demons dancing in my head... */
744 sprintf(buf, "%d", expire_mode);
745 strprompt("System default message expire policy (? for list)",
749 printf("1. Never automatically expire messages\n");
750 printf("2. Expire by message count\n");
751 printf("3. Expire by message age\n");
753 } while ((buf[0] < 49) || (buf[0] > 51));
754 expire_mode = buf[0] - 48;
756 /* ...lunatics and monsters underneath my bed */
757 if (expire_mode == 2) {
758 sprintf(buf, "%d", expire_value);
759 strprompt("Keep how many messages online?", buf, 10);
760 expire_value = atol(buf);
762 if (expire_mode == 3) {
763 sprintf(buf, "%d", expire_value);
764 strprompt("Keep messages for how many days?", buf, 10);
765 expire_value = atol(buf);
768 printf("Save this configuration? ");
770 serv_puts("CONF set");
773 for (a = 0; a < 28; ++a)
774 serv_puts(&sc[a][0]);
777 snprintf(buf, sizeof buf, "SPEX site|%d|%d",
778 expire_mode, expire_value);
786 * support function for do_internet_configuration()
788 void get_inet_rec_type(char *buf) {
791 keyopt(" <1> localhost (Alias for this computer)\n");
792 keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
793 keyopt(" <3> smart-host (Forward all outbound mail to this host)\n");
794 sel = intprompt("Which one", 1, 1, 3);
796 case 1: strcpy(buf, "localhost");
798 case 2: strcpy(buf, "gatewaydomain");
800 case 3: strcpy(buf, "smarthost");
807 * Internet mail configuration
809 void do_internet_configuration(void) {
819 sprintf(buf, "CONF getsys|%s", INTERNETCFG);
822 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
824 if (num_recs == 1) recs = malloc(sizeof(char *));
825 else recs = realloc(recs, (sizeof(char *)) * num_recs);
826 recs[num_recs-1] = malloc(SIZ);
827 strcpy(recs[num_recs-1], buf);
834 printf(" Host or domain ");
835 printf(" Record type \n");
838 printf("-------------------------------------------------- ");
839 printf("--------------------\n");
840 for (i=0; i<num_recs; ++i) {
843 extract(buf, recs[i], 0);
845 printf("%-50s ", buf);
846 extract(buf, recs[i], 1);
847 color(BRIGHT_MAGENTA);
848 printf("%-20s\n", buf);
852 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
857 recs = malloc(sizeof(char *));
858 else recs = realloc(recs,
859 (sizeof(char *)) * num_recs);
860 newprompt("Enter host name: ",
863 get_inet_rec_type(&buf[strlen(buf)]);
864 recs[num_recs-1] = strdup(buf);
867 i = intprompt("Delete which one",
871 for (j=i; j<num_recs; ++j)
875 sprintf(buf, "CONF putsys|%s",
880 for (i=0; i<num_recs; ++i) {
886 printf("%s\n", &buf[4]);
891 quitting = boolprompt(
892 "Quit without saving", 0);
897 } while (quitting == 0);
900 for (i=0; i<num_recs; ++i) free(recs[i]);
908 * Edit network configuration for room sharing, mailing lists, etc.
910 void network_config_management(char *entrytype, char *comment) {
911 char filename[PATH_MAX];
912 char changefile[PATH_MAX];
923 if (strlen(editor_path) == 0) {
924 printf("You must have an external editor configured in order"
925 " to use this function.\n");
929 snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
930 snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
932 tempfp = fopen(filename, "w");
933 if (tempfp == NULL) {
934 printf("Cannot open %s: %s\n", filename, strerror(errno));
938 fprintf(tempfp, "# Configuration for room: %s\n", room_name);
939 fprintf(tempfp, "# %s\n", comment);
940 fprintf(tempfp, "# Specify one per line.\n"
946 while(serv_gets(buf), strcmp(buf, "000")) {
947 extract(instr, buf, 0);
948 if (!strcasecmp(instr, entrytype)) {
949 extract(addr, buf, 1);
950 fprintf(tempfp, "%s\n", addr);
956 e_ex_code = 1; /* start with a failed exit code */
958 cksum = file_checksum(filename);
959 if (editor_pid == 0) {
960 chmod(filename, 0600);
962 execlp(editor_path, editor_path, filename, NULL);
965 if (editor_pid > 0) {
968 b = ka_wait(&e_ex_code);
969 } while ((b != editor_pid) && (b >= 0));
974 if (file_checksum(filename) == cksum) {
975 printf("*** Not saving changes.\n");
979 if (e_ex_code == 0) { /* Save changes */
980 changefp = fopen(changefile, "w");
984 while(serv_gets(buf), strcmp(buf, "000")) {
985 extract(instr, buf, 0);
986 if (strcasecmp(instr, entrytype)) {
987 fprintf(changefp, "%s\n", buf);
991 tempfp = fopen(filename, "r");
992 while (fgets(buf, sizeof buf, tempfp) != NULL) {
993 for (i=0; i<strlen(buf); ++i) {
994 if (buf[i] == '#') buf[i] = 0;
997 if (strlen(buf) > 0) {
998 fprintf(changefp, "%s|%s\n", entrytype, buf);
1004 /* now write it to the server... */
1007 if (buf[0] == '4') {
1008 changefp = fopen(changefile, "r");
1009 if (changefp != NULL) {
1010 while (fgets(buf, sizeof buf,
1011 changefp) != NULL) {
1012 buf[strlen(buf) - 1] = 0;
1021 unlink(filename); /* Delete the temporary files */
1027 * IGnet node configuration
1029 void do_ignet_configuration(void) {
1039 sprintf(buf, "CONF getsys|%s", IGNETCFG);
1042 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1044 if (num_recs == 1) recs = malloc(sizeof(char *));
1045 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1046 recs[num_recs-1] = malloc(SIZ);
1047 strcpy(recs[num_recs-1], buf);
1052 color(BRIGHT_WHITE);
1062 "-------------------------------- "
1064 for (i=0; i<num_recs; ++i) {
1066 printf("%3d ", i+1);
1067 extract(buf, recs[i], 0);
1069 printf("%-16s ", buf);
1070 extract(buf, recs[i], 1);
1071 color(BRIGHT_MAGENTA);
1072 printf("%-16s ", buf);
1073 extract(buf, recs[i], 2);
1075 printf("%-32s ", buf);
1076 extract(buf, recs[i], 3);
1077 color(BRIGHT_MAGENTA);
1078 printf("%-3s\n", buf);
1082 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1087 recs = malloc(sizeof(char *));
1088 else recs = realloc(recs,
1089 (sizeof(char *)) * num_recs);
1090 newprompt("Enter node name : ", buf, 16);
1092 newprompt("Enter shared secret: ",
1093 &buf[strlen(buf)], 16);
1095 newprompt("Enter host or IP : ",
1096 &buf[strlen(buf)], 32);
1097 strcat(buf, "|504");
1098 strprompt("Enter port number : ",
1099 &buf[strlen(buf)-3], 5);
1100 recs[num_recs-1] = strdup(buf);
1103 i = intprompt("Delete which one",
1104 1, 1, num_recs) - 1;
1107 for (j=i; j<num_recs; ++j)
1108 recs[j] = recs[j+1];
1111 sprintf(buf, "CONF putsys|%s", IGNETCFG);
1114 if (buf[0] == '4') {
1115 for (i=0; i<num_recs; ++i) {
1121 printf("%s\n", &buf[4]);
1126 quitting = boolprompt(
1127 "Quit without saving", 0);
1132 } while (quitting == 0);
1135 for (i=0; i<num_recs; ++i) free(recs[i]);