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