2 * Text client functions for reading and writing of messages
4 * Copyright (c) 1987-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
51 #include <libcitadel.h>
53 #include "citadel_ipc.h"
54 #include "citadel_decls.h"
63 #define MAXWORDBUF SIZ
64 #define NO_REPLY_TO "nobody ... xxxxxx"
67 char reply_subject[SIZ];
68 char reply_references[SIZ];
69 char reply_inreplyto[SIZ];
73 char text[MAXWORDBUF];
76 void stty_ctdl(int cmd);
77 int haschar(const char *st, int ch);
78 void ctdl_getline(char *string, int lim);
79 int file_checksum(char *filename);
80 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
82 unsigned long *msg_arr = NULL;
85 char rc_alt_semantics;
86 extern char room_name[];
87 extern char tempdir[];
88 extern unsigned room_flags;
89 extern unsigned room_flags2;
91 extern long highest_msg_read;
94 extern int screenwidth;
95 extern int screenheight;
96 extern long maxmsgnum;
99 extern char is_room_aide;
100 extern char fullname[];
102 extern unsigned userflags;
103 extern char sigcaught;
104 extern char printcmd[];
105 extern int rc_allow_attachments;
106 extern int rc_display_message_numbers;
107 extern int rc_force_mail_prompts;
108 extern int editor_pid;
109 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
111 char urls[MAXURLS][SIZ];
113 int has_images = 0; /* Current msg has images */
114 struct parts *last_message_parts = NULL; /* Parts from last msg */
118 void ka_sigcatch(int signum)
121 signal(SIGALRM, ka_sigcatch);
122 CtdlIPCNoop(ipc_for_signal_handlers);
127 * server keep-alive version of wait() (needed for external editor)
129 pid_t ka_wait(int *kstatus)
134 signal(SIGALRM, ka_sigcatch);
138 } while (errno == EINTR);
139 signal(SIGALRM, SIG_IGN);
146 * version of system() that uses ka_wait()
148 int ka_system(char *shc)
157 perror("Cannot fork");
159 return ((pid_t) childpid);
163 execlp("/bin/sh", "sh", "-c", shc, NULL);
169 waitpid = ka_wait(&retcode);
170 } while (waitpid != childpid);
180 * add a newline to the buffer...
182 void add_newline(struct cittext *textlist)
187 while (ptr->next != NULL)
190 while (ptr->text[strlen(ptr->text) - 1] == 32)
191 ptr->text[strlen(ptr->text) - 1] = 0;
192 /* strcat(ptr->text,"\n"); */
194 ptr->next = (struct cittext *)
195 malloc(sizeof(struct cittext));
198 strcpy(ptr->text, "");
203 * add a word to the buffer...
205 void add_word(struct cittext *textlist, char *wordbuf)
210 while (ptr->next != NULL)
213 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
214 ptr->next = (struct cittext *)
215 malloc(sizeof(struct cittext));
218 strcpy(ptr->text, "");
221 strcat(ptr->text, wordbuf);
222 strcat(ptr->text, " ");
227 * begin editing of an opened file pointed to by fp
229 void citedit(FILE *fp)
231 int a, prev, finished, b, last_space;
233 struct cittext *textlist = NULL;
235 char wordbuf[MAXWORDBUF];
238 /* first, load the text into the buffer */
240 textlist = (struct cittext *) malloc(sizeof(struct cittext));
241 textlist->next = NULL;
242 strcpy(textlist->text, "");
246 while (a = getc(fp), a >= 0) {
248 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
249 add_word(textlist, wordbuf);
251 if ((prev == 13) || (prev == 10)) {
252 add_word(textlist, "\n");
253 add_newline(textlist);
254 add_word(textlist, "");
257 wordbuf[strlen(wordbuf) + 1] = 0;
258 wordbuf[strlen(wordbuf)] = a;
260 if (strlen(wordbuf) + 3 > screenwidth) {
261 add_word(textlist, wordbuf);
269 prev = (appending ? 13 : (-1));
281 if ((a != 32) && (prev == 13)) {
282 add_word(textlist, "\n");
286 if ((a == 32) && (prev == 13)) {
287 add_word(textlist, "\n");
288 add_newline(textlist);
292 if (!IsEmptyStr(wordbuf)) {
293 wordbuf[strlen(wordbuf) - 1] = 0;
298 } else if (a == 23) {
300 wordbuf[strlen(wordbuf) - 1] = 0;
304 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
305 } else if (a == 13) {
307 if (IsEmptyStr(wordbuf))
310 for (b = 0; b < strlen(wordbuf); ++b)
311 if (wordbuf[b] == 32) {
319 add_word(textlist, wordbuf);
324 wordbuf[strlen(wordbuf) + 1] = 0;
325 wordbuf[strlen(wordbuf)] = a;
327 if ((strlen(wordbuf) + 3) > screenwidth) {
329 for (b = 0; b < strlen(wordbuf); ++b)
330 if (wordbuf[b] == 32)
332 if (last_space >= 0) {
333 for (b = 0; b < strlen(wordbuf); ++b)
334 if (wordbuf[b] == 32) {
342 for (b = 0; b < strlen(wordbuf); ++b) {
347 scr_printf("\n%s", wordbuf);
349 add_word(textlist, wordbuf);
355 } while (finished == 0);
358 /* write the buffer back to disk */
360 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
361 fprintf(fp, "%s", ptr->text);
365 rv = ftruncate(fileno(fp), ftell(fp));
367 /* and deallocate the memory we used */
368 while (textlist != NULL) {
369 ptr = textlist->next;
377 * Free the struct parts
379 void free_parts(struct parts *p)
381 struct parts *a_part = p;
387 a_part = a_part->next;
394 * Read a message from the server
396 int read_message(CtdlIPC *ipc,
397 long num, /* message number */
398 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
399 FILE *dest) /* Destination file, NULL for screen */
406 struct ctdlipcmessage *message = NULL;
407 int r; /* IPC response code */
408 char *converted_text = NULL;
415 int final_line_is_blank = 0;
422 strcpy(reply_to, NO_REPLY_TO);
423 strcpy(reply_subject, "");
424 strcpy(reply_references, "");
425 strcpy(reply_inreplyto, "");
427 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
429 scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
432 free_parts(message->attachments);
438 fprintf(dest, "\n ");
444 if (pagin == 1 && !dest) {
448 /* View headers only */
450 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
451 message->nhdr ? "yes" : "no",
452 message->author, message->type,
454 if (!IsEmptyStr(message->subject)) {
455 scr_printf("subj=%s\n", message->subject);
457 if (!IsEmptyStr(message->email)) {
458 scr_printf("rfca=%s\n", message->email);
460 scr_printf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
461 message->hnod, message->room,
463 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,
473 ptr->disposition, ptr->mimetype,
480 free_parts(message->attachments);
485 if (rc_display_message_numbers) {
487 fprintf(dest, "[#%s] ", message->msgid);
492 scr_printf("#%s", message->msgid);
497 if (nhdr == 1 && !is_room_aide) {
499 fprintf(dest, " ****");
504 fmt_date(now, sizeof now, message->time, 0);
506 fprintf(dest, "%s from %s ", now, message->author);
507 if (!IsEmptyStr(message->email)) {
508 fprintf(dest, "<%s> ", message->email);
512 scr_printf("%s ", now);
516 scr_printf("%s ", message->author);
517 if (!IsEmptyStr(message->email)) {
521 scr_printf("%s", message->email);
526 if (!IsEmptyStr(message->node)) {
527 if ((room_flags & QR_NETWORK)
528 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
529 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
530 if (IsEmptyStr(message->email)) {
532 fprintf(dest, "@%s ", message->node);
536 color(BRIGHT_YELLOW);
537 scr_printf("%s ", message->node);
542 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
543 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
545 fprintf(dest, "(%s) ", message->hnod);
550 scr_printf("%s", message->hnod);
555 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
557 fprintf(dest, "in %s> ", message->room);
561 color(BRIGHT_MAGENTA);
562 scr_printf("%s> ", message->room);
565 if (!IsEmptyStr(message->recipient)) {
567 fprintf(dest, "to %s ", message->recipient);
572 scr_printf("%s ", message->recipient);
583 /* Set the reply-to address to an Internet e-mail address if possible
585 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
586 if (!IsEmptyStr(message->author)) {
587 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
590 safestrncpy(reply_to, message->email, sizeof reply_to);
594 /* But if we can't do that, set it to a Citadel address.
596 if (!strcmp(reply_to, NO_REPLY_TO)) {
597 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
598 message->author, message->node);
601 if (message->msgid != NULL) {
602 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
605 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
606 safestrncpy(reply_references, message->references, sizeof reply_references);
609 if (message->subject != NULL) {
610 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
611 if (!IsEmptyStr(message->subject)) {
613 fprintf(dest, "Subject: %s\n",
617 scr_printf("Subject: ");
619 scr_printf("%s\n", message->subject);
624 if (pagin == 1 && !dest) {
628 /******* end of header output, start of message text output *******/
631 * Convert HTML to plain text, formatting for the actual width
632 * of the client screen.
634 if (!strcasecmp(message->content_type, "text/html")) {
635 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
636 if (converted_text != NULL) {
638 message->text = converted_text;
643 /* Text/plain is a different type */
644 if (!strcasecmp(message->content_type, "text/plain")) {
649 num_urls = 0; /* Start with a clean slate */
650 searchptr = message->text;
651 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
652 searchptr = strstr(searchptr, "http://");
653 if (searchptr != NULL) {
654 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
655 for (i = 0; i < strlen(urls[num_urls]); i++) {
656 ch = urls[num_urls][i];
657 if (ch == '>' || ch == '\"' || ch == ')' ||
658 ch == ' ' || ch == '\n') {
659 urls[num_urls][i] = 0;
671 if (format_type == 0) {
672 fr = fmout(screenwidth, NULL, message->text, dest, 1);
674 /* renderer for text/plain */
676 lineptr = message->text;
679 nextline = strchr(lineptr, '\n');
680 if (nextline != NULL) {
683 if (*nextline == 0) nextline = NULL;
686 if (sigcaught == 0) {
687 linelen = strlen(lineptr);
688 if (linelen && (lineptr[linelen-1] == '\r')) {
689 lineptr[--linelen] = 0;
692 fprintf(dest, "%s\n", lineptr);
694 scr_printf("%s\n", lineptr);
697 if (lineptr[0] == 0) final_line_is_blank = 1;
698 else final_line_is_blank = 0;
703 if (!final_line_is_blank) {
713 /* Enumerate any attachments */
714 if ( (pagin == 1) && (message->attachments) ) {
717 for (ptr = message->attachments; ptr; ptr = ptr->next) {
718 if ( (!strcasecmp(ptr->disposition, "attachment"))
719 || (!strcasecmp(ptr->disposition, "inline"))
720 || (!strcasecmp(ptr->disposition, ""))
722 if ( (strcasecmp(ptr->number, message->mime_chosen))
723 && (!IsEmptyStr(ptr->mimetype))
727 color(BRIGHT_MAGENTA);
728 scr_printf("%s", ptr->number);
732 scr_printf("%s", ptr->filename);
734 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
735 if (!strncmp(ptr->mimetype, "image/", 6)) {
743 /* Save the attachments info for later */
744 last_message_parts = message->attachments;
750 if (pagin == 1 && !dest)
757 * replace string function for the built-in editor
759 void replace_string(char *filename, long int startpos)
768 int substitutions = 0;
772 scr_printf("Enter text to be replaced:\n: ");
773 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
774 if (IsEmptyStr(srch_str)) {
778 scr_printf("Enter text to replace it with:\n: ");
779 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
781 fp = fopen(filename, "r+");
787 fseek(fp, startpos, 0);
789 while (a = getc(fp), a > 0) {
791 buf[strlen(buf) + 1] = 0;
792 buf[strlen(buf)] = a;
793 if (strlen(buf) >= strlen(srch_str)) {
794 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
795 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
796 strcpy(ptr, rplc_str);
800 if (strlen(buf) > 384) {
803 rv = fwrite((char *) buf, 128, 1, fp);
804 strcpy(buf, &buf[128]);
810 if (!IsEmptyStr(buf)) {
811 rv = fwrite((char *) buf, strlen(buf), 1, fp);
815 rv = truncate(filename, wpos);
816 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
820 * Function to begin composing a new message
822 int client_make_message(CtdlIPC *ipc,
823 char *filename, /* temporary file name */
824 char *recipient, /* NULL if it's not mail */
828 char *subject, /* buffer to store subject line */
836 char *editor_path = NULL;
841 if ((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2]))
843 editor_path = editor_paths[mode-2];
844 } else if (!IsEmptyStr(editor_paths[0]))
846 editor_path = editor_paths[0];
848 scr_printf("*** No editor available; using built-in editor.\n");
853 fmt_date(datestr, sizeof datestr, time(NULL), 0);
856 if (room_flags & QR_ANONONLY && !recipient) {
857 snprintf(header, sizeof header, " ****");
860 snprintf(header, sizeof header,
863 (is_anonymous ? "[anonymous]" : fullname)
865 if (!IsEmptyStr(recipient)) {
866 size_t tmp = strlen(header);
867 snprintf(&header[tmp], sizeof header - tmp,
868 " to %s", recipient);
871 scr_printf("%s\n", header);
872 if (subject != NULL) if (!IsEmptyStr(subject)) {
873 scr_printf("Subject: %s\n", subject);
876 if ( (subject_required) && (IsEmptyStr(subject)) ) {
877 newprompt("Subject: ", subject, 70);
883 scr_printf("(Press ctrl-d when finished)\n");
887 fp = fopen(filename, "r");
889 fmout(screenwidth, fp, NULL, NULL, 0);
893 fp = fopen(filename, "w");
895 scr_printf("*** Error opening temp file!\n %s: %s\n",
896 filename, strerror(errno)
907 fp = fopen(filename, "r+");
909 scr_printf("*** Error opening temp file!\n %s: %s\n",
910 filename, strerror(errno)
919 fp = fopen(filename, "a");
921 scr_printf("*** Error opening temp file!\n"
923 filename, strerror(errno));
943 default: /* allow 2+ modes */
944 e_ex_code = 1; /* start with a failed exit code */
945 stty_ctdl(SB_RESTORE);
947 cksum = file_checksum(filename);
948 if (editor_pid == 0) {
951 chmod(filename, 0600);
952 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
954 execlp(editor_path, editor_path, filename, NULL);
960 b = ka_wait(&e_ex_code);
961 } while ((b != editor_pid) && (b >= 0));
967 MECR: if (mode >= 2) {
968 if (file_checksum(filename) == cksum) {
969 scr_printf("*** Aborted message.\n");
972 if (e_ex_code == 0) {
978 b = keymenu("Entry command (? for options)",
988 if (b == 'a') goto MEABT;
989 if (b == 'c') goto ME1;
990 if (b == 's') goto MEFIN;
992 scr_printf(" %s from %s", datestr, fullname);
993 if (!IsEmptyStr(recipient)) {
994 scr_printf(" to %s", recipient);
997 if (subject != NULL) if (!IsEmptyStr(subject)) {
998 scr_printf("Subject: %s\n", subject);
1000 fp = fopen(filename, "r");
1002 fmout(screenwidth, fp, NULL, NULL, 0);
1009 replace_string(filename, 0L);
1016 if (subject != NULL) {
1017 newprompt("Subject: ", subject, 70);
1024 MEABT: scr_printf("Are you sure? ");
1028 MEABT2: unlink(filename);
1034 * Make sure there's room in msg_arr[] for at least one more.
1036 void check_msg_arr_size(void) {
1037 if ((num_msgs + 1) > msg_arr_size) {
1038 msg_arr_size += 512;
1039 msg_arr = realloc(msg_arr,
1040 ((sizeof(long)) * msg_arr_size) );
1046 * break_big_lines() - break up lines that are >1024 characters
1047 * otherwise the server will truncate
1049 void break_big_lines(char *msg) {
1058 while (strlen(ptr) > 1000) {
1059 break_here = strchr(&ptr[900], ' ');
1060 if ((break_here == NULL) || (break_here > &ptr[999])) {
1061 break_here = &ptr[999];
1070 * entmsg() - edit and create a message
1071 * returns 0 if message was saved
1073 int entmsg(CtdlIPC *ipc,
1074 int is_reply, /* nonzero if this was a <R>eply command */
1076 int masquerade /* prompt for a non-default display name? */
1085 struct ctdlipcmessage message;
1086 unsigned long *msgarr = NULL;
1087 int r; /* IPC response code */
1088 int subject_required = 0;
1091 scr_printf("You may not enter messages in this type of room.\n");
1102 strcpy(subject, "");
1105 * First, check to see if we have permission to enter a message in
1106 * this room. The server will return an error code if we can't.
1108 strcpy(message.recipient, "");
1109 strcpy(message.author, "");
1110 strcpy(message.subject, "");
1111 strcpy(message.references, "");
1112 message.text = ""; /* point to "", changes later */
1113 message.anonymous = 0;
1114 message.type = mode;
1117 newprompt("Display name for this message: ", message.author, 40);
1120 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1122 if (r / 100 != 2 && r / 10 != 57) {
1123 scr_printf("%s\n", buf);
1127 /* Error code 570 is special. It means that we CAN enter a message
1128 * in this room, but a recipient needs to be specified.
1135 /* If the user is a dumbass, tell them how to type. */
1136 if ((userflags & US_EXPERT) == 0) {
1137 formout(ipc, "entermsg");
1140 /* Handle the selection of a recipient, if necessary. */
1142 if (need_recp == 1) {
1143 if (axlevel >= AxProbU) {
1145 strcpy(buf, reply_to);
1147 scr_printf("Enter recipient: ");
1148 ctdl_getline(buf, (SIZ-100) );
1149 if (IsEmptyStr(buf))
1153 strcpy(buf, "sysop");
1155 strcpy(message.recipient, buf);
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 if (room_flags & QR_ANONOPT) {
1189 scr_printf("Anonymous (Y/N)? ");
1191 message.anonymous = 1;
1194 /* If it's mail, we've got to check the validity of the recipient... */
1195 if (!IsEmptyStr(message.recipient)) {
1196 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1198 scr_printf("%s\n", buf);
1203 /* Learn the number of the newest message in in the room, so we can
1204 * tell upon saving whether someone else has posted too.
1207 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1209 scr_printf("%s\n", buf);
1211 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1215 /* Now compose the message... */
1216 if (client_make_message(ipc, temp, message.recipient,
1217 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1218 if (msgarr) free(msgarr);
1222 /* Reopen the temp file that was created, so we can send it */
1223 fp = fopen(temp, "r");
1225 if (!fp || !(message.text = load_message_from_file(fp))) {
1226 scr_printf("*** Internal error while trying to save message!\n"
1228 temp, strerror(errno));
1235 /* Break lines that are >1024 characters, otherwise the server
1236 * will truncate them.
1238 break_big_lines(message.text);
1240 /* Transmit message to the server */
1241 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1243 scr_printf("%s\n", buf);
1247 /* Yes, unlink it now, so it doesn't stick around if we crash */
1250 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1252 if (msgarr) free(msgarr);
1254 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1256 scr_printf("%s\n", buf);
1258 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1262 /* get new highest message number in room to set lrp for goto... */
1263 maxmsgnum = msgarr[num_msgs - 1];
1265 /* now see if anyone else has posted in here */
1267 for (a = 0; a < num_msgs; ++a) {
1268 if (msgarr[a] > highmsg) {
1272 if (msgarr) free(msgarr);
1275 /* In the Mail> room, this algorithm always counts one message
1276 * higher than in public rooms, so we decrement it by one.
1283 scr_printf("*** 1 additional message has been entered "
1284 "in this room by another user.\n");
1287 scr_printf("*** %d additional messages have been entered "
1288 "in this room by other users.\n", b);
1296 * Do editing on a quoted file
1298 void process_quote(void)
1300 FILE *qfile, *tfile;
1302 int line, qstart, qend;
1304 /* Unlink the second temp file as soon as it's opened, so it'll get
1305 * deleted even if the program dies
1307 qfile = fopen(temp2, "r");
1310 /* Display the quotable text with line numbers added */
1312 if (fgets(buf, 128, qfile) == NULL) {
1313 /* we're skipping a line here */
1315 while (fgets(buf, 128, qfile) != NULL) {
1316 scr_printf("%3d %s", ++line, buf);
1318 scr_printf("Begin quoting at [1] : ");
1319 ctdl_getline(buf, 4);
1320 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1321 scr_printf(" End quoting at [%d] : ", line);
1322 ctdl_getline(buf, 4);
1323 qend = (buf[0] == 0) ? (line) : atoi(buf);
1326 if (fgets(buf, 128, qfile) == NULL) {
1327 /* we're skipping a line here */
1329 tfile = fopen(temp, "w");
1330 while (fgets(buf, 128, qfile) != NULL) {
1331 if ((++line >= qstart) && (line <= qend))
1332 fprintf(tfile, " >%s", buf);
1334 fprintf(tfile, " \n");
1343 * List the URL's which were embedded in the previous message
1345 void list_urls(CtdlIPC *ipc)
1351 if (num_urls == 0) {
1352 scr_printf("There were no URL's in the previous message.\n\n");
1356 for (i = 0; i < num_urls; ++i) {
1357 scr_printf("%3d %s\n", i + 1, urls[i]);
1360 if ((i = num_urls) != 1)
1361 i = intprompt("Display which one", 1, 1, num_urls);
1363 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1370 * Run image viewer in background
1372 int do_image_view(const char *filename)
1377 snprintf(cmd, sizeof cmd, imagecmd, filename);
1384 if (childpid == 0) {
1386 pid_t grandchildpid;
1388 grandchildpid = fork();
1389 if (grandchildpid < 0) {
1390 return grandchildpid;
1393 if (grandchildpid == 0) {
1398 nullfd = open("/dev/null", O_WRONLY);
1405 retcode = system(cmd);
1415 if (grandchildpid > 0) {
1423 waitpid(childpid, &retcode, 0);
1432 * View an image attached to a message
1434 void image_view(CtdlIPC *ipc, unsigned long msg)
1436 struct parts *ptr = last_message_parts;
1440 /* Run through available parts */
1441 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1442 if ((!strcasecmp(ptr->disposition, "attachment")
1443 || !strcasecmp(ptr->disposition, "inline"))
1444 && !strncmp(ptr->mimetype, "image/", 6)) {
1447 strcpy(part, ptr->number);
1454 strprompt("View which part (0 when done)", part, SIZ-1);
1456 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1457 if ((!strcasecmp(ptr->disposition, "attachment")
1458 || !strcasecmp(ptr->disposition, "inline"))
1459 && !strncmp(ptr->mimetype, "image/", 6)
1460 && !strcasecmp(ptr->number, part)) {
1463 void *file = NULL; /* The downloaded file */
1468 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1470 scr_printf("%s\n", buf);
1474 len = (size_t)extract_long(buf, 0);
1475 progress(ipc, len, len);
1477 CtdlMakeTempFileName(tmp, sizeof tmp);
1478 strcat(tmp, ptr->filename);
1479 save_buffer(file, len, tmp);
1493 * Read the messages in the current room
1495 void readmsgs(CtdlIPC *ipc,
1496 enum MessageList c, /* see listing in citadel_ipc.h */
1497 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1498 int q /* Number of msgs to read (if c==3) */
1500 int a, b, e, f, g, start;
1506 char prtfile[PATH_MAX];
1509 char targ[ROOMNAMELEN];
1510 char filename[PATH_MAX];
1511 char save_to[PATH_MAX];
1512 void *attachment = NULL; /* Downloaded attachment */
1513 FILE *dest = NULL; /* Alternate destination other than screen */
1514 int r; /* IPC response code */
1515 static int att_seq = 0; /* Attachment download sequence number */
1516 int rv = 0; /* silence the stupid warn_unused_result warnings */
1523 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1529 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1531 scr_printf("%s\n", cmd);
1533 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1537 if (num_msgs == 0) { /* TODO look at this later */
1538 if (c == LastMessages) return;
1539 scr_printf("*** There are no ");
1540 if (c == NewMessages) scr_printf("new ");
1541 if (c == OldMessages) scr_printf("old ");
1542 scr_printf("messages in this room.\n");
1546 /* this loop cycles through each message... */
1547 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1548 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1549 while (msg_arr[a] == 0L) {
1551 if ((a == num_msgs) || (a == (-1)))
1555 RAGAIN: pagin = ((arcflag == 0)
1557 && (userflags & US_PAGINATOR)) ? 1 : 0;
1559 /* If we're doing a quote, set the screenwidth to 72 */
1561 hold_sw = screenwidth;
1565 /* If printing or archiving, set the screenwidth to 80 */
1567 hold_sw = screenwidth;
1571 /* clear parts list */
1572 free_parts(last_message_parts);
1573 last_message_parts = NULL;
1575 /* now read the message... */
1576 e = read_message(ipc, msg_arr[a], pagin, dest);
1578 /* ...and set the screenwidth back if we have to */
1579 if ((quotflag) || (arcflag)) {
1580 screenwidth = hold_sw;
1582 RMSGREAD: scr_flush();
1583 highest_msg_read = msg_arr[a];
1588 enable_color = hold_color;
1597 enable_color = hold_color;
1600 if (freopen(prtfile, "r", stdin) == NULL) {
1601 /* we probably should handle the error condition here */
1603 stty_ctdl(SB_RESTORE);
1604 ka_system(printcmd);
1605 stty_ctdl(SB_NO_INTR);
1612 } while ((g != f) && (g >= 0));
1613 scr_printf("Message printed.\n");
1615 if (rc_alt_semantics && c == 1) {
1618 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1622 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1623 && (((room_flags & QR_MAILBOX) == 0)
1624 || (rc_force_mail_prompts == 0))) {
1629 color(BRIGHT_WHITE);
1630 scr_printf("%d", num_msgs - a - 1);
1634 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1635 if (rc_url_cmd[0] && num_urls)
1636 keyopt("<U>RLview ");
1637 if (has_images > 0 && !IsEmptyStr(imagecmd))
1638 keyopt("<I>mages ");
1639 keyopt("<?>help -> ");
1642 e = (inkey() & 127);
1644 /* return key same as <N> */ if (e == 10)
1646 /* space key same as <N> */ if (e == 32)
1648 /* del/move for aides only */
1649 if ( (!is_room_aide)
1650 && ((room_flags & QR_MAILBOX) == 0)
1651 && ((room_flags2 & QR2_COLLABDEL) == 0)
1653 if ((e == 'd') || (e == 'm'))
1656 /* print only if available */
1657 if ((e == 'p') && (IsEmptyStr(printcmd)))
1659 /* can't file if not allowed */
1661 && (rc_allow_attachments == 0))
1663 /* link only if browser avail*/
1665 && (IsEmptyStr(rc_url_cmd)))
1668 && (IsEmptyStr(imagecmd) || !has_images))
1670 } while ((e != 'a') && (e != 'n') && (e != 's')
1671 && (e != 'd') && (e != 'm') && (e != 'p')
1672 && (e != 'q') && (e != 'b') && (e != 'h')
1673 && (e != 'r') && (e != 'f') && (e != '?')
1674 && (e != 'u') && (e != 'c') && (e != 'y')
1675 && (e != 'i') && (e != 'o') );
1681 scr_printf("Again");
1684 scr_printf("Delete");
1696 scr_printf("Print");
1699 scr_printf("reply Quoted");
1705 scr_printf("Header");
1708 scr_printf("Reply");
1711 scr_printf("Open attachments");
1717 scr_printf("URL's");
1720 scr_printf("mY next");
1725 scr_printf("? <help>");
1728 if (userflags & US_DISAPPEAR || e == 'i')
1729 scr_printf("\r%79s\r", "");
1734 DONE_QUOTING: switch (e) {
1736 scr_printf("Options available here:\n"
1737 " ? Help (prints this message)\n"
1738 " S Stop reading immediately\n"
1739 " A Again (repeats last message)\n"
1740 " N Next (continue with next message)\n"
1741 " Y My Next (continue with next message you authored)\n"
1742 " B Back (go back to previous message)\n");
1744 || (room_flags & QR_MAILBOX)
1745 || (room_flags2 & QR2_COLLABDEL)
1747 scr_printf(" D Delete this message\n"
1748 " M Move message to another room\n");
1750 scr_printf(" C Copy message to another room\n");
1751 if (!IsEmptyStr(printcmd))
1752 scr_printf(" P Print this message\n");
1754 " Q Reply to this message, quoting portions of it\n"
1755 " H Headers (display message headers only)\n");
1757 scr_printf(" R Reply to this message\n");
1758 if (rc_allow_attachments) {
1759 scr_printf(" O (Open attachments)\n");
1760 scr_printf(" F (save attachments to a File)\n");
1762 if (!IsEmptyStr(rc_url_cmd))
1763 scr_printf(" U (list URL's for display)\n");
1764 if (!IsEmptyStr(imagecmd) && has_images > 0)
1765 scr_printf(" I Image viewer\n");
1770 dest = fopen(prtfile, "w");
1772 hold_color = enable_color;
1777 dest = fopen(temp2, "w");
1779 hold_color = enable_color;
1791 newprompt("Enter target room: ",
1792 targ, ROOMNAMELEN - 1);
1793 if (!IsEmptyStr(targ)) {
1794 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1795 msg_arr[a], targ, cmd);
1796 scr_printf("%s\n", cmd);
1802 if (r / 100 != 2) /* r will be init'ed, FIXME */
1803 goto RMSGREAD; /* the logic here sucks */
1807 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1808 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1809 filename, &attachment, progress, cmd);
1811 scr_printf("%s\n", cmd);
1813 extract_token(filename, cmd, 2, '|', sizeof filename);
1815 * Part 1 won't have a filename; use the
1816 * subject of the message instead. IO
1818 if (IsEmptyStr(filename)) {
1819 strcpy(filename, reply_subject);
1821 if (e == 'o') { /* open attachment */
1822 mkdir(tempdir, 0700);
1823 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1827 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1828 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1831 else { /* save attachment to disk */
1832 destination_directory(save_to, filename);
1833 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1842 scr_printf("*** Delete this message? ");
1844 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1845 scr_printf("%s\n", cmd);
1853 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1856 savedpos = num_msgs;
1857 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1858 num_msgs = savedpos;
1864 image_view(ipc, msg_arr[a]);
1867 { /* hack hack hack */
1868 /* find the next message by me, stay here if we find nothing */
1871 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1873 /* This is repetitively dumb, but that's what computers are for.
1874 We have to load up messages until we find one by us */
1877 struct ctdlipcmessage *msg = NULL;
1879 /* read the header so we can get 'from=' */
1880 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1881 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1882 a = lasta; /* meesa current */
1890 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1894 } /* end for loop */
1895 } /* end read routine */
1901 * View and edit a system message
1903 void edit_system_message(CtdlIPC *ipc, char *which_message)
1907 char write_cmd[SIZ];
1909 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1910 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1911 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1912 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1919 * Verify the message base
1921 void check_message_base(CtdlIPC *ipc)
1924 char *transcript = NULL;
1925 int r; /* IPC response code */
1928 ("Please read the documentation before running this command.\n"
1929 "Having done so, do you still want to check the message base? ");
1933 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1935 scr_printf("%s\n", buf);
1939 while (transcript && !IsEmptyStr(transcript)) {
1940 extract_token(buf, transcript, 0, '\n', sizeof buf);
1941 remove_token(transcript, 0, '\n');
1942 scr_printf("%s\n", buf);
1944 if (transcript) free(transcript);
1950 * Loads the contents of a file into memory. Caller must free the allocated
1953 char *load_message_from_file(FILE *src)
1959 fseek(src, 0, SEEK_END);
1963 dest = (char *)calloc(1, i + 1);
1970 g = fread(dest + got, 1, i - got, src);
1973 /* Interrupted system call, keep going */
1976 /* At this point we have either EOF or error */