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