]> code.citadel.org Git - citadel.git/blob - citadel/messages.c
* added vcard_to_html() function
[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         if (subject != NULL) if (strlen(subject) > 0) {
708                 scr_printf("Subject: %s\n", subject);
709         }
710
711         beg = 0L;
712
713         if (mode == 1) {
714                 scr_printf("(Press ctrl-d when finished)\n");
715         }
716
717         if (mode == 0) {
718                 fp = fopen(filename, "r");
719                 if (fp != NULL) {
720                         fmout(screenwidth, fp, NULL, 0, screenheight, 0, 0);
721                         beg = ftell(fp);
722                         fclose(fp);
723                 } else {
724                         fp = fopen(filename, "w");
725                         if (fp == NULL) {
726                                 err_printf("*** Error opening temp file!\n"
727                                         "    %s: %s\n",
728                                         filename, strerror(errno));
729                         return(1);
730                         }
731                         fclose(fp);
732                 }
733         }
734
735 ME1:    switch (mode) {
736
737         case 0:
738                 fp = fopen(filename, "r+");
739                 if (fp == NULL) {
740                         err_printf("*** Error opening temp file!\n"
741                                 "    %s: %s\n",
742                                 filename, strerror(errno));
743                         return(1);
744                 }
745                 citedit(fp);
746                 fclose(fp);
747                 goto MECR;
748
749         case 1:
750                 fp = fopen(filename, "a");
751                 if (fp == NULL) {
752                         err_printf("*** Error opening temp file!\n"
753                                 "    %s: %s\n",
754                                 filename, strerror(errno));
755                         return(1);
756                 }
757                 do {
758                         a = inkey();
759                         if (a == 255)
760                                 a = 32;
761                         if (a == 13)
762                                 a = 10;
763                         if (a != 4) {
764                                 putc(a, fp);
765                                 scr_putc(a);
766                         }
767                         if (a == 10)
768                                 scr_putc(10);
769                 } while (a != 4);
770                 fclose(fp);
771                 break;
772
773         case 2:
774                 e_ex_code = 1;  /* start with a failed exit code */
775                 editor_pid = fork();
776                 cksum = file_checksum(filename);
777                 if (editor_pid == 0) {
778                         chmod(filename, 0600);
779                         screen_reset();
780                         sttybbs(SB_RESTORE);
781                         setenv("WINDOW_TITLE", header, 1);
782                         execlp(editor_path, editor_path, filename, NULL);
783                         exit(1);
784                 }
785                 if (editor_pid > 0)
786                         do {
787                                 e_ex_code = 0;
788                                 b = ka_wait(&e_ex_code);
789                         } while ((b != editor_pid) && (b >= 0));
790                 editor_pid = (-1);
791                 sttybbs(0);
792                 screen_set();
793                 break;
794         }
795
796 MECR:   if (mode == 2) {
797                 if (file_checksum(filename) == cksum) {
798                         err_printf("*** Aborted message.\n");
799                         e_ex_code = 1;
800                 }
801                 if (e_ex_code == 0)
802                         goto MEFIN;
803                 goto MEABT2;
804         }
805
806         b = keymenu("Entry command (? for options)",
807                     "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
808                     "add s<U>bject|"
809                     "<R>eplace string|<H>old message");
810
811         if (b == 'a')
812                 goto MEABT;
813         if (b == 'c')
814                 goto ME1;
815         if (b == 's')
816                 goto MEFIN;
817         if (b == 'p') {
818                 scr_printf(" %s from %s", datestr, fullname);
819                 if (strlen(recipient) > 0)
820                         scr_printf(" to %s", recipient);
821                 scr_printf("\n");
822                 if (subject != NULL) if (strlen(subject) > 0) {
823                         scr_printf("Subject: %s\n", subject);
824                 }
825                 fp = fopen(filename, "r");
826                 if (fp != NULL) {
827                         fmout(screenwidth, fp, NULL,
828                               ((userflags & US_PAGINATOR) ? 1 : 0),
829                               screenheight, 0, 0);
830                         beg = ftell(fp);
831                         fclose(fp);
832                 }
833                 goto MECR;
834         }
835         if (b == 'r') {
836                 replace_string(filename, 0L);
837                 goto MECR;
838         }
839         if (b == 'h') {
840                 return (2);
841         }
842         if (b == 'u') {
843                 if (subject != NULL) {
844                         newprompt("Subject: ", subject, 70);
845                 }
846                 goto MECR;
847         }
848
849 MEFIN:  return (0);
850
851 MEABT:  scr_printf("Are you sure? ");
852         if (yesno() == 0) {
853                 goto ME1;
854         }
855 MEABT2: unlink(filename);
856         return (2);
857 }
858
859 /*
860  * Transmit message text to the server.
861  * 
862  * This loop also implements a "tick" counter that displays the progress, if
863  * we're sending something that will take a long time to transmit.
864  */
865 void transmit_message(FILE *fp)
866 {
867         char buf[SIZ];
868         int ch, a;
869         long msglen;
870         time_t lasttick;
871
872         fseek(fp, 0L, SEEK_END);
873         msglen = ftell(fp);
874         rewind(fp);
875         lasttick = time(NULL);
876         strcpy(buf, "");
877         while (ch = getc(fp), (ch >= 0)) {
878                 if (ch == 10) {
879                         if (!strcmp(buf, "000"))
880                                 strcpy(buf, ">000");
881                         serv_puts(buf);
882                         strcpy(buf, "");
883                 } else {
884                         a = strlen(buf);
885                         buf[a + 1] = 0;
886                         buf[a] = ch;
887                         if ((ch == 32) && (strlen(buf) > 200)) {
888                                 buf[a] = 0;
889                                 if (!strcmp(buf, "000"))
890                                         strcpy(buf, ">000");
891                                 serv_puts(buf);
892                                 strcpy(buf, "");
893                         }
894                         if (strlen(buf) > 250) {
895                                 if (!strcmp(buf, "000"))
896                                         strcpy(buf, ">000");
897                                 serv_puts(buf);
898                                 strcpy(buf, "");
899                         }
900                 }
901
902                 if ((time(NULL) - lasttick) > 2L) {
903                         scr_printf(" %3ld%% completed\r",
904                                ((ftell(fp) * 100L) / msglen));
905                         scr_flush();
906                         lasttick = time(NULL);
907                 }
908
909         }
910         serv_puts(buf);
911         scr_printf("                \r");
912         scr_flush();
913 }
914
915
916
917 /*
918  * entmsg()  -  edit and create a message
919  *              returns 0 if message was saved
920  */
921 int entmsg(int is_reply,        /* nonzero if this was a <R>eply command */
922                 int c)          /* */
923 {
924         char buf[300];
925         char cmd[SIZ];
926         int a, b;
927         int need_recp = 0;
928         int mode;
929         long highmsg;
930         FILE *fp;
931         char subject[SIZ];
932
933         if (c > 0)
934                 mode = 1;
935         else
936                 mode = 0;
937
938         strcpy(subject, "");
939
940         /*
941          * First, check to see if we have permission to enter a message in
942          * this room.  The server will return an error code if we can't.
943          */
944         sprintf(cmd, "ENT0 0||0|%d", mode);
945         serv_puts(cmd);
946         serv_gets(cmd);
947
948         if ((strncmp(cmd, "570", 3)) && (strncmp(cmd, "200", 3))) {
949                 scr_printf("%s\n", &cmd[4]);
950                 return (1);
951         }
952
953         /* Error code 570 is special.  It means that we CAN enter a message
954          * in this room, but a recipient needs to be specified.
955          */
956         need_recp = 0;
957         if (!strncmp(cmd, "570", 3))
958                 need_recp = 1;
959
960         /* If the user is a dumbass, tell them how to type. */
961         if ((userflags & US_EXPERT) == 0) {
962                 formout("entermsg");
963         }
964
965         /* Handle the selection of a recipient, if necessary. */
966         strcpy(buf, "");
967         if (need_recp == 1) {
968                 if (axlevel >= 2) {
969                         if (is_reply) {
970                                 strcpy(buf, reply_to);
971                         } else {
972                                 scr_printf("Enter recipient: ");
973                                 getline(buf, (SIZ-100) );
974                                 if (strlen(buf) == 0)
975                                         return (1);
976                         }
977                 } else
978                         strcpy(buf, "sysop");
979         }
980
981         if (is_reply) {
982                 if (strlen(reply_subject) > 0) {
983                         if (!strncasecmp(reply_subject,
984                            "Re: ", 3)) {
985                                 strcpy(subject, reply_subject);
986                         }
987                         else {
988                                 snprintf(subject,
989                                         sizeof subject,
990                                         "Re: %s",
991                                         reply_subject);
992                         }
993                 }
994         }
995
996         b = 0;
997         if (room_flags & QR_ANONOPT) {
998                 scr_printf("Anonymous (Y/N)? ");
999                 if (yesno() == 1)
1000                         b = 1;
1001         }
1002
1003         /* If it's mail, we've got to check the validity of the recipient... */
1004         if (strlen(buf) > 0) {
1005                 sprintf(cmd, "ENT0 0|%s|%d|%d|%s", buf, b, mode, subject);
1006                 serv_puts(cmd);
1007                 serv_gets(cmd);
1008                 if (cmd[0] != '2') {
1009                         scr_printf("%s\n", &cmd[4]);
1010                         return (1);
1011                 }
1012         }
1013
1014         /* Learn the number of the newest message in in the room, so we can
1015          * tell upon saving whether someone else has posted too.
1016          */
1017         num_msgs = 0;
1018         serv_puts("MSGS LAST|1");
1019         serv_gets(cmd);
1020         if (cmd[0] != '1') {
1021                 scr_printf("%s\n", &cmd[5]);
1022         } else {
1023                 while (serv_gets(cmd), strcmp(cmd, "000")) {
1024                         msg_arr[num_msgs++] = atol(cmd);
1025                 }
1026         }
1027
1028         /* Now compose the message... */
1029         if (client_make_message(temp, buf, b, 0, c, subject) != 0) {
1030                 return (2);
1031         }
1032
1033         /* Reopen the temp file that was created, so we can send it */
1034         fp = fopen(temp, "r");
1035
1036         /* Yes, unlink it now, so it doesn't stick around if we crash */
1037         unlink(temp);
1038
1039         if (fp == NULL) {
1040                 err_printf("*** Internal error while trying to save message!\n"
1041                         "    %s: %s\n",
1042                         temp, strerror(errno));
1043                 return(errno);
1044         }
1045
1046         /* Transmit message to the server */
1047         sprintf(cmd, "ENT0 1|%s|%d|%d|%s|", buf, b, mode, subject);
1048         serv_puts(cmd);
1049         serv_gets(cmd);
1050         if (cmd[0] != '4') {
1051                 scr_printf("%s\n", &cmd[4]);
1052                 return (1);
1053         }
1054
1055         transmit_message(fp);
1056         serv_puts("000");
1057
1058         fclose(fp);
1059
1060         highmsg = msg_arr[num_msgs - 1];
1061         num_msgs = 0;
1062         serv_puts("MSGS NEW");
1063         serv_gets(cmd);
1064         if (cmd[0] != '1') {
1065                 scr_printf("%s\n", &cmd[5]);
1066         } else {
1067                 while (serv_gets(cmd), strcmp(cmd, "000")) {
1068                         msg_arr[num_msgs++] = atol(cmd);
1069                 }
1070         }
1071
1072         /* get new highest message number in room to set lrp for goto... */
1073         maxmsgnum = msg_arr[num_msgs - 1];
1074
1075         /* now see if anyone else has posted in here */
1076         b = (-1);
1077         for (a = 0; a < num_msgs; ++a) {
1078                 if (msg_arr[a] > highmsg) {
1079                         ++b;
1080                 }
1081         }
1082
1083         /* In the Mail> room, this algorithm always counts one message
1084          * higher than in public rooms, so we decrement it by one.
1085          */
1086         if (need_recp) {
1087                 --b;
1088         }
1089
1090         if (b == 1) {
1091                 scr_printf("*** 1 additional message has been entered "
1092                         "in this room by another user.\n");
1093         }
1094         else if (b > 1) {
1095                 scr_printf("*** %d additional messages have been entered "
1096                         "in this room by other users.\n", b);
1097         }
1098
1099         return(0);
1100 }
1101
1102 /*
1103  * Do editing on a quoted file
1104  */
1105 void process_quote(void)
1106 {
1107         FILE *qfile, *tfile;
1108         char buf[128];
1109         int line, qstart, qend;
1110
1111         /* Unlink the second temp file as soon as it's opened, so it'll get
1112          * deleted even if the program dies
1113          */
1114         qfile = fopen(temp2, "r");
1115         unlink(temp2);
1116
1117         /* Display the quotable text with line numbers added */
1118         line = 0;
1119         fgets(buf, 128, qfile);
1120         while (fgets(buf, 128, qfile) != NULL) {
1121                 scr_printf("%2d %s", ++line, buf);
1122         }
1123         scr_printf("Begin quoting at [ 1] : ");
1124         getline(buf, 3);
1125         qstart = (buf[0] == 0) ? (1) : atoi(buf);
1126         scr_printf("  End quoting at [%d] : ", line);
1127         getline(buf, 3);
1128         qend = (buf[0] == 0) ? (line) : atoi(buf);
1129         rewind(qfile);
1130         line = 0;
1131         fgets(buf, 128, qfile);
1132         tfile = fopen(temp, "w");
1133         while (fgets(buf, 128, qfile) != NULL) {
1134                 if ((++line >= qstart) && (line <= qend))
1135                         fprintf(tfile, " >%s", buf);
1136         }
1137         fprintf(tfile, " \n");
1138         fclose(qfile);
1139         fclose(tfile);
1140         chmod(temp, 0666);
1141 }
1142
1143
1144
1145 /*
1146  * List the URL's which were embedded in the previous message
1147  */
1148 void list_urls()
1149 {
1150         int i;
1151         char cmd[SIZ];
1152
1153         if (num_urls == 0) {
1154                 scr_printf("There were no URL's in the previous message.\n\n");
1155                 return;
1156         }
1157
1158         for (i = 0; i < num_urls; ++i) {
1159                 scr_printf("%3d %s\n", i + 1, urls[i]);
1160         }
1161
1162         if ((i = num_urls) != 1)
1163                 i = intprompt("Display which one", 1, 1, num_urls);
1164
1165         sprintf(cmd, rc_url_cmd, urls[i - 1]);
1166         system(cmd);
1167         scr_printf("\n");
1168 }
1169
1170 /*
1171  * Read the messages in the current room
1172  */
1173 void readmsgs(
1174         int c,          /* 0=Read all  1=Read new  2=Read old 3=Read last q */
1175         int rdir,       /* 1=Forward (-1)=Reverse */
1176         int q           /* Number of msgs to read (if c==3) */
1177 ) {
1178         int a, b, e, f, g, start;
1179         int savedpos;
1180         int hold_sw = 0;
1181         char arcflag = 0;
1182         char quotflag = 0;
1183         int hold_color = 0;
1184         char prtfile[PATH_MAX];
1185         char pagin;
1186         char cmd[SIZ];
1187         char targ[ROOMNAMELEN];
1188         char filename[SIZ];
1189         FILE *dest = NULL;      /* Alternate destination other than screen */
1190
1191         if (c < 0)
1192                 b = (MAXMSGS - 1);
1193         else
1194                 b = 0;
1195
1196         strcpy(prtfile, tmpnam(NULL));
1197
1198         num_msgs = 0;
1199         strcpy(cmd, "MSGS ");
1200         switch (c) {
1201         case 0:
1202                 strcat(cmd, "ALL");
1203                 break;
1204         case 1:
1205                 strcat(cmd, "NEW");
1206                 break;
1207         case 2:
1208                 strcat(cmd, "OLD");
1209                 break;
1210         case 3:
1211                 sprintf(&cmd[strlen(cmd)], "LAST|%d", q);
1212                 break;
1213         }
1214         serv_puts(cmd);
1215         serv_gets(cmd);
1216         if (cmd[0] != '1') {
1217                 scr_printf("%s\n", &cmd[5]);
1218         } else {
1219                 while (serv_gets(cmd), strcmp(cmd, "000")) {
1220                         if (num_msgs == MAXMSGS) {
1221                                 memcpy(&msg_arr[0], &msg_arr[1],
1222                                        (sizeof(long) * (MAXMSGS - 1)));
1223                                 --num_msgs;
1224                         }
1225                         msg_arr[num_msgs++] = atol(cmd);
1226                 }
1227         }
1228
1229         if (num_msgs == 0) {
1230                 if (c == 3) return;
1231                 scr_printf("*** There are no ");
1232                 if (c == 1) scr_printf("new ");
1233                 if (c == 2) scr_printf("old ");
1234                 scr_printf("messages in this room.\n");
1235                 return;
1236         }
1237
1238         lines_printed = 0;
1239
1240         /* this loop cycles through each message... */
1241         start = ((rdir == 1) ? 0 : (num_msgs - 1));
1242         for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1243                 while (msg_arr[a] == 0L) {
1244                         a = a + rdir;
1245                         if ((a == MAXMSGS) || (a == (-1)))
1246                                 return;
1247                 }
1248
1249 RAGAIN:         pagin = ((arcflag == 0)
1250                          && (quotflag == 0)
1251                          && (userflags & US_PAGINATOR)) ? 1 : 0;
1252
1253                 /* If we're doing a quote, set the screenwidth to 72 */
1254                 if (quotflag) {
1255                         hold_sw = screenwidth;
1256                         screenwidth = 72;
1257                 }
1258
1259                 /* If printing or archiving, set the screenwidth to 80 */
1260                 if (arcflag) {
1261                         hold_sw = screenwidth;
1262                         screenwidth = 80;
1263                 }
1264
1265                 /* now read the message... */
1266                 e = read_message(msg_arr[a], pagin, dest);
1267
1268                 /* ...and set the screenwidth back if we have to */
1269                 if ((quotflag) || (arcflag)) {
1270                         screenwidth = hold_sw;
1271                 }
1272 RMSGREAD:       scr_flush();
1273                 highest_msg_read = msg_arr[a];
1274                 if (quotflag) {
1275                         fclose(dest);
1276                         dest = NULL;
1277                         quotflag = 0;
1278                         enable_color = hold_color;
1279                         process_quote();
1280                 }
1281                 if (arcflag) {
1282                         fclose(dest);
1283                         dest = NULL;
1284                         arcflag = 0;
1285                         enable_color = hold_color;
1286                         f = fork();
1287                         if (f == 0) {
1288                                 freopen(prtfile, "r", stdin);
1289                                 screen_reset();
1290                                 sttybbs(SB_RESTORE);
1291                                 ka_system(printcmd);
1292                                 sttybbs(SB_NO_INTR);
1293                                 screen_set();
1294                                 unlink(prtfile);
1295                                 exit(0);
1296                         }
1297                         if (f > 0)
1298                                 do {
1299                                         g = wait(NULL);
1300                                 } while ((g != f) && (g >= 0));
1301                         scr_printf("Message printed.\n");
1302                 }
1303                 if (rc_alt_semantics && c == 1) {
1304                         char buf[SIZ];
1305
1306                         snprintf(buf, sizeof(buf), "SEEN %ld", msg_arr[a]);
1307                         serv_puts(buf);
1308                         serv_gets(buf); /* Don't need to check this? */
1309                 }
1310                 if (e == 3)
1311                         return;
1312                 if (((userflags & US_NOPROMPT) || (e == 2))
1313                     && (((room_flags & QR_MAILBOX) == 0)
1314                         || (rc_force_mail_prompts == 0))) {
1315                         e = 'n';
1316                 } else {
1317                         color(DIM_WHITE);
1318                         scr_printf("(");
1319                         color(BRIGHT_WHITE);
1320                         scr_printf("%d", num_msgs - a - 1);
1321                         color(DIM_WHITE);
1322                         scr_printf(") ");
1323
1324                         keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top m<Y> next ");
1325                         if (rc_url_cmd[0] && num_urls)
1326                                 keyopt("<U>RLview ");
1327                         keyopt("<?>help -> ");
1328
1329                         do {
1330                                 lines_printed = 2;
1331                                 e = (inkey() & 127);
1332                                 e = tolower(e);
1333 /* return key same as <N> */ if (e == 13)
1334                                         e = 'n';
1335 /* space key same as <N> */ if (e == 32)
1336                                         e = 'n';
1337 /* del/move for aides only */
1338                                     if ((!is_room_aide)
1339                                         && ((room_flags & QR_MAILBOX) ==
1340                                             0)) {
1341                                         if ((e == 'd') || (e == 'm'))
1342                                                 e = 0;
1343                                 }
1344 /* print only if available */
1345                                 if ((e == 'p') && (strlen(printcmd) == 0))
1346                                         e = 0;
1347 /* can't file if not allowed */
1348                                     if ((e == 'f')
1349                                         && (rc_allow_attachments == 0))
1350                                         e = 0;
1351 /* link only if browser avail*/
1352                                     if ((e == 'u')
1353                                         && (strlen(rc_url_cmd) == 0))
1354                                         e = 0;
1355                         } while ((e != 'a') && (e != 'n') && (e != 's')
1356                                  && (e != 'd') && (e != 'm') && (e != 'p')
1357                                  && (e != 'q') && (e != 'b') && (e != 'h')
1358                                  && (e != 'r') && (e != 'f') && (e != '?')
1359                                  && (e != 'u') && (e != 'c') && (e != 'y'));
1360                         switch (e) {
1361                         case 's':
1362                                 scr_printf("Stop");
1363                                 break;
1364                         case 'a':
1365                                 scr_printf("Again");
1366                                 break;
1367                         case 'd':
1368                                 scr_printf("Delete");
1369                                 break;
1370                         case 'm':
1371                                 scr_printf("Move");
1372                                 break;
1373                         case 'c':
1374                                 scr_printf("Copy");
1375                                 break;
1376                         case 'n':
1377                                 scr_printf("Next");
1378                                 break;
1379                         case 'p':
1380                                 scr_printf("Print");
1381                                 break;
1382                         case 'q':
1383                                 scr_printf("Quote");
1384                                 break;
1385                         case 'b':
1386                                 scr_printf("Back");
1387                                 break;
1388                         case 'h':
1389                                 scr_printf("Header");
1390                                 break;
1391                         case 'r':
1392                                 scr_printf("Reply");
1393                                 break;
1394                         case 'f':
1395                                 scr_printf("File");
1396                                 break;
1397                         case 'u':
1398                                 scr_printf("URL's");
1399                                 break;
1400                         case 'y':
1401                                 scr_printf("mY next");
1402                                 break;
1403                         case '?':
1404                                 scr_printf("? <help>");
1405                                 break;
1406                         }
1407                         if (userflags & US_DISAPPEAR)
1408                                 scr_printf("\r%79s\r", "");
1409                         else
1410                                 scr_printf("\n");
1411                         scr_flush();
1412                 }
1413                 switch (e) {
1414                 case '?':
1415                         scr_printf("Options available here:\n"
1416                                 " ?  Help (prints this message)\n"
1417                                 " S  Stop reading immediately\n"
1418                                 " A  Again (repeats last message)\n"
1419                                 " N  Next (continue with next message)\n"
1420                                 " Y  My Next (continue with next message you authored)\n"
1421                                 " B  Back (go back to previous message)\n");
1422                         if ((is_room_aide)
1423                             || (room_flags & QR_MAILBOX)) {
1424                                 scr_printf(" D  Delete this message\n"
1425                                         " M  Move message to another room\n");
1426                         }
1427                         scr_printf(" C  Copy message to another room\n");
1428                         if (strlen(printcmd) > 0)
1429                                 scr_printf(" P  Print this message\n");
1430                         scr_printf(
1431                                 " Q  Quote portions of this message for your next post\n"
1432                                 " H  Headers (display message headers only)\n");
1433                         if (is_mail)
1434                                 scr_printf(" R  Reply to this message\n");
1435                         if (rc_allow_attachments)
1436                                 scr_printf
1437                                     (" F  (save attachments to a file)\n");
1438                         if (strlen(rc_url_cmd) > 0)
1439                                 scr_printf(" U  (list URL's for display)\n");
1440                         scr_printf("\n");
1441                         goto RMSGREAD;
1442                 case 'p':
1443                         scr_flush();
1444                         dest = fopen(prtfile, "w");
1445                         arcflag = 1;
1446                         hold_color = enable_color;
1447                         enable_color = 0;
1448                         goto RAGAIN;
1449                 case 'q':
1450                         scr_flush();
1451                         dest = fopen(temp2, "w");
1452                         quotflag = 1;
1453                         hold_color = enable_color;
1454                         enable_color = 0;
1455                         goto RAGAIN;
1456                 case 's':
1457                         return;
1458                 case 'a':
1459                         goto RAGAIN;
1460                 case 'b':
1461                         a = a - (rdir * 2);
1462                         break;
1463                 case 'm':
1464                 case 'c':
1465                         newprompt("Enter target room: ",
1466                                   targ, ROOMNAMELEN - 1);
1467                         if (strlen(targ) > 0) {
1468                                 sprintf(cmd, "MOVE %ld|%s|%d",
1469                                         msg_arr[a], targ,
1470                                         (e == 'c' ? 1 : 0));
1471                                 serv_puts(cmd);
1472                                 serv_gets(cmd);
1473                                 scr_printf("%s\n", &cmd[4]);
1474                                 if (cmd[0] == '2')
1475                                         msg_arr[a] = 0L;
1476                         } else {
1477                                 goto RMSGREAD;
1478                         }
1479                         if (cmd[0] != '2')
1480                                 goto RMSGREAD;
1481                         break;
1482                 case 'f':
1483                         newprompt("Which section? ", filename,
1484                                   ((sizeof filename) - 1));
1485                         snprintf(cmd, sizeof cmd,
1486                                  "OPNA %ld|%s", msg_arr[a], filename);
1487                         serv_puts(cmd);
1488                         serv_gets(cmd);
1489                         if (cmd[0] == '2') {
1490                                 extract(filename, &cmd[4], 2);
1491                                 download_to_local_disk(filename,
1492                                                        extract_int(&cmd[4],
1493                                                                    0));
1494                         } else {
1495                                 scr_printf("%s\n", &cmd[4]);
1496                         }
1497                         goto RMSGREAD;
1498                 case 'd':
1499                         scr_printf("*** Delete this message? ");
1500                         if (yesno() == 1) {
1501                                 sprintf(cmd, "DELE %ld", msg_arr[a]);
1502                                 serv_puts(cmd);
1503                                 serv_gets(cmd);
1504                                 scr_printf("%s\n", &cmd[4]);
1505                                 if (cmd[0] == '2')
1506                                         msg_arr[a] = 0L;
1507                         } else {
1508                                 goto RMSGREAD;
1509                         }
1510                         break;
1511                 case 'h':
1512                         read_message(msg_arr[a], READ_HEADER, NULL);
1513                         goto RMSGREAD;
1514                 case 'r':
1515                         savedpos = num_msgs;
1516                         entmsg(1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1517                         num_msgs = savedpos;
1518                         goto RMSGREAD;
1519                 case 'u':
1520                         list_urls();
1521                         goto RMSGREAD;
1522             case 'y':
1523           { /* hack hack hack */
1524             /* find the next message by me, stay here if we find nothing */
1525             int finda;
1526             int lasta = a;
1527             for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1528               {
1529                 /* this is repetitivly dumb, but that's what computers are for.
1530                    We have to load up messages until we find one by us */
1531                 char buf[SIZ];
1532                 int founda = 0;
1533                 
1534                 sprintf(buf, "MSG0 %ld|%d", msg_arr[finda], 1); /* read the header so we can get 'from=' */
1535                 serv_puts(buf);
1536                 serv_gets(buf);
1537                 while (serv_gets(buf), strcmp(buf, "000")) 
1538                   {
1539                         if ((!strncasecmp(buf, "from=", 5)) && (finda != a)) /* Skip current message. */
1540                       { 
1541                         if (strcasecmp(buf+5, fullname) == 0)
1542                           {
1543                             a = lasta; /* meesa current */
1544                             founda = 1;
1545                           }
1546                           }
1547                   }
1548                     // we are now in synch with the server
1549                 if (founda)
1550                   break; /* for */
1551                 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1552               } /* for */
1553           } /* case 'y' */
1554       } /* switch */
1555         }                       /* end for loop */
1556 }                               /* end read routine */
1557
1558
1559
1560
1561 /*
1562  * View and edit a system message
1563  */
1564 void edit_system_message(char *which_message)
1565 {
1566         char desc[64];
1567         char read_cmd[64];
1568         char write_cmd[64];
1569
1570         sprintf(desc, "system message '%s'", which_message);
1571         sprintf(read_cmd, "MESG %s", which_message);
1572         sprintf(write_cmd, "EMSG %s", which_message);
1573         do_edit(desc, read_cmd, "NOOP", write_cmd);
1574 }
1575
1576
1577
1578
1579 /*
1580  * Verify the message base
1581  */
1582 void check_message_base(void)
1583 {
1584         char buf[SIZ];
1585
1586         scr_printf
1587             ("Please read the documentation before running this command.\n"
1588             "Having done so, do you still want to check the message base? ");
1589         if (yesno() == 0)
1590                 return;
1591
1592         serv_puts("FSCK");
1593         serv_gets(buf);
1594         if (buf[0] != '1') {
1595                 scr_printf("%s\n", &buf[4]);
1596                 return;
1597         }
1598
1599         while (serv_gets(buf), strcmp(buf, "000")) {
1600                 scr_printf("%s\n", buf);
1601         }
1602 }