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