fd9fda48906f510771e0d17b2e04906d9619eafb
[citadel.git] / citadel / messages.c
1 /*
2  * $Id$
3  *
4  * Citadel/UX message support routines
5  * see copyright.txt for copyright information
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <signal.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <sys/wait.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <stdarg.h>
23 #include "citadel.h"
24 #include "messages.h"
25 #include "commands.h"
26 #include "rooms.h"
27 #include "tools.h"
28 #ifndef HAVE_SNPRINTF
29 #include "snprintf.h"
30 #endif
31
32 #define MAXWORDBUF 256
33 #define MAXMSGS 512
34
35 struct cittext {
36         struct cittext *next;
37         char text[MAXWORDBUF];
38 };
39
40 void sttybbs(int cmd);
41 int fmout(int width, FILE * fp, char pagin, int height, int starting_lp,
42           char subst);
43 int haschar(char *st, int ch);
44 int checkpagin(int lp, int pagin, int height);
45 void getline(char *string, int lim);
46 void formout(char *name);
47 int yesno(void);
48 void newprompt(char *prompt, char *str, int len);
49 int file_checksum(char *filename);
50 void do_edit(char *desc, char *read_cmd, char *check_cmd, char *write_cmd);
51
52 char reply_to[512];
53 long msg_arr[MAXMSGS];
54 int num_msgs;
55 extern char room_name[];
56 extern unsigned room_flags;
57 extern long highest_msg_read;
58 extern struct CtdlServInfo serv_info;
59 extern char temp[];
60 extern char temp2[];
61 extern int screenwidth;
62 extern int screenheight;
63 extern long maxmsgnum;
64 extern char is_mail;
65 extern char is_aide;
66 extern char is_room_aide;
67 extern char fullname[];
68 extern char axlevel;
69 extern unsigned userflags;
70 extern char sigcaught;
71 extern char editor_path[];
72 extern char printcmd[];
73 extern int rc_allow_attachments;
74 extern int rc_display_message_numbers;
75 extern int rc_force_mail_prompts;
76
77 extern int editor_pid;
78
79 void ka_sigcatch(int signum)
80 {
81         char buf[256];
82         alarm(S_KEEPALIVE);
83         signal(SIGALRM, ka_sigcatch);
84         serv_puts("NOOP");
85         serv_gets(buf);
86 }
87
88
89 /*
90  * server keep-alive version of wait() (needed for external editor)
91  */
92 pid_t ka_wait(int *kstatus)
93 {
94         pid_t p;
95
96         alarm(S_KEEPALIVE);
97         signal(SIGALRM, ka_sigcatch);
98         do {
99                 errno = 0;
100                 p = wait(kstatus);
101         } while (errno == EINTR);
102         signal(SIGALRM, SIG_IGN);
103         alarm(0);
104         return (p);
105 }
106
107
108 /*
109  * version of system() that uses ka_wait()
110  */
111 int ka_system(char *shc)
112 {
113         pid_t childpid;
114         pid_t waitpid;
115         int retcode;
116
117         childpid = fork();
118         if (childpid < 0) {
119                 color(BRIGHT_RED);
120                 perror("Cannot fork");
121                 color(DIM_WHITE);
122                 return ((pid_t) childpid);
123         }
124
125         if (childpid == 0) {
126                 execlp("/bin/sh", "sh", "-c", shc, NULL);
127                 exit(127);
128         }
129
130         if (childpid > 0) {
131                 do {
132                         waitpid = ka_wait(&retcode);
133                 } while (waitpid != childpid);
134                 return (retcode);
135         }
136
137         return (-1);
138 }
139
140
141
142 /*
143  * add a newline to the buffer...
144  */
145 void add_newline(struct cittext *textlist)
146 {
147         struct cittext *ptr;
148
149         ptr = textlist;
150         while (ptr->next != NULL)
151                 ptr = ptr->next;
152
153         while (ptr->text[strlen(ptr->text) - 1] == 32)
154                 ptr->text[strlen(ptr->text) - 1] = 0;
155         /* strcat(ptr->text,"\n"); */
156
157         ptr->next = (struct cittext *)
158             malloc(sizeof(struct cittext));
159         ptr = ptr->next;
160         ptr->next = NULL;
161         strcpy(ptr->text, "");
162 }
163
164
165 /*
166  * add a word to the buffer...
167  */
168 void add_word(struct cittext *textlist, char *wordbuf)
169 {
170         struct cittext *ptr;
171
172         ptr = textlist;
173         while (ptr->next != NULL)
174                 ptr = ptr->next;
175
176         if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
177                 ptr->next = (struct cittext *)
178                     malloc(sizeof(struct cittext));
179                 ptr = ptr->next;
180                 ptr->next = NULL;
181                 strcpy(ptr->text, "");
182         }
183
184         strcat(ptr->text, wordbuf);
185         strcat(ptr->text, " ");
186 }
187
188
189 /*
190  * begin editing of an opened file pointed to by fp
191  */
192 void citedit(FILE * fp)
193 {
194         int a, prev, finished, b, last_space;
195         int appending = 0;
196         struct cittext *textlist = NULL;
197         struct cittext *ptr;
198         char wordbuf[MAXWORDBUF];
199
200         /* first, load the text into the buffer */
201         fseek(fp, 0L, 0);
202         textlist = (struct cittext *) malloc(sizeof(struct cittext));
203         textlist->next = NULL;
204         strcpy(textlist->text, "");
205
206         strcpy(wordbuf, "");
207         prev = (-1);
208         while (a = getc(fp), a >= 0) {
209                 appending = 1;
210                 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
211                         add_word(textlist, wordbuf);
212                         strcpy(wordbuf, "");
213                         if ((prev == 13) || (prev == 10)) {
214                                 add_word(textlist, "\n");
215                                 add_newline(textlist);
216                                 add_word(textlist, "");
217                         }
218                 } else {
219                         wordbuf[strlen(wordbuf) + 1] = 0;
220                         wordbuf[strlen(wordbuf)] = a;
221                 }
222                 if (strlen(wordbuf) + 3 > screenwidth) {
223                         add_word(textlist, wordbuf);
224                         strcpy(wordbuf, "");
225                 }
226                 prev = a;
227         }
228
229         /* get text */
230         finished = 0;
231         prev = (appending ? 13 : (-1));
232         strcpy(wordbuf, "");
233         async_ka_start();
234         do {
235                 a = inkey();
236                 if (a == 10)
237                         a = 13;
238                 if (a == 9)
239                         a = 32;
240                 if (a == 127)
241                         a = 8;
242
243         /******* new ***********/
244                 if ((a > 32) && (a < 127) && (prev == 13)) {
245                         add_word(textlist, "\n");
246                         printf(" ");
247                 }
248         /***********************/
249
250                 if ((a == 32) && (prev == 13)) {
251                         add_word(textlist, "\n");
252                         add_newline(textlist);
253                 }
254
255                 if (a == 8) {
256                         if (strlen(wordbuf) > 0) {
257                                 wordbuf[strlen(wordbuf) - 1] = 0;
258                                 putc(8, stdout);
259                                 putc(32, stdout);
260                                 putc(8, stdout);
261                         }
262                 } else if (a == 13) {
263                         printf("\n");
264                         if (strlen(wordbuf) == 0)
265                                 finished = 1;
266                         else {
267                                 for (b = 0; b < strlen(wordbuf); ++b)
268                                         if (wordbuf[b] == 32) {
269                                                 wordbuf[b] = 0;
270                                                 add_word(textlist,
271                                                          wordbuf);
272                                                 strcpy(wordbuf,
273                                                        &wordbuf[b + 1]);
274                                                 b = 0;
275                                         }
276                                 add_word(textlist, wordbuf);
277                                 strcpy(wordbuf, "");
278                         }
279                 } else {
280                         putc(a, stdout);
281                         wordbuf[strlen(wordbuf) + 1] = 0;
282                         wordbuf[strlen(wordbuf)] = a;
283                 }
284                 if ((strlen(wordbuf) + 3) > screenwidth) {
285                         last_space = (-1);
286                         for (b = 0; b < strlen(wordbuf); ++b)
287                                 if (wordbuf[b] == 32)
288                                         last_space = b;
289                         if (last_space >= 0) {
290                                 for (b = 0; b < strlen(wordbuf); ++b)
291                                         if (wordbuf[b] == 32) {
292                                                 wordbuf[b] = 0;
293                                                 add_word(textlist,
294                                                          wordbuf);
295                                                 strcpy(wordbuf,
296                                                        &wordbuf[b + 1]);
297                                                 b = 0;
298                                         }
299                                 for (b = 0; b < strlen(wordbuf); ++b) {
300                                         putc(8, stdout);
301                                         putc(32, stdout);
302                                         putc(8, stdout);
303                                 }
304                                 printf("\n%s", wordbuf);
305                         } else {
306                                 add_word(textlist, wordbuf);
307                                 strcpy(wordbuf, "");
308                                 printf("\n");
309                         }
310                 }
311                 prev = a;
312         } while (finished == 0);
313         async_ka_end();
314
315         /* write the buffer back to disk */
316         fseek(fp, 0L, 0);
317         for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
318                 fprintf(fp, "%s", ptr->text);
319         }
320         putc(10, fp);
321         fflush(fp);
322         ftruncate(fileno(fp), ftell(fp));
323
324         /* and deallocate the memory we used */
325         while (textlist != NULL) {
326                 ptr = textlist->next;
327                 free(textlist);
328                 textlist = ptr;
329         }
330 }
331
332 /* Read a message from the server
333  */
334 int read_message(
335         long num,   /* message number */
336         char pagin) /* 0 = normal read, 1 = read with pagination, 2 = header */
337 {
338         char buf[256];
339         char m_subject[256];
340         char from[256], node[256], rfca[256];
341         char now[256];
342         int format_type = 0;
343         int fr = 0;
344         int nhdr = 0;
345
346         sigcaught = 0;
347         sttybbs(1);
348
349         sprintf(buf, "MSG0 %ld|%d", num, (pagin == READ_HEADER ? 1 : 0));
350         serv_puts(buf);
351         serv_gets(buf);
352         if (buf[0] != '1') {
353                 printf("*** msg #%ld: %s\n", num, buf);
354                 ++lines_printed;
355                 lines_printed =
356                     checkpagin(lines_printed, pagin, screenheight);
357                 sttybbs(0);
358                 return (0);
359         }
360
361         strcpy(m_subject, "");
362         strcpy(reply_to, "nobody ... xxxxx");
363         strcpy(from, "");
364         strcpy(node, "");
365         strcpy(rfca, "");
366
367         printf("\n");
368         ++lines_printed;
369         lines_printed = checkpagin(lines_printed, pagin, screenheight);
370         printf(" ");
371         if (pagin == 1) {
372                 color(BRIGHT_CYAN);
373         }
374
375         if (pagin == 2) {
376                 while (serv_gets(buf), strcmp(buf, "000")) {
377                         if (buf[4] == '=') {
378                                 printf("%s\n", buf);
379                                 ++lines_printed;
380                                 lines_printed =
381                                     checkpagin(lines_printed,
382                                                pagin, screenheight);
383                         }
384                 }
385                 sttybbs(0);
386                 return (0);
387         }
388
389         while (serv_gets(buf), strncasecmp(buf, "text", 4)) {
390                 if (!strncasecmp(buf, "nhdr=yes", 8))
391                         nhdr = 1;
392                 if (!strncasecmp(buf, "from=", 5)) {
393                         strcpy(from, &buf[5]);
394                 }
395                 if (nhdr == 1)
396                         buf[0] = '_';
397                 if (!strncasecmp(buf, "type=", 5))
398                         format_type = atoi(&buf[5]);
399                 if ((!strncasecmp(buf, "msgn=", 5))
400                     && (rc_display_message_numbers)) {
401                         color(DIM_WHITE);
402                         printf("[");
403                         color(BRIGHT_WHITE);
404                         printf("#%s", &buf[5]);
405                         color(DIM_WHITE);
406                         printf("] ");
407                 }
408                 if (!strncasecmp(buf, "from=", 5)) {
409                         color(DIM_WHITE);
410                         printf("from ");
411                         color(BRIGHT_CYAN);
412                         printf("%s ", &buf[5]);
413                 }
414                 if (!strncasecmp(buf, "subj=", 5))
415                         strcpy(m_subject, &buf[5]);
416
417                 if (!strncasecmp(buf, "rfca=", 5)) {
418                         safestrncpy(rfca, &buf[5], sizeof(rfca) - 5);
419                         color(DIM_WHITE);
420                         printf("<");
421                         color(BRIGHT_BLUE);
422                         printf("%s", &buf[5]);
423                         color(DIM_WHITE);
424                         printf("> ");
425                 }
426                 if ((!strncasecmp(buf, "hnod=", 5))
427                     && (strcasecmp(&buf[5], serv_info.serv_humannode))
428                     && (strlen(rfca) == 0)) {
429                         color(DIM_WHITE);
430                         printf("(");
431                         color(BRIGHT_WHITE);
432                         printf("%s", &buf[5]);
433                         color(DIM_WHITE);
434                         printf(") ");
435                 }
436                 if ((!strncasecmp(buf, "room=", 5))
437                     && (strcasecmp(&buf[5], room_name))
438                     && (strlen(rfca) == 0)) {
439                         color(DIM_WHITE);
440                         printf("in ");
441                         color(BRIGHT_MAGENTA);
442                         printf("%s> ", &buf[5]);
443                 }
444
445                 if (!strncasecmp(buf, "node=", 5)) {
446                         safestrncpy(node, &buf[5], sizeof(buf) - 5);
447                         if ((room_flags & QR_NETWORK)
448                             ||
449                             ((strcasecmp
450                               (&buf[5], serv_info.serv_nodename)
451                               &&
452                               (strcasecmp(&buf[5], serv_info.serv_fqdn)))))
453                         {
454                                 if (strlen(rfca) == 0) {
455                                         color(DIM_WHITE);
456                                         printf("@");
457                                         color(BRIGHT_YELLOW);
458                                         printf("%s ", &buf[5]);
459                                 }
460                         }
461                 }
462
463                 if (!strncasecmp(buf, "rcpt=", 5)) {
464                         color(DIM_WHITE);
465                         printf("to ");
466                         color(BRIGHT_CYAN);
467                         printf("%s ", &buf[5]);
468                 }
469                 if (!strncasecmp(buf, "time=", 5)) {
470                         fmt_date(now, atol(&buf[5]));
471                         printf("%s ", now);
472                 }
473         }
474
475         if (nhdr == 1) {
476                 if (!is_room_aide) {
477                         printf(" ****");
478                 } else {
479                         printf(" %s", from);
480                 }
481         }
482         printf("\n");
483
484         if (strlen(rfca) > 0) {
485                 strcpy(reply_to, rfca);
486         } else {
487                 snprintf(reply_to, sizeof(reply_to), "%s @ %s", from,
488                          node);
489         }
490
491         if (pagin == 1)
492                 color(BRIGHT_WHITE);
493         ++lines_printed;
494         lines_printed = checkpagin(lines_printed, pagin, screenheight);
495
496         if (strlen(m_subject) > 0) {
497                 printf("Subject: %s\n", m_subject);
498                 ++lines_printed;
499                 lines_printed =
500                     checkpagin(lines_printed, pagin, screenheight);
501         }
502
503         if (format_type == 0) {
504                 fr = fmout(screenwidth, NULL,
505                            ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
506         } else {
507                 while (serv_gets(buf), strcmp(buf, "000")) {
508                         if (sigcaught == 0) {
509                                 printf("%s\n", buf);
510                                 lines_printed = lines_printed + 1 +
511                                     (strlen(buf) / screenwidth);
512                                 lines_printed =
513                                     checkpagin(lines_printed, pagin,
514                                                screenheight);
515                         }
516                 }
517                 fr = sigcaught;
518         }
519         printf("\n");
520         ++lines_printed;
521         lines_printed = checkpagin(lines_printed, pagin, screenheight);
522
523         if (pagin == 1)
524                 color(DIM_WHITE);
525         sttybbs(0);
526         return (fr);
527 }
528
529 /*
530  * replace string function for the built-in editor
531  */
532 void replace_string(char *filename, long int startpos)
533 {
534         char buf[512];
535         char srch_str[128];
536         char rplc_str[128];
537         FILE *fp;
538         int a;
539         long rpos, wpos;
540         char *ptr;
541         int substitutions = 0;
542         long msglen = 0L;
543
544         printf("Enter text to be replaced:\n: ");
545         getline(srch_str, 128);
546         if (strlen(srch_str) == 0)
547                 return;
548
549         printf("Enter text to replace it with:\n: ");
550         getline(rplc_str, 128);
551
552         fp = fopen(filename, "r+");
553         if (fp == NULL)
554                 return;
555
556         wpos = startpos;
557         fseek(fp, startpos, 0);
558         strcpy(buf, "");
559         while (a = getc(fp), a > 0) {
560                 ++msglen;
561                 buf[strlen(buf) + 1] = 0;
562                 buf[strlen(buf)] = a;
563                 if (strlen(buf) >= strlen(srch_str)) {
564                         ptr = (&buf[strlen(buf) - strlen(srch_str)]);
565                         if (!strncasecmp(ptr, srch_str, strlen(srch_str))) {
566                                 strcpy(ptr, rplc_str);
567                                 ++substitutions;
568                         }
569                 }
570                 if (strlen(buf) > 384) {
571                         rpos = ftell(fp);
572                         fseek(fp, wpos, 0);
573                         fwrite((char *) buf, 128, 1, fp);
574                         strcpy(buf, &buf[128]);
575                         wpos = ftell(fp);
576                         fseek(fp, rpos, 0);
577                 }
578         }
579         fseek(fp, wpos, 0);
580         if (strlen(buf) > 0)
581                 fwrite((char *) buf, strlen(buf), 1, fp);
582         wpos = ftell(fp);
583         fclose(fp);
584         truncate(filename, wpos);
585         printf("<R>eplace made %d substitution(s).\n\n", substitutions);
586 }
587
588 /*
589  * Function to begin composing a new message
590  */
591 int make_message(char *filename,        /* temporary file name */
592                  char *recipient,       /* NULL if it's not mail */
593                  int anon_type, /* see MES_ types in header file */
594                  int format_type, int mode)
595 {
596         FILE *fp;
597         int a, b, e_ex_code;
598         long beg;
599         char datestr[64];
600         int cksum = 0;
601
602         if (mode == 2)
603                 if (strlen(editor_path) == 0) {
604                         printf
605                             ("*** No editor available, using built-in editor\n");
606                         mode = 0;
607                 }
608
609         fmt_date(datestr, time(NULL));
610
611         if (room_flags & QR_ANONONLY) {
612                 printf(" ****");
613         } else {
614                 printf(" %s from %s", datestr, fullname);
615                 if (strlen(recipient) > 0)
616                         printf(" to %s", recipient);
617         }
618         printf("\n");
619
620         beg = 0L;
621
622         if (mode == 1)
623                 printf("(Press ctrl-d when finished)\n");
624         if (mode == 0) {
625                 fp = fopen(filename, "r");
626                 if (fp != NULL) {
627                         fmout(screenwidth, fp, 0, screenheight, 0, 0);
628                         beg = ftell(fp);
629                         fclose(fp);
630                 } else {
631                         fp = fopen(filename, "w");
632                         fclose(fp);
633                 }
634         }
635
636 ME1:    switch (mode) {
637
638         case 0:
639                 fp = fopen(filename, "r+");
640                 citedit(fp);
641                 fclose(fp);
642                 goto MECR;
643
644         case 1:
645                 fp = fopen(filename, "w");
646                 do {
647                         a = inkey();
648                         if (a == 255)
649                                 a = 32;
650                         if (a == 13)
651                                 a = 10;
652                         if (a != 4) {
653                                 putc(a, fp);
654                                 putc(a, stdout);
655                         }
656                         if (a == 10)
657                                 putc(13, stdout);
658                 } while (a != 4);
659                 fclose(fp);
660                 break;
661
662         case 2:
663                 e_ex_code = 1;  /* start with a failed exit code */
664                 editor_pid = fork();
665                 cksum = file_checksum(filename);
666                 if (editor_pid == 0) {
667                         chmod(filename, 0600);
668                         sttybbs(SB_RESTORE);
669                         execlp(editor_path, editor_path, filename, NULL);
670                         exit(1);
671                 }
672                 if (editor_pid > 0)
673                         do {
674                                 e_ex_code = 0;
675                                 b = ka_wait(&e_ex_code);
676                         } while ((b != editor_pid) && (b >= 0));
677                 editor_pid = (-1);
678                 sttybbs(0);
679                 break;
680         }
681
682 MECR:   if (mode == 2) {
683                 if (file_checksum(filename) == cksum) {
684                         printf("*** Aborted message.\n");
685                         e_ex_code = 1;
686                 }
687                 if (e_ex_code == 0)
688                         goto MEFIN;
689                 goto MEABT2;
690         }
691
692         b = keymenu("Entry command (? for options)",
693                     "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
694                     "<R>eplace string|<H>old message");
695
696         if (b == 'a')
697                 goto MEABT;
698         if (b == 'c')
699                 goto ME1;
700         if (b == 's')
701                 goto MEFIN;
702         if (b == 'p') {
703                 printf(" %s from %s", datestr, fullname);
704                 if (strlen(recipient) > 0)
705                         printf(" to %s", recipient);
706                 printf("\n");
707                 fp = fopen(filename, "r");
708                 if (fp != NULL) {
709                         fmout(screenwidth, fp,
710                               ((userflags & US_PAGINATOR) ? 1 : 0),
711                               screenheight, 0, 0);
712                         beg = ftell(fp);
713                         fclose(fp);
714                 }
715                 goto MECR;
716         }
717         if (b == 'r') {
718                 replace_string(filename, 0L);
719                 goto MECR;
720         }
721         if (b == 'h') {
722                 return (2);
723         }
724
725 MEFIN:  return (0);
726
727 MEABT:  printf("Are you sure? ");
728         if (yesno() == 0) {
729                 goto ME1;
730         }
731 MEABT2: unlink(filename);
732         return (2);
733 }
734
735 /*
736  * Transmit message text to the server.
737  * 
738  * This loop also implements a "tick" counter that displays the progress, if
739  * we're sending something that will take a long time to transmit.
740  */
741 void transmit_message(FILE *fp)
742 {
743         char buf[256];
744         int ch, a;
745         long msglen;
746         time_t lasttick;
747
748         fseek(fp, 0L, SEEK_END);
749         msglen = ftell(fp);
750         rewind(fp);
751         lasttick = time(NULL);
752         strcpy(buf, "");
753         while (ch = getc(fp), (ch >= 0)) {
754                 if (ch == 10) {
755                         if (!strcmp(buf, "000"))
756                                 strcpy(buf, ">000");
757                         serv_puts(buf);
758                         strcpy(buf, "");
759                 } else {
760                         a = strlen(buf);
761                         buf[a + 1] = 0;
762                         buf[a] = ch;
763                         if ((ch == 32) && (strlen(buf) > 200)) {
764                                 buf[a] = 0;
765                                 if (!strcmp(buf, "000"))
766                                         strcpy(buf, ">000");
767                                 serv_puts(buf);
768                                 strcpy(buf, "");
769                         }
770                         if (strlen(buf) > 250) {
771                                 if (!strcmp(buf, "000"))
772                                         strcpy(buf, ">000");
773                                 serv_puts(buf);
774                                 strcpy(buf, "");
775                         }
776                 }
777
778                 if ((time(NULL) - lasttick) > 2L) {
779                         printf(" %3ld%% completed\r",
780                                ((ftell(fp) * 100L) / msglen));
781                         fflush(stdout);
782                         lasttick = time(NULL);
783                 }
784
785         }
786         serv_puts(buf);
787         printf("                \r");
788         fflush(stdout);
789 }
790
791
792
793 /*
794  * entmsg()  -  edit and create a message
795  *              returns 0 if message was saved
796  */
797 int entmsg(int is_reply,        /* nonzero if this was a <R>eply command */
798                 int c)          /* */
799 {
800         char buf[300];
801         char cmd[256];
802         int a, b;
803         int need_recp = 0;
804         int mode;
805         long highmsg;
806         FILE *fp;
807
808         if (c > 0)
809                 mode = 1;
810         else
811                 mode = 0;
812
813         /*
814          * First, check to see if we have permission to enter a message in
815          * this room.  The server will return an error code if we can't.
816          */
817         sprintf(cmd, "ENT0 0||0|%d", mode);
818         serv_puts(cmd);
819         serv_gets(cmd);
820
821         if ((strncmp(cmd, "570", 3)) && (strncmp(cmd, "200", 3))) {
822                 printf("%s\n", &cmd[4]);
823                 return (1);
824         }
825
826         /* Error code 570 is special.  It means that we CAN enter a message
827          * in this room, but a recipient needs to be specified.
828          */
829         need_recp = 0;
830         if (!strncmp(cmd, "570", 3))
831                 need_recp = 1;
832
833         /* If the user is a dumbass, tell them how to type. */
834         if ((userflags & US_EXPERT) == 0)
835                 formout("entermsg");
836
837         /* Handle the selection of a recipient, if necessary. */
838         strcpy(buf, "");
839         if (need_recp == 1) {
840                 if (axlevel >= 2) {
841                         if (is_reply) {
842                                 strcpy(buf, reply_to);
843                         } else {
844                                 printf("Enter recipient: ");
845                                 getline(buf, 60);
846                                 if (strlen(buf) == 0)
847                                         return (1);
848                         }
849                 } else
850                         strcpy(buf, "sysop");
851         }
852
853         b = 0;
854         if (room_flags & QR_ANONOPT) {
855                 printf("Anonymous (Y/N)? ");
856                 if (yesno() == 1)
857                         b = 1;
858         }
859
860         /* If it's mail, we've got to check the validity of the recipient... */
861         if (strlen(buf) > 0) {
862                 sprintf(cmd, "ENT0 0|%s|%d|%d", buf, b, mode);
863                 serv_puts(cmd);
864                 serv_gets(cmd);
865                 if (cmd[0] != '2') {
866                         printf("%s\n", &cmd[4]);
867                         return (1);
868                 }
869         }
870
871         /* Learn the number of the newest message in in the room, so we can
872          * tell upon saving whether someone else has posted too.
873          */
874         num_msgs = 0;
875         serv_puts("MSGS LAST|1");
876         serv_gets(cmd);
877         if (cmd[0] != '1') {
878                 printf("%s\n", &cmd[5]);
879         } else {
880                 while (serv_gets(cmd), strcmp(cmd, "000")) {
881                         msg_arr[num_msgs++] = atol(cmd);
882                 }
883         }
884
885         /* Now compose the message... */
886         if (make_message(temp, buf, b, 0, c) != 0) {
887                 return (2);
888         }
889
890         /* ...and transmit it to the server. */
891         sprintf(cmd, "ENT0 1|%s|%d|%d||", buf, b, mode);
892         serv_puts(cmd);
893         serv_gets(cmd);
894         if (cmd[0] != '4') {
895                 printf("%s\n", &cmd[4]);
896                 return (1);
897         }
898         fp = fopen(temp, "r");
899         if (fp != NULL) {
900                 transmit_message(fp);
901                 fclose(fp);
902         }
903         serv_puts("000");
904         unlink(temp);
905
906         highmsg = msg_arr[num_msgs - 1];
907         num_msgs = 0;
908         serv_puts("MSGS NEW");
909         serv_gets(cmd);
910         if (cmd[0] != '1') {
911                 printf("%s\n", &cmd[5]);
912         } else {
913                 while (serv_gets(cmd), strcmp(cmd, "000")) {
914                         msg_arr[num_msgs++] = atol(cmd);
915                 }
916         }
917
918         /* get new highest message number in room to set lrp for goto... */
919         maxmsgnum = msg_arr[num_msgs - 1];
920
921         /* now see if anyone else has posted in here */
922         b = (-1);
923         for (a = 0; a < num_msgs; ++a)
924                 if (msg_arr[a] > highmsg)
925                         ++b;
926
927         /* in the Mail> room, this algorithm always counts one message
928          * higher than in public rooms, so we decrement it by one */
929         if (need_recp)
930                 --b;
931
932         if (b == 1) {
933                 printf("*** 1 additional message has been entered "
934                         "in this room by another user.\n");
935         }
936         else if (b > 1) {
937                 printf("*** %d additional messages have been entered "
938                         "in this room by other users.\n", b);
939         }
940
941         return(0);
942 }
943
944 /*
945  * Do editing on a quoted file
946  */
947 void process_quote(void)
948 {
949         FILE *qfile, *tfile;
950         char buf[128];
951         int line, qstart, qend;
952
953         /* Unlink the second temp file as soon as it's opened, so it'll get
954          * deleted even if the program dies
955          */
956         qfile = fopen(temp2, "r");
957         unlink(temp2);
958
959         /* Display the quotable text with line numbers added */
960         line = 0;
961         fgets(buf, 128, qfile);
962         while (fgets(buf, 128, qfile) != NULL) {
963                 printf("%2d %s", ++line, buf);
964         }
965         printf("Begin quoting at [ 1] : ");
966         getline(buf, 3);
967         qstart = (buf[0] == 0) ? (1) : atoi(buf);
968         printf("  End quoting at [%d] : ", line);
969         getline(buf, 3);
970         qend = (buf[0] == 0) ? (line) : atoi(buf);
971         rewind(qfile);
972         line = 0;
973         fgets(buf, 128, qfile);
974         tfile = fopen(temp, "w");
975         while (fgets(buf, 128, qfile) != NULL) {
976                 if ((++line >= qstart) && (line <= qend))
977                         fprintf(tfile, " >%s", buf);
978         }
979         fprintf(tfile, " \n");
980         fclose(qfile);
981         fclose(tfile);
982         chmod(temp, 0666);
983 }
984
985
986
987 /*
988  * List the URL's which were embedded in the previous message
989  */
990 void list_urls()
991 {
992         int i;
993         char cmd[256];
994
995         if (num_urls == 0) {
996                 printf("There were no URL's in the previous message.\n\n");
997                 return;
998         }
999
1000         for (i = 0; i < num_urls; ++i) {
1001                 printf("%3d %s\n", i + 1, urls[i]);
1002         }
1003
1004         if ((i = num_urls) != 1)
1005                 i = intprompt("Display which one", 1, 1, num_urls);
1006
1007         sprintf(cmd, rc_url_cmd, urls[i - 1]);
1008         system(cmd);
1009         printf("\n");
1010 }
1011
1012 /*
1013  * Read the messages in the current room
1014  */
1015 void readmsgs(
1016         int c,          /* 0=Read all  1=Read new  2=Read old 3=Read last q */
1017         int rdir,       /* 1=Forward (-1)=Reverse */
1018         int q           /* Number of msgs to read (if c==3) */
1019 ) {
1020         int a, b, e, f, g, start;
1021         int savedpos;
1022         int hold_sw = 0;
1023         char arcflag = 0;
1024         char quotflag = 0;
1025         int hold_color = 0;
1026         char prtfile[PATH_MAX];
1027         char pagin;
1028         char cmd[256];
1029         char targ[ROOMNAMELEN];
1030         char filename[256];
1031
1032         if (c < 0)
1033                 b = (MAXMSGS - 1);
1034         else
1035                 b = 0;
1036
1037         strcpy(prtfile, tmpnam(NULL));
1038
1039         num_msgs = 0;
1040         strcpy(cmd, "MSGS ");
1041         switch (c) {
1042         case 0:
1043                 strcat(cmd, "ALL");
1044                 break;
1045         case 1:
1046                 strcat(cmd, "NEW");
1047                 break;
1048         case 2:
1049                 strcat(cmd, "OLD");
1050                 break;
1051         case 3:
1052                 sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1053                 break;
1054         }
1055         serv_puts(cmd);
1056         serv_gets(cmd);
1057         if (cmd[0] != '1') {
1058                 printf("%s\n", &cmd[5]);
1059         } else {
1060                 while (serv_gets(cmd), strcmp(cmd, "000")) {
1061                         if (num_msgs == MAXMSGS) {
1062                                 memcpy(&msg_arr[0], &msg_arr[1],
1063                                        (sizeof(long) * (MAXMSGS - 1)));
1064                                 --num_msgs;
1065                         }
1066                         msg_arr[num_msgs++] = atol(cmd);
1067                 }
1068         }
1069
1070         lines_printed = 0;
1071
1072         /* this loop cycles through each message... */
1073         start = ((rdir == 1) ? 0 : (num_msgs - 1));
1074         for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1075                 while (msg_arr[a] == 0L) {
1076                         a = a + rdir;
1077                         if ((a == MAXMSGS) || (a == (-1)))
1078                                 return;
1079                 }
1080
1081 RAGAIN:         pagin = ((arcflag == 0)
1082                          && (quotflag == 0)
1083                          && (userflags & US_PAGINATOR)) ? 1 : 0;
1084
1085                 /* If we're doing a quote, set the screenwidth to 72 */
1086                 if (quotflag) {
1087                         hold_sw = screenwidth;
1088                         screenwidth = 72;
1089                 }
1090
1091                 /* If printing or archiving, set the screenwidth to 80 */
1092                 if (arcflag) {
1093                         hold_sw = screenwidth;
1094                         screenwidth = 80;
1095                 }
1096
1097                 /* now read the message... */
1098                 e = read_message(msg_arr[a], pagin);
1099
1100                 /* ...and set the screenwidth back if we have to */
1101                 if ((quotflag) || (arcflag)) {
1102                         screenwidth = hold_sw;
1103                 }
1104 RMSGREAD:       fflush(stdout);
1105                 highest_msg_read = msg_arr[a];
1106                 if (quotflag) {
1107                         freopen("/dev/tty", "r+", stdout);
1108                         quotflag = 0;
1109                         enable_color = hold_color;
1110                         process_quote();
1111                 }
1112                 if (arcflag) {
1113                         freopen("/dev/tty", "r+", stdout);
1114                         arcflag = 0;
1115                         enable_color = hold_color;
1116                         f = fork();
1117                         if (f == 0) {
1118                                 freopen(prtfile, "r", stdin);
1119                                 sttybbs(SB_RESTORE);
1120                                 ka_system(printcmd);
1121                                 sttybbs(SB_NO_INTR);
1122                                 unlink(prtfile);
1123                                 exit(0);
1124                         }
1125                         if (f > 0)
1126                                 do {
1127                                         g = wait(NULL);
1128                                 } while ((g != f) && (g >= 0));
1129                         printf("Message printed.\n");
1130                 }
1131                 if (e == 3)
1132                         return;
1133                 if (((userflags & US_NOPROMPT) || (e == 2))
1134                     && (((room_flags & QR_MAILBOX) == 0)
1135                         || (rc_force_mail_prompts == 0))) {
1136                         e = 'n';
1137                 } else {
1138                         color(DIM_WHITE);
1139                         printf("(");
1140                         color(BRIGHT_WHITE);
1141                         printf("%d", num_msgs - a - 1);
1142                         color(DIM_WHITE);
1143                         printf(") ");
1144
1145                         keyopt
1146                             ("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1147                         if (rc_url_cmd[0] && num_urls)
1148                                 keyopt("<U>RL View ");
1149                         keyopt("<?>Help/others -> ");
1150
1151                         do {
1152                                 lines_printed = 2;
1153                                 e = (inkey() & 127);
1154                                 e = tolower(e);
1155 /* return key same as <N> */ if (e == 13)
1156                                         e = 'n';
1157 /* space key same as <N> */ if (e == 32)
1158                                         e = 'n';
1159 /* del/move for aides only */
1160                                     if ((!is_room_aide)
1161                                         && ((room_flags & QR_MAILBOX) ==
1162                                             0)) {
1163                                         if ((e == 'd') || (e == 'm')
1164                                             || (e == 'c'))
1165                                                 e = 0;
1166                                 }
1167 /* print only if available */
1168                                 if ((e == 'p') && (strlen(printcmd) == 0))
1169                                         e = 0;
1170 /* can't file if not allowed */
1171                                     if ((e == 'f')
1172                                         && (rc_allow_attachments == 0))
1173                                         e = 0;
1174 /* link only if browser avail*/
1175                                     if ((e == 'u')
1176                                         && (strlen(rc_url_cmd) == 0))
1177                                         e = 0;
1178                         } while ((e != 'a') && (e != 'n') && (e != 's')
1179                                  && (e != 'd') && (e != 'm') && (e != 'p')
1180                                  && (e != 'q') && (e != 'b') && (e != 'h')
1181                                  && (e != 'r') && (e != 'f') && (e != '?')
1182                                  && (e != 'u') && (e != 'c'));
1183                         switch (e) {
1184                         case 's':
1185                                 printf("Stop\r");
1186                                 break;
1187                         case 'a':
1188                                 printf("Again\r");
1189                                 break;
1190                         case 'd':
1191                                 printf("Delete\r");
1192                                 break;
1193                         case 'm':
1194                                 printf("Move\r");
1195                                 break;
1196                         case 'c':
1197                                 printf("Copy\r");
1198                                 break;
1199                         case 'n':
1200                                 printf("Next\r");
1201                                 break;
1202                         case 'p':
1203                                 printf("Print\r");
1204                                 break;
1205                         case 'q':
1206                                 printf("Quote\r");
1207                                 break;
1208                         case 'b':
1209                                 printf("Back\r");
1210                                 break;
1211                         case 'h':
1212                                 printf("Header\r");
1213                                 break;
1214                         case 'r':
1215                                 printf("Reply\r");
1216                                 break;
1217                         case 'f':
1218                                 printf("File\r");
1219                                 break;
1220                         case 'u':
1221                                 printf("URL's\r");
1222                                 break;
1223                         case '?':
1224                                 printf("? <help>\r");
1225                                 break;
1226                         }
1227                         if (userflags & US_DISAPPEAR)
1228                                 printf("\r%79s\r", "");
1229                         else
1230                                 printf("\n");
1231                         fflush(stdout);
1232                 }
1233                 switch (e) {
1234                 case '?':
1235                         printf("Options available here:\n");
1236                         printf(" ?  Help (prints this message)\n");
1237                         printf(" S  Stop reading immediately\n");
1238                         printf(" A  Again (repeats last message)\n");
1239                         printf(" N  Next (continue with next message)\n");
1240                         printf(" B  Back (go back to previous message)\n");
1241                         if ((is_room_aide)
1242                             || (room_flags & QR_MAILBOX)) {
1243                                 printf(" D  Delete this message\n");
1244                                 printf
1245                                     (" M  Move message to another room\n");
1246                                 printf
1247                                     (" C  Copy message to another room\n");
1248                         }
1249                         if (strlen(printcmd) > 0)
1250                                 printf(" P  Print this message\n");
1251                         printf
1252                             (" Q  Quote portions of this message for your next post\n");
1253                         printf
1254                             (" H  Headers (display message headers only)\n");
1255                         if (is_mail)
1256                                 printf(" R  Reply to this message\n");
1257                         if (rc_allow_attachments)
1258                                 printf
1259                                     (" F  (save attachments to a file)\n");
1260                         if (strlen(rc_url_cmd) > 0)
1261                                 printf(" U  (list URL's for display)\n");
1262                         printf("\n");
1263                         goto RMSGREAD;
1264                 case 'p':
1265                         fflush(stdout);
1266                         freopen(prtfile, "w", stdout);
1267                         arcflag = 1;
1268                         hold_color = enable_color;
1269                         enable_color = 0;
1270                         goto RAGAIN;
1271                 case 'q':
1272                         fflush(stdout);
1273                         freopen(temp2, "w", stdout);
1274                         quotflag = 1;
1275                         hold_color = enable_color;
1276                         enable_color = 0;
1277                         goto RAGAIN;
1278                 case 's':
1279                         return;
1280                 case 'a':
1281                         goto RAGAIN;
1282                 case 'b':
1283                         a = a - (rdir * 2);
1284                         break;
1285                 case 'm':
1286                 case 'c':
1287                         newprompt("Enter target room: ",
1288                                   targ, ROOMNAMELEN - 1);
1289                         if (strlen(targ) > 0) {
1290                                 sprintf(cmd, "MOVE %ld|%s|%d",
1291                                         msg_arr[a], targ,
1292                                         (e == 'c' ? 1 : 0));
1293                                 serv_puts(cmd);
1294                                 serv_gets(cmd);
1295                                 printf("%s\n", &cmd[4]);
1296                                 if (cmd[0] == '2')
1297                                         msg_arr[a] = 0L;
1298                         } else {
1299                                 goto RMSGREAD;
1300                         }
1301                         if (cmd[0] != '2')
1302                                 goto RMSGREAD;
1303                         break;
1304                 case 'f':
1305                         newprompt("Which section? ", filename,
1306                                   ((sizeof filename) - 1));
1307                         snprintf(cmd, sizeof cmd,
1308                                  "OPNA %ld|%s", msg_arr[a], filename);
1309                         serv_puts(cmd);
1310                         serv_gets(cmd);
1311                         if (cmd[0] == '2') {
1312                                 extract(filename, &cmd[4], 2);
1313                                 download_to_local_disk(filename,
1314                                                        extract_int(&cmd[4],
1315                                                                    0));
1316                         } else {
1317                                 printf("%s\n", &cmd[4]);
1318                         }
1319                         goto RMSGREAD;
1320                 case 'd':
1321                         printf("*** Delete this message? ");
1322                         if (yesno() == 1) {
1323                                 sprintf(cmd, "DELE %ld", msg_arr[a]);
1324                                 serv_puts(cmd);
1325                                 serv_gets(cmd);
1326                                 printf("%s\n", &cmd[4]);
1327                                 if (cmd[0] == '2')
1328                                         msg_arr[a] = 0L;
1329                         } else {
1330                                 goto RMSGREAD;
1331                         }
1332                         break;
1333                 case 'h':
1334                         read_message(msg_arr[a], READ_HEADER);
1335                         goto RMSGREAD;
1336                 case 'r':
1337                         savedpos = num_msgs;
1338                         entmsg(1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1339                         num_msgs = savedpos;
1340                         goto RMSGREAD;
1341                 case 'u':
1342                         list_urls();
1343                         goto RMSGREAD;
1344                 }
1345         }                       /* end for loop */
1346 }                               /* end read routine */
1347
1348
1349
1350
1351 /*
1352  * View and edit a system message
1353  */
1354 void edit_system_message(char *which_message)
1355 {
1356         char desc[64];
1357         char read_cmd[64];
1358         char write_cmd[64];
1359
1360         sprintf(desc, "system message '%s'", which_message);
1361         sprintf(read_cmd, "MESG %s", which_message);
1362         sprintf(write_cmd, "EMSG %s", which_message);
1363         do_edit(desc, read_cmd, "NOOP", write_cmd);
1364 }
1365
1366
1367
1368
1369 /*
1370  * Verify the message base
1371  */
1372 void check_message_base(void)
1373 {
1374         char buf[256];
1375
1376         printf
1377             ("Please read the documentation before running this command.\n"
1378             "Having done so, do you still want to check the message base? ");
1379         if (yesno() == 0)
1380                 return;
1381
1382         serv_puts("FSCK");
1383         serv_gets(buf);
1384         if (buf[0] != '1') {
1385                 printf("%s\n", &buf[4]);
1386                 return;
1387         }
1388
1389         while (serv_gets(buf), strcmp(buf, "000")) {
1390                 printf("%s\n", buf);
1391         }
1392 }