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, or disclosure is subject to the GNU General Public License version 3.
11 #include "textclient.h"
13 #define MAXWORDBUF SIZ
14 #define NO_REPLY_TO "nobody ... xxxxxx"
17 char reply_subject[SIZ];
18 char reply_references[SIZ];
19 char reply_inreplyto[SIZ];
23 char text[MAXWORDBUF];
26 void stty_ctdl(int cmd);
27 int haschar(const char *st, int ch);
28 int file_checksum(char *filename);
29 void progress(CtdlIPC * ipc, unsigned long curr, unsigned long cmax);
31 unsigned long *msg_arr = NULL;
34 extern char room_name[];
35 extern char tempdir[];
36 extern unsigned room_flags;
37 extern unsigned room_flags2;
39 extern long highest_msg_read;
42 extern int screenwidth;
43 extern int screenheight;
44 extern long maxmsgnum;
47 extern char is_room_aide;
48 extern char fullname[];
50 extern unsigned userflags;
51 extern char sigcaught;
52 extern char printcmd[];
53 extern int rc_allow_attachments;
54 extern int rc_display_message_numbers;
55 extern int rc_force_mail_prompts;
56 extern int editor_pid;
57 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
59 char urls[MAXURLS][SIZ];
61 int has_images = 0; /* Current msg has images */
62 struct parts *last_message_parts = NULL; /* Parts from last msg */
65 void ka_sigcatch(int signum) {
67 signal(SIGALRM, ka_sigcatch);
68 CtdlIPCNoop(ipc_for_signal_handlers);
73 * server keep-alive version of wait() (needed for external editor)
75 pid_t ka_wait(int *kstatus) {
79 signal(SIGALRM, ka_sigcatch);
83 } while (errno == EINTR);
84 signal(SIGALRM, SIG_IGN);
91 * version of system() that uses ka_wait()
93 int ka_system(char *shc) {
101 perror("Cannot fork");
103 return ((pid_t) childpid);
107 execlp("/bin/sh", "sh", "-c", shc, NULL);
113 waitpid = ka_wait(&retcode);
114 } while (waitpid != childpid);
123 * add a newline to the buffer...
125 void add_newline(struct cittext *textlist) {
129 while (ptr->next != NULL)
132 while (ptr->text[strlen(ptr->text) - 1] == 32)
133 ptr->text[strlen(ptr->text) - 1] = 0;
135 ptr->next = (struct cittext *)
136 malloc(sizeof(struct cittext));
139 strcpy(ptr->text, "");
144 * add a word to the buffer...
146 void add_word(struct cittext *textlist, char *wordbuf) {
150 while (ptr->next != NULL)
153 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
154 ptr->next = (struct cittext *)
155 malloc(sizeof(struct cittext));
158 strcpy(ptr->text, "");
161 strcat(ptr->text, wordbuf);
162 strcat(ptr->text, " ");
167 * begin editing of an opened file pointed to by fp
169 void citedit(FILE * fp) {
170 int a, prev, finished, b, last_space;
172 struct cittext *textlist = NULL;
174 char wordbuf[MAXWORDBUF];
177 /* first, load the text into the buffer */
179 textlist = (struct cittext *) malloc(sizeof(struct cittext));
180 textlist->next = NULL;
181 strcpy(textlist->text, "");
185 while (a = getc(fp), a >= 0) {
187 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
188 add_word(textlist, wordbuf);
190 if ((prev == 13) || (prev == 10)) {
191 add_word(textlist, "\n");
192 add_newline(textlist);
193 add_word(textlist, "");
197 wordbuf[strlen(wordbuf) + 1] = 0;
198 wordbuf[strlen(wordbuf)] = a;
200 if (strlen(wordbuf) + 3 > screenwidth) {
201 add_word(textlist, wordbuf);
209 prev = (appending ? 13 : (-1));
220 if ((a != 32) && (prev == 13)) {
221 add_word(textlist, "\n");
225 if ((a == 32) && (prev == 13)) {
226 add_word(textlist, "\n");
227 add_newline(textlist);
231 if (!IsEmptyStr(wordbuf)) {
232 wordbuf[strlen(wordbuf) - 1] = 0;
240 wordbuf[strlen(wordbuf) - 1] = 0;
244 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
248 if (IsEmptyStr(wordbuf))
251 for (b = 0; b < strlen(wordbuf); ++b)
252 if (wordbuf[b] == 32) {
254 add_word(textlist, wordbuf);
255 strcpy(wordbuf, &wordbuf[b + 1]);
258 add_word(textlist, wordbuf);
264 wordbuf[strlen(wordbuf) + 1] = 0;
265 wordbuf[strlen(wordbuf)] = a;
267 if ((strlen(wordbuf) + 3) > screenwidth) {
269 for (b = 0; b < strlen(wordbuf); ++b)
270 if (wordbuf[b] == 32)
272 if (last_space >= 0) {
273 for (b = 0; b < strlen(wordbuf); ++b)
274 if (wordbuf[b] == 32) {
276 add_word(textlist, wordbuf);
277 strcpy(wordbuf, &wordbuf[b + 1]);
280 for (b = 0; b < strlen(wordbuf); ++b) {
285 scr_printf("\n%s", wordbuf);
288 add_word(textlist, wordbuf);
294 } while (finished == 0);
296 /* write the buffer back to disk */
298 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
299 fprintf(fp, "%s", ptr->text);
303 rv = ftruncate(fileno(fp), ftell(fp));
305 scr_printf("failed to set message buffer: %s\n", strerror(errno));
308 /* and deallocate the memory we used */
309 while (textlist != NULL) {
310 ptr = textlist->next;
318 * Free the struct parts
320 void free_parts(struct parts *p) {
321 struct parts *a_part = p;
327 a_part = a_part->next;
334 * This is a mini RFC2047 decoder.
335 * It only handles strings encoded from UTF-8 as Quoted-printable.
336 * We can do this "in place" because the converted string will always be smaller than the source string.
338 void mini_2047_decode(char *s) {
339 if (!s) { // no null strings allowed!
343 char *qstart = strstr(s, "=?UTF-8?Q?"); // Must start with this string
348 char *qend = strstr(qstart + 10, "?="); // Must end with this string
353 if (qend <= qstart) { // And there must be something in between them.
357 // The string has qualified for conversion.
359 strcpy(qend, ""); // Strip the trailer
360 strcpy(qstart, &qstart[10]); // Strip the header
362 char *r = qstart; // Pointer to where in the string we're reading
363 char *w = s; // Pointer to where in the string we're writing
365 while (*r) { // Loop through the source string
366 if (r[0] == '=') { // "=" means read a hex character
372 sscanf(ch, "%02x", &c);
377 else if (r[0] == '_') { // "_" is a space
382 else { // anything else pass through literally
388 w[0] = 0; // null terminate
393 * Read a message from the server
395 int read_message(CtdlIPC * ipc, long num, /* message number */
396 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
397 FILE * dest /* Destination file, NULL for screen */
404 struct ctdlipcmessage *message = NULL;
405 int r; /* IPC response code */
406 char *converted_text = NULL;
413 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 ");
443 if (pagin == 1 && !dest) {
447 /* View headers only */
449 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
450 message->nhdr ? "yes" : "no", message->author, message->type, message->msgid);
451 if (!IsEmptyStr(message->subject)) {
452 scr_printf("subj=%s\n", message->subject);
454 if (!IsEmptyStr(message->email)) {
455 scr_printf("rfca=%s\n", message->email);
457 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);
485 scr_printf("#%s", message->msgid);
490 if (nhdr == 1 && !is_room_aide) {
492 fprintf(dest, " ****");
500 localtime_r(&message->time, &thetime);
501 strftime(now, sizeof now, "%F %R", &thetime);
503 fprintf(dest, "%s from %s ", now, message->author);
504 if (!message->is_local) {
505 fprintf(dest, "<%s> ", message->email);
510 scr_printf("%s ", now);
514 scr_printf("%s ", message->author);
515 if (!message->is_local) {
519 scr_printf("%s", message->email);
524 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
526 fprintf(dest, "in %s> ", message->room);
531 color(BRIGHT_MAGENTA);
532 scr_printf("%s> ", message->room);
535 if (!IsEmptyStr(message->recipient)) {
537 fprintf(dest, "to %s ", message->recipient);
543 scr_printf("%s ", message->recipient);
555 // Set the reply-to address to an Internet e-mail address if possible
556 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
557 if (!IsEmptyStr(message->author)) {
558 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
561 strncpy(reply_to, message->email, sizeof reply_to);
565 // But if we can't do that, set it to a Citadel address.
566 if (!strcmp(reply_to, NO_REPLY_TO)) {
567 strncpy(reply_to, message->author, sizeof(reply_to));
570 if (message->msgid != NULL) {
571 strncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
574 if (message->references != NULL) {
575 if (!IsEmptyStr(message->references)) {
576 strncpy(reply_references, message->references, sizeof reply_references);
580 if (message->subject != NULL) {
581 strncpy(reply_subject, message->subject, sizeof reply_subject);
582 if (!IsEmptyStr(message->subject)) {
584 fprintf(dest, "Subject: %s\n", message->subject);
588 scr_printf("Subject: ");
590 mini_2047_decode(message->subject);
591 scr_printf("%s\n", message->subject);
597 if (pagin == 1 && !dest) {
601 /******* end of header output, start of message text output *******/
604 * Convert HTML to plain text, formatting for the actual width
605 * of the client screen.
607 if (!strcasecmp(message->content_type, "text/html")) {
608 converted_text = html_to_ascii(message->text, 0, screenwidth, (enable_color ? 1 : 0));
609 if (converted_text != NULL) {
611 message->text = converted_text;
616 /* Text/plain is a different type */
617 if (!strcasecmp(message->content_type, "text/plain")) {
621 /* Render text/x-markdown as plain text */
622 if (!strcasecmp(message->content_type, "text/x-markdown")) {
627 static char *urlprefixes[] = {
633 num_urls = 0; /* Start with a clean slate */
634 for (p = 0; p < (sizeof urlprefixes / sizeof(char *)); ++p) {
635 searchptr = message->text;
636 while ((searchptr != NULL) && (num_urls < MAXURLS)) {
637 searchptr = strstr(searchptr, urlprefixes[p]);
638 if (searchptr != NULL) {
639 strncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
640 for (i = 0; i < strlen(urls[num_urls]); i++) {
641 ch = urls[num_urls][i];
642 if (ch == '>' || ch == '\"' || ch == ')' || ch == ' ' || ch == '\n') {
643 urls[num_urls][i] = 0;
656 if (format_type == 0) {
657 fr = fmout(screenwidth, NULL, message->text, dest, 1);
660 /* renderer for text/plain */
662 lineptr = message->text;
665 nextline = strchr(lineptr, '\n');
666 if (nextline != NULL) {
673 if (sigcaught == 0) {
674 linelen = strlen(lineptr);
675 if (linelen && (lineptr[linelen - 1] == '\r')) {
676 lineptr[--linelen] = 0;
679 fprintf(dest, "%s\n", lineptr);
682 scr_printf("%s\n", lineptr);
686 final_line_is_blank = 1;
688 final_line_is_blank = 0;
693 if (!final_line_is_blank) {
703 /* Enumerate any attachments */
704 if ((pagin == 1) && (message->attachments)) {
707 for (ptr = message->attachments; ptr; ptr = ptr->next) {
708 if ((!strcasecmp(ptr->disposition, "attachment"))
709 || (!strcasecmp(ptr->disposition, "inline"))
710 || (!strcasecmp(ptr->disposition, ""))
712 if ((strcasecmp(ptr->number, message->mime_chosen))
713 && (!IsEmptyStr(ptr->mimetype))
717 color(BRIGHT_MAGENTA);
718 scr_printf("%s", ptr->number);
722 scr_printf("%s", ptr->filename);
724 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
725 if (!strncmp(ptr->mimetype, "image/", 6)) {
733 /* Save the attachments info for later */
734 last_message_parts = message->attachments;
740 if (pagin == 1 && !dest)
748 * replace string function for the built-in editor
750 void replace_string(char *filename, long int startpos) {
758 int substitutions = 0;
762 newprompt("Enter text to be replaced: ", srch_str, (sizeof(srch_str) - 1));
763 if (IsEmptyStr(srch_str)) {
767 newprompt("Enter text to replace it with: ", rplc_str, (sizeof(rplc_str) - 1));
769 fp = fopen(filename, "r+");
775 fseek(fp, startpos, 0);
777 while (a = getc(fp), a > 0) {
779 buf[strlen(buf) + 1] = 0;
780 buf[strlen(buf)] = a;
781 if (strlen(buf) >= strlen(srch_str)) {
782 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
783 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
784 strcpy(ptr, rplc_str);
788 if (strlen(buf) > 384) {
791 rv = fwrite((char *) buf, 128, 1, fp);
793 scr_printf("failed to replace string: %s\n", strerror(errno));
796 strcpy(buf, &buf[128]);
802 if (!IsEmptyStr(buf)) {
803 rv = fwrite((char *) buf, strlen(buf), 1, fp);
807 rv = truncate(filename, wpos);
808 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
812 // Function to begin composing a new message
813 int client_make_message(CtdlIPC * ipc, char *filename, // temporary file name
814 char *recipient, // NULL if it's not mail
815 int is_anonymous, int format_type, int mode, char *subject, // buffer to store subject line
816 int subject_required) {
824 if ((mode == 2) && (IsEmptyStr(editor_path))) {
825 scr_printf("*** No editor available; using built-in editor.\n");
830 time_t now = time(NULL);
831 localtime_r(&now, &thetime);
832 strftime(datestr, sizeof datestr, "%F %R", &thetime);
835 if (room_flags & QR_ANONONLY && !recipient) {
836 snprintf(header, sizeof header, " ****");
839 snprintf(header, sizeof header, " %s from %s", datestr, (is_anonymous ? "[anonymous]" : fullname));
840 if (!IsEmptyStr(recipient)) {
841 size_t tmp = strlen(header);
842 snprintf(&header[tmp], sizeof header - tmp, " to %s", recipient);
845 scr_printf("%s\n", header);
847 if (!IsEmptyStr(subject)) {
848 scr_printf("Subject: %s\n", subject);
851 if ((subject_required) && (IsEmptyStr(subject))) {
852 newprompt("Subject: ", subject, 70);
856 scr_printf("(Press ctrl-d when finished)\n");
860 fp = fopen(filename, "r");
862 fmout(screenwidth, fp, NULL, NULL, 0);
865 scr_printf("failed to get stream position %s\n", strerror(errno));
869 fp = fopen(filename, "w");
871 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno));
881 fp = fopen(filename, "r+");
883 scr_printf("*** Error opening temp file!\n %s: %s\n", filename, strerror(errno)
892 fp = fopen(filename, "a");
894 scr_printf("*** Error opening temp file!\n" " %s: %s\n", filename, strerror(errno));
914 default: /* allow 2+ modes */
915 e_ex_code = 1; /* start with a failed exit code */
916 stty_ctdl(SB_RESTORE);
918 cksum = file_checksum(filename);
919 if (editor_pid == 0) {
922 chmod(filename, 0600);
923 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
925 execlp(editor_path, editor_path, filename, NULL);
931 b = ka_wait(&e_ex_code);
932 } while ((b != editor_pid) && (b >= 0));
938 MECR:if (mode >= 2) {
939 if (file_checksum(filename) == cksum) {
940 scr_printf("*** Aborted message.\n");
943 if (e_ex_code == 0) {
949 b = keymenu("Entry command (? for options)",
951 "<C>ontinue|" "<S>ave message|" "<P>rint formatted|" "add s<U>bject|" "<R>eplace string|" "<H>old message");
960 scr_printf(" %s from %s", datestr, fullname);
961 if (!IsEmptyStr(recipient)) {
962 scr_printf(" to %s", recipient);
966 if (!IsEmptyStr(subject)) {
967 scr_printf("Subject: %s\n", subject);
969 fp = fopen(filename, "r");
971 fmout(screenwidth, fp, NULL, NULL, 0);
974 scr_printf("failed to get stream position %s\n", strerror(errno));
980 replace_string(filename, 0L);
987 if (subject != NULL) {
988 newprompt("Subject: ", subject, 70);
995 MEABT:scr_printf("Are you sure? ");
999 MEABT2:unlink(filename);
1005 * Make sure there's room in msg_arr[] for at least one more.
1007 void check_msg_arr_size(void) {
1008 if ((num_msgs + 1) > msg_arr_size) {
1009 msg_arr_size += 512;
1010 msg_arr = realloc(msg_arr, ((sizeof(long)) * msg_arr_size));
1016 * break_big_lines() - break up lines that are >1024 characters
1017 * otherwise the server will truncate
1019 void break_big_lines(char *msg) {
1028 while (strlen(ptr) > 1000) {
1029 break_here = strchr(&ptr[900], ' ');
1030 if ((break_here == NULL) || (break_here > &ptr[999])) {
1031 break_here = &ptr[999];
1040 * entmsg() - edit and create a message
1041 * returns 0 if message was saved
1043 int entmsg(CtdlIPC * ipc, int is_reply, /* nonzero if this was a <R>eply command */
1045 int masquerade /* prompt for a non-default display name? */
1054 struct ctdlipcmessage message;
1055 unsigned long *msgarr = NULL;
1056 int r; /* IPC response code */
1057 int subject_required = 0;
1060 * First, check to see if we have permission to enter a message in
1061 * this room. The server will return an error code if we can't.
1063 if (entmsg_ok == ENTMSG_OK_YES) {
1064 /* no problem, go right ahead */
1066 else if (entmsg_ok == ENTMSG_OK_BLOG) {
1068 scr_printf("WARNING: this is a BLOG room.\n");
1069 scr_printf("The '<E>nter Message' command will create a BLOG POST.\n");
1070 scr_printf("If you want to leave a comment or reply to a comment, use the '<R>eply' command.\n");
1071 scr_printf("Do you really want to create a new blog post? ");
1078 scr_printf("You may not enter messages in this type of room.\n");
1089 strcpy(subject, "");
1091 strcpy(message.recipient, "");
1092 strcpy(message.author, "");
1093 strcpy(message.subject, "");
1094 strcpy(message.references, "");
1095 message.text = ""; /* point to "", changes later */
1096 message.anonymous = 0;
1097 message.type = mode;
1100 newprompt("Display name for this message: ", message.author, 40);
1105 if (!IsEmptyStr(reply_subject)) {
1106 if (!strncasecmp(reply_subject, "Re: ", 3)) {
1107 strcpy(message.subject, reply_subject);
1110 snprintf(message.subject, sizeof message.subject, "Re: %s", reply_subject);
1114 /* Trim down excessively long lists of thread references. We eliminate the
1115 * second one in the list so that the thread root remains intact.
1117 int rrtok = num_tokens(reply_references, '|');
1118 int rrlen = strlen(reply_references);
1119 if (((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10)) {
1120 remove_token(reply_references, 1, '|');
1123 snprintf(message.references, sizeof message.references, "%s%s%s",
1124 reply_references, (IsEmptyStr(reply_references) ? "" : "|"), reply_inreplyto);
1127 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1129 if (r / 100 != 2 && r / 10 != 57) {
1130 scr_printf("%s\n", buf);
1134 /* Error code 570 is special. It means that we CAN enter a message
1135 * in this room, but a recipient needs to be specified.
1142 /* If the user is a dumbass, tell them how to type. */
1143 if ((userflags & US_EXPERT) == 0) {
1144 scr_printf("Entering message. Word wrap will give you soft linebreaks. Pressing the\n");
1145 scr_printf("'enter' key will give you a hard linebreak and an indent. Press 'enter' twice\n");
1146 scr_printf("when finished.\n");
1149 /* Handle the selection of a recipient, if necessary. */
1151 if (need_recp == 1) {
1152 if (axlevel >= AxProbU) {
1154 strcpy(buf, reply_to);
1157 newprompt("Enter recipient: ", buf, SIZ - 100);
1158 if (IsEmptyStr(buf)) {
1164 strcpy(buf, "sysop");
1167 strcpy(message.recipient, buf);
1169 if (room_flags & QR_ANONOPT) {
1170 scr_printf("Anonymous (Y/N)? ");
1172 message.anonymous = 1;
1175 /* If it's mail, we've got to check the validity of the recipient... */
1176 if (!IsEmptyStr(message.recipient)) {
1177 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1179 scr_printf("%s\n", buf);
1184 /* Learn the number of the newest message in in the room, so we can
1185 * tell upon saving whether someone else has posted too.
1188 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1190 scr_printf("%s\n", buf);
1193 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1196 /* Now compose the message... */
1197 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject, subject_required) != 0) {
1203 /* Reopen the temp file that was created, so we can send it */
1204 fp = fopen(temp, "r");
1206 if (!fp || !(message.text = load_message_from_file(fp))) {
1207 scr_printf("*** Internal error while trying to save message!\n" "%s: %s\n", temp, strerror(errno));
1215 /* Break lines that are >1024 characters, otherwise the server
1216 * will truncate them.
1218 break_big_lines(message.text);
1220 /* Transmit message to the server */
1221 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1223 scr_printf("%s\n", buf);
1227 /* Yes, unlink it now, so it doesn't stick around if we crash */
1231 highmsg = msgarr[num_msgs - 1];
1236 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1238 scr_printf("%s\n", buf);
1241 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++);
1244 /* get new highest message number in room to set lrp for goto... */
1245 maxmsgnum = msgarr[num_msgs - 1];
1247 /* now see if anyone else has posted in here */
1249 for (a = 0; a < num_msgs; ++a) {
1250 if (msgarr[a] > highmsg) {
1259 /* In the Mail> room, this algorithm always counts one message
1260 * higher than in public rooms, so we decrement it by one.
1267 scr_printf("*** 1 additional message has been entered in this room by another user.\n");
1270 scr_printf("*** %d additional messages have been entered in this room by other users.\n", b);
1279 * Do editing on a quoted file
1281 void process_quote(void) {
1282 FILE *qfile, *tfile;
1284 int line, qstart, qend;
1286 // Unlink the second temp file as soon as it's opened, so it'll get
1287 // deleted even if the program dies
1288 qfile = fopen(temp2, "r");
1291 /* Display the quotable text with line numbers added */
1293 if (fgets(buf, 128, qfile) == NULL) {
1294 /* we're skipping a line here */
1296 while (fgets(buf, 128, qfile) != NULL) {
1297 scr_printf("%3d %s", ++line, buf);
1300 qstart = intprompt("Begin quoting at", 1, 1, line);
1301 qend = intprompt(" End quoting at", line, qstart, line);
1305 if (fgets(buf, 128, qfile) == NULL) {
1306 /* we're skipping a line here */
1308 tfile = fopen(temp, "w");
1309 while (fgets(buf, 128, qfile) != NULL) {
1310 if ((++line >= qstart) && (line <= qend)) {
1311 fprintf(tfile, " >%s", buf);
1314 fprintf(tfile, " \n");
1322 * List the URLs which were embedded in the previous message
1324 void list_urls(CtdlIPC * ipc) {
1329 if (num_urls == 0) {
1330 scr_printf("There were no URLs in the previous message.\n\n");
1334 for (i = 0; i < num_urls; ++i) {
1335 scr_printf("%3d %s\n", i + 1, urls[i]);
1338 if ((i = num_urls) != 1) {
1339 i = intprompt("Display which one", 1, 1, num_urls);
1342 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1349 * Run image viewer in background
1351 int do_image_view(const char *filename) {
1355 snprintf(cmd, sizeof cmd, imagecmd, filename);
1362 if (childpid == 0) {
1364 pid_t grandchildpid;
1366 grandchildpid = fork();
1367 if (grandchildpid < 0) {
1368 return grandchildpid;
1371 if (grandchildpid == 0) {
1376 nullfd = open("/dev/null", O_WRONLY);
1383 retcode = system(cmd);
1393 if (grandchildpid > 0) {
1401 waitpid(childpid, &retcode, 0);
1410 * View an image attached to a message
1412 void image_view(CtdlIPC * ipc, unsigned long msg) {
1413 struct parts *ptr = last_message_parts;
1417 /* Run through available parts */
1418 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1419 if ((!strcasecmp(ptr->disposition, "attachment")
1420 || !strcasecmp(ptr->disposition, "inline"))
1421 && !strncmp(ptr->mimetype, "image/", 6)) {
1424 strcpy(part, ptr->number);
1431 strprompt("View which part (0 when done)", part, SIZ - 1);
1433 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1434 if ((!strcasecmp(ptr->disposition, "attachment")
1435 || !strcasecmp(ptr->disposition, "inline"))
1436 && !strncmp(ptr->mimetype, "image/", 6)
1437 && !strcasecmp(ptr->number, part)) {
1440 void *file = NULL; /* The downloaded file */
1445 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1447 scr_printf("%s\n", buf);
1452 len = (size_t) extract_long(buf, 0);
1453 progress(ipc, len, len);
1455 CtdlMakeTempFileName(tmp, sizeof tmp);
1456 strcat(tmp, ptr->filename);
1457 save_buffer(file, len, tmp);
1471 * Read the messages in the current room
1473 void readmsgs(CtdlIPC * ipc, enum MessageList c, // see listing in citadel_ipc.h
1474 enum MessageDirection rdir, // 1=Forward (-1)=Reverse
1475 int q // Number of msgs to read (if c==3)
1477 int a, e, f, g, start;
1483 char prtfile[PATH_MAX];
1486 char targ[ROOMNAMELEN];
1487 char filename[PATH_MAX];
1488 char save_to[PATH_MAX];
1489 void *attachment = NULL; /* Downloaded attachment */
1490 FILE *dest = NULL; /* Alternate destination other than screen */
1491 int r; /* IPC response code */
1492 static int att_seq = 0; /* Attachment download sequence number */
1493 int rv = 0; /* silence the stupid warn_unused_result warnings */
1495 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1501 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1503 scr_printf("%s\n", cmd);
1506 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++);
1509 if (num_msgs == 0) {
1510 if (c == LastMessages) {
1513 scr_printf("*** There are no ");
1514 if (c == NewMessages)
1516 if (c == OldMessages)
1518 scr_printf("messages in this room.\n");
1522 /* this loop cycles through each message... */
1523 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1524 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1525 while (msg_arr[a] == 0L) {
1527 if ((a == num_msgs) || (a == (-1)))
1531 RAGAIN:pagin = ((arcflag == 0)
1533 && (userflags & US_PAGINATOR)) ? 1 : 0;
1535 /* If we're doing a quote, set the screenwidth to 72 */
1537 hold_sw = screenwidth;
1541 /* If printing or archiving, set the screenwidth to 80 */
1543 hold_sw = screenwidth;
1547 /* clear parts list */
1548 free_parts(last_message_parts);
1549 last_message_parts = NULL;
1551 /* now read the message... */
1552 e = read_message(ipc, msg_arr[a], pagin, dest);
1554 /* ...and set the screenwidth back if we have to */
1555 if ((quotflag) || (arcflag)) {
1556 screenwidth = hold_sw;
1559 highest_msg_read = msg_arr[a];
1564 enable_color = hold_color;
1573 enable_color = hold_color;
1576 if (freopen(prtfile, "r", stdin) == NULL) {
1577 /* we probably should handle the error condition here */
1579 stty_ctdl(SB_RESTORE);
1580 ka_system(printcmd);
1581 stty_ctdl(SB_NO_INTR);
1588 } while ((g != f) && (g >= 0));
1589 scr_printf("Message printed.\n");
1593 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1594 && (((room_flags & QR_MAILBOX) == 0)
1595 || (rc_force_mail_prompts == 0))) {
1601 color(BRIGHT_WHITE);
1602 scr_printf("%d", num_msgs - a - 1);
1606 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1607 if (rc_url_cmd[0] && num_urls)
1608 keyopt("<U>RLview ");
1609 if (has_images > 0 && !IsEmptyStr(imagecmd))
1610 keyopt("<I>mages ");
1611 keyopt("<?>help -> ");
1614 e = (inkey() & 127);
1617 /* return key same as <N> */ if (e == 10)
1620 /* 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'))
1632 /* print only if available */
1633 if ((e == 'p') && (IsEmptyStr(printcmd)))
1636 /* can't file if not allowed */
1638 && (rc_allow_attachments == 0))
1641 /* link only if browser avail*/
1643 && (IsEmptyStr(rc_url_cmd)))
1646 && (IsEmptyStr(imagecmd) || !has_images))
1648 } while ((e != 'a') && (e != 'n') && (e != 's')
1649 && (e != 'd') && (e != 'm') && (e != 'p')
1650 && (e != 'q') && (e != 'b') && (e != 'h')
1651 && (e != 'r') && (e != 'f') && (e != '?')
1652 && (e != 'u') && (e != 'c') && (e != 'y')
1653 && (e != 'i') && (e != 'o'));
1659 scr_printf("Again");
1662 scr_printf("Delete");
1674 scr_printf("Print");
1677 scr_printf("reply Quoted");
1683 scr_printf("Header");
1686 scr_printf("Reply");
1689 scr_printf("Open attachments");
1695 scr_printf("URL's");
1698 scr_printf("mY next");
1703 scr_printf("? <help>");
1706 if (userflags & US_DISAPPEAR || e == 'i')
1707 scr_printf("\r%79s\r", "");
1711 DONE_QUOTING:switch (e) {
1713 scr_printf("Options available here:\n"
1714 " ? Help (prints this message)\n"
1715 " S Stop reading immediately\n"
1716 " A Again (repeats last message)\n"
1717 " N Next (continue with next message)\n"
1718 " Y My Next (continue with next message you authored)\n"
1719 " B Back (go back to previous message)\n");
1720 if ((is_room_aide) || (room_flags & QR_MAILBOX) || (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);
1773 if (r / 100 != 2) /* r will be init'ed, FIXME */
1774 goto RMSGREAD; /* the logic here sucks */
1778 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1779 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a], filename, &attachment, progress, cmd);
1781 scr_printf("%s\n", cmd);
1784 extract_token(filename, cmd, 2, '|', sizeof filename);
1786 * Part 1 won't have a filename; use the
1787 * subject of the message instead. IO
1789 if (IsEmptyStr(filename)) {
1790 strcpy(filename, reply_subject);
1792 if (e == 'o') { /* open attachment */
1793 mkdir(tempdir, 0700);
1794 snprintf(save_to, sizeof save_to, "%s/%04x.%s", tempdir, ++att_seq, filename);
1795 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1796 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1799 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1801 else { /* save attachment to disk */
1802 destination_directory(save_to, filename);
1803 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1812 scr_printf("*** Delete this message? ");
1814 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1815 scr_printf("%s\n", cmd);
1824 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1827 savedpos = num_msgs;
1828 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1829 num_msgs = savedpos;
1835 image_view(ipc, msg_arr[a]);
1838 { /* hack hack hack */
1839 /* find the next message by me, stay here if we find nothing */
1842 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir) {
1843 /* This is repetitively dumb, but that's what computers are for.
1844 We have to load up messages until we find one by us */
1847 struct ctdlipcmessage *msg = NULL;
1849 /* read the header so we can get 'from=' */
1850 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1851 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1852 a = lasta; /* meesa current */
1860 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1864 } /* end for loop */
1865 } /* end read routine */
1869 * View and edit a system message
1871 void edit_system_message(CtdlIPC * ipc, char *which_message) {
1874 char write_cmd[SIZ];
1876 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1877 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1878 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1879 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1884 * Loads the contents of a file into memory. Caller must free the allocated memory.
1886 char *load_message_from_file(FILE * src) {
1891 fseek(src, 0, SEEK_END);
1895 dest = (char *) calloc(1, i + 1);
1902 g = fread(dest + got, 1, i - got, src);
1905 /* Interrupted system call, keep going */
1908 /* At this point we have either EOF or error */