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