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