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