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