* upgraded new version of tiny_mce
[citadel.git] / citadel / messages.c
blob3f295f9cbb5a65a163fff64bad5534f2ead3bb6f
1 /*
2 * $Id$
4 * Citadel message support routines
5 * see copyright.txt for copyright information
6 */
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>
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
33 #ifdef HAVE_PTHREAD_H
34 #include <pthread.h>
35 #endif
37 #include <stdarg.h>
38 #include <libcitadel.h>
39 #include "citadel.h"
40 #include "citadel_ipc.h"
41 #include "citadel_decls.h"
42 #include "messages.h"
43 #include "commands.h"
44 #include "rooms.h"
45 #ifndef HAVE_SNPRINTF
46 #include "snprintf.h"
47 #endif
48 #include "screen.h"
50 #define MAXWORDBUF SIZ
51 #define NO_REPLY_TO "nobody ... xxxxxx"
53 char reply_to[SIZ];
54 char reply_subject[SIZ];
55 char reply_references[SIZ];
56 char reply_inreplyto[SIZ];
58 struct cittext {
59 struct cittext *next;
60 char text[MAXWORDBUF];
63 void stty_ctdl(int cmd);
64 int haschar(const char *st, int ch);
65 void ctdl_getline(char *string, int lim);
66 int file_checksum(char *filename);
67 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
69 unsigned long *msg_arr = NULL;
70 int msg_arr_size = 0;
71 int num_msgs;
72 char rc_alt_semantics;
73 extern char room_name[];
74 extern char tempdir[];
75 extern unsigned room_flags;
76 extern unsigned room_flags2;
77 extern long highest_msg_read;
78 extern char temp[];
79 extern char temp2[];
80 extern int screenwidth;
81 extern int screenheight;
82 extern long maxmsgnum;
83 extern char is_mail;
84 extern char is_aide;
85 extern char is_room_aide;
86 extern char fullname[];
87 extern char axlevel;
88 extern unsigned userflags;
89 extern char sigcaught;
90 extern char printcmd[];
91 extern int rc_allow_attachments;
92 extern int rc_display_message_numbers;
93 extern int rc_force_mail_prompts;
94 extern int editor_pid;
95 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
96 int num_urls = 0;
97 char urls[MAXURLS][SIZ];
98 char imagecmd[SIZ];
99 int has_images = 0; /* Current msg has images */
100 struct parts *last_message_parts = NULL; /* Parts from last msg */
104 void ka_sigcatch(int signum)
106 alarm(S_KEEPALIVE);
107 signal(SIGALRM, ka_sigcatch);
108 CtdlIPCNoop(ipc_for_signal_handlers);
113 * server keep-alive version of wait() (needed for external editor)
115 pid_t ka_wait(int *kstatus)
117 pid_t p;
119 alarm(S_KEEPALIVE);
120 signal(SIGALRM, ka_sigcatch);
121 do {
122 errno = 0;
123 p = wait(kstatus);
124 } while (errno == EINTR);
125 signal(SIGALRM, SIG_IGN);
126 alarm(0);
127 return (p);
132 * version of system() that uses ka_wait()
134 int ka_system(char *shc)
136 pid_t childpid;
137 pid_t waitpid;
138 int retcode;
140 childpid = fork();
141 if (childpid < 0) {
142 color(BRIGHT_RED);
143 perror("Cannot fork");
144 color(DIM_WHITE);
145 return ((pid_t) childpid);
148 if (childpid == 0) {
149 execlp("/bin/sh", "sh", "-c", shc, NULL);
150 exit(127);
153 if (childpid > 0) {
154 do {
155 waitpid = ka_wait(&retcode);
156 } while (waitpid != childpid);
157 return (retcode);
160 return (-1);
166 * add a newline to the buffer...
168 void add_newline(struct cittext *textlist)
170 struct cittext *ptr;
172 ptr = textlist;
173 while (ptr->next != NULL)
174 ptr = ptr->next;
176 while (ptr->text[strlen(ptr->text) - 1] == 32)
177 ptr->text[strlen(ptr->text) - 1] = 0;
178 /* strcat(ptr->text,"\n"); */
180 ptr->next = (struct cittext *)
181 malloc(sizeof(struct cittext));
182 ptr = ptr->next;
183 ptr->next = NULL;
184 strcpy(ptr->text, "");
189 * add a word to the buffer...
191 void add_word(struct cittext *textlist, char *wordbuf)
193 struct cittext *ptr;
195 ptr = textlist;
196 while (ptr->next != NULL)
197 ptr = ptr->next;
199 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
200 ptr->next = (struct cittext *)
201 malloc(sizeof(struct cittext));
202 ptr = ptr->next;
203 ptr->next = NULL;
204 strcpy(ptr->text, "");
207 strcat(ptr->text, wordbuf);
208 strcat(ptr->text, " ");
213 * begin editing of an opened file pointed to by fp
215 void citedit(CtdlIPC *ipc, FILE * fp)
217 int a, prev, finished, b, last_space;
218 int appending = 0;
219 struct cittext *textlist = NULL;
220 struct cittext *ptr;
221 char wordbuf[MAXWORDBUF];
223 /* first, load the text into the buffer */
224 fseek(fp, 0L, 0);
225 textlist = (struct cittext *) malloc(sizeof(struct cittext));
226 textlist->next = NULL;
227 strcpy(textlist->text, "");
229 strcpy(wordbuf, "");
230 prev = (-1);
231 while (a = getc(fp), a >= 0) {
232 appending = 1;
233 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
234 add_word(textlist, wordbuf);
235 strcpy(wordbuf, "");
236 if ((prev == 13) || (prev == 10)) {
237 add_word(textlist, "\n");
238 add_newline(textlist);
239 add_word(textlist, "");
241 } else {
242 wordbuf[strlen(wordbuf) + 1] = 0;
243 wordbuf[strlen(wordbuf)] = a;
245 if (strlen(wordbuf) + 3 > screenwidth) {
246 add_word(textlist, wordbuf);
247 strcpy(wordbuf, "");
249 prev = a;
252 /* get text */
253 finished = 0;
254 prev = (appending ? 13 : (-1));
255 strcpy(wordbuf, "");
256 async_ka_start();
257 do {
258 a = inkey();
259 if (a == 10)
260 a = 13;
261 if (a == 9)
262 a = 32;
263 if (a == 127)
264 a = 8;
266 if ((a != 32) && (prev == 13)) {
267 add_word(textlist, "\n");
268 scr_printf(" ");
271 if ((a == 32) && (prev == 13)) {
272 add_word(textlist, "\n");
273 add_newline(textlist);
276 if (a == 8) {
277 if (!IsEmptyStr(wordbuf)) {
278 wordbuf[strlen(wordbuf) - 1] = 0;
279 scr_putc(8);
280 scr_putc(32);
281 scr_putc(8);
283 } else if (a == 23) {
284 do {
285 wordbuf[strlen(wordbuf) - 1] = 0;
286 scr_putc(8);
287 scr_putc(32);
288 scr_putc(8);
289 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
290 } else if (a == 13) {
291 scr_printf("\n");
292 if (IsEmptyStr(wordbuf))
293 finished = 1;
294 else {
295 for (b = 0; b < strlen(wordbuf); ++b)
296 if (wordbuf[b] == 32) {
297 wordbuf[b] = 0;
298 add_word(textlist,
299 wordbuf);
300 strcpy(wordbuf,
301 &wordbuf[b + 1]);
302 b = 0;
304 add_word(textlist, wordbuf);
305 strcpy(wordbuf, "");
307 } else {
308 scr_putc(a);
309 wordbuf[strlen(wordbuf) + 1] = 0;
310 wordbuf[strlen(wordbuf)] = a;
312 if ((strlen(wordbuf) + 3) > screenwidth) {
313 last_space = (-1);
314 for (b = 0; b < strlen(wordbuf); ++b)
315 if (wordbuf[b] == 32)
316 last_space = b;
317 if (last_space >= 0) {
318 for (b = 0; b < strlen(wordbuf); ++b)
319 if (wordbuf[b] == 32) {
320 wordbuf[b] = 0;
321 add_word(textlist,
322 wordbuf);
323 strcpy(wordbuf,
324 &wordbuf[b + 1]);
325 b = 0;
327 for (b = 0; b < strlen(wordbuf); ++b) {
328 scr_putc(8);
329 scr_putc(32);
330 scr_putc(8);
332 scr_printf("\n%s", wordbuf);
333 } else {
334 add_word(textlist, wordbuf);
335 strcpy(wordbuf, "");
336 scr_printf("\n");
339 prev = a;
340 } while (finished == 0);
341 async_ka_end();
343 /* write the buffer back to disk */
344 fseek(fp, 0L, 0);
345 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
346 fprintf(fp, "%s", ptr->text);
348 putc(10, fp);
349 fflush(fp);
350 ftruncate(fileno(fp), ftell(fp));
352 /* and deallocate the memory we used */
353 while (textlist != NULL) {
354 ptr = textlist->next;
355 free(textlist);
356 textlist = ptr;
362 * Free the struct parts
364 void free_parts(struct parts *p)
366 struct parts *a_part = p;
368 while (a_part) {
369 struct parts *q;
371 q = a_part;
372 a_part = a_part->next;
373 free(q);
379 * Read a message from the server
381 int read_message(CtdlIPC *ipc,
382 long num, /* message number */
383 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
384 FILE *dest) /* Destination file, NULL for screen */
386 char buf[SIZ];
387 char now[SIZ];
388 int format_type = 0;
389 int fr = 0;
390 int nhdr = 0;
391 struct ctdlipcmessage *message = NULL;
392 int r; /* IPC response code */
393 char *converted_text = NULL;
394 char *lineptr;
395 char *nextline;
396 char *searchptr;
397 int i;
398 char ch;
399 int linelen;
400 int final_line_is_blank = 0;
402 has_images = 0;
404 sigcaught = 0;
405 stty_ctdl(1);
407 strcpy(reply_to, NO_REPLY_TO);
408 strcpy(reply_subject, "");
409 strcpy(reply_references, "");
410 strcpy(reply_inreplyto, "");
412 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
413 if (r / 100 != 1) {
414 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
415 ++lines_printed;
416 lines_printed = checkpagin(lines_printed, pagin, screenheight);
417 stty_ctdl(0);
418 free(message->text);
419 free_parts(message->attachments);
420 free(message);
421 return (0);
424 if (dest) {
425 fprintf(dest, "\n ");
426 } else {
427 scr_printf("\n");
428 ++lines_printed;
429 lines_printed = checkpagin(lines_printed, pagin, screenheight);
430 if (pagin != 2)
431 scr_printf(" ");
433 if (pagin == 1 && !dest) {
434 color(BRIGHT_CYAN);
437 /* View headers only */
438 if (pagin == 2) {
439 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
440 message->nhdr ? "yes" : "no",
441 message->author, message->type,
442 message->msgid);
443 if (!IsEmptyStr(message->subject)) {
444 pprintf("subj=%s\n", message->subject);
446 if (!IsEmptyStr(message->email)) {
447 pprintf("rfca=%s\n", message->email);
449 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
450 message->hnod, message->room,
451 message->node,
452 asctime(localtime(&message->time)));
453 if (!IsEmptyStr(message->recipient)) {
454 pprintf("rcpt=%s\n", message->recipient);
456 if (message->attachments) {
457 struct parts *ptr;
459 for (ptr = message->attachments; ptr; ptr = ptr->next) {
460 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
461 ptr->name, ptr->filename, ptr->number,
462 ptr->disposition, ptr->mimetype,
463 ptr->length);
466 pprintf("\n");
467 stty_ctdl(0);
468 free(message->text);
469 free_parts(message->attachments);
470 free(message);
471 return (0);
474 if (rc_display_message_numbers) {
475 if (dest) {
476 fprintf(dest, "[#%s] ", message->msgid);
477 } else {
478 color(DIM_WHITE);
479 scr_printf("[");
480 color(BRIGHT_WHITE);
481 scr_printf("#%s", message->msgid);
482 color(DIM_WHITE);
483 scr_printf("] ");
486 if (nhdr == 1 && !is_room_aide) {
487 if (dest) {
488 fprintf(dest, " ****");
489 } else {
490 scr_printf(" ****");
492 } else {
493 fmt_date(now, sizeof now, message->time, 0);
494 if (dest) {
495 fprintf(dest, "%s from %s ", now, message->author);
496 if (!IsEmptyStr(message->email)) {
497 fprintf(dest, "<%s> ", message->email);
499 } else {
500 color(BRIGHT_CYAN);
501 scr_printf("%s ", now);
502 color(DIM_WHITE);
503 scr_printf("from ");
504 color(BRIGHT_CYAN);
505 scr_printf("%s ", message->author);
506 if (!IsEmptyStr(message->email)) {
507 color(DIM_WHITE);
508 scr_printf("<");
509 color(BRIGHT_BLUE);
510 scr_printf("%s", message->email);
511 color(DIM_WHITE);
512 scr_printf("> ");
515 if (!IsEmptyStr(message->node)) {
516 if ((room_flags & QR_NETWORK)
517 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
518 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
519 if (IsEmptyStr(message->email)) {
520 if (dest) {
521 fprintf(dest, "@%s ", message->node);
522 } else {
523 color(DIM_WHITE);
524 scr_printf("@");
525 color(BRIGHT_YELLOW);
526 scr_printf("%s ", message->node);
531 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
532 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
533 if (dest) {
534 fprintf(dest, "(%s) ", message->hnod);
535 } else {
536 color(DIM_WHITE);
537 scr_printf("(");
538 color(BRIGHT_WHITE);
539 scr_printf("%s", message->hnod);
540 color(DIM_WHITE);
541 scr_printf(") ");
544 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
545 if (dest) {
546 fprintf(dest, "in %s> ", message->room);
547 } else {
548 color(DIM_WHITE);
549 scr_printf("in ");
550 color(BRIGHT_MAGENTA);
551 scr_printf("%s> ", message->room);
554 if (!IsEmptyStr(message->recipient)) {
555 if (dest) {
556 fprintf(dest, "to %s ", message->recipient);
557 } else {
558 color(DIM_WHITE);
559 scr_printf("to ");
560 color(BRIGHT_CYAN);
561 scr_printf("%s ", message->recipient);
566 if (dest) {
567 fprintf(dest, "\n");
568 } else {
569 scr_printf("\n");
572 /* Set the reply-to address to an Internet e-mail address if possible
574 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
575 if (!IsEmptyStr(message->author)) {
576 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
578 else {
579 safestrncpy(reply_to, message->email, sizeof reply_to);
583 /* But if we can't do that, set it to a Citadel address.
585 if (!strcmp(reply_to, NO_REPLY_TO)) {
586 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
587 message->author, message->node);
590 if (!dest) {
591 ++lines_printed;
592 lines_printed = checkpagin(lines_printed, pagin, screenheight);
596 if (message->msgid != NULL) {
597 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
600 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
601 safestrncpy(reply_references, message->references, sizeof reply_references);
604 if (message->subject != NULL) {
605 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
606 if (!IsEmptyStr(message->subject)) {
607 if (dest) {
608 fprintf(dest, "Subject: %s\n",
609 message->subject);
610 } else {
611 color(DIM_WHITE);
612 scr_printf("Subject: ");
613 color(BRIGHT_CYAN);
614 scr_printf("%s\n", message->subject);
615 ++lines_printed;
616 lines_printed = checkpagin(lines_printed,
617 pagin, screenheight);
622 if (pagin == 1 && !dest) {
623 color(BRIGHT_WHITE);
626 /******* end of header output, start of message text output *******/
629 * Convert HTML to plain text, formatting for the actual width
630 * of the client screen.
632 if (!strcasecmp(message->content_type, "text/html")) {
633 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
634 if (converted_text != NULL) {
635 free(message->text);
636 message->text = converted_text;
637 format_type = 1;
641 /* Text/plain is a different type */
642 if (!strcasecmp(message->content_type, "text/plain")) {
643 format_type = 1;
646 /* Extract URL's */
647 num_urls = 0; /* Start with a clean slate */
648 searchptr = message->text;
649 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
650 searchptr = strstr(searchptr, "http://");
651 if (searchptr != NULL) {
652 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
653 for (i = 0; i < strlen(urls[num_urls]); i++) {
654 ch = urls[num_urls][i];
655 if (ch == '>' || ch == '\"' || ch == ')' ||
656 ch == ' ' || ch == '\n') {
657 urls[num_urls][i] = 0;
658 break;
661 num_urls++;
662 ++searchptr;
667 * Here we go
669 if (format_type == 0) {
670 fr = fmout(screenwidth, NULL, message->text, dest,
671 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
672 } else {
673 /* renderer for text/plain */
675 lineptr = message->text;
677 do {
678 nextline = strchr(lineptr, '\n');
679 if (nextline != NULL) {
680 *nextline = 0;
681 ++nextline;
682 if (*nextline == 0) nextline = NULL;
685 if (sigcaught == 0) {
686 linelen = strlen(lineptr);
687 if (linelen && (lineptr[linelen-1] == '\r')) {
688 lineptr[--linelen] = 0;
690 if (dest) {
691 fprintf(dest, "%s\n", lineptr);
692 } else {
693 scr_printf("%s\n", lineptr);
694 lines_printed = lines_printed + 1 +
695 (linelen / screenwidth);
696 lines_printed =
697 checkpagin(lines_printed, pagin,
698 screenheight);
701 if (lineptr[0] == 0) final_line_is_blank = 1;
702 else final_line_is_blank = 0;
703 lineptr = nextline;
704 } while (nextline);
705 fr = sigcaught;
707 if (!final_line_is_blank) {
708 if (dest) {
709 fprintf(dest, "\n");
711 else {
712 scr_printf("\n");
713 ++lines_printed;
714 lines_printed = checkpagin(lines_printed, pagin, screenheight);
715 fr = sigcaught;
719 /* Enumerate any attachments */
720 if ( (pagin == 1) && (message->attachments) ) {
721 struct parts *ptr;
723 for (ptr = message->attachments; ptr; ptr = ptr->next) {
724 if ( (!strcasecmp(ptr->disposition, "attachment"))
725 || (!strcasecmp(ptr->disposition, "inline"))
726 || (!strcasecmp(ptr->disposition, ""))
728 if ( (strcasecmp(ptr->number, message->mime_chosen))
729 && (!IsEmptyStr(ptr->mimetype))
731 color(DIM_WHITE);
732 pprintf("Part ");
733 color(BRIGHT_MAGENTA);
734 pprintf("%s", ptr->number);
735 color(DIM_WHITE);
736 pprintf(": ");
737 color(BRIGHT_CYAN);
738 pprintf("%s", ptr->filename);
739 color(DIM_WHITE);
740 pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
741 if (!strncmp(ptr->mimetype, "image/", 6)) {
742 has_images++;
749 /* Save the attachments info for later */
750 last_message_parts = message->attachments;
752 /* Now we're done */
753 free(message->text);
754 free(message);
756 if (pagin == 1 && !dest)
757 color(DIM_WHITE);
758 stty_ctdl(0);
759 return (fr);
763 * replace string function for the built-in editor
765 void replace_string(char *filename, long int startpos)
767 char buf[512];
768 char srch_str[128];
769 char rplc_str[128];
770 FILE *fp;
771 int a;
772 long rpos, wpos;
773 char *ptr;
774 int substitutions = 0;
775 long msglen = 0L;
777 scr_printf("Enter text to be replaced:\n: ");
778 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
779 if (IsEmptyStr(srch_str))
780 return;
782 scr_printf("Enter text to replace it with:\n: ");
783 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
785 fp = fopen(filename, "r+");
786 if (fp == NULL)
787 return;
789 wpos = startpos;
790 fseek(fp, startpos, 0);
791 strcpy(buf, "");
792 while (a = getc(fp), a > 0) {
793 ++msglen;
794 buf[strlen(buf) + 1] = 0;
795 buf[strlen(buf)] = a;
796 if (strlen(buf) >= strlen(srch_str)) {
797 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
798 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
799 strcpy(ptr, rplc_str);
800 ++substitutions;
803 if (strlen(buf) > 384) {
804 rpos = ftell(fp);
805 fseek(fp, wpos, 0);
806 fwrite((char *) buf, 128, 1, fp);
807 strcpy(buf, &buf[128]);
808 wpos = ftell(fp);
809 fseek(fp, rpos, 0);
812 fseek(fp, wpos, 0);
813 if (!IsEmptyStr(buf))
814 fwrite((char *) buf, strlen(buf), 1, fp);
815 wpos = ftell(fp);
816 fclose(fp);
817 truncate(filename, wpos);
818 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
822 * Function to begin composing a new message
824 int client_make_message(CtdlIPC *ipc,
825 char *filename, /* temporary file name */
826 char *recipient, /* NULL if it's not mail */
827 int is_anonymous,
828 int format_type,
829 int mode,
830 char *subject, /* buffer to store subject line */
831 int subject_required)
833 FILE *fp;
834 int a, b, e_ex_code;
835 long beg;
836 char datestr[SIZ];
837 char header[SIZ];
838 char *editor_path = NULL;
839 int cksum = 0;
841 if (mode >= 2)
843 if((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2])) {
844 editor_path = editor_paths[mode-2];
845 } else if (!IsEmptyStr(editor_paths[0])) {
846 editor_path = editor_paths[0];
847 } else {
848 err_printf("*** No editor available, "
849 "using built-in editor\n");
850 mode = 0;
854 fmt_date(datestr, sizeof datestr, time(NULL), 0);
855 header[0] = 0;
857 if (room_flags & QR_ANONONLY && !recipient) {
858 snprintf(header, sizeof header, " ****");
860 else {
861 snprintf(header, sizeof header,
862 " %s from %s",
863 datestr,
864 (is_anonymous ? "[anonymous]" : fullname)
866 if (!IsEmptyStr(recipient)) {
867 size_t tmp = strlen(header);
868 snprintf(&header[tmp], sizeof header - tmp,
869 " to %s", recipient);
872 scr_printf("%s\n", header);
873 if (subject != NULL) if (!IsEmptyStr(subject)) {
874 scr_printf("Subject: %s\n", subject);
877 if ( (subject_required) && (IsEmptyStr(subject)) ) {
878 newprompt("Subject: ", subject, 70);
881 beg = 0L;
883 if (mode == 1) {
884 scr_printf("(Press ctrl-d when finished)\n");
887 if (mode == 0) {
888 fp = fopen(filename, "r");
889 if (fp != NULL) {
890 fmout(screenwidth, fp, NULL, NULL, 0,
891 screenheight, 0, 0);
892 beg = ftell(fp);
893 fclose(fp);
894 } else {
895 fp = fopen(filename, "w");
896 if (fp == NULL) {
897 err_printf("*** Error opening temp file!\n"
898 " %s: %s\n",
899 filename, strerror(errno));
900 return(1);
902 fclose(fp);
906 ME1: switch (mode) {
908 case 0:
909 fp = fopen(filename, "r+");
910 if (fp == NULL) {
911 err_printf("*** Error opening temp file!\n"
912 " %s: %s\n",
913 filename, strerror(errno));
914 return(1);
916 citedit(ipc, fp);
917 fclose(fp);
918 goto MECR;
920 case 1:
921 fp = fopen(filename, "a");
922 if (fp == NULL) {
923 err_printf("*** Error opening temp file!\n"
924 " %s: %s\n",
925 filename, strerror(errno));
926 return(1);
928 do {
929 a = inkey();
930 if (a == 255)
931 a = 32;
932 if (a == 13)
933 a = 10;
934 if (a != 4) {
935 putc(a, fp);
936 scr_putc(a);
938 if (a == 10)
939 scr_putc(10);
940 } while (a != 4);
941 fclose(fp);
942 break;
944 case 2:
945 default: /* allow 2+ modes */
946 e_ex_code = 1; /* start with a failed exit code */
947 screen_reset();
948 stty_ctdl(SB_RESTORE);
949 editor_pid = fork();
950 cksum = file_checksum(filename);
951 if (editor_pid == 0) {
952 char tmp[SIZ];
954 chmod(filename, 0600);
955 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
956 putenv(tmp);
957 execlp(editor_path, editor_path, filename, NULL);
958 exit(1);
960 if (editor_pid > 0)
961 do {
962 e_ex_code = 0;
963 b = ka_wait(&e_ex_code);
964 } while ((b != editor_pid) && (b >= 0));
965 editor_pid = (-1);
966 stty_ctdl(0);
967 screen_set();
968 break;
971 MECR: if (mode >= 2) {
972 if (file_checksum(filename) == cksum) {
973 err_printf("*** Aborted message.\n");
974 e_ex_code = 1;
976 if (e_ex_code == 0) {
977 goto MEFIN;
979 goto MEABT2;
982 b = keymenu("Entry command (? for options)",
983 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
984 "add s<U>bject|"
985 "<R>eplace string|<H>old message");
987 if (b == 'a') goto MEABT;
988 if (b == 'c') goto ME1;
989 if (b == 's') goto MEFIN;
990 if (b == 'p') {
991 scr_printf(" %s from %s", datestr, fullname);
992 if (!IsEmptyStr(recipient)) {
993 scr_printf(" to %s", recipient);
995 scr_printf("\n");
996 if (subject != NULL) if (!IsEmptyStr(subject)) {
997 scr_printf("Subject: %s\n", subject);
999 fp = fopen(filename, "r");
1000 if (fp != NULL) {
1001 fmout(screenwidth, fp, NULL, NULL,
1002 ((userflags & US_PAGINATOR) ? 1 : 0),
1003 screenheight, 0, 0);
1004 beg = ftell(fp);
1005 fclose(fp);
1007 goto MECR;
1009 if (b == 'r') {
1010 replace_string(filename, 0L);
1011 goto MECR;
1013 if (b == 'h') {
1014 return (2);
1016 if (b == 'u') {
1017 if (subject != NULL) {
1018 newprompt("Subject: ", subject, 70);
1020 goto MECR;
1023 MEFIN: return (0);
1025 MEABT: scr_printf("Are you sure? ");
1026 if (yesno() == 0) {
1027 goto ME1;
1029 MEABT2: unlink(filename);
1030 return (2);
1035 * Make sure there's room in msg_arr[] for at least one more.
1037 void check_msg_arr_size(void) {
1038 if ((num_msgs + 1) > msg_arr_size) {
1039 msg_arr_size += 512;
1040 msg_arr = realloc(msg_arr,
1041 ((sizeof(long)) * msg_arr_size) );
1047 * break_big_lines() - break up lines that are >1024 characters
1048 * otherwise the server will truncate
1050 void break_big_lines(char *msg) {
1051 char *ptr;
1052 char *break_here;
1054 if (msg == NULL) {
1055 return;
1058 ptr = msg;
1059 while (strlen(ptr) > 1000) {
1060 break_here = strchr(&ptr[900], ' ');
1061 if ((break_here == NULL) || (break_here > &ptr[999])) {
1062 break_here = &ptr[999];
1064 *break_here = '\n';
1065 ptr = break_here++;
1071 * entmsg() - edit and create a message
1072 * returns 0 if message was saved
1074 int entmsg(CtdlIPC *ipc,
1075 int is_reply, /* nonzero if this was a <R>eply command */
1076 int c, /* mode */
1077 int masquerade /* prompt for a non-default display name? */
1079 char buf[SIZ];
1080 int a, b;
1081 int need_recp = 0;
1082 int mode;
1083 long highmsg = 0L;
1084 FILE *fp;
1085 char subject[SIZ];
1086 struct ctdlipcmessage message;
1087 unsigned long *msgarr = NULL;
1088 int r; /* IPC response code */
1089 int subject_required = 0;
1091 if (c > 0)
1092 mode = 1;
1093 else
1094 mode = 0;
1096 strcpy(subject, "");
1099 * First, check to see if we have permission to enter a message in
1100 * this room. The server will return an error code if we can't.
1102 strcpy(message.recipient, "");
1103 strcpy(message.author, "");
1104 strcpy(message.subject, "");
1105 strcpy(message.references, "");
1106 message.text = ""; /* point to "", changes later */
1107 message.anonymous = 0;
1108 message.type = mode;
1110 if (masquerade) {
1111 newprompt("Display name for this message: ", message.author, 40);
1114 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1116 if (r / 100 != 2 && r / 10 != 57) {
1117 scr_printf("%s\n", buf);
1118 return (1);
1121 /* Error code 570 is special. It means that we CAN enter a message
1122 * in this room, but a recipient needs to be specified.
1124 need_recp = 0;
1125 if (r / 10 == 57) {
1126 need_recp = 1;
1129 /* If the user is a dumbass, tell them how to type. */
1130 if ((userflags & US_EXPERT) == 0) {
1131 formout(ipc, "entermsg");
1134 /* Handle the selection of a recipient, if necessary. */
1135 strcpy(buf, "");
1136 if (need_recp == 1) {
1137 if (axlevel >= 2) {
1138 if (is_reply) {
1139 strcpy(buf, reply_to);
1140 } else {
1141 scr_printf("Enter recipient: ");
1142 ctdl_getline(buf, (SIZ-100) );
1143 if (IsEmptyStr(buf))
1144 return (1);
1146 } else
1147 strcpy(buf, "sysop");
1149 strcpy(message.recipient, buf);
1151 if (is_reply) {
1153 if (!IsEmptyStr(reply_subject)) {
1154 if (!strncasecmp(reply_subject,
1155 "Re: ", 3)) {
1156 strcpy(message.subject, reply_subject);
1158 else {
1159 snprintf(message.subject,
1160 sizeof message.subject,
1161 "Re: %s",
1162 reply_subject);
1166 /* Trim down excessively long lists of thread references. We eliminate the
1167 * second one in the list so that the thread root remains intact.
1169 int rrtok = num_tokens(reply_references, '|');
1170 int rrlen = strlen(reply_references);
1171 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1172 remove_token(reply_references, 1, '|');
1175 snprintf(message.references, sizeof message.references, "%s%s%s",
1176 reply_references,
1177 (IsEmptyStr(reply_references) ? "" : "|"),
1178 reply_inreplyto
1182 if (room_flags & QR_ANONOPT) {
1183 scr_printf("Anonymous (Y/N)? ");
1184 if (yesno() == 1)
1185 message.anonymous = 1;
1188 /* If it's mail, we've got to check the validity of the recipient... */
1189 if (!IsEmptyStr(message.recipient)) {
1190 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1191 if (r / 100 != 2) {
1192 scr_printf("%s\n", buf);
1193 return (1);
1197 /* Learn the number of the newest message in in the room, so we can
1198 * tell upon saving whether someone else has posted too.
1200 num_msgs = 0;
1201 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1202 if (r / 100 != 1) {
1203 scr_printf("%s\n", buf);
1204 } else {
1205 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1209 /* Now compose the message... */
1210 if (client_make_message(ipc, temp, message.recipient,
1211 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1212 if (msgarr) free(msgarr);
1213 return (2);
1216 /* Reopen the temp file that was created, so we can send it */
1217 fp = fopen(temp, "r");
1219 if (!fp || !(message.text = load_message_from_file(fp))) {
1220 err_printf("*** Internal error while trying to save message!\n"
1221 "%s: %s\n",
1222 temp, strerror(errno));
1223 unlink(temp);
1224 return(errno);
1227 if (fp) fclose(fp);
1229 /* Break lines that are >1024 characters, otherwise the server
1230 * will truncate them.
1232 break_big_lines(message.text);
1234 /* Transmit message to the server */
1235 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1236 if (r / 100 != 4) {
1237 scr_printf("%s\n", buf);
1238 return (1);
1241 /* Yes, unlink it now, so it doesn't stick around if we crash */
1242 unlink(temp);
1244 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1246 if (msgarr) free(msgarr);
1247 msgarr = NULL;
1248 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1249 if (r / 100 != 1) {
1250 scr_printf("%s\n", buf);
1251 } else {
1252 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1256 /* get new highest message number in room to set lrp for goto... */
1257 maxmsgnum = msgarr[num_msgs - 1];
1259 /* now see if anyone else has posted in here */
1260 b = (-1);
1261 for (a = 0; a < num_msgs; ++a) {
1262 if (msgarr[a] > highmsg) {
1263 ++b;
1266 if (msgarr) free(msgarr);
1267 msgarr = NULL;
1269 /* In the Mail> room, this algorithm always counts one message
1270 * higher than in public rooms, so we decrement it by one.
1272 if (need_recp) {
1273 --b;
1276 if (b == 1) {
1277 scr_printf("*** 1 additional message has been entered "
1278 "in this room by another user.\n");
1280 else if (b > 1) {
1281 scr_printf("*** %d additional messages have been entered "
1282 "in this room by other users.\n", b);
1284 free(message.text);
1286 return(0);
1290 * Do editing on a quoted file
1292 void process_quote(void)
1294 FILE *qfile, *tfile;
1295 char buf[128];
1296 int line, qstart, qend;
1298 /* Unlink the second temp file as soon as it's opened, so it'll get
1299 * deleted even if the program dies
1301 qfile = fopen(temp2, "r");
1302 unlink(temp2);
1304 /* Display the quotable text with line numbers added */
1305 line = 0;
1306 fgets(buf, 128, qfile);
1307 while (fgets(buf, 128, qfile) != NULL) {
1308 scr_printf("%3d %s", ++line, buf);
1310 scr_printf("Begin quoting at [1] : ");
1311 ctdl_getline(buf, 4);
1312 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1313 scr_printf(" End quoting at [%d] : ", line);
1314 ctdl_getline(buf, 4);
1315 qend = (buf[0] == 0) ? (line) : atoi(buf);
1316 rewind(qfile);
1317 line = 0;
1318 fgets(buf, 128, qfile);
1319 tfile = fopen(temp, "w");
1320 while (fgets(buf, 128, qfile) != NULL) {
1321 if ((++line >= qstart) && (line <= qend))
1322 fprintf(tfile, " >%s", buf);
1324 fprintf(tfile, " \n");
1325 fclose(qfile);
1326 fclose(tfile);
1327 chmod(temp, 0666);
1333 * List the URL's which were embedded in the previous message
1335 void list_urls(CtdlIPC *ipc)
1337 int i;
1338 char cmd[SIZ];
1340 if (num_urls == 0) {
1341 scr_printf("There were no URL's in the previous message.\n\n");
1342 return;
1345 for (i = 0; i < num_urls; ++i) {
1346 scr_printf("%3d %s\n", i + 1, urls[i]);
1349 if ((i = num_urls) != 1)
1350 i = intprompt("Display which one", 1, 1, num_urls);
1352 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1353 system(cmd);
1354 scr_printf("\n");
1359 * Run image viewer in background
1361 int do_image_view(const char *filename)
1363 char cmd[SIZ];
1364 pid_t childpid;
1366 snprintf(cmd, sizeof cmd, imagecmd, filename);
1367 childpid = fork();
1368 if (childpid < 0) {
1369 unlink(filename);
1370 return childpid;
1373 if (childpid == 0) {
1374 int retcode;
1375 pid_t grandchildpid;
1377 grandchildpid = fork();
1378 if (grandchildpid < 0) {
1379 return grandchildpid;
1382 if (grandchildpid == 0) {
1383 int nullfd;
1384 int outfd = -1;
1385 int errfd = -1;
1387 nullfd = open("/dev/null", O_WRONLY);
1388 if (nullfd > -1) {
1389 dup2(1, outfd);
1390 dup2(2, errfd);
1391 dup2(nullfd, 1);
1392 dup2(nullfd, 2);
1394 retcode = system(cmd);
1395 if (nullfd > -1) {
1396 dup2(outfd, 1);
1397 dup2(errfd, 2);
1398 close(nullfd);
1400 unlink(filename);
1401 exit(retcode);
1404 if (grandchildpid > 0) {
1405 exit(0);
1409 if (childpid > 0) {
1410 int retcode;
1412 waitpid(childpid, &retcode, 0);
1413 return retcode;
1416 return -1;
1421 * View an image attached to a message
1423 void image_view(CtdlIPC *ipc, unsigned long msg)
1425 struct parts *ptr = last_message_parts;
1426 char part[SIZ];
1427 int found = 0;
1429 /* Run through available parts */
1430 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1431 if ((!strcasecmp(ptr->disposition, "attachment")
1432 || !strcasecmp(ptr->disposition, "inline"))
1433 && !strncmp(ptr->mimetype, "image/", 6)) {
1434 found++;
1435 if (found == 1) {
1436 strcpy(part, ptr->number);
1441 while (found > 0) {
1442 if (found > 1)
1443 strprompt("View which part (0 when done)", part, SIZ-1);
1444 found = -found;
1445 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1446 if ((!strcasecmp(ptr->disposition, "attachment")
1447 || !strcasecmp(ptr->disposition, "inline"))
1448 && !strncmp(ptr->mimetype, "image/", 6)
1449 && !strcasecmp(ptr->number, part)) {
1450 char tmp[PATH_MAX];
1451 char buf[SIZ];
1452 void *file = NULL; /* The downloaded file */
1453 int r;
1455 /* view image */
1456 found = -found;
1457 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1458 if (r / 100 != 2) {
1459 scr_printf("%s\n", buf);
1460 } else {
1461 size_t len;
1463 len = (size_t)extract_long(buf, 0);
1464 progress(ipc, len, len);
1465 scr_flush();
1466 CtdlMakeTempFileName(tmp, sizeof tmp);
1467 strcat(tmp, ptr->filename);
1468 save_buffer(file, len, tmp);
1469 free(file);
1470 do_image_view(tmp);
1472 break;
1475 if (found == 1)
1476 break;
1482 * Read the messages in the current room
1484 void readmsgs(CtdlIPC *ipc,
1485 enum MessageList c, /* see listing in citadel_ipc.h */
1486 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1487 int q /* Number of msgs to read (if c==3) */
1489 int a, b, e, f, g, start;
1490 int savedpos;
1491 int hold_sw = 0;
1492 char arcflag = 0;
1493 char quotflag = 0;
1494 int hold_color = 0;
1495 char prtfile[PATH_MAX];
1496 char pagin;
1497 char cmd[SIZ];
1498 char targ[ROOMNAMELEN];
1499 char filename[PATH_MAX];
1500 char save_to[PATH_MAX];
1501 void *attachment = NULL; /* Downloaded attachment */
1502 FILE *dest = NULL; /* Alternate destination other than screen */
1503 int r; /* IPC response code */
1504 static int att_seq = 0; /* Attachment download sequence number */
1506 if (c < 0)
1507 b = (num_msgs - 1);
1508 else
1509 b = 0;
1511 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1513 if (msg_arr) {
1514 free(msg_arr);
1515 msg_arr = NULL;
1517 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1518 if (r / 100 != 1) {
1519 scr_printf("%s\n", cmd);
1520 } else {
1521 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1525 if (num_msgs == 0) { /* TODO look at this later */
1526 if (c == LastMessages) return;
1527 scr_printf("*** There are no ");
1528 if (c == NewMessages) scr_printf("new ");
1529 if (c == OldMessages) scr_printf("old ");
1530 scr_printf("messages in this room.\n");
1531 return;
1534 lines_printed = 0;
1536 /* this loop cycles through each message... */
1537 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1538 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1539 while (msg_arr[a] == 0L) {
1540 a = a + rdir;
1541 if ((a == num_msgs) || (a == (-1)))
1542 return;
1545 RAGAIN: pagin = ((arcflag == 0)
1546 && (quotflag == 0)
1547 && (userflags & US_PAGINATOR)) ? 1 : 0;
1549 /* If we're doing a quote, set the screenwidth to 72 */
1550 if (quotflag) {
1551 hold_sw = screenwidth;
1552 screenwidth = 72;
1555 /* If printing or archiving, set the screenwidth to 80 */
1556 if (arcflag) {
1557 hold_sw = screenwidth;
1558 screenwidth = 80;
1561 /* clear parts list */
1562 free_parts(last_message_parts);
1563 last_message_parts = NULL;
1565 /* now read the message... */
1566 e = read_message(ipc, msg_arr[a], pagin, dest);
1568 /* ...and set the screenwidth back if we have to */
1569 if ((quotflag) || (arcflag)) {
1570 screenwidth = hold_sw;
1572 RMSGREAD: scr_flush();
1573 highest_msg_read = msg_arr[a];
1574 if (quotflag) {
1575 fclose(dest);
1576 dest = NULL;
1577 quotflag = 0;
1578 enable_color = hold_color;
1579 process_quote();
1580 e = 'r';
1581 goto DONE_QUOTING;
1583 if (arcflag) {
1584 fclose(dest);
1585 dest = NULL;
1586 arcflag = 0;
1587 enable_color = hold_color;
1588 f = fork();
1589 if (f == 0) {
1590 freopen(prtfile, "r", stdin);
1591 screen_reset();
1592 stty_ctdl(SB_RESTORE);
1593 ka_system(printcmd);
1594 stty_ctdl(SB_NO_INTR);
1595 screen_set();
1596 unlink(prtfile);
1597 exit(0);
1599 if (f > 0)
1600 do {
1601 g = wait(NULL);
1602 } while ((g != f) && (g >= 0));
1603 scr_printf("Message printed.\n");
1605 if (rc_alt_semantics && c == 1) {
1606 char buf[SIZ];
1608 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1610 if (e == SIGQUIT)
1611 return;
1612 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1613 && (((room_flags & QR_MAILBOX) == 0)
1614 || (rc_force_mail_prompts == 0))) {
1615 e = 'n';
1616 } else {
1617 color(DIM_WHITE);
1618 scr_printf("(");
1619 color(BRIGHT_WHITE);
1620 scr_printf("%d", num_msgs - a - 1);
1621 color(DIM_WHITE);
1622 scr_printf(") ");
1624 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1625 if (rc_url_cmd[0] && num_urls)
1626 keyopt("<U>RLview ");
1627 if (has_images > 0 && !IsEmptyStr(imagecmd))
1628 keyopt("<I>mages ");
1629 keyopt("<?>help -> ");
1631 do {
1632 lines_printed = 2;
1633 e = (inkey() & 127);
1634 e = tolower(e);
1635 /* return key same as <N> */ if (e == 10)
1636 e = 'n';
1637 /* space key same as <N> */ if (e == 32)
1638 e = 'n';
1639 /* del/move for aides only */
1640 if ( (!is_room_aide)
1641 && ((room_flags & QR_MAILBOX) == 0)
1642 && ((room_flags2 & QR2_COLLABDEL) == 0)
1644 if ((e == 'd') || (e == 'm'))
1645 e = 0;
1647 /* print only if available */
1648 if ((e == 'p') && (IsEmptyStr(printcmd)))
1649 e = 0;
1650 /* can't file if not allowed */
1651 if ((e == 'f')
1652 && (rc_allow_attachments == 0))
1653 e = 0;
1654 /* link only if browser avail*/
1655 if ((e == 'u')
1656 && (IsEmptyStr(rc_url_cmd)))
1657 e = 0;
1658 if ((e == 'i')
1659 && (IsEmptyStr(imagecmd) || !has_images))
1660 e = 0;
1661 } while ((e != 'a') && (e != 'n') && (e != 's')
1662 && (e != 'd') && (e != 'm') && (e != 'p')
1663 && (e != 'q') && (e != 'b') && (e != 'h')
1664 && (e != 'r') && (e != 'f') && (e != '?')
1665 && (e != 'u') && (e != 'c') && (e != 'y')
1666 && (e != 'i') && (e != 'o') );
1667 switch (e) {
1668 case 's':
1669 scr_printf("Stop");
1670 break;
1671 case 'a':
1672 scr_printf("Again");
1673 break;
1674 case 'd':
1675 scr_printf("Delete");
1676 break;
1677 case 'm':
1678 scr_printf("Move");
1679 break;
1680 case 'c':
1681 scr_printf("Copy");
1682 break;
1683 case 'n':
1684 scr_printf("Next");
1685 break;
1686 case 'p':
1687 scr_printf("Print");
1688 break;
1689 case 'q':
1690 scr_printf("reply Quoted");
1691 break;
1692 case 'b':
1693 scr_printf("Back");
1694 break;
1695 case 'h':
1696 scr_printf("Header");
1697 break;
1698 case 'r':
1699 scr_printf("Reply");
1700 break;
1701 case 'o':
1702 scr_printf("Open attachments");
1703 break;
1704 case 'f':
1705 scr_printf("File");
1706 break;
1707 case 'u':
1708 scr_printf("URL's");
1709 break;
1710 case 'y':
1711 scr_printf("mY next");
1712 break;
1713 case 'i':
1714 break;
1715 case '?':
1716 scr_printf("? <help>");
1717 break;
1719 if (userflags & US_DISAPPEAR || e == 'i')
1720 scr_printf("\r%79s\r", "");
1721 else
1722 scr_printf("\n");
1723 scr_flush();
1725 DONE_QUOTING: switch (e) {
1726 case '?':
1727 scr_printf("Options available here:\n"
1728 " ? Help (prints this message)\n"
1729 " S Stop reading immediately\n"
1730 " A Again (repeats last message)\n"
1731 " N Next (continue with next message)\n"
1732 " Y My Next (continue with next message you authored)\n"
1733 " B Back (go back to previous message)\n");
1734 if ( (is_room_aide)
1735 || (room_flags & QR_MAILBOX)
1736 || (room_flags2 & QR2_COLLABDEL)
1738 scr_printf(" D Delete this message\n"
1739 " M Move message to another room\n");
1741 scr_printf(" C Copy message to another room\n");
1742 if (!IsEmptyStr(printcmd))
1743 scr_printf(" P Print this message\n");
1744 scr_printf(
1745 " Q Reply to this message, quoting portions of it\n"
1746 " H Headers (display message headers only)\n");
1747 if (is_mail)
1748 scr_printf(" R Reply to this message\n");
1749 if (rc_allow_attachments) {
1750 scr_printf(" O (Open attachments)\n");
1751 scr_printf(" F (save attachments to a File)\n");
1753 if (!IsEmptyStr(rc_url_cmd))
1754 scr_printf(" U (list URL's for display)\n");
1755 if (!IsEmptyStr(imagecmd) && has_images > 0)
1756 scr_printf(" I Image viewer\n");
1757 scr_printf("\n");
1758 goto RMSGREAD;
1759 case 'p':
1760 scr_flush();
1761 dest = fopen(prtfile, "w");
1762 arcflag = 1;
1763 hold_color = enable_color;
1764 enable_color = 0;
1765 goto RAGAIN;
1766 case 'q':
1767 scr_flush();
1768 dest = fopen(temp2, "w");
1769 quotflag = 1;
1770 hold_color = enable_color;
1771 enable_color = 0;
1772 goto RAGAIN;
1773 case 's':
1774 return;
1775 case 'a':
1776 goto RAGAIN;
1777 case 'b':
1778 a = a - (rdir * 2);
1779 break;
1780 case 'm':
1781 case 'c':
1782 newprompt("Enter target room: ",
1783 targ, ROOMNAMELEN - 1);
1784 if (!IsEmptyStr(targ)) {
1785 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1786 msg_arr[a], targ, cmd);
1787 scr_printf("%s\n", cmd);
1788 if (r / 100 == 2)
1789 msg_arr[a] = 0L;
1790 } else {
1791 goto RMSGREAD;
1793 if (r / 100 != 2) /* r will be init'ed, FIXME */
1794 goto RMSGREAD; /* the logic here sucks */
1795 break;
1796 case 'o':
1797 case 'f':
1798 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1799 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1800 filename, &attachment, progress, cmd);
1801 if (r / 100 != 2) {
1802 scr_printf("%s\n", cmd);
1803 } else {
1804 extract_token(filename, cmd, 2, '|', sizeof filename);
1806 * Part 1 won't have a filename; use the
1807 * subject of the message instead. IO
1809 if (IsEmptyStr(filename)) {
1810 strcpy(filename, reply_subject);
1812 if (e == 'o') { /* open attachment */
1813 mkdir(tempdir, 0700);
1814 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1815 tempdir,
1816 ++att_seq,
1817 filename);
1818 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1819 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1820 system(cmd);
1822 else { /* save attachment to disk */
1823 destination_directory(save_to, filename);
1824 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1827 if (attachment) {
1828 free(attachment);
1829 attachment = NULL;
1831 goto RMSGREAD;
1832 case 'd':
1833 scr_printf("*** Delete this message? ");
1834 if (yesno() == 1) {
1835 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1836 scr_printf("%s\n", cmd);
1837 if (r / 100 == 2)
1838 msg_arr[a] = 0L;
1839 } else {
1840 goto RMSGREAD;
1842 break;
1843 case 'h':
1844 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1845 goto RMSGREAD;
1846 case 'r':
1847 savedpos = num_msgs;
1848 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1849 num_msgs = savedpos;
1850 goto RMSGREAD;
1851 case 'u':
1852 list_urls(ipc);
1853 goto RMSGREAD;
1854 case 'i':
1855 image_view(ipc, msg_arr[a]);
1856 goto RMSGREAD;
1857 case 'y':
1858 { /* hack hack hack */
1859 /* find the next message by me, stay here if we find nothing */
1860 int finda;
1861 int lasta = a;
1862 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1864 /* This is repetitively dumb, but that's what computers are for.
1865 We have to load up messages until we find one by us */
1866 char buf[SIZ];
1867 int founda = 0;
1868 struct ctdlipcmessage *msg = NULL;
1870 /* read the header so we can get 'from=' */
1871 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1872 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1873 a = lasta; /* meesa current */
1874 founda = 1;
1877 free(msg);
1879 if (founda)
1880 break; /* for */
1881 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1882 } /* for */
1883 } /* case 'y' */
1884 } /* switch */
1885 } /* end for loop */
1886 } /* end read routine */
1892 * View and edit a system message
1894 void edit_system_message(CtdlIPC *ipc, char *which_message)
1896 char desc[SIZ];
1897 char read_cmd[SIZ];
1898 char write_cmd[SIZ];
1900 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1901 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1902 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1903 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1910 * Verify the message base
1912 void check_message_base(CtdlIPC *ipc)
1914 char buf[SIZ];
1915 char *transcript = NULL;
1916 int r; /* IPC response code */
1918 scr_printf
1919 ("Please read the documentation before running this command.\n"
1920 "Having done so, do you still want to check the message base? ");
1921 if (yesno() == 0)
1922 return;
1924 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1925 if (r / 100 != 1) {
1926 scr_printf("%s\n", buf);
1927 return;
1930 while (transcript && !IsEmptyStr(transcript)) {
1931 lines_printed = 1;
1932 extract_token(buf, transcript, 0, '\n', sizeof buf);
1933 remove_token(transcript, 0, '\n');
1934 pprintf("%s\n", buf);
1936 if (transcript) free(transcript);
1937 return;
1942 * Loads the contents of a file into memory. Caller must free the allocated
1943 * memory.
1945 char *load_message_from_file(FILE *src)
1947 size_t i;
1948 size_t got = 0;
1949 char *dest = NULL;
1951 fseek(src, 0, SEEK_END);
1952 i = ftell(src);
1953 rewind(src);
1955 dest = (char *)calloc(1, i + 1);
1956 if (!dest)
1957 return NULL;
1959 while (got < i) {
1960 size_t g;
1962 g = fread(dest + got, 1, i - got, src);
1963 got += g;
1964 if (g < i - got) {
1965 /* Interrupted system call, keep going */
1966 if (errno == EINTR)
1967 continue;
1968 /* At this point we have either EOF or error */
1969 i = got;
1970 break;
1972 dest[i] = 0;
1975 return dest;