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