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