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);
670 /* Fetch the expire policy (this will silently fail on old servers,
671 * resulting in "default" policy)
673 serv_puts("GPEX site");
676 expire_mode = extract_int(&buf[4], 0);
677 expire_value = extract_int(&buf[4], 1);
681 /* Identification parameters */
683 strprompt("Node name", &sc[0][0], 15);
684 strprompt("Fully qualified domain name", &sc[1][0], 63);
685 strprompt("Human readable node name", &sc[2][0], 20);
686 strprompt("Modem dialup number", &sc[3][0], 15);
687 strprompt("Geographic location of this system", &sc[12][0], 31);
688 strprompt("Name of system administrator", &sc[13][0], 25);
689 strprompt("Paginator prompt", &sc[10][0], 79);
691 /* this prompt is commented out until we finish the moderation system
692 strprompt("Default moderation filter for new users", &sc[25][0], 4);
695 /* Security parameters */
697 sprintf(&sc[7][0], "%d", (boolprompt(
698 "Require registration for new users",
700 strprompt("Initial access level for new users", &sc[6][0], 1);
701 strprompt("Access level required to create rooms", &sc[19][0], 1);
702 sprintf(&sc[4][0], "%d", (boolprompt(
703 "Automatically give room aide privs to a user who creates a private room",
706 sprintf(&sc[8][0], "%d", (boolprompt(
707 "Automatically move problem user messages to twit room",
710 strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
711 sprintf(&sc[11][0], "%d", (boolprompt(
712 "Restrict Internet mail to only those with that privilege",
714 sprintf(&sc[26][0], "%d", (boolprompt(
715 "Allow Aides to Zap (forget) rooms",
718 if (strlen(&sc[18][0]) > 0) logpages = 1;
720 logpages = boolprompt("Log all pages", logpages);
722 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
731 strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
732 strprompt("Maximum concurrent sessions", &sc[14][0], 4);
733 strprompt("Maximum message length", &sc[20][0], 20);
734 strprompt("Minimum number of worker threads", &sc[21][0], 3);
735 strprompt("Maximum number of worker threads", &sc[22][0], 3);
737 /* no longer applicable ... deprecated
738 strprompt("Server-to-server networking password", &sc[15][0], 19);
741 strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
742 strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
743 strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
744 strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
746 /* Expiry settings */
747 strprompt("Default user purge time (days)", &sc[16][0], 5);
748 strprompt("Default room purge time (days)", &sc[17][0], 5);
750 /* Angels and demons dancing in my head... */
752 sprintf(buf, "%d", expire_mode);
753 strprompt("System default message expire policy (? for list)",
757 printf("1. Never automatically expire messages\n");
758 printf("2. Expire by message count\n");
759 printf("3. Expire by message age\n");
761 } while ((buf[0] < 49) || (buf[0] > 51));
762 expire_mode = buf[0] - 48;
764 /* ...lunatics and monsters underneath my bed */
765 if (expire_mode == 2) {
766 sprintf(buf, "%d", expire_value);
767 strprompt("Keep how many messages online?", buf, 10);
768 expire_value = atol(buf);
770 if (expire_mode == 3) {
771 sprintf(buf, "%d", expire_value);
772 strprompt("Keep messages for how many days?", buf, 10);
773 expire_value = atol(buf);
776 printf("Save this configuration? ");
778 serv_puts("CONF set");
781 for (a = 0; a < 29; ++a)
782 serv_puts(&sc[a][0]);
785 snprintf(buf, sizeof buf, "SPEX site|%d|%d",
786 expire_mode, expire_value);
794 * support function for do_internet_configuration()
796 void get_inet_rec_type(char *buf) {
799 keyopt(" <1> localhost (Alias for this computer)\n");
800 keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
801 keyopt(" <3> smart-host (Forward all outbound mail to this host)\n");
802 sel = intprompt("Which one", 1, 1, 3);
804 case 1: strcpy(buf, "localhost");
806 case 2: strcpy(buf, "gatewaydomain");
808 case 3: strcpy(buf, "smarthost");
815 * Internet mail configuration
817 void do_internet_configuration(void) {
827 sprintf(buf, "CONF getsys|%s", INTERNETCFG);
830 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
832 if (num_recs == 1) recs = malloc(sizeof(char *));
833 else recs = realloc(recs, (sizeof(char *)) * num_recs);
834 recs[num_recs-1] = malloc(SIZ);
835 strcpy(recs[num_recs-1], buf);
842 printf(" Host or domain ");
843 printf(" Record type \n");
846 printf("-------------------------------------------------- ");
847 printf("--------------------\n");
848 for (i=0; i<num_recs; ++i) {
851 extract(buf, recs[i], 0);
853 printf("%-50s ", buf);
854 extract(buf, recs[i], 1);
855 color(BRIGHT_MAGENTA);
856 printf("%-20s\n", buf);
860 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
865 recs = malloc(sizeof(char *));
866 else recs = realloc(recs,
867 (sizeof(char *)) * num_recs);
868 newprompt("Enter host name: ",
871 get_inet_rec_type(&buf[strlen(buf)]);
872 recs[num_recs-1] = strdup(buf);
875 i = intprompt("Delete which one",
879 for (j=i; j<num_recs; ++j)
883 sprintf(buf, "CONF putsys|%s",
888 for (i=0; i<num_recs; ++i) {
894 printf("%s\n", &buf[4]);
899 quitting = boolprompt(
900 "Quit without saving", 0);
905 } while (quitting == 0);
908 for (i=0; i<num_recs; ++i) free(recs[i]);
916 * Edit network configuration for room sharing, mailing lists, etc.
918 void network_config_management(char *entrytype, char *comment) {
919 char filename[PATH_MAX];
920 char changefile[PATH_MAX];
931 if (strlen(editor_path) == 0) {
932 printf("You must have an external editor configured in order"
933 " to use this function.\n");
937 snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
938 snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
940 tempfp = fopen(filename, "w");
941 if (tempfp == NULL) {
942 printf("Cannot open %s: %s\n", filename, strerror(errno));
946 fprintf(tempfp, "# Configuration for room: %s\n", room_name);
947 fprintf(tempfp, "# %s\n", comment);
948 fprintf(tempfp, "# Specify one per line.\n"
954 while(serv_gets(buf), strcmp(buf, "000")) {
955 extract(instr, buf, 0);
956 if (!strcasecmp(instr, entrytype)) {
957 extract(addr, buf, 1);
958 fprintf(tempfp, "%s\n", addr);
964 e_ex_code = 1; /* start with a failed exit code */
966 cksum = file_checksum(filename);
967 if (editor_pid == 0) {
968 chmod(filename, 0600);
970 execlp(editor_path, editor_path, filename, NULL);
973 if (editor_pid > 0) {
976 b = ka_wait(&e_ex_code);
977 } while ((b != editor_pid) && (b >= 0));
982 if (file_checksum(filename) == cksum) {
983 printf("*** Not saving changes.\n");
987 if (e_ex_code == 0) { /* Save changes */
988 changefp = fopen(changefile, "w");
992 while(serv_gets(buf), strcmp(buf, "000")) {
993 extract(instr, buf, 0);
994 if (strcasecmp(instr, entrytype)) {
995 fprintf(changefp, "%s\n", buf);
999 tempfp = fopen(filename, "r");
1000 while (fgets(buf, sizeof buf, tempfp) != NULL) {
1001 for (i=0; i<strlen(buf); ++i) {
1002 if (buf[i] == '#') buf[i] = 0;
1005 if (strlen(buf) > 0) {
1006 fprintf(changefp, "%s|%s\n", entrytype, buf);
1012 /* now write it to the server... */
1015 if (buf[0] == '4') {
1016 changefp = fopen(changefile, "r");
1017 if (changefp != NULL) {
1018 while (fgets(buf, sizeof buf,
1019 changefp) != NULL) {
1020 buf[strlen(buf) - 1] = 0;
1029 unlink(filename); /* Delete the temporary files */
1035 * IGnet node configuration
1037 void do_ignet_configuration(void) {
1047 sprintf(buf, "CONF getsys|%s", IGNETCFG);
1050 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1052 if (num_recs == 1) recs = malloc(sizeof(char *));
1053 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1054 recs[num_recs-1] = malloc(SIZ);
1055 strcpy(recs[num_recs-1], buf);
1060 color(BRIGHT_WHITE);
1070 "-------------------------------- "
1072 for (i=0; i<num_recs; ++i) {
1074 printf("%3d ", i+1);
1075 extract(buf, recs[i], 0);
1077 printf("%-16s ", buf);
1078 extract(buf, recs[i], 1);
1079 color(BRIGHT_MAGENTA);
1080 printf("%-16s ", buf);
1081 extract(buf, recs[i], 2);
1083 printf("%-32s ", buf);
1084 extract(buf, recs[i], 3);
1085 color(BRIGHT_MAGENTA);
1086 printf("%-3s\n", buf);
1090 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1095 recs = malloc(sizeof(char *));
1096 else recs = realloc(recs,
1097 (sizeof(char *)) * num_recs);
1098 newprompt("Enter node name : ", buf, 16);
1100 newprompt("Enter shared secret: ",
1101 &buf[strlen(buf)], 16);
1103 newprompt("Enter host or IP : ",
1104 &buf[strlen(buf)], 32);
1105 strcat(buf, "|504");
1106 strprompt("Enter port number : ",
1107 &buf[strlen(buf)-3], 5);
1108 recs[num_recs-1] = strdup(buf);
1111 i = intprompt("Delete which one",
1112 1, 1, num_recs) - 1;
1115 for (j=i; j<num_recs; ++j)
1116 recs[j] = recs[j+1];
1119 sprintf(buf, "CONF putsys|%s", IGNETCFG);
1122 if (buf[0] == '4') {
1123 for (i=0; i<num_recs; ++i) {
1129 printf("%s\n", &buf[4]);
1134 quitting = boolprompt(
1135 "Quit without saving", 0);
1140 } while (quitting == 0);
1143 for (i=0; i<num_recs; ++i) free(recs[i]);