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