1 // Text client functions for reading and writing of messages
3 // Copyright (c) 1987-2020 by the citadel.org team
5 // This program is open source software. Use, duplication, and/or
6 // disclosure are subject to the GNU General Purpose License version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 #include "textclient.h"
15 #define MAXWORDBUF SIZ
16 #define NO_REPLY_TO "nobody ... xxxxxx"
19 char reply_subject[SIZ];
20 char reply_references[SIZ];
21 char reply_inreplyto[SIZ];
25 char text[MAXWORDBUF];
28 void stty_ctdl(int cmd);
29 int haschar(const char *st, int ch);
30 int file_checksum(char *filename);
31 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax);
33 unsigned long *msg_arr = NULL;
36 extern char room_name[];
37 extern char tempdir[];
38 extern unsigned room_flags;
39 extern unsigned room_flags2;
41 extern long highest_msg_read;
44 extern int screenwidth;
45 extern int screenheight;
46 extern long maxmsgnum;
49 extern char is_room_aide;
50 extern char fullname[];
52 extern unsigned userflags;
53 extern char sigcaught;
54 extern char printcmd[];
55 extern int rc_allow_attachments;
56 extern int rc_display_message_numbers;
57 extern int rc_force_mail_prompts;
58 extern int editor_pid;
59 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
61 char urls[MAXURLS][SIZ];
63 int has_images = 0; /* Current msg has images */
64 struct parts *last_message_parts = NULL; /* Parts from last msg */
67 void ka_sigcatch(int signum)
70 signal(SIGALRM, ka_sigcatch);
71 CtdlIPCNoop(ipc_for_signal_handlers);
76 * server keep-alive version of wait() (needed for external editor)
78 pid_t ka_wait(int *kstatus)
83 signal(SIGALRM, ka_sigcatch);
87 } while (errno == EINTR);
88 signal(SIGALRM, SIG_IGN);
95 * version of system() that uses ka_wait()
97 int ka_system(char *shc)
106 perror("Cannot fork");
108 return ((pid_t) childpid);
112 execlp("/bin/sh", "sh", "-c", shc, NULL);
118 waitpid = ka_wait(&retcode);
119 } while (waitpid != childpid);
128 * add a newline to the buffer...
130 void add_newline(struct cittext *textlist)
135 while (ptr->next != NULL)
138 while (ptr->text[strlen(ptr->text) - 1] == 32)
139 ptr->text[strlen(ptr->text) - 1] = 0;
141 ptr->next = (struct cittext *)
142 malloc(sizeof(struct cittext));
145 strcpy(ptr->text, "");
150 * add a word to the buffer...
152 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)
178 int a, prev, finished, b, last_space;
180 struct cittext *textlist = NULL;
182 char wordbuf[MAXWORDBUF];
185 /* first, load the text into the buffer */
187 textlist = (struct cittext *) malloc(sizeof(struct cittext));
188 textlist->next = NULL;
189 strcpy(textlist->text, "");
193 while (a = getc(fp), a >= 0) {
195 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
196 add_word(textlist, wordbuf);
198 if ((prev == 13) || (prev == 10)) {
199 add_word(textlist, "\n");
200 add_newline(textlist);
201 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;
244 } else if (a == 23) {
246 wordbuf[strlen(wordbuf) - 1] = 0;
250 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
251 } else if (a == 13) {
253 if (IsEmptyStr(wordbuf))
256 for (b = 0; b < strlen(wordbuf); ++b)
257 if (wordbuf[b] == 32) {
259 add_word(textlist, wordbuf);
260 strcpy(wordbuf, &wordbuf[b + 1]);
263 add_word(textlist, wordbuf);
268 wordbuf[strlen(wordbuf) + 1] = 0;
269 wordbuf[strlen(wordbuf)] = a;
271 if ((strlen(wordbuf) + 3) > screenwidth) {
273 for (b = 0; b < strlen(wordbuf); ++b)
274 if (wordbuf[b] == 32)
276 if (last_space >= 0) {
277 for (b = 0; b < strlen(wordbuf); ++b)
278 if (wordbuf[b] == 32) {
280 add_word(textlist, wordbuf);
281 strcpy(wordbuf, &wordbuf[b + 1]);
284 for (b = 0; b < strlen(wordbuf); ++b) {
289 scr_printf("\n%s", wordbuf);
291 add_word(textlist, wordbuf);
297 } while (finished == 0);
299 /* write the buffer back to disk */
301 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
302 fprintf(fp, "%s", ptr->text);
306 rv = ftruncate(fileno(fp), ftell(fp));
308 scr_printf("failed to set message buffer: %s\n", strerror(errno));
311 /* and deallocate the memory we used */
312 while (textlist != NULL) {
313 ptr = textlist->next;
321 * Free the struct parts
323 void free_parts(struct parts *p)
325 struct parts *a_part = p;
331 a_part = a_part->next;
338 * This is a mini RFC2047 decoder.
339 * It only handles strings encoded from UTF-8 as Quoted-printable.
340 * We can do this "in place" because the converted string will always be smaller than the source string.
342 void mini_2047_decode(char *s)
344 if (!s) { // no null strings allowed!
348 char *qstart = strstr(s, "=?UTF-8?Q?"); // Must start with this string
353 char *qend = strstr(qstart+10, "?="); // Must end with this string
358 if (qend <= qstart) { // And there must be something in between them.
362 // The string has qualified for conversion.
364 strcpy(qend, ""); // Strip the trailer
365 strcpy(qstart, &qstart[10]); // Strip the header
367 char *r = qstart; // Pointer to where in the string we're reading
368 char *w = s; // Pointer to where in the string we're writing
370 while(*r) { // Loop through the source string
371 if (r[0] == '=') { // "=" means read a hex character
377 sscanf(ch, "%02x", &c);
382 else if (r[0] == '_') { // "_" is a space
387 else { // anything else pass through literally
393 w[0] = 0; // null terminate
398 * Read a message from the server
400 int read_message(CtdlIPC *ipc,
401 long num, /* message number */
402 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
403 FILE *dest /* Destination file, NULL for screen */
410 struct ctdlipcmessage *message = NULL;
411 int r; /* IPC response code */
412 char *converted_text = NULL;
419 int final_line_is_blank = 0;
425 strcpy(reply_to, NO_REPLY_TO);
426 strcpy(reply_subject, "");
427 strcpy(reply_references, "");
428 strcpy(reply_inreplyto, "");
430 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
432 scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
435 free_parts(message->attachments);
441 fprintf(dest, "\n ");
449 if (pagin == 1 && !dest) {
453 /* View headers only */
455 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
456 message->nhdr ? "yes" : "no", message->author, message->type, message->msgid);
457 if (!IsEmptyStr(message->subject)) {
458 scr_printf("subj=%s\n", message->subject);
460 if (!IsEmptyStr(message->email)) {
461 scr_printf("rfca=%s\n", message->email);
463 scr_printf("room=%s\ntime=%s", message->room, asctime(localtime(&message->time)));
464 if (!IsEmptyStr(message->recipient)) {
465 scr_printf("rcpt=%s\n", message->recipient);
467 if (message->attachments) {
470 for (ptr = message->attachments; ptr; ptr = ptr->next) {
471 scr_printf("part=%s|%s|%s|%s|%s|%ld\n",
472 ptr->name, ptr->filename, ptr->number, ptr->disposition, ptr->mimetype, ptr->length);
478 free_parts(message->attachments);
483 if (rc_display_message_numbers) {
485 fprintf(dest, "[#%s] ", message->msgid);
490 scr_printf("#%s", message->msgid);
495 if (nhdr == 1 && !is_room_aide) {
497 fprintf(dest, " ****");
505 localtime_r(&message->time, &thetime);
506 strftime(now, sizeof now, "%F %R", &thetime);
508 fprintf(dest, "%s from %s ", now, message->author);
509 if (!message->is_local) {
510 fprintf(dest, "<%s> ", message->email);
515 scr_printf("%s ", now);
519 scr_printf("%s ", message->author);
520 if (!message->is_local) {
524 scr_printf("%s", message->email);
529 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
531 fprintf(dest, "in %s> ", message->room);
535 color(BRIGHT_MAGENTA);
536 scr_printf("%s> ", message->room);
539 if (!IsEmptyStr(message->recipient)) {
541 fprintf(dest, "to %s ", message->recipient);
546 scr_printf("%s ", message->recipient);
558 /* Set the reply-to address to an Internet e-mail address if possible
560 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
561 if (!IsEmptyStr(message->author)) {
562 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
565 safestrncpy(reply_to, message->email, sizeof reply_to);
569 /* But if we can't do that, set it to a Citadel address.
571 if (!strcmp(reply_to, NO_REPLY_TO)) {
572 safestrncpy(reply_to, message->author, sizeof(reply_to));
575 if (message->msgid != NULL) {
576 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
579 if (message->references != NULL) {
580 if (!IsEmptyStr(message->references)) {
581 safestrncpy(reply_references, message->references, sizeof reply_references);
585 if (message->subject != NULL) {
586 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
587 if (!IsEmptyStr(message->subject)) {
589 fprintf(dest, "Subject: %s\n", message->subject);
592 scr_printf("Subject: ");
594 mini_2047_decode(message->subject);
595 scr_printf("%s\n", message->subject);
601 if (pagin == 1 && !dest) {
605 /******* end of header output, start of message text output *******/
608 * Convert HTML to plain text, formatting for the actual width
609 * of the client screen.
611 if (!strcasecmp(message->content_type, "text/html")) {
612 converted_text = html_to_ascii(message->text, 0, screenwidth);
613 if (converted_text != NULL) {
615 message->text = converted_text;
620 /* Text/plain is a different type */
621 if (!strcasecmp(message->content_type, "text/plain")) {
625 /* Render text/x-markdown as plain text */
626 if (!strcasecmp(message->content_type, "text/x-markdown")) {
631 static char *urlprefixes[] = {
637 num_urls = 0; /* Start with a clean slate */
638 for (p = 0; p < (sizeof urlprefixes / sizeof(char *)); ++p) {
639 searchptr = message->text;
640 while ((searchptr != NULL) && (num_urls < MAXURLS)) {
641 searchptr = strstr(searchptr, urlprefixes[p]);
642 if (searchptr != NULL) {
643 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
644 for (i = 0; i < strlen(urls[num_urls]); i++) {
645 ch = urls[num_urls][i];
646 if (ch == '>' || ch == '\"' || ch == ')' || ch == ' ' || ch == '\n') {
647 urls[num_urls][i] = 0;
660 if (format_type == 0) {
661 fr = fmout(screenwidth, NULL, message->text, dest, 1);
664 /* renderer for text/plain */
666 lineptr = message->text;
669 nextline = strchr(lineptr, '\n');
670 if (nextline != NULL) {
677 if (sigcaught == 0) {
678 linelen = strlen(lineptr);
679 if (linelen && (lineptr[linelen - 1] == '\r')) {
680 lineptr[--linelen] = 0;
683 fprintf(dest, "%s\n", lineptr);
685 scr_printf("%s\n", lineptr);
689 final_line_is_blank = 1;
691 final_line_is_blank = 0;
696 if (!final_line_is_blank) {
705 /* Enumerate any attachments */
706 if ((pagin == 1) && (message->attachments)) {
709 for (ptr = message->attachments; ptr; ptr = ptr->next) {
710 if ((!strcasecmp(ptr->disposition, "attachment"))
711 || (!strcasecmp(ptr->disposition, "inline"))
712 || (!strcasecmp(ptr->disposition, ""))
714 if ((strcasecmp(ptr->number, message->mime_chosen))
715 && (!IsEmptyStr(ptr->mimetype))
719 color(BRIGHT_MAGENTA);
720 scr_printf("%s", ptr->number);
724 scr_printf("%s", ptr->filename);
726 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
727 if (!strncmp(ptr->mimetype, "image/", 6)) {
735 /* Save the attachments info for later */
736 last_message_parts = message->attachments;
742 if (pagin == 1 && !dest)
750 * replace string function for the built-in editor
752 void replace_string(char *filename, long int startpos)
761 int substitutions = 0;
765 newprompt("Enter text to be replaced: ", srch_str, (sizeof(srch_str) - 1));
766 if (IsEmptyStr(srch_str)) {
770 newprompt("Enter text to replace it with: ", rplc_str, (sizeof(rplc_str) - 1));
772 fp = fopen(filename, "r+");
778 fseek(fp, startpos, 0);
780 while (a = getc(fp), a > 0) {
782 buf[strlen(buf) + 1] = 0;
783 buf[strlen(buf)] = a;
784 if (strlen(buf) >= strlen(srch_str)) {
785 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
786 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
787 strcpy(ptr, rplc_str);
791 if (strlen(buf) > 384) {
794 rv = fwrite((char *) buf, 128, 1, fp);
796 scr_printf("failed to replace string: %s\n", strerror(errno));
799 strcpy(buf, &buf[128]);
805 if (!IsEmptyStr(buf)) {
806 rv = fwrite((char *) buf, strlen(buf), 1, fp);
810 rv = truncate(filename, wpos);
811 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
816 * Function to begin composing a new message
818 int client_make_message(CtdlIPC * ipc, char *filename, /* temporary file name */
819 char *recipient, /* NULL if it's not mail */
820 int is_anonymous, int format_type, int mode, char *subject, /* buffer to store subject line */
821 int subject_required)
830 if ((mode == 2) && (IsEmptyStr(editor_path))) {
831 scr_printf("*** No editor available; using built-in editor.\n");
836 time_t now = time(NULL);
837 localtime_r(&now, &thetime);
838 strftime(datestr, sizeof datestr, "%F %R", &thetime);
841 if (room_flags & QR_ANONONLY && !recipient) {
842 snprintf(header, sizeof header, " ****");
844 snprintf(header, sizeof header, " %s from %s", datestr, (is_anonymous ? "[anonymous]" : fullname)
846 if (!IsEmptyStr(recipient)) {
847 size_t tmp = strlen(header);
848 snprintf(&header[tmp], sizeof header - tmp, " to %s", recipient);
851 scr_printf("%s\n", header);
853 if (!IsEmptyStr(subject)) {
854 scr_printf("Subject: %s\n", subject);
857 if ((subject_required) && (IsEmptyStr(subject))) {
858 newprompt("Subject: ", subject, 70);
862 scr_printf("(Press ctrl-d when finished)\n");
866 fp = fopen(filename, "r");
868 fmout(screenwidth, fp, NULL, NULL, 0);
871 scr_printf("failed to get stream position %s\n", strerror(errno));
874 fp = fopen(filename, "w");
876 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno)
887 fp = fopen(filename, "r+");
889 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno)
898 fp = fopen(filename, "a");
900 scr_printf("*** Error opening temp file!\n" " %s: %s\n", filename, strerror(errno));
920 default: /* allow 2+ modes */
921 e_ex_code = 1; /* start with a failed exit code */
922 stty_ctdl(SB_RESTORE);
924 cksum = file_checksum(filename);
925 if (editor_pid == 0) {
928 chmod(filename, 0600);
929 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
931 execlp(editor_path, editor_path, filename, NULL);
937 b = ka_wait(&e_ex_code);
938 } while ((b != editor_pid) && (b >= 0));
944 MECR:if (mode >= 2) {
945 if (file_checksum(filename) == cksum) {
946 scr_printf("*** Aborted message.\n");
949 if (e_ex_code == 0) {
955 b = keymenu("Entry command (? for options)",
957 "<C>ontinue|" "<S>ave message|" "<P>rint formatted|" "add s<U>bject|" "<R>eplace string|" "<H>old message");
966 scr_printf(" %s from %s", datestr, fullname);
967 if (!IsEmptyStr(recipient)) {
968 scr_printf(" to %s", recipient);
972 if (!IsEmptyStr(subject)) {
973 scr_printf("Subject: %s\n", subject);
975 fp = fopen(filename, "r");
977 fmout(screenwidth, fp, NULL, NULL, 0);
980 scr_printf("failed to get stream position %s\n", strerror(errno));
986 replace_string(filename, 0L);
993 if (subject != NULL) {
994 newprompt("Subject: ", subject, 70);
1001 MEABT:scr_printf("Are you sure? ");
1005 MEABT2:unlink(filename);
1011 * Make sure there's room in msg_arr[] for at least one more.
1013 void check_msg_arr_size(void)
1015 if ((num_msgs + 1) > msg_arr_size) {
1016 msg_arr_size += 512;
1017 msg_arr = realloc(msg_arr, ((sizeof(long)) * msg_arr_size));
1023 * break_big_lines() - break up lines that are >1024 characters
1024 * otherwise the server will truncate
1026 void break_big_lines(char *msg)
1036 while (strlen(ptr) > 1000) {
1037 break_here = strchr(&ptr[900], ' ');
1038 if ((break_here == NULL) || (break_here > &ptr[999])) {
1039 break_here = &ptr[999];
1048 * entmsg() - edit and create a message
1049 * returns 0 if message was saved
1051 int entmsg(CtdlIPC * ipc, int is_reply, /* nonzero if this was a <R>eply command */
1053 int masquerade /* prompt for a non-default display name? */
1063 struct ctdlipcmessage message;
1064 unsigned long *msgarr = NULL;
1065 int r; /* IPC response code */
1066 int subject_required = 0;
1068 if (entmsg_ok == ENTMSG_OK_YES) {
1069 /* no problem, go right ahead */
1071 else if (entmsg_ok == ENTMSG_OK_BLOG) {
1073 scr_printf("WARNING: this is a BLOG room.\n");
1074 scr_printf("The '<E>nter Message' command will create a BLOG POST.\n");
1075 scr_printf("If you want to leave a comment or reply to a comment, use the '<R>eply' command.\n");
1076 scr_printf("Do you really want to create a new blog post? ");
1083 scr_printf("You may not enter messages in this type of room.\n");
1094 strcpy(subject, "");
1097 * First, check to see if we have permission to enter a message in
1098 * this room. The server will return an error code if we can't.
1100 strcpy(message.recipient, "");
1101 strcpy(message.author, "");
1102 strcpy(message.subject, "");
1103 strcpy(message.references, "");
1104 message.text = ""; /* point to "", changes later */
1105 message.anonymous = 0;
1106 message.type = mode;
1109 newprompt("Display name for this message: ", message.author, 40);
1114 if (!IsEmptyStr(reply_subject)) {
1115 if (!strncasecmp(reply_subject, "Re: ", 3)) {
1116 strcpy(message.subject, reply_subject);
1118 snprintf(message.subject, sizeof message.subject, "Re: %s", reply_subject);
1122 /* Trim down excessively long lists of thread references. We eliminate the
1123 * second one in the list so that the thread root remains intact.
1125 int rrtok = num_tokens(reply_references, '|');
1126 int rrlen = strlen(reply_references);
1127 if (((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10)) {
1128 remove_token(reply_references, 1, '|');
1131 snprintf(message.references, sizeof message.references, "%s%s%s",
1132 reply_references, (IsEmptyStr(reply_references) ? "" : "|"), reply_inreplyto);
1135 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1137 if (r / 100 != 2 && r / 10 != 57) {
1138 scr_printf("%s\n", buf);
1142 /* Error code 570 is special. It means that we CAN enter a message
1143 * in this room, but a recipient needs to be specified.
1150 /* If the user is a dumbass, tell them how to type. */
1151 if ((userflags & US_EXPERT) == 0) {
1152 scr_printf("Entering message. Word wrap will give you soft linebreaks. Pressing the\n");
1153 scr_printf("'enter' key will give you a hard linebreak and an indent. Press 'enter' twice\n");
1154 scr_printf("when finished.\n");
1157 /* Handle the selection of a recipient, if necessary. */
1159 if (need_recp == 1) {
1160 if (axlevel >= AxProbU) {
1162 strcpy(buf, reply_to);
1164 newprompt("Enter recipient: ", buf, SIZ - 100);
1165 if (IsEmptyStr(buf)) {
1170 strcpy(buf, "sysop");
1172 strcpy(message.recipient, buf);
1174 if (room_flags & QR_ANONOPT) {
1175 scr_printf("Anonymous (Y/N)? ");
1177 message.anonymous = 1;
1180 /* If it's mail, we've got to check the validity of the recipient... */
1181 if (!IsEmptyStr(message.recipient)) {
1182 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1184 scr_printf("%s\n", buf);
1189 /* Learn the number of the newest message in in the room, so we can
1190 * tell upon saving whether someone else has posted too.
1193 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1195 scr_printf("%s\n", buf);
1197 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1200 /* Now compose the message... */
1201 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject, subject_required) != 0) {
1207 /* Reopen the temp file that was created, so we can send it */
1208 fp = fopen(temp, "r");
1210 if (!fp || !(message.text = load_message_from_file(fp))) {
1211 scr_printf("*** Internal error while trying to save message!\n" "%s: %s\n", temp, strerror(errno));
1219 /* Break lines that are >1024 characters, otherwise the server
1220 * will truncate them.
1222 break_big_lines(message.text);
1224 /* Transmit message to the server */
1225 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1227 scr_printf("%s\n", buf);
1231 /* Yes, unlink it now, so it doesn't stick around if we crash */
1235 highmsg = msgarr[num_msgs - 1];
1240 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1242 scr_printf("%s\n", buf);
1244 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1247 /* get new highest message number in room to set lrp for goto... */
1248 maxmsgnum = msgarr[num_msgs - 1];
1250 /* now see if anyone else has posted in here */
1252 for (a = 0; a < num_msgs; ++a) {
1253 if (msgarr[a] > highmsg) {
1261 /* In the Mail> room, this algorithm always counts one message
1262 * higher than in public rooms, so we decrement it by one.
1269 scr_printf("*** 1 additional message has been entered " "in this room by another user.\n");
1271 scr_printf("*** %d additional messages have been entered " "in this room by other users.\n", b);
1280 * Do editing on a quoted file
1282 void process_quote(void)
1284 FILE *qfile, *tfile;
1286 int line, qstart, qend;
1288 /* Unlink the second temp file as soon as it's opened, so it'll get
1289 * deleted even if the program dies
1291 qfile = fopen(temp2, "r");
1294 /* Display the quotable text with line numbers added */
1296 if (fgets(buf, 128, qfile) == NULL) {
1297 /* we're skipping a line here */
1299 while (fgets(buf, 128, qfile) != NULL) {
1300 scr_printf("%3d %s", ++line, buf);
1303 qstart = intprompt("Begin quoting at", 1, 1, line);
1304 qend = intprompt(" End quoting at", line, qstart, line);
1308 if (fgets(buf, 128, qfile) == NULL) {
1309 /* we're skipping a line here */
1311 tfile = fopen(temp, "w");
1312 while (fgets(buf, 128, qfile) != NULL) {
1313 if ((++line >= qstart) && (line <= qend)) {
1314 fprintf(tfile, " >%s", buf);
1317 fprintf(tfile, " \n");
1325 * List the URLs which were embedded in the previous message
1327 void list_urls(CtdlIPC * ipc)
1333 if (num_urls == 0) {
1334 scr_printf("There were no URLs in the previous message.\n\n");
1338 for (i = 0; i < num_urls; ++i) {
1339 scr_printf("%3d %s\n", i + 1, urls[i]);
1342 if ((i = num_urls) != 1)
1343 i = intprompt("Display which one", 1, 1, num_urls);
1345 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1352 * Run image viewer in background
1354 int do_image_view(const char *filename)
1359 snprintf(cmd, sizeof cmd, imagecmd, filename);
1366 if (childpid == 0) {
1368 pid_t grandchildpid;
1370 grandchildpid = fork();
1371 if (grandchildpid < 0) {
1372 return grandchildpid;
1375 if (grandchildpid == 0) {
1380 nullfd = open("/dev/null", O_WRONLY);
1387 retcode = system(cmd);
1397 if (grandchildpid > 0) {
1405 waitpid(childpid, &retcode, 0);
1414 * View an image attached to a message
1416 void image_view(CtdlIPC * ipc, unsigned long msg)
1418 struct parts *ptr = last_message_parts;
1422 /* Run through available parts */
1423 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1424 if ((!strcasecmp(ptr->disposition, "attachment")
1425 || !strcasecmp(ptr->disposition, "inline"))
1426 && !strncmp(ptr->mimetype, "image/", 6)) {
1429 strcpy(part, ptr->number);
1436 strprompt("View which part (0 when done)", part, SIZ - 1);
1438 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1439 if ((!strcasecmp(ptr->disposition, "attachment")
1440 || !strcasecmp(ptr->disposition, "inline"))
1441 && !strncmp(ptr->mimetype, "image/", 6)
1442 && !strcasecmp(ptr->number, part)) {
1445 void *file = NULL; /* The downloaded file */
1450 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1452 scr_printf("%s\n", buf);
1456 len = (size_t) extract_long(buf, 0);
1457 progress(ipc, len, len);
1459 CtdlMakeTempFileName(tmp, sizeof tmp);
1460 strcat(tmp, ptr->filename);
1461 save_buffer(file, len, tmp);
1475 * Read the messages in the current room
1477 void readmsgs(CtdlIPC * ipc, enum MessageList c, /* see listing in citadel_ipc.h */
1478 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1479 int q /* Number of msgs to read (if c==3) */
1482 int a, e, f, g, start;
1488 char prtfile[PATH_MAX];
1491 char targ[ROOMNAMELEN];
1492 char filename[PATH_MAX];
1493 char save_to[PATH_MAX];
1494 void *attachment = NULL; /* Downloaded attachment */
1495 FILE *dest = NULL; /* Alternate destination other than screen */
1496 int r; /* IPC response code */
1497 static int att_seq = 0; /* Attachment download sequence number */
1498 int rv = 0; /* silence the stupid warn_unused_result warnings */
1500 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1506 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1508 scr_printf("%s\n", cmd);
1510 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++);
1513 if (num_msgs == 0) {
1514 if (c == LastMessages) {
1517 scr_printf("*** There are no ");
1518 if (c == NewMessages)
1520 if (c == OldMessages)
1522 scr_printf("messages in this room.\n");
1526 /* this loop cycles through each message... */
1527 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1528 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1529 while (msg_arr[a] == 0L) {
1531 if ((a == num_msgs) || (a == (-1)))
1535 RAGAIN:pagin = ((arcflag == 0)
1537 && (userflags & US_PAGINATOR)) ? 1 : 0;
1539 /* If we're doing a quote, set the screenwidth to 72 */
1541 hold_sw = screenwidth;
1545 /* If printing or archiving, set the screenwidth to 80 */
1547 hold_sw = screenwidth;
1551 /* clear parts list */
1552 free_parts(last_message_parts);
1553 last_message_parts = NULL;
1555 /* now read the message... */
1556 e = read_message(ipc, msg_arr[a], pagin, dest);
1558 /* ...and set the screenwidth back if we have to */
1559 if ((quotflag) || (arcflag)) {
1560 screenwidth = hold_sw;
1563 highest_msg_read = msg_arr[a];
1568 enable_color = hold_color;
1577 enable_color = hold_color;
1580 if (freopen(prtfile, "r", stdin) == NULL) {
1581 /* we probably should handle the error condition here */
1583 stty_ctdl(SB_RESTORE);
1584 ka_system(printcmd);
1585 stty_ctdl(SB_NO_INTR);
1592 } while ((g != f) && (g >= 0));
1593 scr_printf("Message printed.\n");
1597 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1598 && (((room_flags & QR_MAILBOX) == 0)
1599 || (rc_force_mail_prompts == 0))) {
1604 color(BRIGHT_WHITE);
1605 scr_printf("%d", num_msgs - a - 1);
1609 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1610 if (rc_url_cmd[0] && num_urls)
1611 keyopt("<U>RLview ");
1612 if (has_images > 0 && !IsEmptyStr(imagecmd))
1613 keyopt("<I>mages ");
1614 keyopt("<?>help -> ");
1617 e = (inkey() & 127);
1619 /* return key same as <N> */ if (e == 10)
1621 /* space key same as <N> */ if (e == 32)
1623 /* del/move for aides only */
1625 && ((room_flags & QR_MAILBOX) == 0)
1626 && ((room_flags2 & QR2_COLLABDEL) == 0)
1628 if ((e == 'd') || (e == 'm'))
1631 /* print only if available */
1632 if ((e == 'p') && (IsEmptyStr(printcmd)))
1634 /* can't file if not allowed */
1636 && (rc_allow_attachments == 0))
1638 /* link only if browser avail*/
1640 && (IsEmptyStr(rc_url_cmd)))
1643 && (IsEmptyStr(imagecmd) || !has_images))
1645 } while ((e != 'a') && (e != 'n') && (e != 's')
1646 && (e != 'd') && (e != 'm') && (e != 'p')
1647 && (e != 'q') && (e != 'b') && (e != 'h')
1648 && (e != 'r') && (e != 'f') && (e != '?')
1649 && (e != 'u') && (e != 'c') && (e != 'y')
1650 && (e != 'i') && (e != 'o'));
1656 scr_printf("Again");
1659 scr_printf("Delete");
1671 scr_printf("Print");
1674 scr_printf("reply Quoted");
1680 scr_printf("Header");
1683 scr_printf("Reply");
1686 scr_printf("Open attachments");
1692 scr_printf("URL's");
1695 scr_printf("mY next");
1700 scr_printf("? <help>");
1703 if (userflags & US_DISAPPEAR || e == 'i')
1704 scr_printf("\r%79s\r", "");
1708 DONE_QUOTING:switch (e) {
1710 scr_printf("Options available here:\n"
1711 " ? Help (prints this message)\n"
1712 " S Stop reading immediately\n"
1713 " A Again (repeats last message)\n"
1714 " N Next (continue with next message)\n"
1715 " Y My Next (continue with next message you authored)\n"
1716 " B Back (go back to previous message)\n");
1718 || (room_flags & QR_MAILBOX)
1719 || (room_flags2 & QR2_COLLABDEL)
1721 scr_printf(" D Delete this message\n" " M Move message to another room\n");
1723 scr_printf(" C Copy message to another room\n");
1724 if (!IsEmptyStr(printcmd))
1725 scr_printf(" P Print this message\n");
1726 scr_printf(" Q Reply to this message, quoting portions of it\n"
1727 " H Headers (display message headers only)\n");
1729 scr_printf(" R Reply to this message\n");
1730 if (rc_allow_attachments) {
1731 scr_printf(" O (Open attachments)\n");
1732 scr_printf(" F (save attachments to a File)\n");
1734 if (!IsEmptyStr(rc_url_cmd))
1735 scr_printf(" U (list URL's for display)\n");
1736 if (!IsEmptyStr(imagecmd) && has_images > 0)
1737 scr_printf(" I Image viewer\n");
1742 dest = fopen(prtfile, "w");
1744 hold_color = enable_color;
1749 dest = fopen(temp2, "w");
1751 hold_color = enable_color;
1763 newprompt("Enter target room: ", targ, ROOMNAMELEN - 1);
1764 if (!IsEmptyStr(targ)) {
1765 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0), msg_arr[a], targ, cmd);
1766 scr_printf("%s\n", cmd);
1772 if (r / 100 != 2) /* r will be init'ed, FIXME */
1773 goto RMSGREAD; /* the logic here sucks */
1777 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1778 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a], filename, &attachment, progress, cmd);
1780 scr_printf("%s\n", cmd);
1782 extract_token(filename, cmd, 2, '|', sizeof filename);
1784 * Part 1 won't have a filename; use the
1785 * subject of the message instead. IO
1787 if (IsEmptyStr(filename)) {
1788 strcpy(filename, reply_subject);
1790 if (e == 'o') { /* open attachment */
1791 mkdir(tempdir, 0700);
1792 snprintf(save_to, sizeof save_to, "%s/%04x.%s", tempdir, ++att_seq, filename);
1793 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1794 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1797 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1798 } else { /* save attachment to disk */
1799 destination_directory(save_to, filename);
1800 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1809 scr_printf("*** Delete this message? ");
1811 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1812 scr_printf("%s\n", cmd);
1820 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1823 savedpos = num_msgs;
1824 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1825 num_msgs = savedpos;
1831 image_view(ipc, msg_arr[a]);
1834 { /* hack hack hack */
1835 /* find the next message by me, stay here if we find nothing */
1838 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir) {
1839 /* This is repetitively dumb, but that's what computers are for.
1840 We have to load up messages until we find one by us */
1843 struct ctdlipcmessage *msg = NULL;
1845 /* read the header so we can get 'from=' */
1846 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1847 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1848 a = lasta; /* meesa current */
1856 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1860 } /* end for loop */
1861 } /* end read routine */
1865 * View and edit a system message
1867 void edit_system_message(CtdlIPC * ipc, char *which_message)
1871 char write_cmd[SIZ];
1873 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1874 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1875 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1876 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1881 * Loads the contents of a file into memory. Caller must free the allocated memory.
1883 char *load_message_from_file(FILE * src)
1889 fseek(src, 0, SEEK_END);
1893 dest = (char *) calloc(1, i + 1);
1900 g = fread(dest + got, 1, i - got, src);
1903 /* Interrupted system call, keep going */
1906 /* At this point we have either EOF or error */