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 */
170 if (rc_alt_semantics) {
171 if (maxmsgnum == highest_msg_read == 0) {
172 /* fprintf(stderr, "maxmsgnum == highest_msg_read == 0\n"); */
175 snprintf(buf, sizeof(buf), "SLRP %ld",
176 (maxmsgnum > highest_msg_read) ?
177 maxmsgnum : highest_msg_read);
180 serv_puts("SLRP HIGHEST");
184 printf("%s\n", &buf[4]);
188 * only make messages old in this room that have been read
194 sprintf(buf, "SLRP %ld", highest_msg_read);
198 printf("%s\n", &buf[4]);
203 * This routine completes a client upload
205 void do_upload(int fd)
209 long transmitted_bytes, total_bytes;
213 /* learn the size of the file */
214 total_bytes = lseek(fd, 0L, 2);
217 transmitted_bytes = 0L;
218 progress(transmitted_bytes, total_bytes);
220 bytes_to_send = read(fd, tbuf, 4096);
221 if (bytes_to_send > 0) {
222 sprintf(buf, "WRIT %d", bytes_to_send);
226 bytes_expected = atoi(&buf[4]);
227 serv_write(tbuf, bytes_expected);
229 printf("%s\n", &buf[4]);
232 transmitted_bytes = transmitted_bytes + (long) bytes_to_send;
233 progress(transmitted_bytes, total_bytes);
234 } while (bytes_to_send > 0);
236 /* close the upload file, locally and at the server */
240 printf("%s\n", &buf[4]);
245 * client-based uploads (for users with their own clientware)
247 void cli_upload(void)
256 if ((room_flags & QR_UPLOAD) == 0) {
257 printf("*** You cannot upload to this room.\n");
260 newprompt("File to be uploaded: ", flnm, 55);
261 fd = open(flnm, O_RDONLY);
263 printf("Cannot open '%s': %s\n", flnm, strerror(errno));
266 printf("Enter a description of this file:\n");
267 newprompt(": ", desc, 75);
269 /* keep generating filenames in hope of finding a unique one */
273 return; /* fail if tried 10 times */
275 while ((strlen(buf) > 0) && (haschar(buf, '/')))
276 strcpy(buf, &buf[1]);
278 sprintf(&buf[strlen(buf)], "%d", a);
279 sprintf(tbuf, "UOPN %s|%s", buf, desc);
283 printf("%s\n", &buf[4]);
285 } while (buf[0] != '2');
287 /* at this point we have an open upload file at the server */
293 * Function used for various image upload commands
295 void cli_image_upload(char *keyname)
301 sprintf(buf, "UIMG 0|%s", keyname);
305 printf("%s\n", &buf[4]);
308 newprompt("Image file to be uploaded: ", flnm, 55);
309 fd = open(flnm, O_RDONLY);
311 printf("Cannot open '%s': %s\n", flnm, strerror(errno));
314 sprintf(buf, "UIMG 1|%s", keyname);
318 printf("%s\n", &buf[4]);
326 * protocol-based uploads (Xmodem, Ymodem, Zmodem)
329 { /* c = upload mode */
339 if ((room_flags & QR_UPLOAD) == 0) {
340 printf("*** You cannot upload to this room.\n");
343 /* we don't need a filename when receiving batch y/z modem */
344 if ((c == 2) || (c == 3))
347 newprompt("Enter filename: ", flnm, 15);
349 for (a = 0; a < strlen(flnm); ++a)
350 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
351 || (flnm[a] == '?') || (flnm[a] == '*')
352 || (flnm[a] == ';') || (flnm[a] == '&'))
355 newprompt("Enter a short description of the file:\n: ", desc, 150);
357 /* create a temporary directory... */
358 if (mkdir(tempdir, 0700) != 0) {
359 printf("*** Could not create temporary directory %s: %s\n",
360 tempdir, strerror(errno));
363 /* now do the transfer ... in a separate process */
370 printf("Receiving %s - press Ctrl-D to end.\n", flnm);
371 fp = fopen(flnm, "w");
387 execlp("rx", "rx", flnm, NULL);
391 execlp("rb", "rb", NULL);
395 execlp("rz", "rz", NULL);
401 } while ((b != xfer_pid) && (b != (-1)));
405 printf("\r*** Transfer unsuccessful.\n");
409 printf("\r*** Transfer successful. Sending file(s) to server...\n");
410 sprintf(buf, "cd %s; ls", tempdir);
411 lsfp = popen(buf, "r");
413 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
414 flnm[strlen(flnm) - 1] = 0;
415 sprintf(buf, "%s/%s", tempdir, flnm);
416 fd = open(buf, O_RDONLY);
420 sprintf(buf, "UOPN %s|%s", flnm, desc);
422 sprintf(&buf[strlen(buf)],
427 } while ((buf[0] != '2') && (a < 100));
430 a = read(fd, tbuf, 4096);
432 sprintf(buf, "WRIT %d", a);
442 printf("%s\n", &buf[4]);
453 void val_user(char *user, int do_validate)
460 sprintf(cmd, "GREG %s", user);
469 printf("User #%s - %s ", buf, &cmd[4]);
471 printf("PW: %s\n", buf);
490 } while (strcmp(buf, "000"));
491 printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
493 printf("%-30s\n%s\n", user, &cmd[4]);
497 /* now set the access level */
498 ax = intprompt("Access level", ax, 0, 6);
499 sprintf(cmd, "VALI %s|%d", user, ax);
503 printf("%s\n", &cmd[4]);
510 { /* validate new users */
521 printf("%s\n", &cmd[4]);
523 extract(buf, cmd, 0);
524 val_user(&buf[4], 1);
526 } while (finished == 0);
535 signal(SIGINT, SIG_DFL);
536 signal(SIGQUIT, SIG_DFL);
537 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
538 printf("Could not open a shell: %s\n", strerror(errno));
543 } while ((a != b) && (a != (-1)));
548 * <.A>ide <F>ile <D>elete command
550 void deletefile(void)
555 newprompt("Filename: ", filename, 31);
556 if (strlen(filename) == 0)
558 sprintf(cmd, "DELF %s", filename);
561 printf("%s\n", &cmd[4]);
565 * <.A>ide <F>ile <S>end command
567 void netsendfile(void)
569 char filename[32], destsys[20], cmd[SIZ];
571 newprompt("Filename: ", filename, 31);
572 if (strlen(filename) == 0)
574 newprompt("System to send to: ", destsys, 19);
575 sprintf(cmd, "NETF %s|%s", filename, destsys);
578 printf("%s\n", &cmd[4]);
583 * <.A>ide <F>ile <M>ove command
588 char newroom[ROOMNAMELEN];
591 newprompt("Filename: ", filename, 63);
592 if (strlen(filename) == 0)
594 newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
596 sprintf(cmd, "MOVF %s|%s", filename, newroom);
599 printf("%s\n", &cmd[4]);
604 * list of users who have filled out a bio
614 pprintf("%s\n", &buf[4]);
617 while (serv_gets(buf), strcmp(buf, "000")) {
618 if ((pos + strlen(buf) + 5) > screenwidth) {
622 pprintf("%s, ", buf);
623 pos = pos + strlen(buf) + 2;
625 pprintf("%c%c \n\n", 8, 8);
638 newprompt("Read bio for who ('?' for list) : ", who, 25);
640 if (!strcmp(who, "?"))
642 } while (!strcmp(who, "?"));
643 sprintf(buf, "RBIO %s", who);
647 pprintf("%s\n", &buf[4]);
650 while (serv_gets(buf), strcmp(buf, "000")) {
651 pprintf("%s\n", buf);
657 * General system configuration command
659 void do_system_configuration(void)
664 int expire_value = 0;
668 /* Clear out the config buffers */
669 memset(&sc[0][0], 0, sizeof(sc));
671 /* Fetch the current config */
672 serv_puts("CONF get");
676 while (serv_gets(buf), strcmp(buf, "000")) {
678 strcpy(&sc[a][0], buf);
683 /* Fetch the expire policy (this will silently fail on old servers,
684 * resulting in "default" policy)
686 serv_puts("GPEX site");
689 expire_mode = extract_int(&buf[4], 0);
690 expire_value = extract_int(&buf[4], 1);
694 /* Identification parameters */
696 strprompt("Node name", &sc[0][0], 15);
697 strprompt("Fully qualified domain name", &sc[1][0], 63);
698 strprompt("Human readable node name", &sc[2][0], 20);
699 strprompt("Modem dialup number", &sc[3][0], 15);
700 strprompt("Geographic location of this system", &sc[12][0], 31);
701 strprompt("Name of system administrator", &sc[13][0], 25);
702 strprompt("Paginator prompt", &sc[10][0], 79);
704 /* this prompt is commented out until we finish the moderation system
705 strprompt("Default moderation filter for new users", &sc[25][0], 4);
708 /* Security parameters */
710 sprintf(&sc[7][0], "%d", (boolprompt(
711 "Require registration for new users",
713 strprompt("Initial access level for new users", &sc[6][0], 1);
714 strprompt("Access level required to create rooms", &sc[19][0], 1);
715 sprintf(&sc[4][0], "%d", (boolprompt(
716 "Automatically give room aide privs to a user who creates a private room",
719 sprintf(&sc[8][0], "%d", (boolprompt(
720 "Automatically move problem user messages to twit room",
723 strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
724 sprintf(&sc[11][0], "%d", (boolprompt(
725 "Restrict Internet mail to only those with that privilege",
727 sprintf(&sc[26][0], "%d", (boolprompt(
728 "Allow Aides to Zap (forget) rooms",
731 if (strlen(&sc[18][0]) > 0) logpages = 1;
733 logpages = boolprompt("Log all pages", logpages);
735 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
744 strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
745 strprompt("Maximum concurrent sessions", &sc[14][0], 4);
746 strprompt("Maximum message length", &sc[20][0], 20);
747 strprompt("Minimum number of worker threads", &sc[21][0], 3);
748 strprompt("Maximum number of worker threads", &sc[22][0], 3);
750 /* no longer applicable ... deprecated
751 strprompt("Server-to-server networking password", &sc[15][0], 19);
754 strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
755 strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
756 strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
757 strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
759 /* Expiry settings */
760 strprompt("Default user purge time (days)", &sc[16][0], 5);
761 strprompt("Default room purge time (days)", &sc[17][0], 5);
763 /* Angels and demons dancing in my head... */
765 sprintf(buf, "%d", expire_mode);
766 strprompt("System default message expire policy (? for list)",
770 printf("1. Never automatically expire messages\n");
771 printf("2. Expire by message count\n");
772 printf("3. Expire by message age\n");
774 } while ((buf[0] < 49) || (buf[0] > 51));
775 expire_mode = buf[0] - 48;
777 /* ...lunatics and monsters underneath my bed */
778 if (expire_mode == 2) {
779 sprintf(buf, "%d", expire_value);
780 strprompt("Keep how many messages online?", buf, 10);
781 expire_value = atol(buf);
783 if (expire_mode == 3) {
784 sprintf(buf, "%d", expire_value);
785 strprompt("Keep messages for how many days?", buf, 10);
786 expire_value = atol(buf);
789 printf("Save this configuration? ");
791 serv_puts("CONF set");
794 for (a = 0; a < 29; ++a)
795 serv_puts(&sc[a][0]);
798 snprintf(buf, sizeof buf, "SPEX site|%d|%d",
799 expire_mode, expire_value);
807 * support function for do_internet_configuration()
809 void get_inet_rec_type(char *buf) {
812 keyopt(" <1> localhost (Alias for this computer)\n");
813 keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
814 keyopt(" <3> smart-host (Forward all outbound mail to this host)\n");
815 keyopt(" <4> directory (Consult the Global Address Book)\n");
816 sel = intprompt("Which one", 1, 1, 4);
818 case 1: strcpy(buf, "localhost");
820 case 2: strcpy(buf, "gatewaydomain");
822 case 3: strcpy(buf, "smarthost");
824 case 4: strcpy(buf, "directory");
831 * Internet mail configuration
833 void do_internet_configuration(void) {
843 sprintf(buf, "CONF getsys|%s", INTERNETCFG);
846 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
848 if (num_recs == 1) recs = malloc(sizeof(char *));
849 else recs = realloc(recs, (sizeof(char *)) * num_recs);
850 recs[num_recs-1] = malloc(SIZ);
851 strcpy(recs[num_recs-1], buf);
858 printf(" Host or domain ");
859 printf(" Record type \n");
862 printf("-------------------------------------------------- ");
863 printf("--------------------\n");
864 for (i=0; i<num_recs; ++i) {
867 extract(buf, recs[i], 0);
869 printf("%-50s ", buf);
870 extract(buf, recs[i], 1);
871 color(BRIGHT_MAGENTA);
872 printf("%-20s\n", buf);
876 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
881 recs = malloc(sizeof(char *));
882 else recs = realloc(recs,
883 (sizeof(char *)) * num_recs);
884 newprompt("Enter host name: ",
887 get_inet_rec_type(&buf[strlen(buf)]);
888 recs[num_recs-1] = strdup(buf);
891 i = intprompt("Delete which one",
895 for (j=i; j<num_recs; ++j)
899 sprintf(buf, "CONF putsys|%s",
904 for (i=0; i<num_recs; ++i) {
910 printf("%s\n", &buf[4]);
915 quitting = boolprompt(
916 "Quit without saving", 0);
921 } while (quitting == 0);
924 for (i=0; i<num_recs; ++i) free(recs[i]);
932 * Edit network configuration for room sharing, mailing lists, etc.
934 void network_config_management(char *entrytype, char *comment) {
935 char filename[PATH_MAX];
936 char changefile[PATH_MAX];
947 if (strlen(editor_path) == 0) {
948 printf("You must have an external editor configured in order"
949 " to use this function.\n");
953 snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
954 snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
956 tempfp = fopen(filename, "w");
957 if (tempfp == NULL) {
958 printf("Cannot open %s: %s\n", filename, strerror(errno));
962 fprintf(tempfp, "# Configuration for room: %s\n", room_name);
963 fprintf(tempfp, "# %s\n", comment);
964 fprintf(tempfp, "# Specify one per line.\n"
970 while(serv_gets(buf), strcmp(buf, "000")) {
971 extract(instr, buf, 0);
972 if (!strcasecmp(instr, entrytype)) {
973 extract(addr, buf, 1);
974 fprintf(tempfp, "%s\n", addr);
980 e_ex_code = 1; /* start with a failed exit code */
982 cksum = file_checksum(filename);
983 if (editor_pid == 0) {
984 chmod(filename, 0600);
986 setenv("WINDOW_TITLE", "Network configuration", 1);
987 execlp(editor_path, editor_path, filename, NULL);
990 if (editor_pid > 0) {
993 b = ka_wait(&e_ex_code);
994 } while ((b != editor_pid) && (b >= 0));
999 if (file_checksum(filename) == cksum) {
1000 printf("*** Not saving changes.\n");
1004 if (e_ex_code == 0) { /* Save changes */
1005 changefp = fopen(changefile, "w");
1008 if (buf[0] == '1') {
1009 while(serv_gets(buf), strcmp(buf, "000")) {
1010 extract(instr, buf, 0);
1011 if (strcasecmp(instr, entrytype)) {
1012 fprintf(changefp, "%s\n", buf);
1016 tempfp = fopen(filename, "r");
1017 while (fgets(buf, sizeof buf, tempfp) != NULL) {
1018 for (i=0; i<strlen(buf); ++i) {
1019 if (buf[i] == '#') buf[i] = 0;
1022 if (strlen(buf) > 0) {
1023 fprintf(changefp, "%s|%s\n", entrytype, buf);
1029 /* now write it to the server... */
1032 if (buf[0] == '4') {
1033 changefp = fopen(changefile, "r");
1034 if (changefp != NULL) {
1035 while (fgets(buf, sizeof buf,
1036 changefp) != NULL) {
1037 buf[strlen(buf) - 1] = 0;
1046 unlink(filename); /* Delete the temporary files */
1052 * IGnet node configuration
1054 void do_ignet_configuration(void) {
1064 sprintf(buf, "CONF getsys|%s", IGNETCFG);
1067 if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
1069 if (num_recs == 1) recs = malloc(sizeof(char *));
1070 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1071 recs[num_recs-1] = malloc(SIZ);
1072 strcpy(recs[num_recs-1], buf);
1077 color(BRIGHT_WHITE);
1086 "------------------ "
1087 "-------------------------------- "
1089 for (i=0; i<num_recs; ++i) {
1091 printf("%3d ", i+1);
1092 extract(buf, recs[i], 0);
1094 printf("%-16s ", buf);
1095 extract(buf, recs[i], 1);
1096 color(BRIGHT_MAGENTA);
1097 printf("%-18s ", buf);
1098 extract(buf, recs[i], 2);
1100 printf("%-32s ", buf);
1101 extract(buf, recs[i], 3);
1102 color(BRIGHT_MAGENTA);
1103 printf("%-3s\n", buf);
1107 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1112 recs = malloc(sizeof(char *));
1113 else recs = realloc(recs,
1114 (sizeof(char *)) * num_recs);
1115 newprompt("Enter node name : ", buf, 16);
1117 newprompt("Enter shared secret: ",
1118 &buf[strlen(buf)], 18);
1120 newprompt("Enter host or IP : ",
1121 &buf[strlen(buf)], 32);
1122 strcat(buf, "|504");
1123 strprompt("Enter port number : ",
1124 &buf[strlen(buf)-3], 5);
1125 recs[num_recs-1] = strdup(buf);
1128 i = intprompt("Delete which one",
1129 1, 1, num_recs) - 1;
1132 for (j=i; j<num_recs; ++j)
1133 recs[j] = recs[j+1];
1136 sprintf(buf, "CONF putsys|%s", IGNETCFG);
1139 if (buf[0] == '4') {
1140 for (i=0; i<num_recs; ++i) {
1146 printf("%s\n", &buf[4]);
1151 quitting = boolprompt(
1152 "Quit without saving", 0);
1157 } while (quitting == 0);
1160 for (i=0; i<num_recs; ++i) free(recs[i]);