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