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