fix all the <time.h> vs. <sys/time.h> issues, hopefully
[citadel.git] / citadel / routines2.c
1 /* $Id$
2  *
3  * More client-side support functions.
4  * Unlike routines.c, some of these DO use global variables.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <string.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <signal.h>
30 #include <pwd.h>
31 #include <setjmp.h>
32 #include <errno.h>
33 #include <stdarg.h>
34 #include "citadel.h"
35 #include "routines2.h"
36 #include "routines.h"
37 #include "commands.h"
38 #include "tools.h"
39 #include "messages.h"
40 #ifndef HAVE_SNPRINTF
41 #include "snprintf.h"
42 #endif
43
44 void interr(int errnum);
45 void strprompt(char *prompt, char *str, int len);
46 void newprompt(char *prompt, char *str, int len);
47 void sttybbs(int cmd);
48 int inkey(void);
49 void serv_write(char *buf, int nbytes);
50 int haschar(char *st, int ch);
51 void progress(long int curr, long int cmax);
52 int yesno(void);
53
54 extern char temp[];
55 extern char tempdir[];
56 extern char *axdefs[7];
57 extern long highest_msg_read;
58 extern long maxmsgnum;
59 extern unsigned room_flags;
60 extern int screenwidth;
61
62
63 int eopen(char *name, int mode)
64 {
65         int ret;
66         ret = open(name, mode);
67         if (ret < 0) {
68                 fprintf(stderr, "Cannot open file '%s', mode=%d, errno=%d\n",
69                         name, mode, errno);
70                 interr(errno);
71         }
72         return (ret);
73 }
74
75
76 int room_prompt(int qrflags)
77 {                               /* return proper room prompt character */
78         int a;
79         a = '>';
80         if (qrflags & QR_DIRECTORY)
81                 a = ']';
82         if ((a == ']') && (qrflags & QR_NETWORK))
83                 a = '}';
84         if ((a == '>') && (qrflags & QR_NETWORK))
85                 a = ')';
86         return (a);
87 }
88
89 void entregis(void)
90 {                               /* register with name and address */
91
92         char buf[SIZ];
93         char tmpname[SIZ];
94         char tmpaddr[SIZ];
95         char tmpcity[SIZ];
96         char tmpstate[SIZ];
97         char tmpzip[SIZ];
98         char tmpphone[SIZ];
99         char tmpemail[SIZ];
100         char tmpcountry[SIZ];
101         int a;
102
103         strcpy(tmpname, "");
104         strcpy(tmpaddr, "");
105         strcpy(tmpcity, "");
106         strcpy(tmpstate, "");
107         strcpy(tmpzip, "");
108         strcpy(tmpphone, "");
109         strcpy(tmpemail, "");
110         strcpy(tmpcountry, "");
111
112         serv_puts("GREG _SELF_");
113         serv_gets(buf);
114         if (buf[0] == '1') {
115                 a = 0;
116                 while (serv_gets(buf), strcmp(buf, "000")) {
117                         if (a == 2)
118                                 strcpy(tmpname, buf);
119                         if (a == 3)
120                                 strcpy(tmpaddr, buf);
121                         if (a == 4)
122                                 strcpy(tmpcity, buf);
123                         if (a == 5)
124                                 strcpy(tmpstate, buf);
125                         if (a == 6)
126                                 strcpy(tmpzip, buf);
127                         if (a == 7)
128                                 strcpy(tmpphone, buf);
129                         if (a == 9)
130                                 strcpy(tmpemail, buf);
131                         if (a == 10)
132                                 strcpy(tmpcountry, buf);
133                         ++a;
134                 }
135         }
136         strprompt("REAL name", tmpname, 29);
137         strprompt("Address", tmpaddr, 24);
138         strprompt("City/town", tmpcity, 14);
139         strprompt("State/province", tmpstate, 2);
140         strprompt("ZIP/Postal Code", tmpzip, 10);
141         strprompt("Country", tmpcountry, 31);
142         strprompt("Telephone number", tmpphone, 14);
143         strprompt("Email address", tmpemail, 31);
144
145         /* now send the registration info back to the server */
146         serv_puts("REGI");
147         serv_gets(buf);
148         if (buf[0] != '4') {
149                 printf("%s\n", &buf[4]);
150                 return;
151         }
152         serv_puts(tmpname);
153         serv_puts(tmpaddr);
154         serv_puts(tmpcity);
155         serv_puts(tmpstate);
156         serv_puts(tmpzip);
157         serv_puts(tmpphone);
158         serv_puts(tmpemail);
159         serv_puts(tmpcountry);
160         serv_puts("000");
161         printf("\n");
162 }
163
164 void updatels(void)
165 {                               /* make all messages old in current room */
166         char buf[SIZ];
167         serv_puts("SLRP HIGHEST");
168         serv_gets(buf);
169         if (buf[0] != '2')
170                 printf("%s\n", &buf[4]);
171 }
172
173 /*
174  * only make messages old in this room that have been read
175  */
176 void updatelsa(void)
177 {
178         char buf[SIZ];
179         sprintf(buf, "SLRP %ld", highest_msg_read);
180         serv_puts(buf);
181         serv_gets(buf);
182         if (buf[0] != '2')
183                 printf("%s\n", &buf[4]);
184 }
185
186
187 /*
188  * This routine completes a client upload
189  */
190 void do_upload(int fd)
191 {
192         char buf[SIZ];
193         char tbuf[4096];
194         long transmitted_bytes, total_bytes;
195         int bytes_to_send;
196         int bytes_expected;
197
198         /* learn the size of the file */
199         total_bytes = lseek(fd, 0L, 2);
200         lseek(fd, 0L, 0);
201
202         transmitted_bytes = 0L;
203         progress(transmitted_bytes, total_bytes);
204         do {
205                 bytes_to_send = read(fd, tbuf, 4096);
206                 if (bytes_to_send > 0) {
207                         sprintf(buf, "WRIT %d", bytes_to_send);
208                         serv_puts(buf);
209                         serv_gets(buf);
210                         if (buf[0] == '7') {
211                                 bytes_expected = atoi(&buf[4]);
212                                 serv_write(tbuf, bytes_expected);
213                         } else {
214                                 printf("%s\n", &buf[4]);
215                         }
216                 }
217                 transmitted_bytes = transmitted_bytes + (long) bytes_to_send;
218                 progress(transmitted_bytes, total_bytes);
219         } while (bytes_to_send > 0);
220
221         /* close the upload file, locally and at the server */
222         close(fd);
223         serv_puts("UCLS 1");
224         serv_gets(buf);
225         printf("%s\n", &buf[4]);
226 }
227
228
229 /*
230  * client-based uploads (for users with their own clientware)
231  */
232 void cli_upload(void)
233 {
234         char flnm[SIZ];
235         char desc[151];
236         char buf[SIZ];
237         char tbuf[SIZ];
238         int a;
239         int fd;
240
241         if ((room_flags & QR_UPLOAD) == 0) {
242                 printf("*** You cannot upload to this room.\n");
243                 return;
244         }
245         newprompt("File to be uploaded: ", flnm, 55);
246         fd = open(flnm, O_RDONLY);
247         if (fd < 0) {
248                 printf("Cannot open '%s': %s\n", flnm, strerror(errno));
249                 return;
250         }
251         printf("Enter a description of this file:\n");
252         newprompt(": ", desc, 75);
253
254         /* keep generating filenames in hope of finding a unique one */
255         a = 0;
256         do {
257                 if (a == 10)
258                         return; /* fail if tried 10 times */
259                 strcpy(buf, flnm);
260                 while ((strlen(buf) > 0) && (haschar(buf, '/')))
261                         strcpy(buf, &buf[1]);
262                 if (a > 0)
263                         sprintf(&buf[strlen(buf)], "%d", a);
264                 sprintf(tbuf, "UOPN %s|%s", buf, desc);
265                 serv_puts(tbuf);
266                 serv_gets(buf);
267                 if (buf[0] != '2')
268                         printf("%s\n", &buf[4]);
269                 ++a;
270         } while (buf[0] != '2');
271
272         /* at this point we have an open upload file at the server */
273         do_upload(fd);
274 }
275
276
277 /*
278  * Function used for various image upload commands
279  */
280 void cli_image_upload(char *keyname)
281 {
282         char flnm[SIZ];
283         char buf[SIZ];
284         int fd;
285
286         sprintf(buf, "UIMG 0|%s", keyname);
287         serv_puts(buf);
288         serv_gets(buf);
289         if (buf[0] != '2') {
290                 printf("%s\n", &buf[4]);
291                 return;
292         }
293         newprompt("Image file to be uploaded: ", flnm, 55);
294         fd = open(flnm, O_RDONLY);
295         if (fd < 0) {
296                 printf("Cannot open '%s': %s\n", flnm, strerror(errno));
297                 return;
298         }
299         sprintf(buf, "UIMG 1|%s", keyname);
300         serv_puts(buf);
301         serv_gets(buf);
302         if (buf[0] != '2') {
303                 printf("%s\n", &buf[4]);
304                 return;
305         }
306         do_upload(fd);
307 }
308
309
310 /*
311  * protocol-based uploads (Xmodem, Ymodem, Zmodem)
312  */
313 void upload(int c)
314 {                               /* c = upload mode */
315         char flnm[SIZ];
316         char desc[151];
317         char buf[SIZ];
318         char tbuf[4096];
319         int xfer_pid;
320         int a, b;
321         FILE *fp, *lsfp;
322         int fd;
323
324         if ((room_flags & QR_UPLOAD) == 0) {
325                 printf("*** You cannot upload to this room.\n");
326                 return;
327         }
328         /* we don't need a filename when receiving batch y/z modem */
329         if ((c == 2) || (c == 3))
330                 strcpy(flnm, "x");
331         else
332                 newprompt("Enter filename: ", flnm, 15);
333
334         for (a = 0; a < strlen(flnm); ++a)
335                 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
336                     || (flnm[a] == '?') || (flnm[a] == '*')
337                     || (flnm[a] == ';') || (flnm[a] == '&'))
338                         flnm[a] = '_';
339
340         newprompt("Enter a short description of the file:\n: ", desc, 150);
341
342         /* create a temporary directory... */
343         if (mkdir(tempdir, 0700) != 0) {
344                 printf("*** Could not create temporary directory %s: %s\n",
345                        tempdir, strerror(errno));
346                 return;
347         }
348         /* now do the transfer ... in a separate process */
349         xfer_pid = fork();
350         if (xfer_pid == 0) {
351                 chdir(tempdir);
352                 switch (c) {
353                 case 0:
354                         sttybbs(0);
355                         printf("Receiving %s - press Ctrl-D to end.\n", flnm);
356                         fp = fopen(flnm, "w");
357                         do {
358                                 b = inkey();
359                                 if (b == 13) {
360                                         b = 10;
361                                         printf("\r");
362                                 }
363                                 if (b != 4) {
364                                         printf("%c", b);
365                                         putc(b, fp);
366                                 }
367                         } while (b != 4);
368                         fclose(fp);
369                         exit(0);
370                 case 1:
371                         sttybbs(3);
372                         execlp("rx", "rx", flnm, NULL);
373                         exit(1);
374                 case 2:
375                         sttybbs(3);
376                         execlp("rb", "rb", NULL);
377                         exit(1);
378                 case 3:
379                         sttybbs(3);
380                         execlp("rz", "rz", NULL);
381                         exit(1);
382                 }
383         } else
384                 do {
385                         b = ka_wait(&a);
386                 } while ((b != xfer_pid) && (b != (-1)));
387         sttybbs(0);
388
389         if (a != 0) {
390                 printf("\r*** Transfer unsuccessful.\n");
391                 nukedir(tempdir);
392                 return;
393         }
394         printf("\r*** Transfer successful.  Sending file(s) to server...\n");
395         sprintf(buf, "cd %s; ls", tempdir);
396         lsfp = popen(buf, "r");
397         if (lsfp != NULL) {
398                 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
399                         flnm[strlen(flnm) - 1] = 0;
400                         sprintf(buf, "%s/%s", tempdir, flnm);
401                         fd = open(buf, O_RDONLY);
402                         if (fd >= 0) {
403                                 a = 0;
404                                 do {
405                                         sprintf(buf, "UOPN %s|%s", flnm, desc);
406                                         if (a > 0)
407                                                 sprintf(&buf[strlen(buf)],
408                                                         ".%d", a);
409                                         ++a;
410                                         serv_puts(buf);
411                                         serv_gets(buf);
412                                 } while ((buf[0] != '2') && (a < 100));
413                                 if (buf[0] == '2')
414                                         do {
415                                                 a = read(fd, tbuf, 4096);
416                                                 if (a > 0) {
417                                                         sprintf(buf, "WRIT %d", a);
418                                                         serv_puts(buf);
419                                                         serv_gets(buf);
420                                                         if (buf[0] == '7')
421                                                                 serv_write(tbuf, a);
422                                                 }
423                                         } while (a > 0);
424                                 close(fd);
425                                 serv_puts("UCLS 1");
426                                 serv_gets(buf);
427                                 printf("%s\n", &buf[4]);
428                         }
429                 }
430                 pclose(lsfp);
431         }
432         nukedir(tempdir);
433 }
434
435 /* 
436  * validate a user
437  */
438 void val_user(char *user, int do_validate)
439 {
440         int a;
441         char cmd[SIZ];
442         char buf[SIZ];
443         int ax = 0;
444
445         sprintf(cmd, "GREG %s", user);
446         serv_puts(cmd);
447         serv_gets(cmd);
448         if (cmd[0] == '1') {
449                 a = 0;
450                 do {
451                         serv_gets(buf);
452                         ++a;
453                         if (a == 1)
454                                 printf("User #%s - %s  ", buf, &cmd[4]);
455                         if (a == 2)
456                                 printf("PW: %s\n", buf);
457                         if (a == 3)
458                                 printf("%s\n", buf);
459                         if (a == 4)
460                                 printf("%s\n", buf);
461                         if (a == 5)
462                                 printf("%s, ", buf);
463                         if (a == 6)
464                                 printf("%s ", buf);
465                         if (a == 7)
466                                 printf("%s\n", buf);
467                         if (a == 8)
468                                 printf("%s\n", buf);
469                         if (a == 9)
470                                 ax = atoi(buf);
471                         if (a == 10)
472                                 printf("%s\n", buf);
473                         if (a == 11)
474                                 printf("%s\n", buf);
475                 } while (strcmp(buf, "000"));
476                 printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
477         } else {
478                 printf("%-30s\n%s\n", user, &cmd[4]);
479         }
480
481         if (do_validate) {
482                 /* now set the access level */
483                 ax = intprompt("Access level", ax, 0, 6);
484                 sprintf(cmd, "VALI %s|%d", user, ax);
485                 serv_puts(cmd);
486                 serv_gets(cmd);
487                 if (cmd[0] != '2')
488                         printf("%s\n", &cmd[4]);
489         }
490         printf("\n");
491 }
492
493
494 void validate(void)
495 {                               /* validate new users */
496         char cmd[SIZ];
497         char buf[SIZ];
498         int finished = 0;
499
500         do {
501                 serv_puts("GNUR");
502                 serv_gets(cmd);
503                 if (cmd[0] != '3')
504                         finished = 1;
505                 if (cmd[0] == '2')
506                         printf("%s\n", &cmd[4]);
507                 if (cmd[0] == '3') {
508                         extract(buf, cmd, 0);
509                         val_user(&buf[4], 1);
510                 }
511         } while (finished == 0);
512 }
513
514 void subshell(void)
515 {
516         int a, b;
517         a = fork();
518         if (a == 0) {
519                 sttybbs(SB_RESTORE);
520                 signal(SIGINT, SIG_DFL);
521                 signal(SIGQUIT, SIG_DFL);
522                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
523                 printf("Could not open a shell: %s\n", strerror(errno));
524                 exit(errno);
525         }
526         do {
527                 b = ka_wait(NULL);
528         } while ((a != b) && (a != (-1)));
529         sttybbs(0);
530 }
531
532 /*
533  * <.A>ide <F>ile <D>elete command
534  */
535 void deletefile(void)
536 {
537         char filename[32];
538         char cmd[SIZ];
539
540         newprompt("Filename: ", filename, 31);
541         if (strlen(filename) == 0)
542                 return;
543         sprintf(cmd, "DELF %s", filename);
544         serv_puts(cmd);
545         serv_gets(cmd);
546         printf("%s\n", &cmd[4]);
547 }
548
549 /*
550  * <.A>ide <F>ile <S>end command
551  */
552 void netsendfile(void)
553 {
554         char filename[32], destsys[20], cmd[SIZ];
555
556         newprompt("Filename: ", filename, 31);
557         if (strlen(filename) == 0)
558                 return;
559         newprompt("System to send to: ", destsys, 19);
560         sprintf(cmd, "NETF %s|%s", filename, destsys);
561         serv_puts(cmd);
562         serv_gets(cmd);
563         printf("%s\n", &cmd[4]);
564         return;
565 }
566
567 /*
568  * <.A>ide <F>ile <M>ove command
569  */
570 void movefile(void)
571 {
572         char filename[64];
573         char newroom[ROOMNAMELEN];
574         char cmd[SIZ];
575
576         newprompt("Filename: ", filename, 63);
577         if (strlen(filename) == 0)
578                 return;
579         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
580
581         sprintf(cmd, "MOVF %s|%s", filename, newroom);
582         serv_puts(cmd);
583         serv_gets(cmd);
584         printf("%s\n", &cmd[4]);
585 }
586
587
588 /* 
589  * list of users who have filled out a bio
590  */
591 void list_bio(void)
592 {
593         char buf[SIZ];
594         int pos = 1;
595
596         serv_puts("LBIO");
597         serv_gets(buf);
598         if (buf[0] != '1') {
599                 pprintf("%s\n", &buf[4]);
600                 return;
601         }
602         while (serv_gets(buf), strcmp(buf, "000")) {
603                 if ((pos + strlen(buf) + 5) > screenwidth) {
604                         pprintf("\n");
605                         pos = 1;
606                 }
607                 pprintf("%s, ", buf);
608                 pos = pos + strlen(buf) + 2;
609         }
610         pprintf("%c%c  \n\n", 8, 8);
611 }
612
613
614 /*
615  * read bio
616  */
617 void read_bio(void)
618 {
619         char who[SIZ];
620         char buf[SIZ];
621
622         do {
623                 newprompt("Read bio for who ('?' for list) : ", who, 25);
624                 pprintf("\n");
625                 if (!strcmp(who, "?"))
626                         list_bio();
627         } while (!strcmp(who, "?"));
628         sprintf(buf, "RBIO %s", who);
629         serv_puts(buf);
630         serv_gets(buf);
631         if (buf[0] != '1') {
632                 pprintf("%s\n", &buf[4]);
633                 return;
634         }
635         while (serv_gets(buf), strcmp(buf, "000")) {
636                 pprintf("%s\n", buf);
637         }
638 }
639
640
641 /* 
642  * General system configuration command
643  */
644 void do_system_configuration(void)
645 {
646         char buf[SIZ];
647         char sc[28][SIZ];
648         int expire_mode = 0;
649         int expire_value = 0;
650         int a;
651         int logpages = 0;
652
653         /* Clear out the config buffers */
654         memset(&sc[0][0], 0, sizeof(sc));
655
656         /* Fetch the current config */
657         serv_puts("CONF get");
658         serv_gets(buf);
659         if (buf[0] == '1') {
660                 a = 0;
661                 while (serv_gets(buf), strcmp(buf, "000")) {
662                         if (a < 28)
663                                 strcpy(&sc[a][0], buf);
664                         ++a;
665                 }
666         }
667         /* Fetch the expire policy (this will silently fail on old servers,
668          * resulting in "default" policy)
669          */
670         serv_puts("GPEX site");
671         serv_gets(buf);
672         if (buf[0] == '2') {
673                 expire_mode = extract_int(&buf[4], 0);
674                 expire_value = extract_int(&buf[4], 1);
675         }
676
677
678         /* Identification parameters */
679
680         strprompt("Node name", &sc[0][0], 15);
681         strprompt("Fully qualified domain name", &sc[1][0], 63);
682         strprompt("Human readable node name", &sc[2][0], 20);
683         strprompt("Modem dialup number", &sc[3][0], 15);
684         strprompt("Geographic location of this system", &sc[12][0], 31);
685         strprompt("Name of system administrator", &sc[13][0], 25);
686         strprompt("Paginator prompt", &sc[10][0], 79);
687         /* strprompt("Default moderation filter for new users", &sc[25][0], 4); */
688
689         /* Security parameters */
690
691         sprintf(&sc[7][0], "%d", (boolprompt(
692                                     "Require registration for new users",
693                                                     atoi(&sc[7][0]))));
694         strprompt("Initial access level for new users", &sc[6][0], 1);
695         strprompt("Access level required to create rooms", &sc[19][0], 1);
696         sprintf(&sc[4][0], "%d", (boolprompt(
697                                                     "Automatically give room aide privs to a user who creates a private room",
698                                                     atoi(&sc[4][0]))));
699
700         sprintf(&sc[8][0], "%d", (boolprompt(
701                  "Automatically move problem user messages to twit room",
702                                                     atoi(&sc[8][0]))));
703
704         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
705         sprintf(&sc[11][0], "%d", (boolprompt(
706               "Restrict Internet mail to only those with that privilege",
707                                                      atoi(&sc[11][0]))));
708         sprintf(&sc[26][0], "%d", (boolprompt(
709               "Allow Aides to Zap (forget) rooms",
710                                                      atoi(&sc[26][0]))));
711
712         if (strlen(&sc[18][0]) > 0) logpages = 1;
713         else logpages = 0;
714         logpages = boolprompt("Log all pages", logpages);
715         if (logpages) {
716                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
717         }
718         else {
719                 sc[18][0] = 0;
720         }
721
722
723         /* Server tuning */
724
725         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
726         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
727         strprompt("Maximum message length", &sc[20][0], 20);
728         strprompt("Minimum number of worker threads", &sc[21][0], 3);
729         strprompt("Maximum number of worker threads", &sc[22][0], 3);
730         strprompt("Server-to-server networking password", &sc[15][0], 19);
731         strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
732         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
733         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
734
735
736         /* Expiry settings */
737         strprompt("Default user purge time (days)", &sc[16][0], 5);
738         strprompt("Default room purge time (days)", &sc[17][0], 5);
739
740         /* Angels and demons dancing in my head... */
741         do {
742                 sprintf(buf, "%d", expire_mode);
743                 strprompt("System default message expire policy (? for list)",
744                           buf, 1);
745                 if (buf[0] == '?') {
746                         printf("\n");
747                         printf("1. Never automatically expire messages\n");
748                         printf("2. Expire by message count\n");
749                         printf("3. Expire by message age\n");
750                 }
751         } while ((buf[0] < 49) || (buf[0] > 51));
752         expire_mode = buf[0] - 48;
753
754         /* ...lunatics and monsters underneath my bed */
755         if (expire_mode == 2) {
756                 sprintf(buf, "%d", expire_value);
757                 strprompt("Keep how many messages online?", buf, 10);
758                 expire_value = atol(buf);
759         }
760         if (expire_mode == 3) {
761                 sprintf(buf, "%d", expire_value);
762                 strprompt("Keep messages for how many days?", buf, 10);
763                 expire_value = atol(buf);
764         }
765         /* Save it */
766         printf("Save this configuration? ");
767         if (yesno()) {
768                 serv_puts("CONF set");
769                 serv_gets(buf);
770                 if (buf[0] == '4') {
771                         for (a = 0; a < 28; ++a)
772                                 serv_puts(&sc[a][0]);
773                         serv_puts("000");
774                 }
775                 snprintf(buf, sizeof buf, "SPEX site|%d|%d",
776                          expire_mode, expire_value);
777                 serv_puts(buf);
778                 serv_gets(buf);
779         }
780 }
781
782
783 /*
784  * support function for do_internet_configuration()
785  */
786 void get_inet_rec_type(char *buf) {
787         int sel;
788
789         keyopt(" <1> localhost       (Alias for this computer)\n");
790         keyopt(" <2> gateway domain  (Domain for all Citadel systems)\n");
791         keyopt(" <3> smart-host      (Forward all outbound mail to this host)\n");
792         sel = intprompt("Which one", 1, 1, 3);
793         switch(sel) {
794                 case 1: strcpy(buf, "localhost");
795                         return;
796                 case 2: strcpy(buf, "gatewaydomain");
797                         return;
798                 case 3: strcpy(buf, "smarthost");
799                         return;
800         }
801 }
802
803
804 /*
805  * Internet mail configuration
806  */
807 void do_internet_configuration(void) {
808         char buf[SIZ];
809         int num_recs = 0;
810         char **recs = NULL;
811         char ch;
812         int badkey;
813         int i, j;
814         int quitting = 0;
815         
816
817         sprintf(buf, "CONF getsys|%s", INTERNETCFG);
818         serv_puts(buf);
819         serv_gets(buf);
820         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
821                 ++num_recs;
822                 if (num_recs == 1) recs = malloc(sizeof(char *));
823                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
824                 recs[num_recs-1] = malloc(SIZ);
825                 strcpy(recs[num_recs-1], buf);
826         }
827
828         do {
829                 printf("\n");
830                 color(BRIGHT_WHITE);
831                 printf("### ");
832                 printf("                   Host or domain                  ");
833                 printf("   Record type      \n");
834                 color(DIM_WHITE);
835                 printf("--- ");
836                 printf("-------------------------------------------------- ");
837                 printf("--------------------\n");
838                 for (i=0; i<num_recs; ++i) {
839                 color(DIM_WHITE);
840                 printf("%3d ", i+1);
841                 extract(buf, recs[i], 0);
842                 color(BRIGHT_CYAN);
843                 printf("%-50s ", buf);
844                 extract(buf, recs[i], 1);
845                 color(BRIGHT_MAGENTA);
846                 printf("%-20s\n", buf);
847                 color(DIM_WHITE);
848                 }
849
850                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
851                 switch(ch) {
852                         case 'a':
853                                 ++num_recs;
854                                 if (num_recs == 1)
855                                         recs = malloc(sizeof(char *));
856                                 else recs = realloc(recs,
857                                         (sizeof(char *)) * num_recs);
858                                 newprompt("Enter host name: ",
859                                         buf, 50);
860                                 strcat(buf, "|");
861                                 get_inet_rec_type(&buf[strlen(buf)]);
862                                 recs[num_recs-1] = strdup(buf);
863                                 break;
864                         case 'd':
865                                 i = intprompt("Delete which one",
866                                         1, 1, num_recs) - 1;
867                                 free(recs[i]);
868                                 --num_recs;
869                                 for (j=i; j<num_recs; ++j)
870                                         recs[j] = recs[j+1];
871                                 break;
872                         case 's':
873                                 sprintf(buf, "CONF putsys|%s",
874                                         INTERNETCFG);
875                                 serv_puts(buf);
876                                 serv_gets(buf);
877                                 if (buf[0] == '4') {
878                                         for (i=0; i<num_recs; ++i) {
879                                                 serv_puts(recs[i]);
880                                         }
881                                         serv_puts("000");
882                                 }
883                                 else {
884                                         printf("%s\n", &buf[4]);
885                                 }
886                                 quitting = 1;
887                                 break;
888                         case 'q':
889                                 quitting = boolprompt(
890                                         "Quit without saving", 0);
891                                 break;
892                         default:
893                                 badkey = 1;
894                 }
895         } while (quitting == 0);
896
897         if (recs != NULL) {
898                 for (i=0; i<num_recs; ++i) free(recs[i]);
899                 free(recs);
900         }
901 }