2 * Text client functions for reading and writing of messages
4 * Copyright (c) 1987-2017 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <sys/types.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
45 #include <libcitadel.h>
46 #include "citadel_ipc.h"
47 #include "citadel_decls.h"
50 #include "tuiconfig.h"
54 #define MAXWORDBUF SIZ
55 #define NO_REPLY_TO "nobody ... xxxxxx"
58 char reply_subject[SIZ];
59 char reply_references[SIZ];
60 char reply_inreplyto[SIZ];
64 char text[MAXWORDBUF];
67 void stty_ctdl(int cmd);
68 int haschar(const char *st, int ch);
69 int file_checksum(char *filename);
70 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
72 unsigned long *msg_arr = NULL;
75 extern char room_name[];
76 extern char tempdir[];
77 extern unsigned room_flags;
78 extern unsigned room_flags2;
80 extern long highest_msg_read;
83 extern int screenwidth;
84 extern int screenheight;
85 extern long maxmsgnum;
88 extern char is_room_aide;
89 extern char fullname[];
91 extern unsigned userflags;
92 extern char sigcaught;
93 extern char printcmd[];
94 extern int rc_allow_attachments;
95 extern int rc_display_message_numbers;
96 extern int rc_force_mail_prompts;
97 extern int editor_pid;
98 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
100 char urls[MAXURLS][SIZ];
102 int has_images = 0; /* Current msg has images */
103 struct parts *last_message_parts = NULL; /* Parts from last msg */
107 void ka_sigcatch(int signum)
110 signal(SIGALRM, ka_sigcatch);
111 CtdlIPCNoop(ipc_for_signal_handlers);
116 * server keep-alive version of wait() (needed for external editor)
118 pid_t ka_wait(int *kstatus)
123 signal(SIGALRM, ka_sigcatch);
127 } while (errno == EINTR);
128 signal(SIGALRM, SIG_IGN);
135 * version of system() that uses ka_wait()
137 int ka_system(char *shc)
146 perror("Cannot fork");
148 return ((pid_t) childpid);
152 execlp("/bin/sh", "sh", "-c", shc, NULL);
158 waitpid = ka_wait(&retcode);
159 } while (waitpid != childpid);
169 * add a newline to the buffer...
171 void add_newline(struct cittext *textlist)
176 while (ptr->next != NULL)
179 while (ptr->text[strlen(ptr->text) - 1] == 32)
180 ptr->text[strlen(ptr->text) - 1] = 0;
181 /* strcat(ptr->text,"\n"); */
183 ptr->next = (struct cittext *)
184 malloc(sizeof(struct cittext));
187 strcpy(ptr->text, "");
192 * add a word to the buffer...
194 void add_word(struct cittext *textlist, char *wordbuf)
199 while (ptr->next != NULL)
202 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
203 ptr->next = (struct cittext *)
204 malloc(sizeof(struct cittext));
207 strcpy(ptr->text, "");
210 strcat(ptr->text, wordbuf);
211 strcat(ptr->text, " ");
216 * begin editing of an opened file pointed to by fp
218 void citedit(FILE *fp)
220 int a, prev, finished, b, last_space;
222 struct cittext *textlist = NULL;
224 char wordbuf[MAXWORDBUF];
227 /* first, load the text into the buffer */
229 textlist = (struct cittext *) malloc(sizeof(struct cittext));
230 textlist->next = NULL;
231 strcpy(textlist->text, "");
235 while (a = getc(fp), a >= 0) {
237 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
238 add_word(textlist, wordbuf);
240 if ((prev == 13) || (prev == 10)) {
241 add_word(textlist, "\n");
242 add_newline(textlist);
243 add_word(textlist, "");
246 wordbuf[strlen(wordbuf) + 1] = 0;
247 wordbuf[strlen(wordbuf)] = a;
249 if (strlen(wordbuf) + 3 > screenwidth) {
250 add_word(textlist, wordbuf);
258 prev = (appending ? 13 : (-1));
270 if ((a != 32) && (prev == 13)) {
271 add_word(textlist, "\n");
275 if ((a == 32) && (prev == 13)) {
276 add_word(textlist, "\n");
277 add_newline(textlist);
281 if (!IsEmptyStr(wordbuf)) {
282 wordbuf[strlen(wordbuf) - 1] = 0;
287 } else if (a == 23) {
289 wordbuf[strlen(wordbuf) - 1] = 0;
293 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
294 } else if (a == 13) {
296 if (IsEmptyStr(wordbuf))
299 for (b = 0; b < strlen(wordbuf); ++b)
300 if (wordbuf[b] == 32) {
308 add_word(textlist, wordbuf);
313 wordbuf[strlen(wordbuf) + 1] = 0;
314 wordbuf[strlen(wordbuf)] = a;
316 if ((strlen(wordbuf) + 3) > screenwidth) {
318 for (b = 0; b < strlen(wordbuf); ++b)
319 if (wordbuf[b] == 32)
321 if (last_space >= 0) {
322 for (b = 0; b < strlen(wordbuf); ++b)
323 if (wordbuf[b] == 32) {
331 for (b = 0; b < strlen(wordbuf); ++b) {
336 scr_printf("\n%s", wordbuf);
338 add_word(textlist, wordbuf);
344 } while (finished == 0);
347 /* write the buffer back to disk */
349 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
350 fprintf(fp, "%s", ptr->text);
354 rv = ftruncate(fileno(fp), ftell(fp));
356 scr_printf("failed to set message buffer: %s\n", strerror(errno));
359 /* and deallocate the memory we used */
360 while (textlist != NULL) {
361 ptr = textlist->next;
369 * Free the struct parts
371 void free_parts(struct parts *p)
373 struct parts *a_part = p;
379 a_part = a_part->next;
386 * This is a mini RFC2047 decoder.
387 * It only handles strings encoded from UTF-8 as Quoted-printable.
389 void mini_2047_decode(char *s) {
392 char *qstart = strstr(s, "=?UTF-8?Q?");
395 char *qend = strstr(s, "?=");
398 if (qend <= qstart) return;
400 strcpy(qstart, &qstart[10]);
413 sscanf(ch, "%02x", &c);
415 strcpy(&p[1], &p[3]);
426 strcpy(qend, &qend[2]);
430 * Read a message from the server
432 int read_message(CtdlIPC *ipc,
433 long num, /* message number */
434 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
435 FILE *dest) /* Destination file, NULL for screen */
442 struct ctdlipcmessage *message = NULL;
443 int r; /* IPC response code */
444 char *converted_text = NULL;
451 int final_line_is_blank = 0;
458 strcpy(reply_to, NO_REPLY_TO);
459 strcpy(reply_subject, "");
460 strcpy(reply_references, "");
461 strcpy(reply_inreplyto, "");
463 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
465 scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
468 free_parts(message->attachments);
474 fprintf(dest, "\n ");
480 if (pagin == 1 && !dest) {
484 /* View headers only */
486 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
487 message->nhdr ? "yes" : "no",
488 message->author, message->type,
490 if (!IsEmptyStr(message->subject)) {
491 scr_printf("subj=%s\n", message->subject);
493 if (!IsEmptyStr(message->email)) {
494 scr_printf("rfca=%s\n", message->email);
496 scr_printf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
497 message->hnod, message->room,
499 asctime(localtime(&message->time)));
500 if (!IsEmptyStr(message->recipient)) {
501 scr_printf("rcpt=%s\n", message->recipient);
503 if (message->attachments) {
506 for (ptr = message->attachments; ptr; ptr = ptr->next) {
507 scr_printf("part=%s|%s|%s|%s|%s|%ld\n",
508 ptr->name, ptr->filename, ptr->number,
509 ptr->disposition, ptr->mimetype,
517 free_parts(message->attachments);
522 if (rc_display_message_numbers) {
524 fprintf(dest, "[#%s] ", message->msgid);
529 scr_printf("#%s", message->msgid);
534 if (nhdr == 1 && !is_room_aide) {
536 fprintf(dest, " ****");
542 localtime_r(&message->time, &thetime);
543 strftime(now, sizeof now, "%F %R", &thetime);
545 fprintf(dest, "%s from %s ", now, message->author);
546 if (!IsEmptyStr(message->email)) {
547 fprintf(dest, "<%s> ", message->email);
551 scr_printf("%s ", now);
555 scr_printf("%s ", message->author);
556 if (!IsEmptyStr(message->email)) {
560 scr_printf("%s", message->email);
565 if (!IsEmptyStr(message->node)) {
566 if ((room_flags & QR_NETWORK)
567 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
568 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
569 if (IsEmptyStr(message->email)) {
571 fprintf(dest, "@%s ", message->node);
575 color(BRIGHT_YELLOW);
576 scr_printf("%s ", message->node);
581 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
582 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
584 fprintf(dest, "(%s) ", message->hnod);
589 scr_printf("%s", message->hnod);
594 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
596 fprintf(dest, "in %s> ", message->room);
600 color(BRIGHT_MAGENTA);
601 scr_printf("%s> ", message->room);
604 if (!IsEmptyStr(message->recipient)) {
606 fprintf(dest, "to %s ", message->recipient);
611 scr_printf("%s ", message->recipient);
622 /* Set the reply-to address to an Internet e-mail address if possible
624 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
625 if (!IsEmptyStr(message->author)) {
626 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
629 safestrncpy(reply_to, message->email, sizeof reply_to);
633 /* But if we can't do that, set it to a Citadel address.
635 if (!strcmp(reply_to, NO_REPLY_TO)) {
636 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
637 message->author, message->node);
640 if (message->msgid != NULL) {
641 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
644 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
645 safestrncpy(reply_references, message->references, sizeof reply_references);
648 if (message->subject != NULL) {
649 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
650 if (!IsEmptyStr(message->subject)) {
652 fprintf(dest, "Subject: %s\n", message->subject);
655 scr_printf("Subject: ");
657 mini_2047_decode(message->subject);
658 scr_printf("%s\n", message->subject);
663 if (pagin == 1 && !dest) {
667 /******* end of header output, start of message text output *******/
670 * Convert HTML to plain text, formatting for the actual width
671 * of the client screen.
673 if (!strcasecmp(message->content_type, "text/html")) {
674 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
675 if (converted_text != NULL) {
677 message->text = converted_text;
682 /* Text/plain is a different type */
683 if (!strcasecmp(message->content_type, "text/plain")) {
688 static char *urlprefixes[] = {
694 num_urls = 0; /* Start with a clean slate */
695 for (p=0; p<(sizeof urlprefixes / sizeof(char *)); ++p) {
696 searchptr = message->text;
697 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
698 searchptr = strstr(searchptr, urlprefixes[p]);
699 if (searchptr != NULL) {
700 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
701 for (i = 0; i < strlen(urls[num_urls]); i++) {
702 ch = urls[num_urls][i];
703 if (ch == '>' || ch == '\"' || ch == ')' ||
704 ch == ' ' || ch == '\n') {
705 urls[num_urls][i] = 0;
718 if (format_type == 0) {
719 fr = fmout(screenwidth, NULL, message->text, dest, 1);
721 /* renderer for text/plain */
723 lineptr = message->text;
726 nextline = strchr(lineptr, '\n');
727 if (nextline != NULL) {
730 if (*nextline == 0) nextline = NULL;
733 if (sigcaught == 0) {
734 linelen = strlen(lineptr);
735 if (linelen && (lineptr[linelen-1] == '\r')) {
736 lineptr[--linelen] = 0;
739 fprintf(dest, "%s\n", lineptr);
741 scr_printf("%s\n", lineptr);
744 if (lineptr[0] == 0) final_line_is_blank = 1;
745 else final_line_is_blank = 0;
750 if (!final_line_is_blank) {
760 /* Enumerate any attachments */
761 if ( (pagin == 1) && (message->attachments) ) {
764 for (ptr = message->attachments; ptr; ptr = ptr->next) {
765 if ( (!strcasecmp(ptr->disposition, "attachment"))
766 || (!strcasecmp(ptr->disposition, "inline"))
767 || (!strcasecmp(ptr->disposition, ""))
769 if ( (strcasecmp(ptr->number, message->mime_chosen))
770 && (!IsEmptyStr(ptr->mimetype))
774 color(BRIGHT_MAGENTA);
775 scr_printf("%s", ptr->number);
779 scr_printf("%s", ptr->filename);
781 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
782 if (!strncmp(ptr->mimetype, "image/", 6)) {
790 /* Save the attachments info for later */
791 last_message_parts = message->attachments;
797 if (pagin == 1 && !dest)
804 * replace string function for the built-in editor
806 void replace_string(char *filename, long int startpos)
815 int substitutions = 0;
819 newprompt("Enter text to be replaced: ", srch_str, (sizeof(srch_str)-1) );
820 if (IsEmptyStr(srch_str)) {
824 newprompt("Enter text to replace it with: ", rplc_str, (sizeof(rplc_str)-1) );
826 fp = fopen(filename, "r+");
832 fseek(fp, startpos, 0);
834 while (a = getc(fp), a > 0) {
836 buf[strlen(buf) + 1] = 0;
837 buf[strlen(buf)] = a;
838 if (strlen(buf) >= strlen(srch_str)) {
839 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
840 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
841 strcpy(ptr, rplc_str);
845 if (strlen(buf) > 384) {
848 rv = fwrite((char *) buf, 128, 1, fp);
850 scr_printf("failed to replace string: %s\n", strerror(errno));
853 strcpy(buf, &buf[128]);
859 if (!IsEmptyStr(buf)) {
860 rv = fwrite((char *) buf, strlen(buf), 1, fp);
864 rv = truncate(filename, wpos);
865 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
869 * Function to begin composing a new message
871 int client_make_message(CtdlIPC *ipc,
872 char *filename, /* temporary file name */
873 char *recipient, /* NULL if it's not mail */
877 char *subject, /* buffer to store subject line */
887 if ( (mode == 2) && (IsEmptyStr(editor_path)) ) {
888 scr_printf("*** No editor available; using built-in editor.\n");
893 time_t now = time(NULL);
894 localtime_r(&now, &thetime);
895 strftime(datestr, sizeof datestr, "%F %R", &thetime);
898 if (room_flags & QR_ANONONLY && !recipient) {
899 snprintf(header, sizeof header, " ****");
902 snprintf(header, sizeof header,
905 (is_anonymous ? "[anonymous]" : fullname)
907 if (!IsEmptyStr(recipient)) {
908 size_t tmp = strlen(header);
909 snprintf(&header[tmp], sizeof header - tmp,
910 " to %s", recipient);
913 scr_printf("%s\n", header);
914 if (subject != NULL) if (!IsEmptyStr(subject)) {
915 scr_printf("Subject: %s\n", subject);
918 if ( (subject_required) && (IsEmptyStr(subject)) ) {
919 newprompt("Subject: ", subject, 70);
923 scr_printf("(Press ctrl-d when finished)\n");
927 fp = fopen(filename, "r");
929 fmout(screenwidth, fp, NULL, NULL, 0);
932 scr_printf("failed to get stream position %s\n",
936 fp = fopen(filename, "w");
938 scr_printf("*** Error opening temp file!\n %s: %s\n",
939 filename, strerror(errno)
950 fp = fopen(filename, "r+");
952 scr_printf("*** Error opening temp file!\n %s: %s\n",
953 filename, strerror(errno)
962 fp = fopen(filename, "a");
964 scr_printf("*** Error opening temp file!\n"
966 filename, strerror(errno));
986 default: /* allow 2+ modes */
987 e_ex_code = 1; /* start with a failed exit code */
988 stty_ctdl(SB_RESTORE);
990 cksum = file_checksum(filename);
991 if (editor_pid == 0) {
994 chmod(filename, 0600);
995 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
997 execlp(editor_path, editor_path, filename, NULL);
1003 b = ka_wait(&e_ex_code);
1004 } while ((b != editor_pid) && (b >= 0));
1010 MECR: if (mode >= 2) {
1011 if (file_checksum(filename) == cksum) {
1012 scr_printf("*** Aborted message.\n");
1015 if (e_ex_code == 0) {
1021 b = keymenu("Entry command (? for options)",
1025 "<P>rint formatted|"
1031 if (b == 'a') goto MEABT;
1032 if (b == 'c') goto ME1;
1033 if (b == 's') goto MEFIN;
1035 scr_printf(" %s from %s", datestr, fullname);
1036 if (!IsEmptyStr(recipient)) {
1037 scr_printf(" to %s", recipient);
1040 if (subject != NULL) if (!IsEmptyStr(subject)) {
1041 scr_printf("Subject: %s\n", subject);
1043 fp = fopen(filename, "r");
1045 fmout(screenwidth, fp, NULL, NULL, 0);
1048 scr_printf("failed to get stream position %s\n",
1055 replace_string(filename, 0L);
1062 if (subject != NULL) {
1063 newprompt("Subject: ", subject, 70);
1070 MEABT: scr_printf("Are you sure? ");
1074 MEABT2: unlink(filename);
1080 * Make sure there's room in msg_arr[] for at least one more.
1082 void check_msg_arr_size(void) {
1083 if ((num_msgs + 1) > msg_arr_size) {
1084 msg_arr_size += 512;
1085 msg_arr = realloc(msg_arr,
1086 ((sizeof(long)) * msg_arr_size) );
1092 * break_big_lines() - break up lines that are >1024 characters
1093 * otherwise the server will truncate
1095 void break_big_lines(char *msg) {
1104 while (strlen(ptr) > 1000) {
1105 break_here = strchr(&ptr[900], ' ');
1106 if ((break_here == NULL) || (break_here > &ptr[999])) {
1107 break_here = &ptr[999];
1116 * entmsg() - edit and create a message
1117 * returns 0 if message was saved
1119 int entmsg(CtdlIPC *ipc,
1120 int is_reply, /* nonzero if this was a <R>eply command */
1122 int masquerade /* prompt for a non-default display name? */
1131 struct ctdlipcmessage message;
1132 unsigned long *msgarr = NULL;
1133 int r; /* IPC response code */
1134 int subject_required = 0;
1137 scr_printf("You may not enter messages in this type of room.\n");
1148 strcpy(subject, "");
1151 * First, check to see if we have permission to enter a message in
1152 * this room. The server will return an error code if we can't.
1154 strcpy(message.recipient, "");
1155 strcpy(message.author, "");
1156 strcpy(message.subject, "");
1157 strcpy(message.references, "");
1158 message.text = ""; /* point to "", changes later */
1159 message.anonymous = 0;
1160 message.type = mode;
1163 newprompt("Display name for this message: ", message.author, 40);
1168 if (!IsEmptyStr(reply_subject)) {
1169 if (!strncasecmp(reply_subject,
1171 strcpy(message.subject, reply_subject);
1174 snprintf(message.subject,
1175 sizeof message.subject,
1181 /* Trim down excessively long lists of thread references. We eliminate the
1182 * second one in the list so that the thread root remains intact.
1184 int rrtok = num_tokens(reply_references, '|');
1185 int rrlen = strlen(reply_references);
1186 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1187 remove_token(reply_references, 1, '|');
1190 snprintf(message.references, sizeof message.references, "%s%s%s",
1192 (IsEmptyStr(reply_references) ? "" : "|"),
1197 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1199 if (r / 100 != 2 && r / 10 != 57) {
1200 scr_printf("%s\n", buf);
1204 /* Error code 570 is special. It means that we CAN enter a message
1205 * in this room, but a recipient needs to be specified.
1212 /* If the user is a dumbass, tell them how to type. */
1213 if ((userflags & US_EXPERT) == 0) {
1214 scr_printf("Entering message. Word wrap will give you soft linebreaks. Pressing the\n");
1215 scr_printf("'enter' key will give you a hard linebreak and an indent. Press 'enter' twice\n");
1216 scr_printf("when finished.\n");
1219 /* Handle the selection of a recipient, if necessary. */
1221 if (need_recp == 1) {
1222 if (axlevel >= AxProbU) {
1224 strcpy(buf, reply_to);
1226 newprompt("Enter recipient: ", buf, SIZ-100);
1227 if (IsEmptyStr(buf)) {
1232 strcpy(buf, "sysop");
1234 strcpy(message.recipient, buf);
1236 if (room_flags & QR_ANONOPT) {
1237 scr_printf("Anonymous (Y/N)? ");
1239 message.anonymous = 1;
1242 /* If it's mail, we've got to check the validity of the recipient... */
1243 if (!IsEmptyStr(message.recipient)) {
1244 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1246 scr_printf("%s\n", buf);
1251 /* Learn the number of the newest message in in the room, so we can
1252 * tell upon saving whether someone else has posted too.
1255 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1257 scr_printf("%s\n", buf);
1259 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1263 /* Now compose the message... */
1264 if (client_make_message(ipc, temp, message.recipient,
1265 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1266 if (msgarr) free(msgarr);
1270 /* Reopen the temp file that was created, so we can send it */
1271 fp = fopen(temp, "r");
1273 if (!fp || !(message.text = load_message_from_file(fp))) {
1274 scr_printf("*** Internal error while trying to save message!\n"
1276 temp, strerror(errno));
1283 /* Break lines that are >1024 characters, otherwise the server
1284 * will truncate them.
1286 break_big_lines(message.text);
1288 /* Transmit message to the server */
1289 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1291 scr_printf("%s\n", buf);
1295 /* Yes, unlink it now, so it doesn't stick around if we crash */
1298 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1300 if (msgarr) free(msgarr);
1302 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1304 scr_printf("%s\n", buf);
1306 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1310 /* get new highest message number in room to set lrp for goto... */
1311 maxmsgnum = msgarr[num_msgs - 1];
1313 /* now see if anyone else has posted in here */
1315 for (a = 0; a < num_msgs; ++a) {
1316 if (msgarr[a] > highmsg) {
1320 if (msgarr) free(msgarr);
1323 /* In the Mail> room, this algorithm always counts one message
1324 * higher than in public rooms, so we decrement it by one.
1331 scr_printf("*** 1 additional message has been entered "
1332 "in this room by another user.\n");
1335 scr_printf("*** %d additional messages have been entered "
1336 "in this room by other users.\n", b);
1344 * Do editing on a quoted file
1346 void process_quote(void)
1348 FILE *qfile, *tfile;
1350 int line, qstart, qend;
1352 /* Unlink the second temp file as soon as it's opened, so it'll get
1353 * deleted even if the program dies
1355 qfile = fopen(temp2, "r");
1358 /* Display the quotable text with line numbers added */
1360 if (fgets(buf, 128, qfile) == NULL) {
1361 /* we're skipping a line here */
1363 while (fgets(buf, 128, qfile) != NULL) {
1364 scr_printf("%3d %s", ++line, buf);
1367 qstart = intprompt("Begin quoting at", 1, 1, line);
1368 qend = intprompt(" End quoting at", line, qstart, line);
1372 if (fgets(buf, 128, qfile) == NULL) {
1373 /* we're skipping a line here */
1375 tfile = fopen(temp, "w");
1376 while (fgets(buf, 128, qfile) != NULL) {
1377 if ((++line >= qstart) && (line <= qend))
1378 fprintf(tfile, " >%s", buf);
1380 fprintf(tfile, " \n");
1389 * List the URL's which were embedded in the previous message
1391 void list_urls(CtdlIPC *ipc)
1397 if (num_urls == 0) {
1398 scr_printf("There were no URL's in the previous message.\n\n");
1402 for (i = 0; i < num_urls; ++i) {
1403 scr_printf("%3d %s\n", i + 1, urls[i]);
1406 if ((i = num_urls) != 1)
1407 i = intprompt("Display which one", 1, 1, num_urls);
1409 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1412 scr_printf("failed to '%s' by %d\n", cmd, rv);
1418 * Run image viewer in background
1420 int do_image_view(const char *filename)
1425 snprintf(cmd, sizeof cmd, imagecmd, filename);
1432 if (childpid == 0) {
1434 pid_t grandchildpid;
1436 grandchildpid = fork();
1437 if (grandchildpid < 0) {
1438 return grandchildpid;
1441 if (grandchildpid == 0) {
1446 nullfd = open("/dev/null", O_WRONLY);
1453 retcode = system(cmd);
1463 if (grandchildpid > 0) {
1471 waitpid(childpid, &retcode, 0);
1480 * View an image attached to a message
1482 void image_view(CtdlIPC *ipc, unsigned long msg)
1484 struct parts *ptr = last_message_parts;
1488 /* Run through available parts */
1489 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1490 if ((!strcasecmp(ptr->disposition, "attachment")
1491 || !strcasecmp(ptr->disposition, "inline"))
1492 && !strncmp(ptr->mimetype, "image/", 6)) {
1495 strcpy(part, ptr->number);
1502 strprompt("View which part (0 when done)", part, SIZ-1);
1504 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1505 if ((!strcasecmp(ptr->disposition, "attachment")
1506 || !strcasecmp(ptr->disposition, "inline"))
1507 && !strncmp(ptr->mimetype, "image/", 6)
1508 && !strcasecmp(ptr->number, part)) {
1511 void *file = NULL; /* The downloaded file */
1516 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1518 scr_printf("%s\n", buf);
1522 len = (size_t)extract_long(buf, 0);
1523 progress(ipc, len, len);
1525 CtdlMakeTempFileName(tmp, sizeof tmp);
1526 strcat(tmp, ptr->filename);
1527 save_buffer(file, len, tmp);
1541 * Read the messages in the current room
1543 void readmsgs(CtdlIPC *ipc,
1544 enum MessageList c, /* see listing in citadel_ipc.h */
1545 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1546 int q /* Number of msgs to read (if c==3) */
1548 int a, e, f, g, start;
1554 char prtfile[PATH_MAX];
1557 char targ[ROOMNAMELEN];
1558 char filename[PATH_MAX];
1559 char save_to[PATH_MAX];
1560 void *attachment = NULL; /* Downloaded attachment */
1561 FILE *dest = NULL; /* Alternate destination other than screen */
1562 int r; /* IPC response code */
1563 static int att_seq = 0; /* Attachment download sequence number */
1564 int rv = 0; /* silence the stupid warn_unused_result warnings */
1566 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1572 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1574 scr_printf("%s\n", cmd);
1576 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1580 if (num_msgs == 0) { /* TODO look at this later */
1581 if (c == LastMessages) return;
1582 scr_printf("*** There are no ");
1583 if (c == NewMessages) scr_printf("new ");
1584 if (c == OldMessages) scr_printf("old ");
1585 scr_printf("messages in this room.\n");
1589 /* this loop cycles through each message... */
1590 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1591 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1592 while (msg_arr[a] == 0L) {
1594 if ((a == num_msgs) || (a == (-1)))
1598 RAGAIN: pagin = ((arcflag == 0)
1600 && (userflags & US_PAGINATOR)) ? 1 : 0;
1602 /* If we're doing a quote, set the screenwidth to 72 */
1604 hold_sw = screenwidth;
1608 /* If printing or archiving, set the screenwidth to 80 */
1610 hold_sw = screenwidth;
1614 /* clear parts list */
1615 free_parts(last_message_parts);
1616 last_message_parts = NULL;
1618 /* now read the message... */
1619 e = read_message(ipc, msg_arr[a], pagin, dest);
1621 /* ...and set the screenwidth back if we have to */
1622 if ((quotflag) || (arcflag)) {
1623 screenwidth = hold_sw;
1626 highest_msg_read = msg_arr[a];
1631 enable_color = hold_color;
1640 enable_color = hold_color;
1643 if (freopen(prtfile, "r", stdin) == NULL) {
1644 /* we probably should handle the error condition here */
1646 stty_ctdl(SB_RESTORE);
1647 ka_system(printcmd);
1648 stty_ctdl(SB_NO_INTR);
1655 } while ((g != f) && (g >= 0));
1656 scr_printf("Message printed.\n");
1660 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1661 && (((room_flags & QR_MAILBOX) == 0)
1662 || (rc_force_mail_prompts == 0))) {
1667 color(BRIGHT_WHITE);
1668 scr_printf("%d", num_msgs - a - 1);
1672 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1673 if (rc_url_cmd[0] && num_urls)
1674 keyopt("<U>RLview ");
1675 if (has_images > 0 && !IsEmptyStr(imagecmd))
1676 keyopt("<I>mages ");
1677 keyopt("<?>help -> ");
1680 e = (inkey() & 127);
1682 /* return key same as <N> */ if (e == 10)
1684 /* space key same as <N> */ if (e == 32)
1686 /* del/move for aides only */
1687 if ( (!is_room_aide)
1688 && ((room_flags & QR_MAILBOX) == 0)
1689 && ((room_flags2 & QR2_COLLABDEL) == 0)
1691 if ((e == 'd') || (e == 'm'))
1694 /* print only if available */
1695 if ((e == 'p') && (IsEmptyStr(printcmd)))
1697 /* can't file if not allowed */
1699 && (rc_allow_attachments == 0))
1701 /* link only if browser avail*/
1703 && (IsEmptyStr(rc_url_cmd)))
1706 && (IsEmptyStr(imagecmd) || !has_images))
1708 } while ((e != 'a') && (e != 'n') && (e != 's')
1709 && (e != 'd') && (e != 'm') && (e != 'p')
1710 && (e != 'q') && (e != 'b') && (e != 'h')
1711 && (e != 'r') && (e != 'f') && (e != '?')
1712 && (e != 'u') && (e != 'c') && (e != 'y')
1713 && (e != 'i') && (e != 'o') );
1719 scr_printf("Again");
1722 scr_printf("Delete");
1734 scr_printf("Print");
1737 scr_printf("reply Quoted");
1743 scr_printf("Header");
1746 scr_printf("Reply");
1749 scr_printf("Open attachments");
1755 scr_printf("URL's");
1758 scr_printf("mY next");
1763 scr_printf("? <help>");
1766 if (userflags & US_DISAPPEAR || e == 'i')
1767 scr_printf("\r%79s\r", "");
1771 DONE_QUOTING: switch (e) {
1773 scr_printf("Options available here:\n"
1774 " ? Help (prints this message)\n"
1775 " S Stop reading immediately\n"
1776 " A Again (repeats last message)\n"
1777 " N Next (continue with next message)\n"
1778 " Y My Next (continue with next message you authored)\n"
1779 " B Back (go back to previous message)\n");
1781 || (room_flags & QR_MAILBOX)
1782 || (room_flags2 & QR2_COLLABDEL)
1784 scr_printf(" D Delete this message\n"
1785 " M Move message to another room\n");
1787 scr_printf(" C Copy message to another room\n");
1788 if (!IsEmptyStr(printcmd))
1789 scr_printf(" P Print this message\n");
1791 " Q Reply to this message, quoting portions of it\n"
1792 " H Headers (display message headers only)\n");
1794 scr_printf(" R Reply to this message\n");
1795 if (rc_allow_attachments) {
1796 scr_printf(" O (Open attachments)\n");
1797 scr_printf(" F (save attachments to a File)\n");
1799 if (!IsEmptyStr(rc_url_cmd))
1800 scr_printf(" U (list URL's for display)\n");
1801 if (!IsEmptyStr(imagecmd) && has_images > 0)
1802 scr_printf(" I Image viewer\n");
1807 dest = fopen(prtfile, "w");
1809 hold_color = enable_color;
1814 dest = fopen(temp2, "w");
1816 hold_color = enable_color;
1828 newprompt("Enter target room: ",
1829 targ, ROOMNAMELEN - 1);
1830 if (!IsEmptyStr(targ)) {
1831 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1832 msg_arr[a], targ, cmd);
1833 scr_printf("%s\n", cmd);
1839 if (r / 100 != 2) /* r will be init'ed, FIXME */
1840 goto RMSGREAD; /* the logic here sucks */
1844 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1845 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1846 filename, &attachment, progress, cmd);
1848 scr_printf("%s\n", cmd);
1850 extract_token(filename, cmd, 2, '|', sizeof filename);
1852 * Part 1 won't have a filename; use the
1853 * subject of the message instead. IO
1855 if (IsEmptyStr(filename)) {
1856 strcpy(filename, reply_subject);
1858 if (e == 'o') { /* open attachment */
1859 mkdir(tempdir, 0700);
1860 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1864 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1865 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1868 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1870 else { /* save attachment to disk */
1871 destination_directory(save_to, filename);
1872 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1881 scr_printf("*** Delete this message? ");
1883 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1884 scr_printf("%s\n", cmd);
1892 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1895 savedpos = num_msgs;
1896 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1897 num_msgs = savedpos;
1903 image_view(ipc, msg_arr[a]);
1906 { /* hack hack hack */
1907 /* find the next message by me, stay here if we find nothing */
1910 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1912 /* This is repetitively dumb, but that's what computers are for.
1913 We have to load up messages until we find one by us */
1916 struct ctdlipcmessage *msg = NULL;
1918 /* read the header so we can get 'from=' */
1919 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1920 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1921 a = lasta; /* meesa current */
1929 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1933 } /* end for loop */
1934 } /* end read routine */
1940 * View and edit a system message
1942 void edit_system_message(CtdlIPC *ipc, char *which_message)
1946 char write_cmd[SIZ];
1948 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1949 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1950 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1951 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1958 * Loads the contents of a file into memory. Caller must free the allocated
1961 char *load_message_from_file(FILE *src)
1967 fseek(src, 0, SEEK_END);
1971 dest = (char *)calloc(1, i + 1);
1978 g = fread(dest + got, 1, i - got, src);
1981 /* Interrupted system call, keep going */
1984 /* At this point we have either EOF or error */