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