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.
15 #include "textclient.h"
17 #define MAXWORDBUF SIZ
18 #define NO_REPLY_TO "nobody ... xxxxxx"
21 char reply_subject[SIZ];
22 char reply_references[SIZ];
23 char reply_inreplyto[SIZ];
27 char text[MAXWORDBUF];
30 void stty_ctdl(int cmd);
31 int haschar(const char *st, int ch);
32 int file_checksum(char *filename);
33 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax);
35 unsigned long *msg_arr = NULL;
38 extern char room_name[];
39 extern char tempdir[];
40 extern unsigned room_flags;
41 extern unsigned room_flags2;
43 extern long highest_msg_read;
46 extern int screenwidth;
47 extern int screenheight;
48 extern long maxmsgnum;
51 extern char is_room_aide;
52 extern char fullname[];
54 extern unsigned userflags;
55 extern char sigcaught;
56 extern char printcmd[];
57 extern int rc_allow_attachments;
58 extern int rc_display_message_numbers;
59 extern int rc_force_mail_prompts;
60 extern int editor_pid;
61 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
63 char urls[MAXURLS][SIZ];
65 int has_images = 0; /* Current msg has images */
66 struct parts *last_message_parts = NULL; /* Parts from last msg */
70 void ka_sigcatch(int signum)
73 signal(SIGALRM, ka_sigcatch);
74 CtdlIPCNoop(ipc_for_signal_handlers);
79 * server keep-alive version of wait() (needed for external editor)
81 pid_t ka_wait(int *kstatus)
86 signal(SIGALRM, ka_sigcatch);
90 } while (errno == EINTR);
91 signal(SIGALRM, SIG_IGN);
98 * version of system() that uses ka_wait()
100 int ka_system(char *shc)
109 perror("Cannot fork");
111 return ((pid_t) childpid);
115 execlp("/bin/sh", "sh", "-c", shc, NULL);
121 waitpid = ka_wait(&retcode);
122 } while (waitpid != childpid);
132 * add a newline to the buffer...
134 void add_newline(struct cittext *textlist)
139 while (ptr->next != NULL)
142 while (ptr->text[strlen(ptr->text) - 1] == 32)
143 ptr->text[strlen(ptr->text) - 1] = 0;
144 /* strcat(ptr->text,"\n"); */
146 ptr->next = (struct cittext *)
147 malloc(sizeof(struct cittext));
150 strcpy(ptr->text, "");
155 * add a word to the buffer...
157 void add_word(struct cittext *textlist, char *wordbuf)
162 while (ptr->next != NULL)
165 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
166 ptr->next = (struct cittext *)
167 malloc(sizeof(struct cittext));
170 strcpy(ptr->text, "");
173 strcat(ptr->text, wordbuf);
174 strcat(ptr->text, " ");
179 * begin editing of an opened file pointed to by fp
181 void citedit(FILE * fp)
183 int a, prev, finished, b, last_space;
185 struct cittext *textlist = NULL;
187 char wordbuf[MAXWORDBUF];
190 /* first, load the text into the buffer */
192 textlist = (struct cittext *) malloc(sizeof(struct cittext));
193 textlist->next = NULL;
194 strcpy(textlist->text, "");
198 while (a = getc(fp), a >= 0) {
200 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
201 add_word(textlist, wordbuf);
203 if ((prev == 13) || (prev == 10)) {
204 add_word(textlist, "\n");
205 add_newline(textlist);
206 add_word(textlist, "");
209 wordbuf[strlen(wordbuf) + 1] = 0;
210 wordbuf[strlen(wordbuf)] = a;
212 if (strlen(wordbuf) + 3 > screenwidth) {
213 add_word(textlist, wordbuf);
221 prev = (appending ? 13 : (-1));
232 if ((a != 32) && (prev == 13)) {
233 add_word(textlist, "\n");
237 if ((a == 32) && (prev == 13)) {
238 add_word(textlist, "\n");
239 add_newline(textlist);
243 if (!IsEmptyStr(wordbuf)) {
244 wordbuf[strlen(wordbuf) - 1] = 0;
249 } else if (a == 23) {
251 wordbuf[strlen(wordbuf) - 1] = 0;
255 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
256 } else if (a == 13) {
258 if (IsEmptyStr(wordbuf))
261 for (b = 0; b < strlen(wordbuf); ++b)
262 if (wordbuf[b] == 32) {
264 add_word(textlist, wordbuf);
265 strcpy(wordbuf, &wordbuf[b + 1]);
268 add_word(textlist, wordbuf);
273 wordbuf[strlen(wordbuf) + 1] = 0;
274 wordbuf[strlen(wordbuf)] = a;
276 if ((strlen(wordbuf) + 3) > screenwidth) {
278 for (b = 0; b < strlen(wordbuf); ++b)
279 if (wordbuf[b] == 32)
281 if (last_space >= 0) {
282 for (b = 0; b < strlen(wordbuf); ++b)
283 if (wordbuf[b] == 32) {
285 add_word(textlist, wordbuf);
286 strcpy(wordbuf, &wordbuf[b + 1]);
289 for (b = 0; b < strlen(wordbuf); ++b) {
294 scr_printf("\n%s", wordbuf);
296 add_word(textlist, wordbuf);
302 } while (finished == 0);
304 /* write the buffer back to disk */
306 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
307 fprintf(fp, "%s", ptr->text);
311 rv = ftruncate(fileno(fp), ftell(fp));
313 scr_printf("failed to set message buffer: %s\n", strerror(errno));
316 /* and deallocate the memory we used */
317 while (textlist != NULL) {
318 ptr = textlist->next;
326 * Free the struct parts
328 void free_parts(struct parts *p)
330 struct parts *a_part = p;
336 a_part = a_part->next;
343 * This is a mini RFC2047 decoder.
344 * It only handles strings encoded from UTF-8 as Quoted-printable.
346 void mini_2047_decode(char *s)
351 char *qstart = strstr(s, "=?UTF-8?Q?");
355 char *qend = strstr(s, "?=");
362 strcpy(qstart, &qstart[10]);
375 sscanf(ch, "%02x", &c);
377 strcpy(&p[1], &p[3]);
388 strcpy(qend, &qend[2]);
392 * Read a message from the server
394 int read_message(CtdlIPC * ipc, long num, /* message number */
395 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
397 { /* Destination file, NULL for screen */
403 struct ctdlipcmessage *message = NULL;
404 int r; /* IPC response code */
405 char *converted_text = NULL;
412 int final_line_is_blank = 0;
419 strcpy(reply_to, NO_REPLY_TO);
420 strcpy(reply_subject, "");
421 strcpy(reply_references, "");
422 strcpy(reply_inreplyto, "");
424 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
426 scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
429 free_parts(message->attachments);
435 fprintf(dest, "\n ");
442 if (pagin == 1 && !dest) {
446 /* View headers only */
448 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
449 message->nhdr ? "yes" : "no", message->author, message->type, message->msgid);
450 if (!IsEmptyStr(message->subject)) {
451 scr_printf("subj=%s\n", message->subject);
453 if (!IsEmptyStr(message->email)) {
454 scr_printf("rfca=%s\n", message->email);
456 scr_printf("room=%s\ntime=%s", message->room, asctime(localtime(&message->time))
458 if (!IsEmptyStr(message->recipient)) {
459 scr_printf("rcpt=%s\n", message->recipient);
461 if (message->attachments) {
464 for (ptr = message->attachments; ptr; ptr = ptr->next) {
465 scr_printf("part=%s|%s|%s|%s|%s|%ld\n",
466 ptr->name, ptr->filename, ptr->number, ptr->disposition, ptr->mimetype, ptr->length);
472 free_parts(message->attachments);
477 if (rc_display_message_numbers) {
479 fprintf(dest, "[#%s] ", message->msgid);
484 scr_printf("#%s", message->msgid);
489 if (nhdr == 1 && !is_room_aide) {
491 fprintf(dest, " ****");
497 localtime_r(&message->time, &thetime);
498 strftime(now, sizeof now, "%F %R", &thetime);
500 fprintf(dest, "%s from %s ", now, message->author);
501 if (!IsEmptyStr(message->email)) {
502 fprintf(dest, "<%s> ", message->email);
506 scr_printf("%s ", now);
510 scr_printf("%s ", message->author);
511 if (!IsEmptyStr(message->email)) {
515 scr_printf("%s", message->email);
520 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
522 fprintf(dest, "in %s> ", message->room);
526 color(BRIGHT_MAGENTA);
527 scr_printf("%s> ", message->room);
530 if (!IsEmptyStr(message->recipient)) {
532 fprintf(dest, "to %s ", message->recipient);
537 scr_printf("%s ", message->recipient);
548 /* Set the reply-to address to an Internet e-mail address if possible
550 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
551 if (!IsEmptyStr(message->author)) {
552 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
554 safestrncpy(reply_to, message->email, sizeof reply_to);
558 /* But if we can't do that, set it to a Citadel address.
560 if (!strcmp(reply_to, NO_REPLY_TO)) {
561 safestrncpy(reply_to, message->author, sizeof(reply_to));
564 if (message->msgid != NULL) {
565 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
568 if (message->references != NULL)
569 if (!IsEmptyStr(message->references)) {
570 safestrncpy(reply_references, message->references, sizeof reply_references);
573 if (message->subject != NULL) {
574 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
575 if (!IsEmptyStr(message->subject)) {
577 fprintf(dest, "Subject: %s\n", message->subject);
580 scr_printf("Subject: ");
582 mini_2047_decode(message->subject);
583 scr_printf("%s\n", message->subject);
588 if (pagin == 1 && !dest) {
592 /******* end of header output, start of message text output *******/
595 * Convert HTML to plain text, formatting for the actual width
596 * of the client screen.
598 if (!strcasecmp(message->content_type, "text/html")) {
599 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
600 if (converted_text != NULL) {
602 message->text = converted_text;
607 /* Text/plain is a different type */
608 if (!strcasecmp(message->content_type, "text/plain")) {
612 /* Render text/x-markdown as plain text */
613 if (!strcasecmp(message->content_type, "text/x-markdown")) {
618 static char *urlprefixes[] = {
624 num_urls = 0; /* Start with a clean slate */
625 for (p = 0; p < (sizeof urlprefixes / sizeof(char *)); ++p) {
626 searchptr = message->text;
627 while ((searchptr != NULL) && (num_urls < MAXURLS)) {
628 searchptr = strstr(searchptr, urlprefixes[p]);
629 if (searchptr != NULL) {
630 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
631 for (i = 0; i < strlen(urls[num_urls]); i++) {
632 ch = urls[num_urls][i];
633 if (ch == '>' || ch == '\"' || ch == ')' || ch == ' ' || ch == '\n') {
634 urls[num_urls][i] = 0;
647 if (format_type == 0) {
648 fr = fmout(screenwidth, NULL, message->text, dest, 1);
650 /* renderer for text/plain */
652 lineptr = message->text;
655 nextline = strchr(lineptr, '\n');
656 if (nextline != NULL) {
663 if (sigcaught == 0) {
664 linelen = strlen(lineptr);
665 if (linelen && (lineptr[linelen - 1] == '\r')) {
666 lineptr[--linelen] = 0;
669 fprintf(dest, "%s\n", lineptr);
671 scr_printf("%s\n", lineptr);
675 final_line_is_blank = 1;
677 final_line_is_blank = 0;
682 if (!final_line_is_blank) {
691 /* Enumerate any attachments */
692 if ((pagin == 1) && (message->attachments)) {
695 for (ptr = message->attachments; ptr; ptr = ptr->next) {
696 if ((!strcasecmp(ptr->disposition, "attachment"))
697 || (!strcasecmp(ptr->disposition, "inline"))
698 || (!strcasecmp(ptr->disposition, ""))
700 if ((strcasecmp(ptr->number, message->mime_chosen))
701 && (!IsEmptyStr(ptr->mimetype))
705 color(BRIGHT_MAGENTA);
706 scr_printf("%s", ptr->number);
710 scr_printf("%s", ptr->filename);
712 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
713 if (!strncmp(ptr->mimetype, "image/", 6)) {
721 /* Save the attachments info for later */
722 last_message_parts = message->attachments;
728 if (pagin == 1 && !dest)
735 * replace string function for the built-in editor
737 void replace_string(char *filename, long int startpos)
746 int substitutions = 0;
750 newprompt("Enter text to be replaced: ", srch_str, (sizeof(srch_str) - 1));
751 if (IsEmptyStr(srch_str)) {
755 newprompt("Enter text to replace it with: ", rplc_str, (sizeof(rplc_str) - 1));
757 fp = fopen(filename, "r+");
763 fseek(fp, startpos, 0);
765 while (a = getc(fp), a > 0) {
767 buf[strlen(buf) + 1] = 0;
768 buf[strlen(buf)] = a;
769 if (strlen(buf) >= strlen(srch_str)) {
770 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
771 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
772 strcpy(ptr, rplc_str);
776 if (strlen(buf) > 384) {
779 rv = fwrite((char *) buf, 128, 1, fp);
781 scr_printf("failed to replace string: %s\n", strerror(errno));
784 strcpy(buf, &buf[128]);
790 if (!IsEmptyStr(buf)) {
791 rv = fwrite((char *) buf, strlen(buf), 1, fp);
795 rv = truncate(filename, wpos);
796 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
800 * Function to begin composing a new message
802 int client_make_message(CtdlIPC * ipc, char *filename, /* temporary file name */
803 char *recipient, /* NULL if it's not mail */
804 int is_anonymous, int format_type, int mode, char *subject, /* buffer to store subject line */
805 int subject_required)
814 if ((mode == 2) && (IsEmptyStr(editor_path))) {
815 scr_printf("*** No editor available; using built-in editor.\n");
820 time_t now = time(NULL);
821 localtime_r(&now, &thetime);
822 strftime(datestr, sizeof datestr, "%F %R", &thetime);
825 if (room_flags & QR_ANONONLY && !recipient) {
826 snprintf(header, sizeof header, " ****");
828 snprintf(header, sizeof header, " %s from %s", datestr, (is_anonymous ? "[anonymous]" : fullname)
830 if (!IsEmptyStr(recipient)) {
831 size_t tmp = strlen(header);
832 snprintf(&header[tmp], sizeof header - tmp, " to %s", recipient);
835 scr_printf("%s\n", header);
837 if (!IsEmptyStr(subject)) {
838 scr_printf("Subject: %s\n", subject);
841 if ((subject_required) && (IsEmptyStr(subject))) {
842 newprompt("Subject: ", subject, 70);
846 scr_printf("(Press ctrl-d when finished)\n");
850 fp = fopen(filename, "r");
852 fmout(screenwidth, fp, NULL, NULL, 0);
855 scr_printf("failed to get stream position %s\n", strerror(errno));
858 fp = fopen(filename, "w");
860 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno)
871 fp = fopen(filename, "r+");
873 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno)
882 fp = fopen(filename, "a");
884 scr_printf("*** Error opening temp file!\n" " %s: %s\n", filename, strerror(errno));
904 default: /* allow 2+ modes */
905 e_ex_code = 1; /* start with a failed exit code */
906 stty_ctdl(SB_RESTORE);
908 cksum = file_checksum(filename);
909 if (editor_pid == 0) {
912 chmod(filename, 0600);
913 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
915 execlp(editor_path, editor_path, filename, NULL);
921 b = ka_wait(&e_ex_code);
922 } while ((b != editor_pid) && (b >= 0));
928 MECR:if (mode >= 2) {
929 if (file_checksum(filename) == cksum) {
930 scr_printf("*** Aborted message.\n");
933 if (e_ex_code == 0) {
939 b = keymenu("Entry command (? for options)",
941 "<C>ontinue|" "<S>ave message|" "<P>rint formatted|" "add s<U>bject|" "<R>eplace string|" "<H>old message");
950 scr_printf(" %s from %s", datestr, fullname);
951 if (!IsEmptyStr(recipient)) {
952 scr_printf(" to %s", recipient);
956 if (!IsEmptyStr(subject)) {
957 scr_printf("Subject: %s\n", subject);
959 fp = fopen(filename, "r");
961 fmout(screenwidth, fp, NULL, NULL, 0);
964 scr_printf("failed to get stream position %s\n", strerror(errno));
970 replace_string(filename, 0L);
977 if (subject != NULL) {
978 newprompt("Subject: ", subject, 70);
985 MEABT:scr_printf("Are you sure? ");
989 MEABT2:unlink(filename);
995 * Make sure there's room in msg_arr[] for at least one more.
997 void check_msg_arr_size(void)
999 if ((num_msgs + 1) > msg_arr_size) {
1000 msg_arr_size += 512;
1001 msg_arr = realloc(msg_arr, ((sizeof(long)) * msg_arr_size));
1007 * break_big_lines() - break up lines that are >1024 characters
1008 * otherwise the server will truncate
1010 void break_big_lines(char *msg)
1020 while (strlen(ptr) > 1000) {
1021 break_here = strchr(&ptr[900], ' ');
1022 if ((break_here == NULL) || (break_here > &ptr[999])) {
1023 break_here = &ptr[999];
1032 * entmsg() - edit and create a message
1033 * returns 0 if message was saved
1035 int entmsg(CtdlIPC * ipc, int is_reply, /* nonzero if this was a <R>eply command */
1037 int masquerade /* prompt for a non-default display name? */
1047 struct ctdlipcmessage message;
1048 unsigned long *msgarr = NULL;
1049 int r; /* IPC response code */
1050 int subject_required = 0;
1053 scr_printf("You may not enter messages in this type of room.\n");
1063 strcpy(subject, "");
1066 * First, check to see if we have permission to enter a message in
1067 * this room. The server will return an error code if we can't.
1069 strcpy(message.recipient, "");
1070 strcpy(message.author, "");
1071 strcpy(message.subject, "");
1072 strcpy(message.references, "");
1073 message.text = ""; /* point to "", changes later */
1074 message.anonymous = 0;
1075 message.type = mode;
1078 newprompt("Display name for this message: ", message.author, 40);
1083 if (!IsEmptyStr(reply_subject)) {
1084 if (!strncasecmp(reply_subject, "Re: ", 3)) {
1085 strcpy(message.subject, reply_subject);
1087 snprintf(message.subject, sizeof message.subject, "Re: %s", reply_subject);
1091 /* Trim down excessively long lists of thread references. We eliminate the
1092 * second one in the list so that the thread root remains intact.
1094 int rrtok = num_tokens(reply_references, '|');
1095 int rrlen = strlen(reply_references);
1096 if (((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10)) {
1097 remove_token(reply_references, 1, '|');
1100 snprintf(message.references, sizeof message.references, "%s%s%s",
1101 reply_references, (IsEmptyStr(reply_references) ? "" : "|"), reply_inreplyto);
1104 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1106 if (r / 100 != 2 && r / 10 != 57) {
1107 scr_printf("%s\n", buf);
1111 /* Error code 570 is special. It means that we CAN enter a message
1112 * in this room, but a recipient needs to be specified.
1119 /* If the user is a dumbass, tell them how to type. */
1120 if ((userflags & US_EXPERT) == 0) {
1121 scr_printf("Entering message. Word wrap will give you soft linebreaks. Pressing the\n");
1122 scr_printf("'enter' key will give you a hard linebreak and an indent. Press 'enter' twice\n");
1123 scr_printf("when finished.\n");
1126 /* Handle the selection of a recipient, if necessary. */
1128 if (need_recp == 1) {
1129 if (axlevel >= AxProbU) {
1131 strcpy(buf, reply_to);
1133 newprompt("Enter recipient: ", buf, SIZ - 100);
1134 if (IsEmptyStr(buf)) {
1139 strcpy(buf, "sysop");
1141 strcpy(message.recipient, buf);
1143 if (room_flags & QR_ANONOPT) {
1144 scr_printf("Anonymous (Y/N)? ");
1146 message.anonymous = 1;
1149 /* If it's mail, we've got to check the validity of the recipient... */
1150 if (!IsEmptyStr(message.recipient)) {
1151 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1153 scr_printf("%s\n", buf);
1158 /* Learn the number of the newest message in in the room, so we can
1159 * tell upon saving whether someone else has posted too.
1162 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1164 scr_printf("%s\n", buf);
1166 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1169 /* Now compose the message... */
1170 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject, subject_required) != 0) {
1176 /* Reopen the temp file that was created, so we can send it */
1177 fp = fopen(temp, "r");
1179 if (!fp || !(message.text = load_message_from_file(fp))) {
1180 scr_printf("*** Internal error while trying to save message!\n" "%s: %s\n", temp, strerror(errno));
1188 /* Break lines that are >1024 characters, otherwise the server
1189 * will truncate them.
1191 break_big_lines(message.text);
1193 /* Transmit message to the server */
1194 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1196 scr_printf("%s\n", buf);
1200 /* Yes, unlink it now, so it doesn't stick around if we crash */
1204 highmsg = msgarr[num_msgs - 1];
1209 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1211 scr_printf("%s\n", buf);
1213 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1216 /* get new highest message number in room to set lrp for goto... */
1217 maxmsgnum = msgarr[num_msgs - 1];
1219 /* now see if anyone else has posted in here */
1221 for (a = 0; a < num_msgs; ++a) {
1222 if (msgarr[a] > highmsg) {
1230 /* In the Mail> room, this algorithm always counts one message
1231 * higher than in public rooms, so we decrement it by one.
1238 scr_printf("*** 1 additional message has been entered " "in this room by another user.\n");
1240 scr_printf("*** %d additional messages have been entered " "in this room by other users.\n", b);
1248 * Do editing on a quoted file
1250 void process_quote(void)
1252 FILE *qfile, *tfile;
1254 int line, qstart, qend;
1256 /* Unlink the second temp file as soon as it's opened, so it'll get
1257 * deleted even if the program dies
1259 qfile = fopen(temp2, "r");
1262 /* Display the quotable text with line numbers added */
1264 if (fgets(buf, 128, qfile) == NULL) {
1265 /* we're skipping a line here */
1267 while (fgets(buf, 128, qfile) != NULL) {
1268 scr_printf("%3d %s", ++line, buf);
1271 qstart = intprompt("Begin quoting at", 1, 1, line);
1272 qend = intprompt(" End quoting at", line, qstart, line);
1276 if (fgets(buf, 128, qfile) == NULL) {
1277 /* we're skipping a line here */
1279 tfile = fopen(temp, "w");
1280 while (fgets(buf, 128, qfile) != NULL) {
1281 if ((++line >= qstart) && (line <= qend))
1282 fprintf(tfile, " >%s", buf);
1284 fprintf(tfile, " \n");
1293 * List the URL's which were embedded in the previous message
1295 void list_urls(CtdlIPC * ipc)
1301 if (num_urls == 0) {
1302 scr_printf("There were no URL's in the previous message.\n\n");
1306 for (i = 0; i < num_urls; ++i) {
1307 scr_printf("%3d %s\n", i + 1, urls[i]);
1310 if ((i = num_urls) != 1)
1311 i = intprompt("Display which one", 1, 1, num_urls);
1313 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1316 scr_printf("failed to '%s' by %d\n", cmd, rv);
1322 * Run image viewer in background
1324 int do_image_view(const char *filename)
1329 snprintf(cmd, sizeof cmd, imagecmd, filename);
1336 if (childpid == 0) {
1338 pid_t grandchildpid;
1340 grandchildpid = fork();
1341 if (grandchildpid < 0) {
1342 return grandchildpid;
1345 if (grandchildpid == 0) {
1350 nullfd = open("/dev/null", O_WRONLY);
1357 retcode = system(cmd);
1367 if (grandchildpid > 0) {
1375 waitpid(childpid, &retcode, 0);
1384 * View an image attached to a message
1386 void image_view(CtdlIPC * ipc, unsigned long msg)
1388 struct parts *ptr = last_message_parts;
1392 /* Run through available parts */
1393 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1394 if ((!strcasecmp(ptr->disposition, "attachment")
1395 || !strcasecmp(ptr->disposition, "inline"))
1396 && !strncmp(ptr->mimetype, "image/", 6)) {
1399 strcpy(part, ptr->number);
1406 strprompt("View which part (0 when done)", part, SIZ - 1);
1408 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1409 if ((!strcasecmp(ptr->disposition, "attachment")
1410 || !strcasecmp(ptr->disposition, "inline"))
1411 && !strncmp(ptr->mimetype, "image/", 6)
1412 && !strcasecmp(ptr->number, part)) {
1415 void *file = NULL; /* The downloaded file */
1420 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1422 scr_printf("%s\n", buf);
1426 len = (size_t) extract_long(buf, 0);
1427 progress(ipc, len, len);
1429 CtdlMakeTempFileName(tmp, sizeof tmp);
1430 strcat(tmp, ptr->filename);
1431 save_buffer(file, len, tmp);
1445 * Read the messages in the current room
1447 void readmsgs(CtdlIPC * ipc, enum MessageList c, /* see listing in citadel_ipc.h */
1448 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1449 int q /* Number of msgs to read (if c==3) */
1452 int a, e, f, g, start;
1458 char prtfile[PATH_MAX];
1461 char targ[ROOMNAMELEN];
1462 char filename[PATH_MAX];
1463 char save_to[PATH_MAX];
1464 void *attachment = NULL; /* Downloaded attachment */
1465 FILE *dest = NULL; /* Alternate destination other than screen */
1466 int r; /* IPC response code */
1467 static int att_seq = 0; /* Attachment download sequence number */
1468 int rv = 0; /* silence the stupid warn_unused_result warnings */
1470 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1476 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1478 scr_printf("%s\n", cmd);
1480 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++);
1483 if (num_msgs == 0) { /* TODO look at this later */
1484 if (c == LastMessages)
1486 scr_printf("*** There are no ");
1487 if (c == NewMessages)
1489 if (c == OldMessages)
1491 scr_printf("messages in this room.\n");
1495 /* this loop cycles through each message... */
1496 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1497 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1498 while (msg_arr[a] == 0L) {
1500 if ((a == num_msgs) || (a == (-1)))
1504 RAGAIN:pagin = ((arcflag == 0)
1506 && (userflags & US_PAGINATOR)) ? 1 : 0;
1508 /* If we're doing a quote, set the screenwidth to 72 */
1510 hold_sw = screenwidth;
1514 /* If printing or archiving, set the screenwidth to 80 */
1516 hold_sw = screenwidth;
1520 /* clear parts list */
1521 free_parts(last_message_parts);
1522 last_message_parts = NULL;
1524 /* now read the message... */
1525 e = read_message(ipc, msg_arr[a], pagin, dest);
1527 /* ...and set the screenwidth back if we have to */
1528 if ((quotflag) || (arcflag)) {
1529 screenwidth = hold_sw;
1532 highest_msg_read = msg_arr[a];
1537 enable_color = hold_color;
1546 enable_color = hold_color;
1549 if (freopen(prtfile, "r", stdin) == NULL) {
1550 /* we probably should handle the error condition here */
1552 stty_ctdl(SB_RESTORE);
1553 ka_system(printcmd);
1554 stty_ctdl(SB_NO_INTR);
1561 } while ((g != f) && (g >= 0));
1562 scr_printf("Message printed.\n");
1566 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1567 && (((room_flags & QR_MAILBOX) == 0)
1568 || (rc_force_mail_prompts == 0))) {
1573 color(BRIGHT_WHITE);
1574 scr_printf("%d", num_msgs - a - 1);
1578 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1579 if (rc_url_cmd[0] && num_urls)
1580 keyopt("<U>RLview ");
1581 if (has_images > 0 && !IsEmptyStr(imagecmd))
1582 keyopt("<I>mages ");
1583 keyopt("<?>help -> ");
1586 e = (inkey() & 127);
1588 /* return key same as <N> */ if (e == 10)
1590 /* space key same as <N> */ if (e == 32)
1592 /* del/move for aides only */
1594 && ((room_flags & QR_MAILBOX) == 0)
1595 && ((room_flags2 & QR2_COLLABDEL) == 0)
1597 if ((e == 'd') || (e == 'm'))
1600 /* print only if available */
1601 if ((e == 'p') && (IsEmptyStr(printcmd)))
1603 /* can't file if not allowed */
1605 && (rc_allow_attachments == 0))
1607 /* link only if browser avail*/
1609 && (IsEmptyStr(rc_url_cmd)))
1612 && (IsEmptyStr(imagecmd) || !has_images))
1614 } while ((e != 'a') && (e != 'n') && (e != 's')
1615 && (e != 'd') && (e != 'm') && (e != 'p')
1616 && (e != 'q') && (e != 'b') && (e != 'h')
1617 && (e != 'r') && (e != 'f') && (e != '?')
1618 && (e != 'u') && (e != 'c') && (e != 'y')
1619 && (e != 'i') && (e != 'o'));
1625 scr_printf("Again");
1628 scr_printf("Delete");
1640 scr_printf("Print");
1643 scr_printf("reply Quoted");
1649 scr_printf("Header");
1652 scr_printf("Reply");
1655 scr_printf("Open attachments");
1661 scr_printf("URL's");
1664 scr_printf("mY next");
1669 scr_printf("? <help>");
1672 if (userflags & US_DISAPPEAR || e == 'i')
1673 scr_printf("\r%79s\r", "");
1677 DONE_QUOTING:switch (e) {
1679 scr_printf("Options available here:\n"
1680 " ? Help (prints this message)\n"
1681 " S Stop reading immediately\n"
1682 " A Again (repeats last message)\n"
1683 " N Next (continue with next message)\n"
1684 " Y My Next (continue with next message you authored)\n"
1685 " B Back (go back to previous message)\n");
1687 || (room_flags & QR_MAILBOX)
1688 || (room_flags2 & QR2_COLLABDEL)
1690 scr_printf(" D Delete this message\n" " M Move message to another room\n");
1692 scr_printf(" C Copy message to another room\n");
1693 if (!IsEmptyStr(printcmd))
1694 scr_printf(" P Print this message\n");
1695 scr_printf(" Q Reply to this message, quoting portions of it\n"
1696 " H Headers (display message headers only)\n");
1698 scr_printf(" R Reply to this message\n");
1699 if (rc_allow_attachments) {
1700 scr_printf(" O (Open attachments)\n");
1701 scr_printf(" F (save attachments to a File)\n");
1703 if (!IsEmptyStr(rc_url_cmd))
1704 scr_printf(" U (list URL's for display)\n");
1705 if (!IsEmptyStr(imagecmd) && has_images > 0)
1706 scr_printf(" I Image viewer\n");
1711 dest = fopen(prtfile, "w");
1713 hold_color = enable_color;
1718 dest = fopen(temp2, "w");
1720 hold_color = enable_color;
1732 newprompt("Enter target room: ", targ, ROOMNAMELEN - 1);
1733 if (!IsEmptyStr(targ)) {
1734 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0), msg_arr[a], targ, cmd);
1735 scr_printf("%s\n", cmd);
1741 if (r / 100 != 2) /* r will be init'ed, FIXME */
1742 goto RMSGREAD; /* the logic here sucks */
1746 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1747 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a], filename, &attachment, progress, cmd);
1749 scr_printf("%s\n", cmd);
1751 extract_token(filename, cmd, 2, '|', sizeof filename);
1753 * Part 1 won't have a filename; use the
1754 * subject of the message instead. IO
1756 if (IsEmptyStr(filename)) {
1757 strcpy(filename, reply_subject);
1759 if (e == 'o') { /* open attachment */
1760 mkdir(tempdir, 0700);
1761 snprintf(save_to, sizeof save_to, "%s/%04x.%s", tempdir, ++att_seq, filename);
1762 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1763 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1766 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1767 } else { /* save attachment to disk */
1768 destination_directory(save_to, filename);
1769 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1778 scr_printf("*** Delete this message? ");
1780 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1781 scr_printf("%s\n", cmd);
1789 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1792 savedpos = num_msgs;
1793 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1794 num_msgs = savedpos;
1800 image_view(ipc, msg_arr[a]);
1803 { /* hack hack hack */
1804 /* find the next message by me, stay here if we find nothing */
1807 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir) {
1808 /* This is repetitively dumb, but that's what computers are for.
1809 We have to load up messages until we find one by us */
1812 struct ctdlipcmessage *msg = NULL;
1814 /* read the header so we can get 'from=' */
1815 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1816 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1817 a = lasta; /* meesa current */
1825 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1829 } /* end for loop */
1830 } /* end read routine */
1836 * View and edit a system message
1838 void edit_system_message(CtdlIPC * ipc, char *which_message)
1842 char write_cmd[SIZ];
1844 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1845 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1846 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1847 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1854 * Loads the contents of a file into memory. Caller must free the allocated
1857 char *load_message_from_file(FILE * src)
1863 fseek(src, 0, SEEK_END);
1867 dest = (char *) calloc(1, i + 1);
1874 g = fread(dest + got, 1, i - got, src);
1877 /* Interrupted system call, keep going */
1880 /* At this point we have either EOF or error */