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