778f25ae1a14920d8c4c2426fdd0c0120bba1843
[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,
595                 int mode)
596 {
597         FILE *fp;
598         int a, b, e_ex_code;
599         long beg;
600         char datestr[64];
601         int cksum = 0;
602
603         if (mode == 2)
604                 if (strlen(editor_path) == 0) {
605                         printf
606                             ("*** No editor available, using built-in editor\n");
607                         mode = 0;
608                 }
609
610         fmt_date(datestr, time(NULL));
611
612         if (room_flags & QR_ANONONLY) {
613                 printf(" ****");
614         }
615         else {
616                 printf(" %s from %s", datestr, fullname);
617                 if (strlen(recipient) > 0) {
618                         printf(" to %s", recipient);
619                 }
620         }
621         printf("\n");
622
623         beg = 0L;
624
625         if (mode == 1) {
626                 printf("(Press ctrl-d when finished)\n");
627         }
628         if (mode == 0) {
629                 fp = fopen(filename, "r");
630                 if (fp != NULL) {
631                         fmout(screenwidth, fp, 0, screenheight, 0, 0);
632                         beg = ftell(fp);
633                         fclose(fp);
634                 } else {
635                         fp = fopen(filename, "w");
636                         if (fp == NULL) {
637                                 printf("*** Error opening temp file!\n"
638                                         "    %s: %s\n",
639                                         filename, strerror(errno));
640                         return(1);
641                         }
642                         fclose(fp);
643                 }
644         }
645
646 ME1:    switch (mode) {
647
648         case 0:
649                 fp = fopen(filename, "r+");
650                 if (fp == NULL) {
651                         printf("*** Error opening temp file!\n"
652                                 "    %s: %s\n",
653                                 filename, strerror(errno));
654                         return(1);
655                 }
656                 citedit(fp);
657                 fclose(fp);
658                 goto MECR;
659
660         case 1:
661                 fp = fopen(filename, "w");
662                 if (fp == NULL) {
663                         printf("*** Error opening temp file!\n"
664                                 "    %s: %s\n",
665                                 filename, strerror(errno));
666                         return(1);
667                 }
668                 do {
669                         a = inkey();
670                         if (a == 255)
671                                 a = 32;
672                         if (a == 13)
673                                 a = 10;
674                         if (a != 4) {
675                                 putc(a, fp);
676                                 putc(a, stdout);
677                         }
678                         if (a == 10)
679                                 putc(13, stdout);
680                 } while (a != 4);
681                 fclose(fp);
682                 break;
683
684         case 2:
685                 e_ex_code = 1;  /* start with a failed exit code */
686                 editor_pid = fork();
687                 cksum = file_checksum(filename);
688                 if (editor_pid == 0) {
689                         chmod(filename, 0600);
690                         sttybbs(SB_RESTORE);
691                         execlp(editor_path, editor_path, filename, NULL);
692                         exit(1);
693                 }
694                 if (editor_pid > 0)
695                         do {
696                                 e_ex_code = 0;
697                                 b = ka_wait(&e_ex_code);
698                         } while ((b != editor_pid) && (b >= 0));
699                 editor_pid = (-1);
700                 sttybbs(0);
701                 break;
702         }
703
704 MECR:   if (mode == 2) {
705                 if (file_checksum(filename) == cksum) {
706                         printf("*** Aborted message.\n");
707                         e_ex_code = 1;
708                 }
709                 if (e_ex_code == 0)
710                         goto MEFIN;
711                 goto MEABT2;
712         }
713
714         b = keymenu("Entry command (? for options)",
715                     "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
716                     "<R>eplace string|<H>old message");
717
718         if (b == 'a')
719                 goto MEABT;
720         if (b == 'c')
721                 goto ME1;
722         if (b == 's')
723                 goto MEFIN;
724         if (b == 'p') {
725                 printf(" %s from %s", datestr, fullname);
726                 if (strlen(recipient) > 0)
727                         printf(" to %s", recipient);
728                 printf("\n");
729                 fp = fopen(filename, "r");
730                 if (fp != NULL) {
731                         fmout(screenwidth, fp,
732                               ((userflags & US_PAGINATOR) ? 1 : 0),
733                               screenheight, 0, 0);
734                         beg = ftell(fp);
735                         fclose(fp);
736                 }
737                 goto MECR;
738         }
739         if (b == 'r') {
740                 replace_string(filename, 0L);
741                 goto MECR;
742         }
743         if (b == 'h') {
744                 return (2);
745         }
746
747 MEFIN:  return (0);
748
749 MEABT:  printf("Are you sure? ");
750         if (yesno() == 0) {
751                 goto ME1;
752         }
753 MEABT2: unlink(filename);
754         return (2);
755 }
756
757 /*
758  * Transmit message text to the server.
759  * 
760  * This loop also implements a "tick" counter that displays the progress, if
761  * we're sending something that will take a long time to transmit.
762  */
763 void transmit_message(FILE *fp)
764 {
765         char buf[256];
766         int ch, a;
767         long msglen;
768         time_t lasttick;
769
770         fseek(fp, 0L, SEEK_END);
771         msglen = ftell(fp);
772         rewind(fp);
773         lasttick = time(NULL);
774         strcpy(buf, "");
775         while (ch = getc(fp), (ch >= 0)) {
776                 if (ch == 10) {
777                         if (!strcmp(buf, "000"))
778                                 strcpy(buf, ">000");
779                         serv_puts(buf);
780                         strcpy(buf, "");
781                 } else {
782                         a = strlen(buf);
783                         buf[a + 1] = 0;
784                         buf[a] = ch;
785                         if ((ch == 32) && (strlen(buf) > 200)) {
786                                 buf[a] = 0;
787                                 if (!strcmp(buf, "000"))
788                                         strcpy(buf, ">000");
789                                 serv_puts(buf);
790                                 strcpy(buf, "");
791                         }
792                         if (strlen(buf) > 250) {
793                                 if (!strcmp(buf, "000"))
794                                         strcpy(buf, ">000");
795                                 serv_puts(buf);
796                                 strcpy(buf, "");
797                         }
798                 }
799
800                 if ((time(NULL) - lasttick) > 2L) {
801                         printf(" %3ld%% completed\r",
802                                ((ftell(fp) * 100L) / msglen));
803                         fflush(stdout);
804                         lasttick = time(NULL);
805                 }
806
807         }
808         serv_puts(buf);
809         printf("                \r");
810         fflush(stdout);
811 }
812
813
814
815 /*
816  * entmsg()  -  edit and create a message
817  *              returns 0 if message was saved
818  */
819 int entmsg(int is_reply,        /* nonzero if this was a <R>eply command */
820                 int c)          /* */
821 {
822         char buf[300];
823         char cmd[256];
824         int a, b;
825         int need_recp = 0;
826         int mode;
827         long highmsg;
828         FILE *fp;
829
830         if (c > 0)
831                 mode = 1;
832         else
833                 mode = 0;
834
835         /*
836          * First, check to see if we have permission to enter a message in
837          * this room.  The server will return an error code if we can't.
838          */
839         sprintf(cmd, "ENT0 0||0|%d", mode);
840         serv_puts(cmd);
841         serv_gets(cmd);
842
843         if ((strncmp(cmd, "570", 3)) && (strncmp(cmd, "200", 3))) {
844                 printf("%s\n", &cmd[4]);
845                 return (1);
846         }
847
848         /* Error code 570 is special.  It means that we CAN enter a message
849          * in this room, but a recipient needs to be specified.
850          */
851         need_recp = 0;
852         if (!strncmp(cmd, "570", 3))
853                 need_recp = 1;
854
855         /* If the user is a dumbass, tell them how to type. */
856         if ((userflags & US_EXPERT) == 0)
857                 formout("entermsg");
858
859         /* Handle the selection of a recipient, if necessary. */
860         strcpy(buf, "");
861         if (need_recp == 1) {
862                 if (axlevel >= 2) {
863                         if (is_reply) {
864                                 strcpy(buf, reply_to);
865                         } else {
866                                 printf("Enter recipient: ");
867                                 getline(buf, 60);
868                                 if (strlen(buf) == 0)
869                                         return (1);
870                         }
871                 } else
872                         strcpy(buf, "sysop");
873         }
874
875         b = 0;
876         if (room_flags & QR_ANONOPT) {
877                 printf("Anonymous (Y/N)? ");
878                 if (yesno() == 1)
879                         b = 1;
880         }
881
882         /* If it's mail, we've got to check the validity of the recipient... */
883         if (strlen(buf) > 0) {
884                 sprintf(cmd, "ENT0 0|%s|%d|%d", buf, b, mode);
885                 serv_puts(cmd);
886                 serv_gets(cmd);
887                 if (cmd[0] != '2') {
888                         printf("%s\n", &cmd[4]);
889                         return (1);
890                 }
891         }
892
893         /* Learn the number of the newest message in in the room, so we can
894          * tell upon saving whether someone else has posted too.
895          */
896         num_msgs = 0;
897         serv_puts("MSGS LAST|1");
898         serv_gets(cmd);
899         if (cmd[0] != '1') {
900                 printf("%s\n", &cmd[5]);
901         } else {
902                 while (serv_gets(cmd), strcmp(cmd, "000")) {
903                         msg_arr[num_msgs++] = atol(cmd);
904                 }
905         }
906
907         /* Now compose the message... */
908         if (make_message(temp, buf, b, 0, c) != 0) {
909                 return (2);
910         }
911
912         /* Reopen the temp file that was created, so we can send it */
913         fp = fopen(temp, "r");
914
915         /* Yes, unlink it now, so it doesn't stick around if we crash */
916         unlink(temp);
917
918         if (fp == NULL) {
919                 printf("*** Internal error while trying to save message!\n"
920                         "    %s: %s\n",
921                         temp, strerror(errno));
922                 return(errno);
923         }
924
925         /* Transmit message to the server */
926         sprintf(cmd, "ENT0 1|%s|%d|%d||", buf, b, mode);
927         serv_puts(cmd);
928         serv_gets(cmd);
929         if (cmd[0] != '4') {
930                 printf("%s\n", &cmd[4]);
931                 return (1);
932         }
933
934         transmit_message(fp);
935         serv_puts("000");
936
937         fclose(fp);
938
939         highmsg = msg_arr[num_msgs - 1];
940         num_msgs = 0;
941         serv_puts("MSGS NEW");
942         serv_gets(cmd);
943         if (cmd[0] != '1') {
944                 printf("%s\n", &cmd[5]);
945         } else {
946                 while (serv_gets(cmd), strcmp(cmd, "000")) {
947                         msg_arr[num_msgs++] = atol(cmd);
948                 }
949         }
950
951         /* get new highest message number in room to set lrp for goto... */
952         maxmsgnum = msg_arr[num_msgs - 1];
953
954         /* now see if anyone else has posted in here */
955         b = (-1);
956         for (a = 0; a < num_msgs; ++a) {
957                 if (msg_arr[a] > highmsg) {
958                         ++b;
959                 }
960         }
961
962         /* In the Mail> room, this algorithm always counts one message
963          * higher than in public rooms, so we decrement it by one.
964          */
965         if (need_recp) {
966                 --b;
967         }
968
969         if (b == 1) {
970                 printf("*** 1 additional message has been entered "
971                         "in this room by another user.\n");
972         }
973         else if (b > 1) {
974                 printf("*** %d additional messages have been entered "
975                         "in this room by other users.\n", b);
976         }
977
978         return(0);
979 }
980
981 /*
982  * Do editing on a quoted file
983  */
984 void process_quote(void)
985 {
986         FILE *qfile, *tfile;
987         char buf[128];
988         int line, qstart, qend;
989
990         /* Unlink the second temp file as soon as it's opened, so it'll get
991          * deleted even if the program dies
992          */
993         qfile = fopen(temp2, "r");
994         unlink(temp2);
995
996         /* Display the quotable text with line numbers added */
997         line = 0;
998         fgets(buf, 128, qfile);
999         while (fgets(buf, 128, qfile) != NULL) {
1000                 printf("%2d %s", ++line, buf);
1001         }
1002         printf("Begin quoting at [ 1] : ");
1003         getline(buf, 3);
1004         qstart = (buf[0] == 0) ? (1) : atoi(buf);
1005         printf("  End quoting at [%d] : ", line);
1006         getline(buf, 3);
1007         qend = (buf[0] == 0) ? (line) : atoi(buf);
1008         rewind(qfile);
1009         line = 0;
1010         fgets(buf, 128, qfile);
1011         tfile = fopen(temp, "w");
1012         while (fgets(buf, 128, qfile) != NULL) {
1013                 if ((++line >= qstart) && (line <= qend))
1014                         fprintf(tfile, " >%s", buf);
1015         }
1016         fprintf(tfile, " \n");
1017         fclose(qfile);
1018         fclose(tfile);
1019         chmod(temp, 0666);
1020 }
1021
1022
1023
1024 /*
1025  * List the URL's which were embedded in the previous message
1026  */
1027 void list_urls()
1028 {
1029         int i;
1030         char cmd[256];
1031
1032         if (num_urls == 0) {
1033                 printf("There were no URL's in the previous message.\n\n");
1034                 return;
1035         }
1036
1037         for (i = 0; i < num_urls; ++i) {
1038                 printf("%3d %s\n", i + 1, urls[i]);
1039         }
1040
1041         if ((i = num_urls) != 1)
1042                 i = intprompt("Display which one", 1, 1, num_urls);
1043
1044         sprintf(cmd, rc_url_cmd, urls[i - 1]);
1045         system(cmd);
1046         printf("\n");
1047 }
1048
1049 /*
1050  * Read the messages in the current room
1051  */
1052 void readmsgs(
1053         int c,          /* 0=Read all  1=Read new  2=Read old 3=Read last q */
1054         int rdir,       /* 1=Forward (-1)=Reverse */
1055         int q           /* Number of msgs to read (if c==3) */
1056 ) {
1057         int a, b, e, f, g, start;
1058         int savedpos;
1059         int hold_sw = 0;
1060         char arcflag = 0;
1061         char quotflag = 0;
1062         int hold_color = 0;
1063         char prtfile[PATH_MAX];
1064         char pagin;
1065         char cmd[256];
1066         char targ[ROOMNAMELEN];
1067         char filename[256];
1068
1069         if (c < 0)
1070                 b = (MAXMSGS - 1);
1071         else
1072                 b = 0;
1073
1074         strcpy(prtfile, tmpnam(NULL));
1075
1076         num_msgs = 0;
1077         strcpy(cmd, "MSGS ");
1078         switch (c) {
1079         case 0:
1080                 strcat(cmd, "ALL");
1081                 break;
1082         case 1:
1083                 strcat(cmd, "NEW");
1084                 break;
1085         case 2:
1086                 strcat(cmd, "OLD");
1087                 break;
1088         case 3:
1089                 sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1090                 break;
1091         }
1092         serv_puts(cmd);
1093         serv_gets(cmd);
1094         if (cmd[0] != '1') {
1095                 printf("%s\n", &cmd[5]);
1096         } else {
1097                 while (serv_gets(cmd), strcmp(cmd, "000")) {
1098                         if (num_msgs == MAXMSGS) {
1099                                 memcpy(&msg_arr[0], &msg_arr[1],
1100                                        (sizeof(long) * (MAXMSGS - 1)));
1101                                 --num_msgs;
1102                         }
1103                         msg_arr[num_msgs++] = atol(cmd);
1104                 }
1105         }
1106
1107         lines_printed = 0;
1108
1109         /* this loop cycles through each message... */
1110         start = ((rdir == 1) ? 0 : (num_msgs - 1));
1111         for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1112                 while (msg_arr[a] == 0L) {
1113                         a = a + rdir;
1114                         if ((a == MAXMSGS) || (a == (-1)))
1115                                 return;
1116                 }
1117
1118 RAGAIN:         pagin = ((arcflag == 0)
1119                          && (quotflag == 0)
1120                          && (userflags & US_PAGINATOR)) ? 1 : 0;
1121
1122                 /* If we're doing a quote, set the screenwidth to 72 */
1123                 if (quotflag) {
1124                         hold_sw = screenwidth;
1125                         screenwidth = 72;
1126                 }
1127
1128                 /* If printing or archiving, set the screenwidth to 80 */
1129                 if (arcflag) {
1130                         hold_sw = screenwidth;
1131                         screenwidth = 80;
1132                 }
1133
1134                 /* now read the message... */
1135                 e = read_message(msg_arr[a], pagin);
1136
1137                 /* ...and set the screenwidth back if we have to */
1138                 if ((quotflag) || (arcflag)) {
1139                         screenwidth = hold_sw;
1140                 }
1141 RMSGREAD:       fflush(stdout);
1142                 highest_msg_read = msg_arr[a];
1143                 if (quotflag) {
1144                         freopen("/dev/tty", "r+", stdout);
1145                         quotflag = 0;
1146                         enable_color = hold_color;
1147                         process_quote();
1148                 }
1149                 if (arcflag) {
1150                         freopen("/dev/tty", "r+", stdout);
1151                         arcflag = 0;
1152                         enable_color = hold_color;
1153                         f = fork();
1154                         if (f == 0) {
1155                                 freopen(prtfile, "r", stdin);
1156                                 sttybbs(SB_RESTORE);
1157                                 ka_system(printcmd);
1158                                 sttybbs(SB_NO_INTR);
1159                                 unlink(prtfile);
1160                                 exit(0);
1161                         }
1162                         if (f > 0)
1163                                 do {
1164                                         g = wait(NULL);
1165                                 } while ((g != f) && (g >= 0));
1166                         printf("Message printed.\n");
1167                 }
1168                 if (e == 3)
1169                         return;
1170                 if (((userflags & US_NOPROMPT) || (e == 2))
1171                     && (((room_flags & QR_MAILBOX) == 0)
1172                         || (rc_force_mail_prompts == 0))) {
1173                         e = 'n';
1174                 } else {
1175                         color(DIM_WHITE);
1176                         printf("(");
1177                         color(BRIGHT_WHITE);
1178                         printf("%d", num_msgs - a - 1);
1179                         color(DIM_WHITE);
1180                         printf(") ");
1181
1182                         keyopt
1183                             ("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1184                         if (rc_url_cmd[0] && num_urls)
1185                                 keyopt("<U>RL View ");
1186                         keyopt("<?>Help/others -> ");
1187
1188                         do {
1189                                 lines_printed = 2;
1190                                 e = (inkey() & 127);
1191                                 e = tolower(e);
1192 /* return key same as <N> */ if (e == 13)
1193                                         e = 'n';
1194 /* space key same as <N> */ if (e == 32)
1195                                         e = 'n';
1196 /* del/move for aides only */
1197                                     if ((!is_room_aide)
1198                                         && ((room_flags & QR_MAILBOX) ==
1199                                             0)) {
1200                                         if ((e == 'd') || (e == 'm')
1201                                             || (e == 'c'))
1202                                                 e = 0;
1203                                 }
1204 /* print only if available */
1205                                 if ((e == 'p') && (strlen(printcmd) == 0))
1206                                         e = 0;
1207 /* can't file if not allowed */
1208                                     if ((e == 'f')
1209                                         && (rc_allow_attachments == 0))
1210                                         e = 0;
1211 /* link only if browser avail*/
1212                                     if ((e == 'u')
1213                                         && (strlen(rc_url_cmd) == 0))
1214                                         e = 0;
1215                         } while ((e != 'a') && (e != 'n') && (e != 's')
1216                                  && (e != 'd') && (e != 'm') && (e != 'p')
1217                                  && (e != 'q') && (e != 'b') && (e != 'h')
1218                                  && (e != 'r') && (e != 'f') && (e != '?')
1219                                  && (e != 'u') && (e != 'c'));
1220                         switch (e) {
1221                         case 's':
1222                                 printf("Stop\r");
1223                                 break;
1224                         case 'a':
1225                                 printf("Again\r");
1226                                 break;
1227                         case 'd':
1228                                 printf("Delete\r");
1229                                 break;
1230                         case 'm':
1231                                 printf("Move\r");
1232                                 break;
1233                         case 'c':
1234                                 printf("Copy\r");
1235                                 break;
1236                         case 'n':
1237                                 printf("Next\r");
1238                                 break;
1239                         case 'p':
1240                                 printf("Print\r");
1241                                 break;
1242                         case 'q':
1243                                 printf("Quote\r");
1244                                 break;
1245                         case 'b':
1246                                 printf("Back\r");
1247                                 break;
1248                         case 'h':
1249                                 printf("Header\r");
1250                                 break;
1251                         case 'r':
1252                                 printf("Reply\r");
1253                                 break;
1254                         case 'f':
1255                                 printf("File\r");
1256                                 break;
1257                         case 'u':
1258                                 printf("URL's\r");
1259                                 break;
1260                         case '?':
1261                                 printf("? <help>\r");
1262                                 break;
1263                         }
1264                         if (userflags & US_DISAPPEAR)
1265                                 printf("\r%79s\r", "");
1266                         else
1267                                 printf("\n");
1268                         fflush(stdout);
1269                 }
1270                 switch (e) {
1271                 case '?':
1272                         printf("Options available here:\n");
1273                         printf(" ?  Help (prints this message)\n");
1274                         printf(" S  Stop reading immediately\n");
1275                         printf(" A  Again (repeats last message)\n");
1276                         printf(" N  Next (continue with next message)\n");
1277                         printf(" B  Back (go back to previous message)\n");
1278                         if ((is_room_aide)
1279                             || (room_flags & QR_MAILBOX)) {
1280                                 printf(" D  Delete this message\n");
1281                                 printf
1282                                     (" M  Move message to another room\n");
1283                                 printf
1284                                     (" C  Copy message to another room\n");
1285                         }
1286                         if (strlen(printcmd) > 0)
1287                                 printf(" P  Print this message\n");
1288                         printf
1289                             (" Q  Quote portions of this message for your next post\n");
1290                         printf
1291                             (" H  Headers (display message headers only)\n");
1292                         if (is_mail)
1293                                 printf(" R  Reply to this message\n");
1294                         if (rc_allow_attachments)
1295                                 printf
1296                                     (" F  (save attachments to a file)\n");
1297                         if (strlen(rc_url_cmd) > 0)
1298                                 printf(" U  (list URL's for display)\n");
1299                         printf("\n");
1300                         goto RMSGREAD;
1301                 case 'p':
1302                         fflush(stdout);
1303                         freopen(prtfile, "w", stdout);
1304                         arcflag = 1;
1305                         hold_color = enable_color;
1306                         enable_color = 0;
1307                         goto RAGAIN;
1308                 case 'q':
1309                         fflush(stdout);
1310                         freopen(temp2, "w", stdout);
1311                         quotflag = 1;
1312                         hold_color = enable_color;
1313                         enable_color = 0;
1314                         goto RAGAIN;
1315                 case 's':
1316                         return;
1317                 case 'a':
1318                         goto RAGAIN;
1319                 case 'b':
1320                         a = a - (rdir * 2);
1321                         break;
1322                 case 'm':
1323                 case 'c':
1324                         newprompt("Enter target room: ",
1325                                   targ, ROOMNAMELEN - 1);
1326                         if (strlen(targ) > 0) {
1327                                 sprintf(cmd, "MOVE %ld|%s|%d",
1328                                         msg_arr[a], targ,
1329                                         (e == 'c' ? 1 : 0));
1330                                 serv_puts(cmd);
1331                                 serv_gets(cmd);
1332                                 printf("%s\n", &cmd[4]);
1333                                 if (cmd[0] == '2')
1334                                         msg_arr[a] = 0L;
1335                         } else {
1336                                 goto RMSGREAD;
1337                         }
1338                         if (cmd[0] != '2')
1339                                 goto RMSGREAD;
1340                         break;
1341                 case 'f':
1342                         newprompt("Which section? ", filename,
1343                                   ((sizeof filename) - 1));
1344                         snprintf(cmd, sizeof cmd,
1345                                  "OPNA %ld|%s", msg_arr[a], filename);
1346                         serv_puts(cmd);
1347                         serv_gets(cmd);
1348                         if (cmd[0] == '2') {
1349                                 extract(filename, &cmd[4], 2);
1350                                 download_to_local_disk(filename,
1351                                                        extract_int(&cmd[4],
1352                                                                    0));
1353                         } else {
1354                                 printf("%s\n", &cmd[4]);
1355                         }
1356                         goto RMSGREAD;
1357                 case 'd':
1358                         printf("*** Delete this message? ");
1359                         if (yesno() == 1) {
1360                                 sprintf(cmd, "DELE %ld", msg_arr[a]);
1361                                 serv_puts(cmd);
1362                                 serv_gets(cmd);
1363                                 printf("%s\n", &cmd[4]);
1364                                 if (cmd[0] == '2')
1365                                         msg_arr[a] = 0L;
1366                         } else {
1367                                 goto RMSGREAD;
1368                         }
1369                         break;
1370                 case 'h':
1371                         read_message(msg_arr[a], READ_HEADER);
1372                         goto RMSGREAD;
1373                 case 'r':
1374                         savedpos = num_msgs;
1375                         entmsg(1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1376                         num_msgs = savedpos;
1377                         goto RMSGREAD;
1378                 case 'u':
1379                         list_urls();
1380                         goto RMSGREAD;
1381                 }
1382         }                       /* end for loop */
1383 }                               /* end read routine */
1384
1385
1386
1387
1388 /*
1389  * View and edit a system message
1390  */
1391 void edit_system_message(char *which_message)
1392 {
1393         char desc[64];
1394         char read_cmd[64];
1395         char write_cmd[64];
1396
1397         sprintf(desc, "system message '%s'", which_message);
1398         sprintf(read_cmd, "MESG %s", which_message);
1399         sprintf(write_cmd, "EMSG %s", which_message);
1400         do_edit(desc, read_cmd, "NOOP", write_cmd);
1401 }
1402
1403
1404
1405
1406 /*
1407  * Verify the message base
1408  */
1409 void check_message_base(void)
1410 {
1411         char buf[256];
1412
1413         printf
1414             ("Please read the documentation before running this command.\n"
1415             "Having done so, do you still want to check the message base? ");
1416         if (yesno() == 0)
1417                 return;
1418
1419         serv_puts("FSCK");
1420         serv_gets(buf);
1421         if (buf[0] != '1') {
1422                 printf("%s\n", &buf[4]);
1423                 return;
1424         }
1425
1426         while (serv_gets(buf), strcmp(buf, "000")) {
1427                 printf("%s\n", buf);
1428         }
1429 }