2 * Text client functions for reading and writing of messages
4 * Copyright (c) 1987-2018 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("room=%s\ntime=%s",
498 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 (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
567 fprintf(dest, "in %s> ", message->room);
571 color(BRIGHT_MAGENTA);
572 scr_printf("%s> ", message->room);
575 if (!IsEmptyStr(message->recipient)) {
577 fprintf(dest, "to %s ", message->recipient);
582 scr_printf("%s ", message->recipient);
593 /* Set the reply-to address to an Internet e-mail address if possible
595 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
596 if (!IsEmptyStr(message->author)) {
597 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
600 safestrncpy(reply_to, message->email, sizeof reply_to);
604 /* But if we can't do that, set it to a Citadel address.
606 if (!strcmp(reply_to, NO_REPLY_TO)) {
607 safestrncpy(reply_to, message->author, sizeof(reply_to));
610 if (message->msgid != NULL) {
611 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
614 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
615 safestrncpy(reply_references, message->references, sizeof reply_references);
618 if (message->subject != NULL) {
619 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
620 if (!IsEmptyStr(message->subject)) {
622 fprintf(dest, "Subject: %s\n", message->subject);
625 scr_printf("Subject: ");
627 mini_2047_decode(message->subject);
628 scr_printf("%s\n", message->subject);
633 if (pagin == 1 && !dest) {
637 /******* end of header output, start of message text output *******/
640 * Convert HTML to plain text, formatting for the actual width
641 * of the client screen.
643 if (!strcasecmp(message->content_type, "text/html")) {
644 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
645 if (converted_text != NULL) {
647 message->text = converted_text;
652 /* Text/plain is a different type */
653 if (!strcasecmp(message->content_type, "text/plain")) {
658 static char *urlprefixes[] = {
664 num_urls = 0; /* Start with a clean slate */
665 for (p=0; p<(sizeof urlprefixes / sizeof(char *)); ++p) {
666 searchptr = message->text;
667 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
668 searchptr = strstr(searchptr, urlprefixes[p]);
669 if (searchptr != NULL) {
670 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
671 for (i = 0; i < strlen(urls[num_urls]); i++) {
672 ch = urls[num_urls][i];
673 if (ch == '>' || ch == '\"' || ch == ')' ||
674 ch == ' ' || ch == '\n') {
675 urls[num_urls][i] = 0;
688 if (format_type == 0) {
689 fr = fmout(screenwidth, NULL, message->text, dest, 1);
691 /* renderer for text/plain */
693 lineptr = message->text;
696 nextline = strchr(lineptr, '\n');
697 if (nextline != NULL) {
700 if (*nextline == 0) nextline = NULL;
703 if (sigcaught == 0) {
704 linelen = strlen(lineptr);
705 if (linelen && (lineptr[linelen-1] == '\r')) {
706 lineptr[--linelen] = 0;
709 fprintf(dest, "%s\n", lineptr);
711 scr_printf("%s\n", lineptr);
714 if (lineptr[0] == 0) final_line_is_blank = 1;
715 else final_line_is_blank = 0;
720 if (!final_line_is_blank) {
730 /* Enumerate any attachments */
731 if ( (pagin == 1) && (message->attachments) ) {
734 for (ptr = message->attachments; ptr; ptr = ptr->next) {
735 if ( (!strcasecmp(ptr->disposition, "attachment"))
736 || (!strcasecmp(ptr->disposition, "inline"))
737 || (!strcasecmp(ptr->disposition, ""))
739 if ( (strcasecmp(ptr->number, message->mime_chosen))
740 && (!IsEmptyStr(ptr->mimetype))
744 color(BRIGHT_MAGENTA);
745 scr_printf("%s", ptr->number);
749 scr_printf("%s", ptr->filename);
751 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
752 if (!strncmp(ptr->mimetype, "image/", 6)) {
760 /* Save the attachments info for later */
761 last_message_parts = message->attachments;
767 if (pagin == 1 && !dest)
774 * replace string function for the built-in editor
776 void replace_string(char *filename, long int startpos)
785 int substitutions = 0;
789 newprompt("Enter text to be replaced: ", srch_str, (sizeof(srch_str)-1) );
790 if (IsEmptyStr(srch_str)) {
794 newprompt("Enter text to replace it with: ", rplc_str, (sizeof(rplc_str)-1) );
796 fp = fopen(filename, "r+");
802 fseek(fp, startpos, 0);
804 while (a = getc(fp), a > 0) {
806 buf[strlen(buf) + 1] = 0;
807 buf[strlen(buf)] = a;
808 if (strlen(buf) >= strlen(srch_str)) {
809 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
810 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
811 strcpy(ptr, rplc_str);
815 if (strlen(buf) > 384) {
818 rv = fwrite((char *) buf, 128, 1, fp);
820 scr_printf("failed to replace string: %s\n", strerror(errno));
823 strcpy(buf, &buf[128]);
829 if (!IsEmptyStr(buf)) {
830 rv = fwrite((char *) buf, strlen(buf), 1, fp);
834 rv = truncate(filename, wpos);
835 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
839 * Function to begin composing a new message
841 int client_make_message(CtdlIPC *ipc,
842 char *filename, /* temporary file name */
843 char *recipient, /* NULL if it's not mail */
847 char *subject, /* buffer to store subject line */
857 if ( (mode == 2) && (IsEmptyStr(editor_path)) ) {
858 scr_printf("*** No editor available; using built-in editor.\n");
863 time_t now = time(NULL);
864 localtime_r(&now, &thetime);
865 strftime(datestr, sizeof datestr, "%F %R", &thetime);
868 if (room_flags & QR_ANONONLY && !recipient) {
869 snprintf(header, sizeof header, " ****");
872 snprintf(header, sizeof header,
875 (is_anonymous ? "[anonymous]" : fullname)
877 if (!IsEmptyStr(recipient)) {
878 size_t tmp = strlen(header);
879 snprintf(&header[tmp], sizeof header - tmp,
880 " to %s", recipient);
883 scr_printf("%s\n", header);
884 if (subject != NULL) if (!IsEmptyStr(subject)) {
885 scr_printf("Subject: %s\n", subject);
888 if ( (subject_required) && (IsEmptyStr(subject)) ) {
889 newprompt("Subject: ", subject, 70);
893 scr_printf("(Press ctrl-d when finished)\n");
897 fp = fopen(filename, "r");
899 fmout(screenwidth, fp, NULL, NULL, 0);
902 scr_printf("failed to get stream position %s\n",
906 fp = fopen(filename, "w");
908 scr_printf("*** Error opening temp file!\n %s: %s\n",
909 filename, strerror(errno)
920 fp = fopen(filename, "r+");
922 scr_printf("*** Error opening temp file!\n %s: %s\n",
923 filename, strerror(errno)
932 fp = fopen(filename, "a");
934 scr_printf("*** Error opening temp file!\n"
936 filename, strerror(errno));
956 default: /* allow 2+ modes */
957 e_ex_code = 1; /* start with a failed exit code */
958 stty_ctdl(SB_RESTORE);
960 cksum = file_checksum(filename);
961 if (editor_pid == 0) {
964 chmod(filename, 0600);
965 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
967 execlp(editor_path, editor_path, filename, NULL);
973 b = ka_wait(&e_ex_code);
974 } while ((b != editor_pid) && (b >= 0));
980 MECR: if (mode >= 2) {
981 if (file_checksum(filename) == cksum) {
982 scr_printf("*** Aborted message.\n");
985 if (e_ex_code == 0) {
991 b = keymenu("Entry command (? for options)",
1001 if (b == 'a') goto MEABT;
1002 if (b == 'c') goto ME1;
1003 if (b == 's') goto MEFIN;
1005 scr_printf(" %s from %s", datestr, fullname);
1006 if (!IsEmptyStr(recipient)) {
1007 scr_printf(" to %s", recipient);
1010 if (subject != NULL) if (!IsEmptyStr(subject)) {
1011 scr_printf("Subject: %s\n", subject);
1013 fp = fopen(filename, "r");
1015 fmout(screenwidth, fp, NULL, NULL, 0);
1018 scr_printf("failed to get stream position %s\n",
1025 replace_string(filename, 0L);
1032 if (subject != NULL) {
1033 newprompt("Subject: ", subject, 70);
1040 MEABT: scr_printf("Are you sure? ");
1044 MEABT2: unlink(filename);
1050 * Make sure there's room in msg_arr[] for at least one more.
1052 void check_msg_arr_size(void) {
1053 if ((num_msgs + 1) > msg_arr_size) {
1054 msg_arr_size += 512;
1055 msg_arr = realloc(msg_arr,
1056 ((sizeof(long)) * msg_arr_size) );
1062 * break_big_lines() - break up lines that are >1024 characters
1063 * otherwise the server will truncate
1065 void break_big_lines(char *msg) {
1074 while (strlen(ptr) > 1000) {
1075 break_here = strchr(&ptr[900], ' ');
1076 if ((break_here == NULL) || (break_here > &ptr[999])) {
1077 break_here = &ptr[999];
1086 * entmsg() - edit and create a message
1087 * returns 0 if message was saved
1089 int entmsg(CtdlIPC *ipc,
1090 int is_reply, /* nonzero if this was a <R>eply command */
1092 int masquerade /* prompt for a non-default display name? */
1101 struct ctdlipcmessage message;
1102 unsigned long *msgarr = NULL;
1103 int r; /* IPC response code */
1104 int subject_required = 0;
1107 scr_printf("You may not enter messages in this type of room.\n");
1118 strcpy(subject, "");
1121 * First, check to see if we have permission to enter a message in
1122 * this room. The server will return an error code if we can't.
1124 strcpy(message.recipient, "");
1125 strcpy(message.author, "");
1126 strcpy(message.subject, "");
1127 strcpy(message.references, "");
1128 message.text = ""; /* point to "", changes later */
1129 message.anonymous = 0;
1130 message.type = mode;
1133 newprompt("Display name for this message: ", message.author, 40);
1138 if (!IsEmptyStr(reply_subject)) {
1139 if (!strncasecmp(reply_subject,
1141 strcpy(message.subject, reply_subject);
1144 snprintf(message.subject,
1145 sizeof message.subject,
1151 /* Trim down excessively long lists of thread references. We eliminate the
1152 * second one in the list so that the thread root remains intact.
1154 int rrtok = num_tokens(reply_references, '|');
1155 int rrlen = strlen(reply_references);
1156 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1157 remove_token(reply_references, 1, '|');
1160 snprintf(message.references, sizeof message.references, "%s%s%s",
1162 (IsEmptyStr(reply_references) ? "" : "|"),
1167 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1169 if (r / 100 != 2 && r / 10 != 57) {
1170 scr_printf("%s\n", buf);
1174 /* Error code 570 is special. It means that we CAN enter a message
1175 * in this room, but a recipient needs to be specified.
1182 /* If the user is a dumbass, tell them how to type. */
1183 if ((userflags & US_EXPERT) == 0) {
1184 scr_printf("Entering message. Word wrap will give you soft linebreaks. Pressing the\n");
1185 scr_printf("'enter' key will give you a hard linebreak and an indent. Press 'enter' twice\n");
1186 scr_printf("when finished.\n");
1189 /* Handle the selection of a recipient, if necessary. */
1191 if (need_recp == 1) {
1192 if (axlevel >= AxProbU) {
1194 strcpy(buf, reply_to);
1196 newprompt("Enter recipient: ", buf, SIZ-100);
1197 if (IsEmptyStr(buf)) {
1202 strcpy(buf, "sysop");
1204 strcpy(message.recipient, buf);
1206 if (room_flags & QR_ANONOPT) {
1207 scr_printf("Anonymous (Y/N)? ");
1209 message.anonymous = 1;
1212 /* If it's mail, we've got to check the validity of the recipient... */
1213 if (!IsEmptyStr(message.recipient)) {
1214 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1216 scr_printf("%s\n", buf);
1221 /* Learn the number of the newest message in in the room, so we can
1222 * tell upon saving whether someone else has posted too.
1225 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1227 scr_printf("%s\n", buf);
1229 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1233 /* Now compose the message... */
1234 if (client_make_message(ipc, temp, message.recipient,
1235 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1236 if (msgarr) free(msgarr);
1240 /* Reopen the temp file that was created, so we can send it */
1241 fp = fopen(temp, "r");
1243 if (!fp || !(message.text = load_message_from_file(fp))) {
1244 scr_printf("*** Internal error while trying to save message!\n"
1246 temp, strerror(errno));
1253 /* Break lines that are >1024 characters, otherwise the server
1254 * will truncate them.
1256 break_big_lines(message.text);
1258 /* Transmit message to the server */
1259 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1261 scr_printf("%s\n", buf);
1265 /* Yes, unlink it now, so it doesn't stick around if we crash */
1268 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1270 if (msgarr) free(msgarr);
1272 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1274 scr_printf("%s\n", buf);
1276 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1280 /* get new highest message number in room to set lrp for goto... */
1281 maxmsgnum = msgarr[num_msgs - 1];
1283 /* now see if anyone else has posted in here */
1285 for (a = 0; a < num_msgs; ++a) {
1286 if (msgarr[a] > highmsg) {
1290 if (msgarr) free(msgarr);
1293 /* In the Mail> room, this algorithm always counts one message
1294 * higher than in public rooms, so we decrement it by one.
1301 scr_printf("*** 1 additional message has been entered "
1302 "in this room by another user.\n");
1305 scr_printf("*** %d additional messages have been entered "
1306 "in this room by other users.\n", b);
1314 * Do editing on a quoted file
1316 void process_quote(void)
1318 FILE *qfile, *tfile;
1320 int line, qstart, qend;
1322 /* Unlink the second temp file as soon as it's opened, so it'll get
1323 * deleted even if the program dies
1325 qfile = fopen(temp2, "r");
1328 /* Display the quotable text with line numbers added */
1330 if (fgets(buf, 128, qfile) == NULL) {
1331 /* we're skipping a line here */
1333 while (fgets(buf, 128, qfile) != NULL) {
1334 scr_printf("%3d %s", ++line, buf);
1337 qstart = intprompt("Begin quoting at", 1, 1, line);
1338 qend = intprompt(" End quoting at", line, qstart, line);
1342 if (fgets(buf, 128, qfile) == NULL) {
1343 /* we're skipping a line here */
1345 tfile = fopen(temp, "w");
1346 while (fgets(buf, 128, qfile) != NULL) {
1347 if ((++line >= qstart) && (line <= qend))
1348 fprintf(tfile, " >%s", buf);
1350 fprintf(tfile, " \n");
1359 * List the URL's which were embedded in the previous message
1361 void list_urls(CtdlIPC *ipc)
1367 if (num_urls == 0) {
1368 scr_printf("There were no URL's in the previous message.\n\n");
1372 for (i = 0; i < num_urls; ++i) {
1373 scr_printf("%3d %s\n", i + 1, urls[i]);
1376 if ((i = num_urls) != 1)
1377 i = intprompt("Display which one", 1, 1, num_urls);
1379 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1382 scr_printf("failed to '%s' by %d\n", cmd, rv);
1388 * Run image viewer in background
1390 int do_image_view(const char *filename)
1395 snprintf(cmd, sizeof cmd, imagecmd, filename);
1402 if (childpid == 0) {
1404 pid_t grandchildpid;
1406 grandchildpid = fork();
1407 if (grandchildpid < 0) {
1408 return grandchildpid;
1411 if (grandchildpid == 0) {
1416 nullfd = open("/dev/null", O_WRONLY);
1423 retcode = system(cmd);
1433 if (grandchildpid > 0) {
1441 waitpid(childpid, &retcode, 0);
1450 * View an image attached to a message
1452 void image_view(CtdlIPC *ipc, unsigned long msg)
1454 struct parts *ptr = last_message_parts;
1458 /* Run through available parts */
1459 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1460 if ((!strcasecmp(ptr->disposition, "attachment")
1461 || !strcasecmp(ptr->disposition, "inline"))
1462 && !strncmp(ptr->mimetype, "image/", 6)) {
1465 strcpy(part, ptr->number);
1472 strprompt("View which part (0 when done)", part, SIZ-1);
1474 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1475 if ((!strcasecmp(ptr->disposition, "attachment")
1476 || !strcasecmp(ptr->disposition, "inline"))
1477 && !strncmp(ptr->mimetype, "image/", 6)
1478 && !strcasecmp(ptr->number, part)) {
1481 void *file = NULL; /* The downloaded file */
1486 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1488 scr_printf("%s\n", buf);
1492 len = (size_t)extract_long(buf, 0);
1493 progress(ipc, len, len);
1495 CtdlMakeTempFileName(tmp, sizeof tmp);
1496 strcat(tmp, ptr->filename);
1497 save_buffer(file, len, tmp);
1511 * Read the messages in the current room
1513 void readmsgs(CtdlIPC *ipc,
1514 enum MessageList c, /* see listing in citadel_ipc.h */
1515 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1516 int q /* Number of msgs to read (if c==3) */
1518 int a, e, f, g, start;
1524 char prtfile[PATH_MAX];
1527 char targ[ROOMNAMELEN];
1528 char filename[PATH_MAX];
1529 char save_to[PATH_MAX];
1530 void *attachment = NULL; /* Downloaded attachment */
1531 FILE *dest = NULL; /* Alternate destination other than screen */
1532 int r; /* IPC response code */
1533 static int att_seq = 0; /* Attachment download sequence number */
1534 int rv = 0; /* silence the stupid warn_unused_result warnings */
1536 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1542 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1544 scr_printf("%s\n", cmd);
1546 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1550 if (num_msgs == 0) { /* TODO look at this later */
1551 if (c == LastMessages) return;
1552 scr_printf("*** There are no ");
1553 if (c == NewMessages) scr_printf("new ");
1554 if (c == OldMessages) scr_printf("old ");
1555 scr_printf("messages in this room.\n");
1559 /* this loop cycles through each message... */
1560 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1561 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1562 while (msg_arr[a] == 0L) {
1564 if ((a == num_msgs) || (a == (-1)))
1568 RAGAIN: pagin = ((arcflag == 0)
1570 && (userflags & US_PAGINATOR)) ? 1 : 0;
1572 /* If we're doing a quote, set the screenwidth to 72 */
1574 hold_sw = screenwidth;
1578 /* If printing or archiving, set the screenwidth to 80 */
1580 hold_sw = screenwidth;
1584 /* clear parts list */
1585 free_parts(last_message_parts);
1586 last_message_parts = NULL;
1588 /* now read the message... */
1589 e = read_message(ipc, msg_arr[a], pagin, dest);
1591 /* ...and set the screenwidth back if we have to */
1592 if ((quotflag) || (arcflag)) {
1593 screenwidth = hold_sw;
1596 highest_msg_read = msg_arr[a];
1601 enable_color = hold_color;
1610 enable_color = hold_color;
1613 if (freopen(prtfile, "r", stdin) == NULL) {
1614 /* we probably should handle the error condition here */
1616 stty_ctdl(SB_RESTORE);
1617 ka_system(printcmd);
1618 stty_ctdl(SB_NO_INTR);
1625 } while ((g != f) && (g >= 0));
1626 scr_printf("Message printed.\n");
1630 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1631 && (((room_flags & QR_MAILBOX) == 0)
1632 || (rc_force_mail_prompts == 0))) {
1637 color(BRIGHT_WHITE);
1638 scr_printf("%d", num_msgs - a - 1);
1642 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1643 if (rc_url_cmd[0] && num_urls)
1644 keyopt("<U>RLview ");
1645 if (has_images > 0 && !IsEmptyStr(imagecmd))
1646 keyopt("<I>mages ");
1647 keyopt("<?>help -> ");
1650 e = (inkey() & 127);
1652 /* return key same as <N> */ if (e == 10)
1654 /* space key same as <N> */ if (e == 32)
1656 /* del/move for aides only */
1657 if ( (!is_room_aide)
1658 && ((room_flags & QR_MAILBOX) == 0)
1659 && ((room_flags2 & QR2_COLLABDEL) == 0)
1661 if ((e == 'd') || (e == 'm'))
1664 /* print only if available */
1665 if ((e == 'p') && (IsEmptyStr(printcmd)))
1667 /* can't file if not allowed */
1669 && (rc_allow_attachments == 0))
1671 /* link only if browser avail*/
1673 && (IsEmptyStr(rc_url_cmd)))
1676 && (IsEmptyStr(imagecmd) || !has_images))
1678 } while ((e != 'a') && (e != 'n') && (e != 's')
1679 && (e != 'd') && (e != 'm') && (e != 'p')
1680 && (e != 'q') && (e != 'b') && (e != 'h')
1681 && (e != 'r') && (e != 'f') && (e != '?')
1682 && (e != 'u') && (e != 'c') && (e != 'y')
1683 && (e != 'i') && (e != 'o') );
1689 scr_printf("Again");
1692 scr_printf("Delete");
1704 scr_printf("Print");
1707 scr_printf("reply Quoted");
1713 scr_printf("Header");
1716 scr_printf("Reply");
1719 scr_printf("Open attachments");
1725 scr_printf("URL's");
1728 scr_printf("mY next");
1733 scr_printf("? <help>");
1736 if (userflags & US_DISAPPEAR || e == 'i')
1737 scr_printf("\r%79s\r", "");
1741 DONE_QUOTING: switch (e) {
1743 scr_printf("Options available here:\n"
1744 " ? Help (prints this message)\n"
1745 " S Stop reading immediately\n"
1746 " A Again (repeats last message)\n"
1747 " N Next (continue with next message)\n"
1748 " Y My Next (continue with next message you authored)\n"
1749 " B Back (go back to previous message)\n");
1751 || (room_flags & QR_MAILBOX)
1752 || (room_flags2 & QR2_COLLABDEL)
1754 scr_printf(" D Delete this message\n"
1755 " M Move message to another room\n");
1757 scr_printf(" C Copy message to another room\n");
1758 if (!IsEmptyStr(printcmd))
1759 scr_printf(" P Print this message\n");
1761 " Q Reply to this message, quoting portions of it\n"
1762 " H Headers (display message headers only)\n");
1764 scr_printf(" R Reply to this message\n");
1765 if (rc_allow_attachments) {
1766 scr_printf(" O (Open attachments)\n");
1767 scr_printf(" F (save attachments to a File)\n");
1769 if (!IsEmptyStr(rc_url_cmd))
1770 scr_printf(" U (list URL's for display)\n");
1771 if (!IsEmptyStr(imagecmd) && has_images > 0)
1772 scr_printf(" I Image viewer\n");
1777 dest = fopen(prtfile, "w");
1779 hold_color = enable_color;
1784 dest = fopen(temp2, "w");
1786 hold_color = enable_color;
1798 newprompt("Enter target room: ",
1799 targ, ROOMNAMELEN - 1);
1800 if (!IsEmptyStr(targ)) {
1801 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1802 msg_arr[a], targ, cmd);
1803 scr_printf("%s\n", cmd);
1809 if (r / 100 != 2) /* r will be init'ed, FIXME */
1810 goto RMSGREAD; /* the logic here sucks */
1814 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1815 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1816 filename, &attachment, progress, cmd);
1818 scr_printf("%s\n", cmd);
1820 extract_token(filename, cmd, 2, '|', sizeof filename);
1822 * Part 1 won't have a filename; use the
1823 * subject of the message instead. IO
1825 if (IsEmptyStr(filename)) {
1826 strcpy(filename, reply_subject);
1828 if (e == 'o') { /* open attachment */
1829 mkdir(tempdir, 0700);
1830 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1834 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1835 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1838 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1840 else { /* save attachment to disk */
1841 destination_directory(save_to, filename);
1842 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1851 scr_printf("*** Delete this message? ");
1853 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1854 scr_printf("%s\n", cmd);
1862 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1865 savedpos = num_msgs;
1866 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1867 num_msgs = savedpos;
1873 image_view(ipc, msg_arr[a]);
1876 { /* hack hack hack */
1877 /* find the next message by me, stay here if we find nothing */
1880 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1882 /* This is repetitively dumb, but that's what computers are for.
1883 We have to load up messages until we find one by us */
1886 struct ctdlipcmessage *msg = NULL;
1888 /* read the header so we can get 'from=' */
1889 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1890 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1891 a = lasta; /* meesa current */
1899 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1903 } /* end for loop */
1904 } /* end read routine */
1910 * View and edit a system message
1912 void edit_system_message(CtdlIPC *ipc, char *which_message)
1916 char write_cmd[SIZ];
1918 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1919 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1920 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1921 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1928 * Loads the contents of a file into memory. Caller must free the allocated
1931 char *load_message_from_file(FILE *src)
1937 fseek(src, 0, SEEK_END);
1941 dest = (char *)calloc(1, i + 1);
1948 g = fread(dest + got, 1, i - got, src);
1951 /* Interrupted system call, keep going */
1954 /* At this point we have either EOF or error */