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