1 // Text client functions for reading and writing of messages
3 // Beware: this is really old and crappy code, written in the
4 // late 1980s when my coding style was absolute garbage. It
5 // works, but we probably should replace most of it.
7 // Copyright (c) 1987-2022 by the citadel.org team
9 // This program is open source software. Use, duplication, and/or
10 // disclosure are subject to the GNU General Purpose License version 3.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 #include "textclient.h"
19 #define MAXWORDBUF SIZ
20 #define NO_REPLY_TO "nobody ... xxxxxx"
23 char reply_subject[SIZ];
24 char reply_references[SIZ];
25 char reply_inreplyto[SIZ];
29 char text[MAXWORDBUF];
32 void stty_ctdl(int cmd);
33 int haschar(const char *st, int ch);
34 int file_checksum(char *filename);
35 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax);
37 unsigned long *msg_arr = NULL;
40 extern char room_name[];
41 extern char tempdir[];
42 extern unsigned room_flags;
43 extern unsigned room_flags2;
45 extern long highest_msg_read;
48 extern int screenwidth;
49 extern int screenheight;
50 extern long maxmsgnum;
53 extern char is_room_aide;
54 extern char fullname[];
56 extern unsigned userflags;
57 extern char sigcaught;
58 extern char printcmd[];
59 extern int rc_allow_attachments;
60 extern int rc_display_message_numbers;
61 extern int rc_force_mail_prompts;
62 extern int editor_pid;
63 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
65 char urls[MAXURLS][SIZ];
67 int has_images = 0; /* Current msg has images */
68 struct parts *last_message_parts = NULL; /* Parts from last msg */
71 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) {
85 signal(SIGALRM, ka_sigcatch);
89 } while (errno == EINTR);
90 signal(SIGALRM, SIG_IGN);
97 * version of system() that uses ka_wait()
99 int ka_system(char *shc) {
107 perror("Cannot fork");
109 return ((pid_t) childpid);
113 execlp("/bin/sh", "sh", "-c", shc, NULL);
119 waitpid = ka_wait(&retcode);
120 } while (waitpid != childpid);
129 * add a newline to the buffer...
131 void add_newline(struct cittext *textlist)
136 while (ptr->next != NULL)
139 while (ptr->text[strlen(ptr->text) - 1] == 32)
140 ptr->text[strlen(ptr->text) - 1] = 0;
142 ptr->next = (struct cittext *)
143 malloc(sizeof(struct cittext));
146 strcpy(ptr->text, "");
151 * add a word to the buffer...
153 void add_word(struct cittext *textlist, char *wordbuf) {
157 while (ptr->next != NULL)
160 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
161 ptr->next = (struct cittext *)
162 malloc(sizeof(struct cittext));
165 strcpy(ptr->text, "");
168 strcat(ptr->text, wordbuf);
169 strcat(ptr->text, " ");
174 * begin editing of an opened file pointed to by fp
176 void citedit(FILE * fp) {
177 int a, prev, finished, b, last_space;
179 struct cittext *textlist = NULL;
181 char wordbuf[MAXWORDBUF];
184 /* first, load the text into the buffer */
186 textlist = (struct cittext *) malloc(sizeof(struct cittext));
187 textlist->next = NULL;
188 strcpy(textlist->text, "");
192 while (a = getc(fp), a >= 0) {
194 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
195 add_word(textlist, wordbuf);
197 if ((prev == 13) || (prev == 10)) {
198 add_word(textlist, "\n");
199 add_newline(textlist);
200 add_word(textlist, "");
204 wordbuf[strlen(wordbuf) + 1] = 0;
205 wordbuf[strlen(wordbuf)] = a;
207 if (strlen(wordbuf) + 3 > screenwidth) {
208 add_word(textlist, wordbuf);
216 prev = (appending ? 13 : (-1));
227 if ((a != 32) && (prev == 13)) {
228 add_word(textlist, "\n");
232 if ((a == 32) && (prev == 13)) {
233 add_word(textlist, "\n");
234 add_newline(textlist);
238 if (!IsEmptyStr(wordbuf)) {
239 wordbuf[strlen(wordbuf) - 1] = 0;
247 wordbuf[strlen(wordbuf) - 1] = 0;
251 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
255 if (IsEmptyStr(wordbuf))
258 for (b = 0; b < strlen(wordbuf); ++b)
259 if (wordbuf[b] == 32) {
261 add_word(textlist, wordbuf);
262 strcpy(wordbuf, &wordbuf[b + 1]);
265 add_word(textlist, wordbuf);
271 wordbuf[strlen(wordbuf) + 1] = 0;
272 wordbuf[strlen(wordbuf)] = a;
274 if ((strlen(wordbuf) + 3) > screenwidth) {
276 for (b = 0; b < strlen(wordbuf); ++b)
277 if (wordbuf[b] == 32)
279 if (last_space >= 0) {
280 for (b = 0; b < strlen(wordbuf); ++b)
281 if (wordbuf[b] == 32) {
283 add_word(textlist, wordbuf);
284 strcpy(wordbuf, &wordbuf[b + 1]);
287 for (b = 0; b < strlen(wordbuf); ++b) {
292 scr_printf("\n%s", wordbuf);
295 add_word(textlist, wordbuf);
301 } while (finished == 0);
303 /* write the buffer back to disk */
305 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
306 fprintf(fp, "%s", ptr->text);
310 rv = ftruncate(fileno(fp), ftell(fp));
312 scr_printf("failed to set message buffer: %s\n", strerror(errno));
315 /* and deallocate the memory we used */
316 while (textlist != NULL) {
317 ptr = textlist->next;
325 * Free the struct parts
327 void free_parts(struct parts *p) {
328 struct parts *a_part = p;
334 a_part = a_part->next;
341 * This is a mini RFC2047 decoder.
342 * It only handles strings encoded from UTF-8 as Quoted-printable.
343 * We can do this "in place" because the converted string will always be smaller than the source string.
345 void mini_2047_decode(char *s) {
346 if (!s) { // no null strings allowed!
350 char *qstart = strstr(s, "=?UTF-8?Q?"); // Must start with this string
355 char *qend = strstr(qstart+10, "?="); // Must end with this string
360 if (qend <= qstart) { // And there must be something in between them.
364 // The string has qualified for conversion.
366 strcpy(qend, ""); // Strip the trailer
367 strcpy(qstart, &qstart[10]); // Strip the header
369 char *r = qstart; // Pointer to where in the string we're reading
370 char *w = s; // Pointer to where in the string we're writing
372 while(*r) { // Loop through the source string
373 if (r[0] == '=') { // "=" means read a hex character
379 sscanf(ch, "%02x", &c);
384 else if (r[0] == '_') { // "_" is a space
389 else { // anything else pass through literally
395 w[0] = 0; // null terminate
400 * Read a message from the server
402 int read_message(CtdlIPC *ipc,
403 long num, /* message number */
404 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
405 FILE *dest /* Destination file, NULL for screen */
412 struct ctdlipcmessage *message = NULL;
413 int r; /* IPC response code */
414 char *converted_text = NULL;
421 int final_line_is_blank = 0;
427 strcpy(reply_to, NO_REPLY_TO);
428 strcpy(reply_subject, "");
429 strcpy(reply_references, "");
430 strcpy(reply_inreplyto, "");
432 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
434 scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
437 free_parts(message->attachments);
443 fprintf(dest, "\n ");
451 if (pagin == 1 && !dest) {
455 /* View headers only */
457 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
458 message->nhdr ? "yes" : "no", message->author, message->type, message->msgid);
459 if (!IsEmptyStr(message->subject)) {
460 scr_printf("subj=%s\n", message->subject);
462 if (!IsEmptyStr(message->email)) {
463 scr_printf("rfca=%s\n", message->email);
465 scr_printf("room=%s\ntime=%s", message->room, asctime(localtime(&message->time)));
466 if (!IsEmptyStr(message->recipient)) {
467 scr_printf("rcpt=%s\n", message->recipient);
469 if (message->attachments) {
472 for (ptr = message->attachments; ptr; ptr = ptr->next) {
473 scr_printf("part=%s|%s|%s|%s|%s|%ld\n",
474 ptr->name, ptr->filename, ptr->number, ptr->disposition, ptr->mimetype, ptr->length);
480 free_parts(message->attachments);
485 if (rc_display_message_numbers) {
487 fprintf(dest, "[#%s] ", message->msgid);
492 scr_printf("#%s", message->msgid);
497 if (nhdr == 1 && !is_room_aide) {
499 fprintf(dest, " ****");
507 localtime_r(&message->time, &thetime);
508 strftime(now, sizeof now, "%F %R", &thetime);
510 fprintf(dest, "%s from %s ", now, message->author);
511 if (!message->is_local) {
512 fprintf(dest, "<%s> ", message->email);
517 scr_printf("%s ", now);
521 scr_printf("%s ", message->author);
522 if (!message->is_local) {
526 scr_printf("%s", message->email);
531 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
533 fprintf(dest, "in %s> ", message->room);
538 color(BRIGHT_MAGENTA);
539 scr_printf("%s> ", message->room);
542 if (!IsEmptyStr(message->recipient)) {
544 fprintf(dest, "to %s ", message->recipient);
550 scr_printf("%s ", message->recipient);
562 // Set the reply-to address to an Internet e-mail address if possible
563 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
564 if (!IsEmptyStr(message->author)) {
565 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
568 strncpy(reply_to, message->email, sizeof reply_to);
572 // But if we can't do that, set it to a Citadel address.
573 if (!strcmp(reply_to, NO_REPLY_TO)) {
574 strncpy(reply_to, message->author, sizeof(reply_to));
577 if (message->msgid != NULL) {
578 strncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
581 if (message->references != NULL) {
582 if (!IsEmptyStr(message->references)) {
583 strncpy(reply_references, message->references, sizeof reply_references);
587 if (message->subject != NULL) {
588 strncpy(reply_subject, message->subject, sizeof reply_subject);
589 if (!IsEmptyStr(message->subject)) {
591 fprintf(dest, "Subject: %s\n", message->subject);
595 scr_printf("Subject: ");
597 mini_2047_decode(message->subject);
598 scr_printf("%s\n", message->subject);
604 if (pagin == 1 && !dest) {
608 /******* end of header output, start of message text output *******/
611 * Convert HTML to plain text, formatting for the actual width
612 * of the client screen.
614 if (!strcasecmp(message->content_type, "text/html")) {
615 converted_text = html_to_ascii(message->text, 0, screenwidth);
616 if (converted_text != NULL) {
618 message->text = converted_text;
623 /* Text/plain is a different type */
624 if (!strcasecmp(message->content_type, "text/plain")) {
628 /* Render text/x-markdown as plain text */
629 if (!strcasecmp(message->content_type, "text/x-markdown")) {
634 static char *urlprefixes[] = {
640 num_urls = 0; /* Start with a clean slate */
641 for (p = 0; p < (sizeof urlprefixes / sizeof(char *)); ++p) {
642 searchptr = message->text;
643 while ((searchptr != NULL) && (num_urls < MAXURLS)) {
644 searchptr = strstr(searchptr, urlprefixes[p]);
645 if (searchptr != NULL) {
646 strncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
647 for (i = 0; i < strlen(urls[num_urls]); i++) {
648 ch = urls[num_urls][i];
649 if (ch == '>' || ch == '\"' || ch == ')' || ch == ' ' || ch == '\n') {
650 urls[num_urls][i] = 0;
663 if (format_type == 0) {
664 fr = fmout(screenwidth, NULL, message->text, dest, 1);
667 /* renderer for text/plain */
669 lineptr = message->text;
672 nextline = strchr(lineptr, '\n');
673 if (nextline != NULL) {
680 if (sigcaught == 0) {
681 linelen = strlen(lineptr);
682 if (linelen && (lineptr[linelen - 1] == '\r')) {
683 lineptr[--linelen] = 0;
686 fprintf(dest, "%s\n", lineptr);
688 scr_printf("%s\n", lineptr);
692 final_line_is_blank = 1;
694 final_line_is_blank = 0;
699 if (!final_line_is_blank) {
708 /* Enumerate any attachments */
709 if ((pagin == 1) && (message->attachments)) {
712 for (ptr = message->attachments; ptr; ptr = ptr->next) {
713 if ((!strcasecmp(ptr->disposition, "attachment"))
714 || (!strcasecmp(ptr->disposition, "inline"))
715 || (!strcasecmp(ptr->disposition, ""))
717 if ((strcasecmp(ptr->number, message->mime_chosen))
718 && (!IsEmptyStr(ptr->mimetype))
722 color(BRIGHT_MAGENTA);
723 scr_printf("%s", ptr->number);
727 scr_printf("%s", ptr->filename);
729 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
730 if (!strncmp(ptr->mimetype, "image/", 6)) {
738 /* Save the attachments info for later */
739 last_message_parts = message->attachments;
745 if (pagin == 1 && !dest)
753 * replace string function for the built-in editor
755 void replace_string(char *filename, long int startpos) {
763 int substitutions = 0;
767 newprompt("Enter text to be replaced: ", srch_str, (sizeof(srch_str) - 1));
768 if (IsEmptyStr(srch_str)) {
772 newprompt("Enter text to replace it with: ", rplc_str, (sizeof(rplc_str) - 1));
774 fp = fopen(filename, "r+");
780 fseek(fp, startpos, 0);
782 while (a = getc(fp), a > 0) {
784 buf[strlen(buf) + 1] = 0;
785 buf[strlen(buf)] = a;
786 if (strlen(buf) >= strlen(srch_str)) {
787 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
788 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
789 strcpy(ptr, rplc_str);
793 if (strlen(buf) > 384) {
796 rv = fwrite((char *) buf, 128, 1, fp);
798 scr_printf("failed to replace string: %s\n", strerror(errno));
801 strcpy(buf, &buf[128]);
807 if (!IsEmptyStr(buf)) {
808 rv = fwrite((char *) buf, strlen(buf), 1, fp);
812 rv = truncate(filename, wpos);
813 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
817 // Function to begin composing a new message
818 int client_make_message(CtdlIPC * ipc,
819 char *filename, // temporary file name
820 char *recipient, // NULL if it's not mail
824 char *subject, // buffer to store subject line
834 if ((mode == 2) && (IsEmptyStr(editor_path))) {
835 scr_printf("*** No editor available; using built-in editor.\n");
840 time_t now = time(NULL);
841 localtime_r(&now, &thetime);
842 strftime(datestr, sizeof datestr, "%F %R", &thetime);
845 if (room_flags & QR_ANONONLY && !recipient) {
846 snprintf(header, sizeof header, " ****");
849 snprintf(header, sizeof header, " %s from %s", datestr, (is_anonymous ? "[anonymous]" : fullname));
850 if (!IsEmptyStr(recipient)) {
851 size_t tmp = strlen(header);
852 snprintf(&header[tmp], sizeof header - tmp, " to %s", recipient);
855 scr_printf("%s\n", header);
857 if (!IsEmptyStr(subject)) {
858 scr_printf("Subject: %s\n", subject);
861 if ((subject_required) && (IsEmptyStr(subject))) {
862 newprompt("Subject: ", subject, 70);
866 scr_printf("(Press ctrl-d when finished)\n");
870 fp = fopen(filename, "r");
872 fmout(screenwidth, fp, NULL, NULL, 0);
875 scr_printf("failed to get stream position %s\n", strerror(errno));
879 fp = fopen(filename, "w");
881 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno));
891 fp = fopen(filename, "r+");
893 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno)
902 fp = fopen(filename, "a");
904 scr_printf("*** Error opening temp file!\n" " %s: %s\n", filename, strerror(errno));
924 default: /* allow 2+ modes */
925 e_ex_code = 1; /* start with a failed exit code */
926 stty_ctdl(SB_RESTORE);
928 cksum = file_checksum(filename);
929 if (editor_pid == 0) {
932 chmod(filename, 0600);
933 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
935 execlp(editor_path, editor_path, filename, NULL);
941 b = ka_wait(&e_ex_code);
942 } while ((b != editor_pid) && (b >= 0));
948 MECR: if (mode >= 2) {
949 if (file_checksum(filename) == cksum) {
950 scr_printf("*** Aborted message.\n");
953 if (e_ex_code == 0) {
959 b = keymenu("Entry command (? for options)",
976 scr_printf(" %s from %s", datestr, fullname);
977 if (!IsEmptyStr(recipient)) {
978 scr_printf(" to %s", recipient);
982 if (!IsEmptyStr(subject)) {
983 scr_printf("Subject: %s\n", subject);
985 fp = fopen(filename, "r");
987 fmout(screenwidth, fp, NULL, NULL, 0);
990 scr_printf("failed to get stream position %s\n", strerror(errno));
996 replace_string(filename, 0L);
1003 if (subject != NULL) {
1004 newprompt("Subject: ", subject, 70);
1011 MEABT: scr_printf("Are you sure? ");
1015 MEABT2: unlink(filename);
1021 * Make sure there's room in msg_arr[] for at least one more.
1023 void check_msg_arr_size(void) {
1024 if ((num_msgs + 1) > msg_arr_size) {
1025 msg_arr_size += 512;
1026 msg_arr = realloc(msg_arr, ((sizeof(long)) * msg_arr_size));
1032 * break_big_lines() - break up lines that are >1024 characters
1033 * otherwise the server will truncate
1035 void break_big_lines(char *msg) {
1044 while (strlen(ptr) > 1000) {
1045 break_here = strchr(&ptr[900], ' ');
1046 if ((break_here == NULL) || (break_here > &ptr[999])) {
1047 break_here = &ptr[999];
1056 * entmsg() - edit and create a message
1057 * returns 0 if message was saved
1059 int entmsg(CtdlIPC * ipc, int is_reply, /* nonzero if this was a <R>eply command */
1061 int masquerade /* prompt for a non-default display name? */
1070 struct ctdlipcmessage message;
1071 unsigned long *msgarr = NULL;
1072 int r; /* IPC response code */
1073 int subject_required = 0;
1075 if (entmsg_ok == ENTMSG_OK_YES) {
1076 /* no problem, go right ahead */
1078 else if (entmsg_ok == ENTMSG_OK_BLOG) {
1080 scr_printf("WARNING: this is a BLOG room.\n");
1081 scr_printf("The '<E>nter Message' command will create a BLOG POST.\n");
1082 scr_printf("If you want to leave a comment or reply to a comment, use the '<R>eply' command.\n");
1083 scr_printf("Do you really want to create a new blog post? ");
1090 scr_printf("You may not enter messages in this type of room.\n");
1101 strcpy(subject, "");
1104 * First, check to see if we have permission to enter a message in
1105 * this room. The server will return an error code if we can't.
1107 strcpy(message.recipient, "");
1108 strcpy(message.author, "");
1109 strcpy(message.subject, "");
1110 strcpy(message.references, "");
1111 message.text = ""; /* point to "", changes later */
1112 message.anonymous = 0;
1113 message.type = mode;
1116 newprompt("Display name for this message: ", message.author, 40);
1121 if (!IsEmptyStr(reply_subject)) {
1122 if (!strncasecmp(reply_subject, "Re: ", 3)) {
1123 strcpy(message.subject, reply_subject);
1126 snprintf(message.subject, sizeof message.subject, "Re: %s", reply_subject);
1130 /* Trim down excessively long lists of thread references. We eliminate the
1131 * second one in the list so that the thread root remains intact.
1133 int rrtok = num_tokens(reply_references, '|');
1134 int rrlen = strlen(reply_references);
1135 if (((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10)) {
1136 remove_token(reply_references, 1, '|');
1139 snprintf(message.references, sizeof message.references, "%s%s%s",
1140 reply_references, (IsEmptyStr(reply_references) ? "" : "|"), reply_inreplyto);
1143 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1145 if (r / 100 != 2 && r / 10 != 57) {
1146 scr_printf("%s\n", buf);
1150 /* Error code 570 is special. It means that we CAN enter a message
1151 * in this room, but a recipient needs to be specified.
1158 /* If the user is a dumbass, tell them how to type. */
1159 if ((userflags & US_EXPERT) == 0) {
1160 scr_printf("Entering message. Word wrap will give you soft linebreaks. Pressing the\n");
1161 scr_printf("'enter' key will give you a hard linebreak and an indent. Press 'enter' twice\n");
1162 scr_printf("when finished.\n");
1165 /* Handle the selection of a recipient, if necessary. */
1167 if (need_recp == 1) {
1168 if (axlevel >= AxProbU) {
1170 strcpy(buf, reply_to);
1173 newprompt("Enter recipient: ", buf, SIZ - 100);
1174 if (IsEmptyStr(buf)) {
1180 strcpy(buf, "sysop");
1183 strcpy(message.recipient, buf);
1185 if (room_flags & QR_ANONOPT) {
1186 scr_printf("Anonymous (Y/N)? ");
1188 message.anonymous = 1;
1191 /* If it's mail, we've got to check the validity of the recipient... */
1192 if (!IsEmptyStr(message.recipient)) {
1193 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1195 scr_printf("%s\n", buf);
1200 /* Learn the number of the newest message in in the room, so we can
1201 * tell upon saving whether someone else has posted too.
1204 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1206 scr_printf("%s\n", buf);
1209 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1212 /* Now compose the message... */
1213 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject, subject_required) != 0) {
1219 /* Reopen the temp file that was created, so we can send it */
1220 fp = fopen(temp, "r");
1222 if (!fp || !(message.text = load_message_from_file(fp))) {
1223 scr_printf("*** Internal error while trying to save message!\n" "%s: %s\n", temp, strerror(errno));
1231 /* Break lines that are >1024 characters, otherwise the server
1232 * will truncate them.
1234 break_big_lines(message.text);
1236 /* Transmit message to the server */
1237 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1239 scr_printf("%s\n", buf);
1243 /* Yes, unlink it now, so it doesn't stick around if we crash */
1247 highmsg = msgarr[num_msgs - 1];
1252 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1254 scr_printf("%s\n", buf);
1257 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1260 /* get new highest message number in room to set lrp for goto... */
1261 maxmsgnum = msgarr[num_msgs - 1];
1263 /* now see if anyone else has posted in here */
1265 for (a = 0; a < num_msgs; ++a) {
1266 if (msgarr[a] > highmsg) {
1275 /* In the Mail> room, this algorithm always counts one message
1276 * higher than in public rooms, so we decrement it by one.
1283 scr_printf("*** 1 additional message has been entered " "in this room by another user.\n");
1286 scr_printf("*** %d additional messages have been entered " "in this room by other users.\n", b);
1295 * Do editing on a quoted file
1297 void process_quote(void) {
1298 FILE *qfile, *tfile;
1300 int line, qstart, qend;
1302 // Unlink the second temp file as soon as it's opened, so it'll get
1303 // deleted even if the program dies
1304 qfile = fopen(temp2, "r");
1307 /* Display the quotable text with line numbers added */
1309 if (fgets(buf, 128, qfile) == NULL) {
1310 /* we're skipping a line here */
1312 while (fgets(buf, 128, qfile) != NULL) {
1313 scr_printf("%3d %s", ++line, buf);
1316 qstart = intprompt("Begin quoting at", 1, 1, line);
1317 qend = intprompt(" End quoting at", line, qstart, line);
1321 if (fgets(buf, 128, qfile) == NULL) {
1322 /* we're skipping a line here */
1324 tfile = fopen(temp, "w");
1325 while (fgets(buf, 128, qfile) != NULL) {
1326 if ((++line >= qstart) && (line <= qend)) {
1327 fprintf(tfile, " >%s", buf);
1330 fprintf(tfile, " \n");
1338 * List the URLs which were embedded in the previous message
1340 void list_urls(CtdlIPC * ipc) {
1345 if (num_urls == 0) {
1346 scr_printf("There were no URLs in the previous message.\n\n");
1350 for (i = 0; i < num_urls; ++i) {
1351 scr_printf("%3d %s\n", i + 1, urls[i]);
1354 if ((i = num_urls) != 1) {
1355 i = intprompt("Display which one", 1, 1, num_urls);
1358 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1365 * Run image viewer in background
1367 int do_image_view(const char *filename) {
1371 snprintf(cmd, sizeof cmd, imagecmd, filename);
1378 if (childpid == 0) {
1380 pid_t grandchildpid;
1382 grandchildpid = fork();
1383 if (grandchildpid < 0) {
1384 return grandchildpid;
1387 if (grandchildpid == 0) {
1392 nullfd = open("/dev/null", O_WRONLY);
1399 retcode = system(cmd);
1409 if (grandchildpid > 0) {
1417 waitpid(childpid, &retcode, 0);
1426 * View an image attached to a message
1428 void image_view(CtdlIPC * ipc, unsigned long msg) {
1429 struct parts *ptr = last_message_parts;
1433 /* Run through available parts */
1434 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1435 if ((!strcasecmp(ptr->disposition, "attachment")
1436 || !strcasecmp(ptr->disposition, "inline"))
1437 && !strncmp(ptr->mimetype, "image/", 6)) {
1440 strcpy(part, ptr->number);
1447 strprompt("View which part (0 when done)", part, SIZ - 1);
1449 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1450 if ((!strcasecmp(ptr->disposition, "attachment")
1451 || !strcasecmp(ptr->disposition, "inline"))
1452 && !strncmp(ptr->mimetype, "image/", 6)
1453 && !strcasecmp(ptr->number, part)) {
1456 void *file = NULL; /* The downloaded file */
1461 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1463 scr_printf("%s\n", buf);
1467 len = (size_t) extract_long(buf, 0);
1468 progress(ipc, len, len);
1470 CtdlMakeTempFileName(tmp, sizeof tmp);
1471 strcat(tmp, ptr->filename);
1472 save_buffer(file, len, tmp);
1486 * Read the messages in the current room
1488 void readmsgs(CtdlIPC *ipc,
1489 enum MessageList c, // see listing in citadel_ipc.h
1490 enum MessageDirection rdir, // 1=Forward (-1)=Reverse
1491 int q // Number of msgs to read (if c==3)
1493 int a, e, f, g, start;
1499 char prtfile[PATH_MAX];
1502 char targ[ROOMNAMELEN];
1503 char filename[PATH_MAX];
1504 char save_to[PATH_MAX];
1505 void *attachment = NULL; /* Downloaded attachment */
1506 FILE *dest = NULL; /* Alternate destination other than screen */
1507 int r; /* IPC response code */
1508 static int att_seq = 0; /* Attachment download sequence number */
1509 int rv = 0; /* silence the stupid warn_unused_result warnings */
1511 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1517 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1519 scr_printf("%s\n", cmd);
1522 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++);
1525 if (num_msgs == 0) {
1526 if (c == LastMessages) {
1529 scr_printf("*** There are no ");
1530 if (c == NewMessages)
1532 if (c == OldMessages)
1534 scr_printf("messages in this room.\n");
1538 /* this loop cycles through each message... */
1539 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1540 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1541 while (msg_arr[a] == 0L) {
1543 if ((a == num_msgs) || (a == (-1)))
1547 RAGAIN:pagin = ((arcflag == 0)
1549 && (userflags & US_PAGINATOR)) ? 1 : 0;
1551 /* If we're doing a quote, set the screenwidth to 72 */
1553 hold_sw = screenwidth;
1557 /* If printing or archiving, set the screenwidth to 80 */
1559 hold_sw = screenwidth;
1563 /* clear parts list */
1564 free_parts(last_message_parts);
1565 last_message_parts = NULL;
1567 /* now read the message... */
1568 e = read_message(ipc, msg_arr[a], pagin, dest);
1570 /* ...and set the screenwidth back if we have to */
1571 if ((quotflag) || (arcflag)) {
1572 screenwidth = hold_sw;
1575 highest_msg_read = msg_arr[a];
1580 enable_color = hold_color;
1589 enable_color = hold_color;
1592 if (freopen(prtfile, "r", stdin) == NULL) {
1593 /* we probably should handle the error condition here */
1595 stty_ctdl(SB_RESTORE);
1596 ka_system(printcmd);
1597 stty_ctdl(SB_NO_INTR);
1604 } while ((g != f) && (g >= 0));
1605 scr_printf("Message printed.\n");
1609 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1610 && (((room_flags & QR_MAILBOX) == 0)
1611 || (rc_force_mail_prompts == 0))) {
1617 color(BRIGHT_WHITE);
1618 scr_printf("%d", num_msgs - a - 1);
1622 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1623 if (rc_url_cmd[0] && num_urls)
1624 keyopt("<U>RLview ");
1625 if (has_images > 0 && !IsEmptyStr(imagecmd))
1626 keyopt("<I>mages ");
1627 keyopt("<?>help -> ");
1630 e = (inkey() & 127);
1632 /* return key same as <N> */ if (e == 10)
1634 /* space key same as <N> */ if (e == 32)
1636 /* del/move for aides only */
1638 && ((room_flags & QR_MAILBOX) == 0)
1639 && ((room_flags2 & QR2_COLLABDEL) == 0)
1641 if ((e == 'd') || (e == 'm'))
1644 /* print only if available */
1645 if ((e == 'p') && (IsEmptyStr(printcmd)))
1647 /* can't file if not allowed */
1649 && (rc_allow_attachments == 0))
1651 /* link only if browser avail*/
1653 && (IsEmptyStr(rc_url_cmd)))
1656 && (IsEmptyStr(imagecmd) || !has_images))
1658 } while ((e != 'a') && (e != 'n') && (e != 's')
1659 && (e != 'd') && (e != 'm') && (e != 'p')
1660 && (e != 'q') && (e != 'b') && (e != 'h')
1661 && (e != 'r') && (e != 'f') && (e != '?')
1662 && (e != 'u') && (e != 'c') && (e != 'y')
1663 && (e != 'i') && (e != 'o'));
1669 scr_printf("Again");
1672 scr_printf("Delete");
1684 scr_printf("Print");
1687 scr_printf("reply Quoted");
1693 scr_printf("Header");
1696 scr_printf("Reply");
1699 scr_printf("Open attachments");
1705 scr_printf("URL's");
1708 scr_printf("mY next");
1713 scr_printf("? <help>");
1716 if (userflags & US_DISAPPEAR || e == 'i')
1717 scr_printf("\r%79s\r", "");
1721 DONE_QUOTING:switch (e) {
1723 scr_printf("Options available here:\n"
1724 " ? Help (prints this message)\n"
1725 " S Stop reading immediately\n"
1726 " A Again (repeats last message)\n"
1727 " N Next (continue with next message)\n"
1728 " Y My Next (continue with next message you authored)\n"
1729 " B Back (go back to previous message)\n");
1730 if ((is_room_aide) || (room_flags & QR_MAILBOX) || (room_flags2 & QR2_COLLABDEL)) {
1731 scr_printf(" D Delete this message\n" " M Move message to another room\n");
1733 scr_printf(" C Copy message to another room\n");
1734 if (!IsEmptyStr(printcmd))
1735 scr_printf(" P Print this message\n");
1736 scr_printf(" Q Reply to this message, quoting portions of it\n"
1737 " H Headers (display message headers only)\n");
1739 scr_printf(" R Reply to this message\n");
1740 if (rc_allow_attachments) {
1741 scr_printf(" O (Open attachments)\n");
1742 scr_printf(" F (save attachments to a File)\n");
1744 if (!IsEmptyStr(rc_url_cmd))
1745 scr_printf(" U (list URL's for display)\n");
1746 if (!IsEmptyStr(imagecmd) && has_images > 0)
1747 scr_printf(" I Image viewer\n");
1752 dest = fopen(prtfile, "w");
1754 hold_color = enable_color;
1759 dest = fopen(temp2, "w");
1761 hold_color = enable_color;
1773 newprompt("Enter target room: ", targ, ROOMNAMELEN - 1);
1774 if (!IsEmptyStr(targ)) {
1775 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0), msg_arr[a], targ, cmd);
1776 scr_printf("%s\n", cmd);
1783 if (r / 100 != 2) /* r will be init'ed, FIXME */
1784 goto RMSGREAD; /* the logic here sucks */
1788 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1789 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a], filename, &attachment, progress, cmd);
1791 scr_printf("%s\n", cmd);
1794 extract_token(filename, cmd, 2, '|', sizeof filename);
1796 * Part 1 won't have a filename; use the
1797 * subject of the message instead. IO
1799 if (IsEmptyStr(filename)) {
1800 strcpy(filename, reply_subject);
1802 if (e == 'o') { /* open attachment */
1803 mkdir(tempdir, 0700);
1804 snprintf(save_to, sizeof save_to, "%s/%04x.%s", tempdir, ++att_seq, filename);
1805 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1806 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1809 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1811 else { /* save attachment to disk */
1812 destination_directory(save_to, filename);
1813 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1822 scr_printf("*** Delete this message? ");
1824 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1825 scr_printf("%s\n", cmd);
1834 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1837 savedpos = num_msgs;
1838 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1839 num_msgs = savedpos;
1845 image_view(ipc, msg_arr[a]);
1848 { /* hack hack hack */
1849 /* find the next message by me, stay here if we find nothing */
1852 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir) {
1853 /* This is repetitively dumb, but that's what computers are for.
1854 We have to load up messages until we find one by us */
1857 struct ctdlipcmessage *msg = NULL;
1859 /* read the header so we can get 'from=' */
1860 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1861 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1862 a = lasta; /* meesa current */
1870 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1874 } /* end for loop */
1875 } /* end read routine */
1879 * View and edit a system message
1881 void edit_system_message(CtdlIPC * ipc, char *which_message) {
1884 char write_cmd[SIZ];
1886 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1887 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1888 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1889 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1894 * Loads the contents of a file into memory. Caller must free the allocated memory.
1896 char *load_message_from_file(FILE * src) {
1901 fseek(src, 0, SEEK_END);
1905 dest = (char *) calloc(1, i + 1);
1912 g = fread(dest + got, 1, i - got, src);
1915 /* Interrupted system call, keep going */
1918 /* At this point we have either EOF or error */