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