2 * Text client functions for reading and writing of messages
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <sys/types.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
45 #include <libcitadel.h>
46 ///#include "citadel.h"
47 #include "citadel_ipc.h"
48 #include "citadel_decls.h"
51 #include "tuiconfig.h"
53 //#ifndef HAVE_SNPRINTF
54 ///#include "snprintf.h"
58 #define MAXWORDBUF SIZ
59 #define NO_REPLY_TO "nobody ... xxxxxx"
62 char reply_subject[SIZ];
63 char reply_references[SIZ];
64 char reply_inreplyto[SIZ];
68 char text[MAXWORDBUF];
71 void stty_ctdl(int cmd);
72 int haschar(const char *st, int ch);
73 int file_checksum(char *filename);
74 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
76 unsigned long *msg_arr = NULL;
79 extern char room_name[];
80 extern char tempdir[];
81 extern unsigned room_flags;
82 extern unsigned room_flags2;
84 extern long highest_msg_read;
87 extern int screenwidth;
88 extern int screenheight;
89 extern long maxmsgnum;
92 extern char is_room_aide;
93 extern char fullname[];
95 extern unsigned userflags;
96 extern char sigcaught;
97 extern char printcmd[];
98 extern int rc_allow_attachments;
99 extern int rc_display_message_numbers;
100 extern int rc_force_mail_prompts;
101 extern int editor_pid;
102 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
104 char urls[MAXURLS][SIZ];
106 int has_images = 0; /* Current msg has images */
107 struct parts *last_message_parts = NULL; /* Parts from last msg */
111 void ka_sigcatch(int signum)
114 signal(SIGALRM, ka_sigcatch);
115 CtdlIPCNoop(ipc_for_signal_handlers);
120 * server keep-alive version of wait() (needed for external editor)
122 pid_t ka_wait(int *kstatus)
127 signal(SIGALRM, ka_sigcatch);
131 } while (errno == EINTR);
132 signal(SIGALRM, SIG_IGN);
139 * version of system() that uses ka_wait()
141 int ka_system(char *shc)
150 perror("Cannot fork");
152 return ((pid_t) childpid);
156 execlp("/bin/sh", "sh", "-c", shc, NULL);
162 waitpid = ka_wait(&retcode);
163 } while (waitpid != childpid);
173 * add a newline to the buffer...
175 void add_newline(struct cittext *textlist)
180 while (ptr->next != NULL)
183 while (ptr->text[strlen(ptr->text) - 1] == 32)
184 ptr->text[strlen(ptr->text) - 1] = 0;
185 /* strcat(ptr->text,"\n"); */
187 ptr->next = (struct cittext *)
188 malloc(sizeof(struct cittext));
191 strcpy(ptr->text, "");
196 * add a word to the buffer...
198 void add_word(struct cittext *textlist, char *wordbuf)
203 while (ptr->next != NULL)
206 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
207 ptr->next = (struct cittext *)
208 malloc(sizeof(struct cittext));
211 strcpy(ptr->text, "");
214 strcat(ptr->text, wordbuf);
215 strcat(ptr->text, " ");
220 * begin editing of an opened file pointed to by fp
222 void citedit(FILE *fp)
224 int a, prev, finished, b, last_space;
226 struct cittext *textlist = NULL;
228 char wordbuf[MAXWORDBUF];
231 /* first, load the text into the buffer */
233 textlist = (struct cittext *) malloc(sizeof(struct cittext));
234 textlist->next = NULL;
235 strcpy(textlist->text, "");
239 while (a = getc(fp), a >= 0) {
241 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
242 add_word(textlist, wordbuf);
244 if ((prev == 13) || (prev == 10)) {
245 add_word(textlist, "\n");
246 add_newline(textlist);
247 add_word(textlist, "");
250 wordbuf[strlen(wordbuf) + 1] = 0;
251 wordbuf[strlen(wordbuf)] = a;
253 if (strlen(wordbuf) + 3 > screenwidth) {
254 add_word(textlist, wordbuf);
262 prev = (appending ? 13 : (-1));
274 if ((a != 32) && (prev == 13)) {
275 add_word(textlist, "\n");
279 if ((a == 32) && (prev == 13)) {
280 add_word(textlist, "\n");
281 add_newline(textlist);
285 if (!IsEmptyStr(wordbuf)) {
286 wordbuf[strlen(wordbuf) - 1] = 0;
291 } else if (a == 23) {
293 wordbuf[strlen(wordbuf) - 1] = 0;
297 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
298 } else if (a == 13) {
300 if (IsEmptyStr(wordbuf))
303 for (b = 0; b < strlen(wordbuf); ++b)
304 if (wordbuf[b] == 32) {
312 add_word(textlist, wordbuf);
317 wordbuf[strlen(wordbuf) + 1] = 0;
318 wordbuf[strlen(wordbuf)] = a;
320 if ((strlen(wordbuf) + 3) > screenwidth) {
322 for (b = 0; b < strlen(wordbuf); ++b)
323 if (wordbuf[b] == 32)
325 if (last_space >= 0) {
326 for (b = 0; b < strlen(wordbuf); ++b)
327 if (wordbuf[b] == 32) {
335 for (b = 0; b < strlen(wordbuf); ++b) {
340 scr_printf("\n%s", wordbuf);
342 add_word(textlist, wordbuf);
348 } while (finished == 0);
351 /* write the buffer back to disk */
353 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
354 fprintf(fp, "%s", ptr->text);
358 rv = ftruncate(fileno(fp), ftell(fp));
360 scr_printf("failed to set message buffer: %s\n", strerror(errno));
363 /* and deallocate the memory we used */
364 while (textlist != NULL) {
365 ptr = textlist->next;
373 * Free the struct parts
375 void free_parts(struct parts *p)
377 struct parts *a_part = p;
383 a_part = a_part->next;
390 * This is a mini RFC2047 decoder.
391 * It only handles strings encoded from UTF-8 as Quoted-printable.
393 void mini_2047_decode(char *s) {
396 char *qstart = strstr(s, "=?UTF-8?Q?");
399 char *qend = strstr(s, "?=");
402 if (qend <= qstart) return;
404 strcpy(qstart, &qstart[10]);
417 sscanf(ch, "%02x", &c);
419 strcpy(&p[1], &p[3]);
430 strcpy(qend, &qend[2]);
434 * Read a message from the server
436 int read_message(CtdlIPC *ipc,
437 long num, /* message number */
438 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
439 FILE *dest) /* Destination file, NULL for screen */
446 struct ctdlipcmessage *message = NULL;
447 int r; /* IPC response code */
448 char *converted_text = NULL;
455 int final_line_is_blank = 0;
462 strcpy(reply_to, NO_REPLY_TO);
463 strcpy(reply_subject, "");
464 strcpy(reply_references, "");
465 strcpy(reply_inreplyto, "");
467 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
469 scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
472 free_parts(message->attachments);
478 fprintf(dest, "\n ");
484 if (pagin == 1 && !dest) {
488 /* View headers only */
490 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
491 message->nhdr ? "yes" : "no",
492 message->author, message->type,
494 if (!IsEmptyStr(message->subject)) {
495 scr_printf("subj=%s\n", message->subject);
497 if (!IsEmptyStr(message->email)) {
498 scr_printf("rfca=%s\n", message->email);
500 scr_printf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
501 message->hnod, message->room,
503 asctime(localtime(&message->time)));
504 if (!IsEmptyStr(message->recipient)) {
505 scr_printf("rcpt=%s\n", message->recipient);
507 if (message->attachments) {
510 for (ptr = message->attachments; ptr; ptr = ptr->next) {
511 scr_printf("part=%s|%s|%s|%s|%s|%ld\n",
512 ptr->name, ptr->filename, ptr->number,
513 ptr->disposition, ptr->mimetype,
521 free_parts(message->attachments);
526 if (rc_display_message_numbers) {
528 fprintf(dest, "[#%s] ", message->msgid);
533 scr_printf("#%s", message->msgid);
538 if (nhdr == 1 && !is_room_aide) {
540 fprintf(dest, " ****");
545 fmt_date(now, sizeof now, message->time, 0);
547 fprintf(dest, "%s from %s ", now, message->author);
548 if (!IsEmptyStr(message->email)) {
549 fprintf(dest, "<%s> ", message->email);
553 scr_printf("%s ", now);
557 scr_printf("%s ", message->author);
558 if (!IsEmptyStr(message->email)) {
562 scr_printf("%s", message->email);
567 if (!IsEmptyStr(message->node)) {
568 if ((room_flags & QR_NETWORK)
569 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
570 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
571 if (IsEmptyStr(message->email)) {
573 fprintf(dest, "@%s ", message->node);
577 color(BRIGHT_YELLOW);
578 scr_printf("%s ", message->node);
583 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
584 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
586 fprintf(dest, "(%s) ", message->hnod);
591 scr_printf("%s", message->hnod);
596 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
598 fprintf(dest, "in %s> ", message->room);
602 color(BRIGHT_MAGENTA);
603 scr_printf("%s> ", message->room);
606 if (!IsEmptyStr(message->recipient)) {
608 fprintf(dest, "to %s ", message->recipient);
613 scr_printf("%s ", message->recipient);
624 /* Set the reply-to address to an Internet e-mail address if possible
626 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
627 if (!IsEmptyStr(message->author)) {
628 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
631 safestrncpy(reply_to, message->email, sizeof reply_to);
635 /* But if we can't do that, set it to a Citadel address.
637 if (!strcmp(reply_to, NO_REPLY_TO)) {
638 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
639 message->author, message->node);
642 if (message->msgid != NULL) {
643 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
646 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
647 safestrncpy(reply_references, message->references, sizeof reply_references);
650 if (message->subject != NULL) {
651 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
652 if (!IsEmptyStr(message->subject)) {
654 fprintf(dest, "Subject: %s\n", message->subject);
657 scr_printf("Subject: ");
659 mini_2047_decode(message->subject);
660 scr_printf("%s\n", message->subject);
665 if (pagin == 1 && !dest) {
669 /******* end of header output, start of message text output *******/
672 * Convert HTML to plain text, formatting for the actual width
673 * of the client screen.
675 if (!strcasecmp(message->content_type, "text/html")) {
676 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
677 if (converted_text != NULL) {
679 message->text = converted_text;
684 /* Text/plain is a different type */
685 if (!strcasecmp(message->content_type, "text/plain")) {
690 num_urls = 0; /* Start with a clean slate */
691 searchptr = message->text;
692 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
693 searchptr = strstr(searchptr, "http://");
694 if (searchptr != NULL) {
695 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
696 for (i = 0; i < strlen(urls[num_urls]); i++) {
697 ch = urls[num_urls][i];
698 if (ch == '>' || ch == '\"' || ch == ')' ||
699 ch == ' ' || ch == '\n') {
700 urls[num_urls][i] = 0;
712 if (format_type == 0) {
713 fr = fmout(screenwidth, NULL, message->text, dest, 1);
715 /* renderer for text/plain */
717 lineptr = message->text;
720 nextline = strchr(lineptr, '\n');
721 if (nextline != NULL) {
724 if (*nextline == 0) nextline = NULL;
727 if (sigcaught == 0) {
728 linelen = strlen(lineptr);
729 if (linelen && (lineptr[linelen-1] == '\r')) {
730 lineptr[--linelen] = 0;
733 fprintf(dest, "%s\n", lineptr);
735 scr_printf("%s\n", lineptr);
738 if (lineptr[0] == 0) final_line_is_blank = 1;
739 else final_line_is_blank = 0;
744 if (!final_line_is_blank) {
754 /* Enumerate any attachments */
755 if ( (pagin == 1) && (message->attachments) ) {
758 for (ptr = message->attachments; ptr; ptr = ptr->next) {
759 if ( (!strcasecmp(ptr->disposition, "attachment"))
760 || (!strcasecmp(ptr->disposition, "inline"))
761 || (!strcasecmp(ptr->disposition, ""))
763 if ( (strcasecmp(ptr->number, message->mime_chosen))
764 && (!IsEmptyStr(ptr->mimetype))
768 color(BRIGHT_MAGENTA);
769 scr_printf("%s", ptr->number);
773 scr_printf("%s", ptr->filename);
775 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
776 if (!strncmp(ptr->mimetype, "image/", 6)) {
784 /* Save the attachments info for later */
785 last_message_parts = message->attachments;
791 if (pagin == 1 && !dest)
798 * replace string function for the built-in editor
800 void replace_string(char *filename, long int startpos)
809 int substitutions = 0;
813 newprompt("Enter text to be replaced: ", srch_str, (sizeof(srch_str)-1) );
814 if (IsEmptyStr(srch_str)) {
818 newprompt("Enter text to replace it with: ", rplc_str, (sizeof(rplc_str)-1) );
820 fp = fopen(filename, "r+");
826 fseek(fp, startpos, 0);
828 while (a = getc(fp), a > 0) {
830 buf[strlen(buf) + 1] = 0;
831 buf[strlen(buf)] = a;
832 if (strlen(buf) >= strlen(srch_str)) {
833 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
834 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
835 strcpy(ptr, rplc_str);
839 if (strlen(buf) > 384) {
842 rv = fwrite((char *) buf, 128, 1, fp);
844 scr_printf("failed to replace string: %s\n", strerror(errno));
847 strcpy(buf, &buf[128]);
853 if (!IsEmptyStr(buf)) {
854 rv = fwrite((char *) buf, strlen(buf), 1, fp);
858 rv = truncate(filename, wpos);
859 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
863 * Function to begin composing a new message
865 int client_make_message(CtdlIPC *ipc,
866 char *filename, /* temporary file name */
867 char *recipient, /* NULL if it's not mail */
871 char *subject, /* buffer to store subject line */
881 if ( (mode == 2) && (IsEmptyStr(editor_path)) ) {
882 scr_printf("*** No editor available; using built-in editor.\n");
886 fmt_date(datestr, sizeof datestr, time(NULL), 0);
889 if (room_flags & QR_ANONONLY && !recipient) {
890 snprintf(header, sizeof header, " ****");
893 snprintf(header, sizeof header,
896 (is_anonymous ? "[anonymous]" : fullname)
898 if (!IsEmptyStr(recipient)) {
899 size_t tmp = strlen(header);
900 snprintf(&header[tmp], sizeof header - tmp,
901 " to %s", recipient);
904 scr_printf("%s\n", header);
905 if (subject != NULL) if (!IsEmptyStr(subject)) {
906 scr_printf("Subject: %s\n", subject);
909 if ( (subject_required) && (IsEmptyStr(subject)) ) {
910 newprompt("Subject: ", subject, 70);
914 scr_printf("(Press ctrl-d when finished)\n");
918 fp = fopen(filename, "r");
920 fmout(screenwidth, fp, NULL, NULL, 0);
923 scr_printf("failed to get stream position %s\n",
927 fp = fopen(filename, "w");
929 scr_printf("*** Error opening temp file!\n %s: %s\n",
930 filename, strerror(errno)
941 fp = fopen(filename, "r+");
943 scr_printf("*** Error opening temp file!\n %s: %s\n",
944 filename, strerror(errno)
953 fp = fopen(filename, "a");
955 scr_printf("*** Error opening temp file!\n"
957 filename, strerror(errno));
977 default: /* allow 2+ modes */
978 e_ex_code = 1; /* start with a failed exit code */
979 stty_ctdl(SB_RESTORE);
981 cksum = file_checksum(filename);
982 if (editor_pid == 0) {
985 chmod(filename, 0600);
986 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
988 execlp(editor_path, editor_path, filename, NULL);
994 b = ka_wait(&e_ex_code);
995 } while ((b != editor_pid) && (b >= 0));
1001 MECR: if (mode >= 2) {
1002 if (file_checksum(filename) == cksum) {
1003 scr_printf("*** Aborted message.\n");
1006 if (e_ex_code == 0) {
1012 b = keymenu("Entry command (? for options)",
1016 "<P>rint formatted|"
1022 if (b == 'a') goto MEABT;
1023 if (b == 'c') goto ME1;
1024 if (b == 's') goto MEFIN;
1026 scr_printf(" %s from %s", datestr, fullname);
1027 if (!IsEmptyStr(recipient)) {
1028 scr_printf(" to %s", recipient);
1031 if (subject != NULL) if (!IsEmptyStr(subject)) {
1032 scr_printf("Subject: %s\n", subject);
1034 fp = fopen(filename, "r");
1036 fmout(screenwidth, fp, NULL, NULL, 0);
1039 scr_printf("failed to get stream position %s\n",
1046 replace_string(filename, 0L);
1053 if (subject != NULL) {
1054 newprompt("Subject: ", subject, 70);
1061 MEABT: scr_printf("Are you sure? ");
1065 MEABT2: unlink(filename);
1071 * Make sure there's room in msg_arr[] for at least one more.
1073 void check_msg_arr_size(void) {
1074 if ((num_msgs + 1) > msg_arr_size) {
1075 msg_arr_size += 512;
1076 msg_arr = realloc(msg_arr,
1077 ((sizeof(long)) * msg_arr_size) );
1083 * break_big_lines() - break up lines that are >1024 characters
1084 * otherwise the server will truncate
1086 void break_big_lines(char *msg) {
1095 while (strlen(ptr) > 1000) {
1096 break_here = strchr(&ptr[900], ' ');
1097 if ((break_here == NULL) || (break_here > &ptr[999])) {
1098 break_here = &ptr[999];
1107 * entmsg() - edit and create a message
1108 * returns 0 if message was saved
1110 int entmsg(CtdlIPC *ipc,
1111 int is_reply, /* nonzero if this was a <R>eply command */
1113 int masquerade /* prompt for a non-default display name? */
1122 struct ctdlipcmessage message;
1123 unsigned long *msgarr = NULL;
1124 int r; /* IPC response code */
1125 int subject_required = 0;
1128 scr_printf("You may not enter messages in this type of room.\n");
1139 strcpy(subject, "");
1142 * First, check to see if we have permission to enter a message in
1143 * this room. The server will return an error code if we can't.
1145 strcpy(message.recipient, "");
1146 strcpy(message.author, "");
1147 strcpy(message.subject, "");
1148 strcpy(message.references, "");
1149 message.text = ""; /* point to "", changes later */
1150 message.anonymous = 0;
1151 message.type = mode;
1154 newprompt("Display name for this message: ", message.author, 40);
1159 if (!IsEmptyStr(reply_subject)) {
1160 if (!strncasecmp(reply_subject,
1162 strcpy(message.subject, reply_subject);
1165 snprintf(message.subject,
1166 sizeof message.subject,
1172 /* Trim down excessively long lists of thread references. We eliminate the
1173 * second one in the list so that the thread root remains intact.
1175 int rrtok = num_tokens(reply_references, '|');
1176 int rrlen = strlen(reply_references);
1177 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1178 remove_token(reply_references, 1, '|');
1181 snprintf(message.references, sizeof message.references, "%s%s%s",
1183 (IsEmptyStr(reply_references) ? "" : "|"),
1188 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1190 if (r / 100 != 2 && r / 10 != 57) {
1191 scr_printf("%s\n", buf);
1195 /* Error code 570 is special. It means that we CAN enter a message
1196 * in this room, but a recipient needs to be specified.
1203 /* If the user is a dumbass, tell them how to type. */
1204 if ((userflags & US_EXPERT) == 0) {
1205 formout(ipc, "entermsg");
1208 /* Handle the selection of a recipient, if necessary. */
1210 if (need_recp == 1) {
1211 if (axlevel >= AxProbU) {
1213 strcpy(buf, reply_to);
1215 newprompt("Enter recipient: ", buf, SIZ-100);
1216 if (IsEmptyStr(buf)) {
1221 strcpy(buf, "sysop");
1223 strcpy(message.recipient, buf);
1225 if (room_flags & QR_ANONOPT) {
1226 scr_printf("Anonymous (Y/N)? ");
1228 message.anonymous = 1;
1231 /* If it's mail, we've got to check the validity of the recipient... */
1232 if (!IsEmptyStr(message.recipient)) {
1233 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1235 scr_printf("%s\n", buf);
1240 /* Learn the number of the newest message in in the room, so we can
1241 * tell upon saving whether someone else has posted too.
1244 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1246 scr_printf("%s\n", buf);
1248 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1252 /* Now compose the message... */
1253 if (client_make_message(ipc, temp, message.recipient,
1254 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1255 if (msgarr) free(msgarr);
1259 /* Reopen the temp file that was created, so we can send it */
1260 fp = fopen(temp, "r");
1262 if (!fp || !(message.text = load_message_from_file(fp))) {
1263 scr_printf("*** Internal error while trying to save message!\n"
1265 temp, strerror(errno));
1272 /* Break lines that are >1024 characters, otherwise the server
1273 * will truncate them.
1275 break_big_lines(message.text);
1277 /* Transmit message to the server */
1278 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1280 scr_printf("%s\n", buf);
1284 /* Yes, unlink it now, so it doesn't stick around if we crash */
1287 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1289 if (msgarr) free(msgarr);
1291 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1293 scr_printf("%s\n", buf);
1295 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1299 /* get new highest message number in room to set lrp for goto... */
1300 maxmsgnum = msgarr[num_msgs - 1];
1302 /* now see if anyone else has posted in here */
1304 for (a = 0; a < num_msgs; ++a) {
1305 if (msgarr[a] > highmsg) {
1309 if (msgarr) free(msgarr);
1312 /* In the Mail> room, this algorithm always counts one message
1313 * higher than in public rooms, so we decrement it by one.
1320 scr_printf("*** 1 additional message has been entered "
1321 "in this room by another user.\n");
1324 scr_printf("*** %d additional messages have been entered "
1325 "in this room by other users.\n", b);
1333 * Do editing on a quoted file
1335 void process_quote(void)
1337 FILE *qfile, *tfile;
1339 int line, qstart, qend;
1341 /* Unlink the second temp file as soon as it's opened, so it'll get
1342 * deleted even if the program dies
1344 qfile = fopen(temp2, "r");
1347 /* Display the quotable text with line numbers added */
1349 if (fgets(buf, 128, qfile) == NULL) {
1350 /* we're skipping a line here */
1352 while (fgets(buf, 128, qfile) != NULL) {
1353 scr_printf("%3d %s", ++line, buf);
1356 qstart = intprompt("Begin quoting at", 1, 1, line);
1357 qend = intprompt(" End quoting at", line, qstart, line);
1361 if (fgets(buf, 128, qfile) == NULL) {
1362 /* we're skipping a line here */
1364 tfile = fopen(temp, "w");
1365 while (fgets(buf, 128, qfile) != NULL) {
1366 if ((++line >= qstart) && (line <= qend))
1367 fprintf(tfile, " >%s", buf);
1369 fprintf(tfile, " \n");
1378 * List the URL's which were embedded in the previous message
1380 void list_urls(CtdlIPC *ipc)
1386 if (num_urls == 0) {
1387 scr_printf("There were no URL's in the previous message.\n\n");
1391 for (i = 0; i < num_urls; ++i) {
1392 scr_printf("%3d %s\n", i + 1, urls[i]);
1395 if ((i = num_urls) != 1)
1396 i = intprompt("Display which one", 1, 1, num_urls);
1398 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1401 scr_printf("failed to '%s' by %d\n", cmd, rv);
1407 * Run image viewer in background
1409 int do_image_view(const char *filename)
1414 snprintf(cmd, sizeof cmd, imagecmd, filename);
1421 if (childpid == 0) {
1423 pid_t grandchildpid;
1425 grandchildpid = fork();
1426 if (grandchildpid < 0) {
1427 return grandchildpid;
1430 if (grandchildpid == 0) {
1435 nullfd = open("/dev/null", O_WRONLY);
1442 retcode = system(cmd);
1452 if (grandchildpid > 0) {
1460 waitpid(childpid, &retcode, 0);
1469 * View an image attached to a message
1471 void image_view(CtdlIPC *ipc, unsigned long msg)
1473 struct parts *ptr = last_message_parts;
1477 /* Run through available parts */
1478 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1479 if ((!strcasecmp(ptr->disposition, "attachment")
1480 || !strcasecmp(ptr->disposition, "inline"))
1481 && !strncmp(ptr->mimetype, "image/", 6)) {
1484 strcpy(part, ptr->number);
1491 strprompt("View which part (0 when done)", part, SIZ-1);
1493 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1494 if ((!strcasecmp(ptr->disposition, "attachment")
1495 || !strcasecmp(ptr->disposition, "inline"))
1496 && !strncmp(ptr->mimetype, "image/", 6)
1497 && !strcasecmp(ptr->number, part)) {
1500 void *file = NULL; /* The downloaded file */
1505 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1507 scr_printf("%s\n", buf);
1511 len = (size_t)extract_long(buf, 0);
1512 progress(ipc, len, len);
1514 CtdlMakeTempFileName(tmp, sizeof tmp);
1515 strcat(tmp, ptr->filename);
1516 save_buffer(file, len, tmp);
1530 * Read the messages in the current room
1532 void readmsgs(CtdlIPC *ipc,
1533 enum MessageList c, /* see listing in citadel_ipc.h */
1534 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1535 int q /* Number of msgs to read (if c==3) */
1537 int a, e, f, g, start;
1543 char prtfile[PATH_MAX];
1546 char targ[ROOMNAMELEN];
1547 char filename[PATH_MAX];
1548 char save_to[PATH_MAX];
1549 void *attachment = NULL; /* Downloaded attachment */
1550 FILE *dest = NULL; /* Alternate destination other than screen */
1551 int r; /* IPC response code */
1552 static int att_seq = 0; /* Attachment download sequence number */
1553 int rv = 0; /* silence the stupid warn_unused_result warnings */
1555 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1561 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1563 scr_printf("%s\n", cmd);
1565 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1569 if (num_msgs == 0) { /* TODO look at this later */
1570 if (c == LastMessages) return;
1571 scr_printf("*** There are no ");
1572 if (c == NewMessages) scr_printf("new ");
1573 if (c == OldMessages) scr_printf("old ");
1574 scr_printf("messages in this room.\n");
1578 /* this loop cycles through each message... */
1579 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1580 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1581 while (msg_arr[a] == 0L) {
1583 if ((a == num_msgs) || (a == (-1)))
1587 RAGAIN: pagin = ((arcflag == 0)
1589 && (userflags & US_PAGINATOR)) ? 1 : 0;
1591 /* If we're doing a quote, set the screenwidth to 72 */
1593 hold_sw = screenwidth;
1597 /* If printing or archiving, set the screenwidth to 80 */
1599 hold_sw = screenwidth;
1603 /* clear parts list */
1604 free_parts(last_message_parts);
1605 last_message_parts = NULL;
1607 /* now read the message... */
1608 e = read_message(ipc, msg_arr[a], pagin, dest);
1610 /* ...and set the screenwidth back if we have to */
1611 if ((quotflag) || (arcflag)) {
1612 screenwidth = hold_sw;
1615 highest_msg_read = msg_arr[a];
1620 enable_color = hold_color;
1629 enable_color = hold_color;
1632 if (freopen(prtfile, "r", stdin) == NULL) {
1633 /* we probably should handle the error condition here */
1635 stty_ctdl(SB_RESTORE);
1636 ka_system(printcmd);
1637 stty_ctdl(SB_NO_INTR);
1644 } while ((g != f) && (g >= 0));
1645 scr_printf("Message printed.\n");
1649 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1650 && (((room_flags & QR_MAILBOX) == 0)
1651 || (rc_force_mail_prompts == 0))) {
1656 color(BRIGHT_WHITE);
1657 scr_printf("%d", num_msgs - a - 1);
1661 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1662 if (rc_url_cmd[0] && num_urls)
1663 keyopt("<U>RLview ");
1664 if (has_images > 0 && !IsEmptyStr(imagecmd))
1665 keyopt("<I>mages ");
1666 keyopt("<?>help -> ");
1669 e = (inkey() & 127);
1671 /* return key same as <N> */ if (e == 10)
1673 /* space key same as <N> */ if (e == 32)
1675 /* del/move for aides only */
1676 if ( (!is_room_aide)
1677 && ((room_flags & QR_MAILBOX) == 0)
1678 && ((room_flags2 & QR2_COLLABDEL) == 0)
1680 if ((e == 'd') || (e == 'm'))
1683 /* print only if available */
1684 if ((e == 'p') && (IsEmptyStr(printcmd)))
1686 /* can't file if not allowed */
1688 && (rc_allow_attachments == 0))
1690 /* link only if browser avail*/
1692 && (IsEmptyStr(rc_url_cmd)))
1695 && (IsEmptyStr(imagecmd) || !has_images))
1697 } while ((e != 'a') && (e != 'n') && (e != 's')
1698 && (e != 'd') && (e != 'm') && (e != 'p')
1699 && (e != 'q') && (e != 'b') && (e != 'h')
1700 && (e != 'r') && (e != 'f') && (e != '?')
1701 && (e != 'u') && (e != 'c') && (e != 'y')
1702 && (e != 'i') && (e != 'o') );
1708 scr_printf("Again");
1711 scr_printf("Delete");
1723 scr_printf("Print");
1726 scr_printf("reply Quoted");
1732 scr_printf("Header");
1735 scr_printf("Reply");
1738 scr_printf("Open attachments");
1744 scr_printf("URL's");
1747 scr_printf("mY next");
1752 scr_printf("? <help>");
1755 if (userflags & US_DISAPPEAR || e == 'i')
1756 scr_printf("\r%79s\r", "");
1760 DONE_QUOTING: switch (e) {
1762 scr_printf("Options available here:\n"
1763 " ? Help (prints this message)\n"
1764 " S Stop reading immediately\n"
1765 " A Again (repeats last message)\n"
1766 " N Next (continue with next message)\n"
1767 " Y My Next (continue with next message you authored)\n"
1768 " B Back (go back to previous message)\n");
1770 || (room_flags & QR_MAILBOX)
1771 || (room_flags2 & QR2_COLLABDEL)
1773 scr_printf(" D Delete this message\n"
1774 " M Move message to another room\n");
1776 scr_printf(" C Copy message to another room\n");
1777 if (!IsEmptyStr(printcmd))
1778 scr_printf(" P Print this message\n");
1780 " Q Reply to this message, quoting portions of it\n"
1781 " H Headers (display message headers only)\n");
1783 scr_printf(" R Reply to this message\n");
1784 if (rc_allow_attachments) {
1785 scr_printf(" O (Open attachments)\n");
1786 scr_printf(" F (save attachments to a File)\n");
1788 if (!IsEmptyStr(rc_url_cmd))
1789 scr_printf(" U (list URL's for display)\n");
1790 if (!IsEmptyStr(imagecmd) && has_images > 0)
1791 scr_printf(" I Image viewer\n");
1796 dest = fopen(prtfile, "w");
1798 hold_color = enable_color;
1803 dest = fopen(temp2, "w");
1805 hold_color = enable_color;
1817 newprompt("Enter target room: ",
1818 targ, ROOMNAMELEN - 1);
1819 if (!IsEmptyStr(targ)) {
1820 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1821 msg_arr[a], targ, cmd);
1822 scr_printf("%s\n", cmd);
1828 if (r / 100 != 2) /* r will be init'ed, FIXME */
1829 goto RMSGREAD; /* the logic here sucks */
1833 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1834 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1835 filename, &attachment, progress, cmd);
1837 scr_printf("%s\n", cmd);
1839 extract_token(filename, cmd, 2, '|', sizeof filename);
1841 * Part 1 won't have a filename; use the
1842 * subject of the message instead. IO
1844 if (IsEmptyStr(filename)) {
1845 strcpy(filename, reply_subject);
1847 if (e == 'o') { /* open attachment */
1848 mkdir(tempdir, 0700);
1849 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1853 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1854 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1857 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1859 else { /* save attachment to disk */
1860 destination_directory(save_to, filename);
1861 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1870 scr_printf("*** Delete this message? ");
1872 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1873 scr_printf("%s\n", cmd);
1881 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1884 savedpos = num_msgs;
1885 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1886 num_msgs = savedpos;
1892 image_view(ipc, msg_arr[a]);
1895 { /* hack hack hack */
1896 /* find the next message by me, stay here if we find nothing */
1899 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1901 /* This is repetitively dumb, but that's what computers are for.
1902 We have to load up messages until we find one by us */
1905 struct ctdlipcmessage *msg = NULL;
1907 /* read the header so we can get 'from=' */
1908 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1909 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1910 a = lasta; /* meesa current */
1918 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1922 } /* end for loop */
1923 } /* end read routine */
1929 * View and edit a system message
1931 void edit_system_message(CtdlIPC *ipc, char *which_message)
1935 char write_cmd[SIZ];
1937 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1938 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1939 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1940 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1947 * Loads the contents of a file into memory. Caller must free the allocated
1950 char *load_message_from_file(FILE *src)
1956 fseek(src, 0, SEEK_END);
1960 dest = (char *)calloc(1, i + 1);
1967 g = fread(dest + got, 1, i - got, src);
1970 /* Interrupted system call, keep going */
1973 /* At this point we have either EOF or error */