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