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