4 * Text client functions for reading and writing of messages
6 * Copyright (c) 1987-2010 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
53 #include <libcitadel.h>
55 #include "citadel_ipc.h"
56 #include "citadel_decls.h"
65 #define MAXWORDBUF SIZ
66 #define NO_REPLY_TO "nobody ... xxxxxx"
69 char reply_subject[SIZ];
70 char reply_references[SIZ];
71 char reply_inreplyto[SIZ];
75 char text[MAXWORDBUF];
78 void stty_ctdl(int cmd);
79 int haschar(const char *st, int ch);
80 void ctdl_getline(char *string, int lim);
81 int file_checksum(char *filename);
82 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
84 unsigned long *msg_arr = NULL;
87 char rc_alt_semantics;
88 extern char room_name[];
89 extern char tempdir[];
90 extern unsigned room_flags;
91 extern unsigned room_flags2;
93 extern long highest_msg_read;
96 extern int screenwidth;
97 extern int screenheight;
98 extern long maxmsgnum;
101 extern char is_room_aide;
102 extern char fullname[];
104 extern unsigned userflags;
105 extern char sigcaught;
106 extern char printcmd[];
107 extern int rc_allow_attachments;
108 extern int rc_display_message_numbers;
109 extern int rc_force_mail_prompts;
110 extern int editor_pid;
111 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
113 char urls[MAXURLS][SIZ];
115 int has_images = 0; /* Current msg has images */
116 struct parts *last_message_parts = NULL; /* Parts from last msg */
120 void ka_sigcatch(int signum)
123 signal(SIGALRM, ka_sigcatch);
124 CtdlIPCNoop(ipc_for_signal_handlers);
129 * server keep-alive version of wait() (needed for external editor)
131 pid_t ka_wait(int *kstatus)
136 signal(SIGALRM, ka_sigcatch);
140 } while (errno == EINTR);
141 signal(SIGALRM, SIG_IGN);
148 * version of system() that uses ka_wait()
150 int ka_system(char *shc)
159 perror("Cannot fork");
161 return ((pid_t) childpid);
165 execlp("/bin/sh", "sh", "-c", shc, NULL);
171 waitpid = ka_wait(&retcode);
172 } while (waitpid != childpid);
182 * add a newline to the buffer...
184 void add_newline(struct cittext *textlist)
189 while (ptr->next != NULL)
192 while (ptr->text[strlen(ptr->text) - 1] == 32)
193 ptr->text[strlen(ptr->text) - 1] = 0;
194 /* strcat(ptr->text,"\n"); */
196 ptr->next = (struct cittext *)
197 malloc(sizeof(struct cittext));
200 strcpy(ptr->text, "");
205 * add a word to the buffer...
207 void add_word(struct cittext *textlist, char *wordbuf)
212 while (ptr->next != NULL)
215 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
216 ptr->next = (struct cittext *)
217 malloc(sizeof(struct cittext));
220 strcpy(ptr->text, "");
223 strcat(ptr->text, wordbuf);
224 strcat(ptr->text, " ");
229 * begin editing of an opened file pointed to by fp
231 void citedit(FILE *fp)
233 int a, prev, finished, b, last_space;
235 struct cittext *textlist = NULL;
237 char wordbuf[MAXWORDBUF];
240 /* first, load the text into the buffer */
242 textlist = (struct cittext *) malloc(sizeof(struct cittext));
243 textlist->next = NULL;
244 strcpy(textlist->text, "");
248 while (a = getc(fp), a >= 0) {
250 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
251 add_word(textlist, wordbuf);
253 if ((prev == 13) || (prev == 10)) {
254 add_word(textlist, "\n");
255 add_newline(textlist);
256 add_word(textlist, "");
259 wordbuf[strlen(wordbuf) + 1] = 0;
260 wordbuf[strlen(wordbuf)] = a;
262 if (strlen(wordbuf) + 3 > screenwidth) {
263 add_word(textlist, wordbuf);
271 prev = (appending ? 13 : (-1));
283 if ((a != 32) && (prev == 13)) {
284 add_word(textlist, "\n");
288 if ((a == 32) && (prev == 13)) {
289 add_word(textlist, "\n");
290 add_newline(textlist);
294 if (!IsEmptyStr(wordbuf)) {
295 wordbuf[strlen(wordbuf) - 1] = 0;
300 } else if (a == 23) {
302 wordbuf[strlen(wordbuf) - 1] = 0;
306 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
307 } else if (a == 13) {
309 if (IsEmptyStr(wordbuf))
312 for (b = 0; b < strlen(wordbuf); ++b)
313 if (wordbuf[b] == 32) {
321 add_word(textlist, wordbuf);
326 wordbuf[strlen(wordbuf) + 1] = 0;
327 wordbuf[strlen(wordbuf)] = a;
329 if ((strlen(wordbuf) + 3) > screenwidth) {
331 for (b = 0; b < strlen(wordbuf); ++b)
332 if (wordbuf[b] == 32)
334 if (last_space >= 0) {
335 for (b = 0; b < strlen(wordbuf); ++b)
336 if (wordbuf[b] == 32) {
344 for (b = 0; b < strlen(wordbuf); ++b) {
349 scr_printf("\n%s", wordbuf);
351 add_word(textlist, wordbuf);
357 } while (finished == 0);
360 /* write the buffer back to disk */
362 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
363 fprintf(fp, "%s", ptr->text);
367 rv = ftruncate(fileno(fp), ftell(fp));
369 /* and deallocate the memory we used */
370 while (textlist != NULL) {
371 ptr = textlist->next;
379 * Free the struct parts
381 void free_parts(struct parts *p)
383 struct parts *a_part = p;
389 a_part = a_part->next;
396 * Read a message from the server
398 int read_message(CtdlIPC *ipc,
399 long num, /* message number */
400 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
401 FILE *dest) /* Destination file, NULL for screen */
408 struct ctdlipcmessage *message = NULL;
409 int r; /* IPC response code */
410 char *converted_text = NULL;
417 int final_line_is_blank = 0;
424 strcpy(reply_to, NO_REPLY_TO);
425 strcpy(reply_subject, "");
426 strcpy(reply_references, "");
427 strcpy(reply_inreplyto, "");
429 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
431 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
433 lines_printed = checkpagin(lines_printed, pagin, screenheight);
436 free_parts(message->attachments);
442 fprintf(dest, "\n ");
446 lines_printed = checkpagin(lines_printed, pagin, screenheight);
450 if (pagin == 1 && !dest) {
454 /* View headers only */
456 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
457 message->nhdr ? "yes" : "no",
458 message->author, message->type,
460 if (!IsEmptyStr(message->subject)) {
461 pprintf("subj=%s\n", message->subject);
463 if (!IsEmptyStr(message->email)) {
464 pprintf("rfca=%s\n", message->email);
466 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
467 message->hnod, message->room,
469 asctime(localtime(&message->time)));
470 if (!IsEmptyStr(message->recipient)) {
471 pprintf("rcpt=%s\n", message->recipient);
473 if (message->attachments) {
476 for (ptr = message->attachments; ptr; ptr = ptr->next) {
477 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
478 ptr->name, ptr->filename, ptr->number,
479 ptr->disposition, ptr->mimetype,
486 free_parts(message->attachments);
491 if (rc_display_message_numbers) {
493 fprintf(dest, "[#%s] ", message->msgid);
498 scr_printf("#%s", message->msgid);
503 if (nhdr == 1 && !is_room_aide) {
505 fprintf(dest, " ****");
510 fmt_date(now, sizeof now, message->time, 0);
512 fprintf(dest, "%s from %s ", now, message->author);
513 if (!IsEmptyStr(message->email)) {
514 fprintf(dest, "<%s> ", message->email);
518 scr_printf("%s ", now);
522 scr_printf("%s ", message->author);
523 if (!IsEmptyStr(message->email)) {
527 scr_printf("%s", message->email);
532 if (!IsEmptyStr(message->node)) {
533 if ((room_flags & QR_NETWORK)
534 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
535 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
536 if (IsEmptyStr(message->email)) {
538 fprintf(dest, "@%s ", message->node);
542 color(BRIGHT_YELLOW);
543 scr_printf("%s ", message->node);
548 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
549 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
551 fprintf(dest, "(%s) ", message->hnod);
556 scr_printf("%s", message->hnod);
561 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
563 fprintf(dest, "in %s> ", message->room);
567 color(BRIGHT_MAGENTA);
568 scr_printf("%s> ", message->room);
571 if (!IsEmptyStr(message->recipient)) {
573 fprintf(dest, "to %s ", message->recipient);
578 scr_printf("%s ", message->recipient);
589 /* Set the reply-to address to an Internet e-mail address if possible
591 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
592 if (!IsEmptyStr(message->author)) {
593 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
596 safestrncpy(reply_to, message->email, sizeof reply_to);
600 /* But if we can't do that, set it to a Citadel address.
602 if (!strcmp(reply_to, NO_REPLY_TO)) {
603 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
604 message->author, message->node);
609 lines_printed = checkpagin(lines_printed, pagin, screenheight);
613 if (message->msgid != NULL) {
614 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
617 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
618 safestrncpy(reply_references, message->references, sizeof reply_references);
621 if (message->subject != NULL) {
622 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
623 if (!IsEmptyStr(message->subject)) {
625 fprintf(dest, "Subject: %s\n",
629 scr_printf("Subject: ");
631 scr_printf("%s\n", message->subject);
633 lines_printed = checkpagin(lines_printed,
634 pagin, screenheight);
639 if (pagin == 1 && !dest) {
643 /******* end of header output, start of message text output *******/
646 * Convert HTML to plain text, formatting for the actual width
647 * of the client screen.
649 if (!strcasecmp(message->content_type, "text/html")) {
650 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
651 if (converted_text != NULL) {
653 message->text = converted_text;
658 /* Text/plain is a different type */
659 if (!strcasecmp(message->content_type, "text/plain")) {
664 num_urls = 0; /* Start with a clean slate */
665 searchptr = message->text;
666 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
667 searchptr = strstr(searchptr, "http://");
668 if (searchptr != NULL) {
669 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
670 for (i = 0; i < strlen(urls[num_urls]); i++) {
671 ch = urls[num_urls][i];
672 if (ch == '>' || ch == '\"' || ch == ')' ||
673 ch == ' ' || ch == '\n') {
674 urls[num_urls][i] = 0;
686 if (format_type == 0) {
687 fr = fmout(screenwidth, NULL, message->text, dest,
688 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
690 /* renderer for text/plain */
692 lineptr = message->text;
695 nextline = strchr(lineptr, '\n');
696 if (nextline != NULL) {
699 if (*nextline == 0) nextline = NULL;
702 if (sigcaught == 0) {
703 linelen = strlen(lineptr);
704 if (linelen && (lineptr[linelen-1] == '\r')) {
705 lineptr[--linelen] = 0;
708 fprintf(dest, "%s\n", lineptr);
710 scr_printf("%s\n", lineptr);
711 lines_printed = lines_printed + 1 +
712 (linelen / screenwidth);
714 checkpagin(lines_printed, pagin,
718 if (lineptr[0] == 0) final_line_is_blank = 1;
719 else final_line_is_blank = 0;
724 if (!final_line_is_blank) {
731 lines_printed = checkpagin(lines_printed, pagin, screenheight);
736 /* Enumerate any attachments */
737 if ( (pagin == 1) && (message->attachments) ) {
740 for (ptr = message->attachments; ptr; ptr = ptr->next) {
741 if ( (!strcasecmp(ptr->disposition, "attachment"))
742 || (!strcasecmp(ptr->disposition, "inline"))
743 || (!strcasecmp(ptr->disposition, ""))
745 if ( (strcasecmp(ptr->number, message->mime_chosen))
746 && (!IsEmptyStr(ptr->mimetype))
750 color(BRIGHT_MAGENTA);
751 pprintf("%s", ptr->number);
755 pprintf("%s", ptr->filename);
757 pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
758 if (!strncmp(ptr->mimetype, "image/", 6)) {
766 /* Save the attachments info for later */
767 last_message_parts = message->attachments;
773 if (pagin == 1 && !dest)
780 * replace string function for the built-in editor
782 void replace_string(char *filename, long int startpos)
791 int substitutions = 0;
795 scr_printf("Enter text to be replaced:\n: ");
796 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
797 if (IsEmptyStr(srch_str)) {
801 scr_printf("Enter text to replace it with:\n: ");
802 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
804 fp = fopen(filename, "r+");
810 fseek(fp, startpos, 0);
812 while (a = getc(fp), a > 0) {
814 buf[strlen(buf) + 1] = 0;
815 buf[strlen(buf)] = a;
816 if (strlen(buf) >= strlen(srch_str)) {
817 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
818 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
819 strcpy(ptr, rplc_str);
823 if (strlen(buf) > 384) {
826 rv = fwrite((char *) buf, 128, 1, fp);
827 strcpy(buf, &buf[128]);
833 if (!IsEmptyStr(buf)) {
834 rv = fwrite((char *) buf, strlen(buf), 1, fp);
838 rv = truncate(filename, wpos);
839 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
843 * Function to begin composing a new message
845 int client_make_message(CtdlIPC *ipc,
846 char *filename, /* temporary file name */
847 char *recipient, /* NULL if it's not mail */
851 char *subject, /* buffer to store subject line */
859 char *editor_path = NULL;
864 if ((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2]))
866 editor_path = editor_paths[mode-2];
867 } else if (!IsEmptyStr(editor_paths[0]))
869 editor_path = editor_paths[0];
871 err_printf("*** No editor available; using built-in editor.\n");
876 fmt_date(datestr, sizeof datestr, time(NULL), 0);
879 if (room_flags & QR_ANONONLY && !recipient) {
880 snprintf(header, sizeof header, " ****");
883 snprintf(header, sizeof header,
886 (is_anonymous ? "[anonymous]" : fullname)
888 if (!IsEmptyStr(recipient)) {
889 size_t tmp = strlen(header);
890 snprintf(&header[tmp], sizeof header - tmp,
891 " to %s", recipient);
894 scr_printf("%s\n", header);
895 if (subject != NULL) if (!IsEmptyStr(subject)) {
896 scr_printf("Subject: %s\n", subject);
899 if ( (subject_required) && (IsEmptyStr(subject)) ) {
900 newprompt("Subject: ", subject, 70);
906 scr_printf("(Press ctrl-d when finished)\n");
910 fp = fopen(filename, "r");
912 fmout(screenwidth, fp, NULL, NULL, 0,
917 fp = fopen(filename, "w");
919 err_printf("*** Error opening temp file!\n %s: %s\n",
920 filename, strerror(errno)
931 fp = fopen(filename, "r+");
933 err_printf("*** Error opening temp file!\n %s: %s\n",
934 filename, strerror(errno)
943 fp = fopen(filename, "a");
945 err_printf("*** Error opening temp file!\n"
947 filename, strerror(errno));
967 default: /* allow 2+ modes */
968 e_ex_code = 1; /* start with a failed exit code */
970 stty_ctdl(SB_RESTORE);
972 cksum = file_checksum(filename);
973 if (editor_pid == 0) {
976 chmod(filename, 0600);
977 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
979 execlp(editor_path, editor_path, filename, NULL);
985 b = ka_wait(&e_ex_code);
986 } while ((b != editor_pid) && (b >= 0));
993 MECR: if (mode >= 2) {
994 if (file_checksum(filename) == cksum) {
995 err_printf("*** Aborted message.\n");
998 if (e_ex_code == 0) {
1004 b = keymenu("Entry command (? for options)",
1008 "<P>rint formatted|"
1014 if (b == 'a') goto MEABT;
1015 if (b == 'c') goto ME1;
1016 if (b == 's') goto MEFIN;
1018 scr_printf(" %s from %s", datestr, fullname);
1019 if (!IsEmptyStr(recipient)) {
1020 scr_printf(" to %s", recipient);
1023 if (subject != NULL) if (!IsEmptyStr(subject)) {
1024 scr_printf("Subject: %s\n", subject);
1026 fp = fopen(filename, "r");
1028 fmout(screenwidth, fp, NULL, NULL,
1029 ((userflags & US_PAGINATOR) ? 1 : 0),
1030 screenheight, 0, 0);
1037 replace_string(filename, 0L);
1044 if (subject != NULL) {
1045 newprompt("Subject: ", subject, 70);
1052 MEABT: scr_printf("Are you sure? ");
1056 MEABT2: unlink(filename);
1062 * Make sure there's room in msg_arr[] for at least one more.
1064 void check_msg_arr_size(void) {
1065 if ((num_msgs + 1) > msg_arr_size) {
1066 msg_arr_size += 512;
1067 msg_arr = realloc(msg_arr,
1068 ((sizeof(long)) * msg_arr_size) );
1074 * break_big_lines() - break up lines that are >1024 characters
1075 * otherwise the server will truncate
1077 void break_big_lines(char *msg) {
1086 while (strlen(ptr) > 1000) {
1087 break_here = strchr(&ptr[900], ' ');
1088 if ((break_here == NULL) || (break_here > &ptr[999])) {
1089 break_here = &ptr[999];
1098 * entmsg() - edit and create a message
1099 * returns 0 if message was saved
1101 int entmsg(CtdlIPC *ipc,
1102 int is_reply, /* nonzero if this was a <R>eply command */
1104 int masquerade /* prompt for a non-default display name? */
1113 struct ctdlipcmessage message;
1114 unsigned long *msgarr = NULL;
1115 int r; /* IPC response code */
1116 int subject_required = 0;
1119 scr_printf("You may not enter messages in this type of room.\n");
1130 strcpy(subject, "");
1133 * First, check to see if we have permission to enter a message in
1134 * this room. The server will return an error code if we can't.
1136 strcpy(message.recipient, "");
1137 strcpy(message.author, "");
1138 strcpy(message.subject, "");
1139 strcpy(message.references, "");
1140 message.text = ""; /* point to "", changes later */
1141 message.anonymous = 0;
1142 message.type = mode;
1145 newprompt("Display name for this message: ", message.author, 40);
1148 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1150 if (r / 100 != 2 && r / 10 != 57) {
1151 scr_printf("%s\n", buf);
1155 /* Error code 570 is special. It means that we CAN enter a message
1156 * in this room, but a recipient needs to be specified.
1163 /* If the user is a dumbass, tell them how to type. */
1164 if ((userflags & US_EXPERT) == 0) {
1165 formout(ipc, "entermsg");
1168 /* Handle the selection of a recipient, if necessary. */
1170 if (need_recp == 1) {
1171 if (axlevel >= AxProbU) {
1173 strcpy(buf, reply_to);
1175 scr_printf("Enter recipient: ");
1176 ctdl_getline(buf, (SIZ-100) );
1177 if (IsEmptyStr(buf))
1181 strcpy(buf, "sysop");
1183 strcpy(message.recipient, buf);
1187 if (!IsEmptyStr(reply_subject)) {
1188 if (!strncasecmp(reply_subject,
1190 strcpy(message.subject, reply_subject);
1193 snprintf(message.subject,
1194 sizeof message.subject,
1200 /* Trim down excessively long lists of thread references. We eliminate the
1201 * second one in the list so that the thread root remains intact.
1203 int rrtok = num_tokens(reply_references, '|');
1204 int rrlen = strlen(reply_references);
1205 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1206 remove_token(reply_references, 1, '|');
1209 snprintf(message.references, sizeof message.references, "%s%s%s",
1211 (IsEmptyStr(reply_references) ? "" : "|"),
1216 if (room_flags & QR_ANONOPT) {
1217 scr_printf("Anonymous (Y/N)? ");
1219 message.anonymous = 1;
1222 /* If it's mail, we've got to check the validity of the recipient... */
1223 if (!IsEmptyStr(message.recipient)) {
1224 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1226 scr_printf("%s\n", buf);
1231 /* Learn the number of the newest message in in the room, so we can
1232 * tell upon saving whether someone else has posted too.
1235 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1237 scr_printf("%s\n", buf);
1239 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1243 /* Now compose the message... */
1244 if (client_make_message(ipc, temp, message.recipient,
1245 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1246 if (msgarr) free(msgarr);
1250 /* Reopen the temp file that was created, so we can send it */
1251 fp = fopen(temp, "r");
1253 if (!fp || !(message.text = load_message_from_file(fp))) {
1254 err_printf("*** Internal error while trying to save message!\n"
1256 temp, strerror(errno));
1263 /* Break lines that are >1024 characters, otherwise the server
1264 * will truncate them.
1266 break_big_lines(message.text);
1268 /* Transmit message to the server */
1269 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1271 scr_printf("%s\n", buf);
1275 /* Yes, unlink it now, so it doesn't stick around if we crash */
1278 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1280 if (msgarr) free(msgarr);
1282 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1284 scr_printf("%s\n", buf);
1286 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1290 /* get new highest message number in room to set lrp for goto... */
1291 maxmsgnum = msgarr[num_msgs - 1];
1293 /* now see if anyone else has posted in here */
1295 for (a = 0; a < num_msgs; ++a) {
1296 if (msgarr[a] > highmsg) {
1300 if (msgarr) free(msgarr);
1303 /* In the Mail> room, this algorithm always counts one message
1304 * higher than in public rooms, so we decrement it by one.
1311 scr_printf("*** 1 additional message has been entered "
1312 "in this room by another user.\n");
1315 scr_printf("*** %d additional messages have been entered "
1316 "in this room by other users.\n", b);
1324 * Do editing on a quoted file
1326 void process_quote(void)
1328 FILE *qfile, *tfile;
1330 int line, qstart, qend;
1332 /* Unlink the second temp file as soon as it's opened, so it'll get
1333 * deleted even if the program dies
1335 qfile = fopen(temp2, "r");
1338 /* Display the quotable text with line numbers added */
1340 if (fgets(buf, 128, qfile) == NULL) {
1341 /* we're skipping a line here */
1343 while (fgets(buf, 128, qfile) != NULL) {
1344 scr_printf("%3d %s", ++line, buf);
1346 scr_printf("Begin quoting at [1] : ");
1347 ctdl_getline(buf, 4);
1348 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1349 scr_printf(" End quoting at [%d] : ", line);
1350 ctdl_getline(buf, 4);
1351 qend = (buf[0] == 0) ? (line) : atoi(buf);
1354 if (fgets(buf, 128, qfile) == NULL) {
1355 /* we're skipping a line here */
1357 tfile = fopen(temp, "w");
1358 while (fgets(buf, 128, qfile) != NULL) {
1359 if ((++line >= qstart) && (line <= qend))
1360 fprintf(tfile, " >%s", buf);
1362 fprintf(tfile, " \n");
1371 * List the URL's which were embedded in the previous message
1373 void list_urls(CtdlIPC *ipc)
1379 if (num_urls == 0) {
1380 scr_printf("There were no URL's in the previous message.\n\n");
1384 for (i = 0; i < num_urls; ++i) {
1385 scr_printf("%3d %s\n", i + 1, urls[i]);
1388 if ((i = num_urls) != 1)
1389 i = intprompt("Display which one", 1, 1, num_urls);
1391 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1398 * Run image viewer in background
1400 int do_image_view(const char *filename)
1405 snprintf(cmd, sizeof cmd, imagecmd, filename);
1412 if (childpid == 0) {
1414 pid_t grandchildpid;
1416 grandchildpid = fork();
1417 if (grandchildpid < 0) {
1418 return grandchildpid;
1421 if (grandchildpid == 0) {
1426 nullfd = open("/dev/null", O_WRONLY);
1433 retcode = system(cmd);
1443 if (grandchildpid > 0) {
1451 waitpid(childpid, &retcode, 0);
1460 * View an image attached to a message
1462 void image_view(CtdlIPC *ipc, unsigned long msg)
1464 struct parts *ptr = last_message_parts;
1468 /* Run through available parts */
1469 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1470 if ((!strcasecmp(ptr->disposition, "attachment")
1471 || !strcasecmp(ptr->disposition, "inline"))
1472 && !strncmp(ptr->mimetype, "image/", 6)) {
1475 strcpy(part, ptr->number);
1482 strprompt("View which part (0 when done)", part, SIZ-1);
1484 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1485 if ((!strcasecmp(ptr->disposition, "attachment")
1486 || !strcasecmp(ptr->disposition, "inline"))
1487 && !strncmp(ptr->mimetype, "image/", 6)
1488 && !strcasecmp(ptr->number, part)) {
1491 void *file = NULL; /* The downloaded file */
1496 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1498 scr_printf("%s\n", buf);
1502 len = (size_t)extract_long(buf, 0);
1503 progress(ipc, len, len);
1505 CtdlMakeTempFileName(tmp, sizeof tmp);
1506 strcat(tmp, ptr->filename);
1507 save_buffer(file, len, tmp);
1521 * Read the messages in the current room
1523 void readmsgs(CtdlIPC *ipc,
1524 enum MessageList c, /* see listing in citadel_ipc.h */
1525 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1526 int q /* Number of msgs to read (if c==3) */
1528 int a, b, e, f, g, start;
1534 char prtfile[PATH_MAX];
1537 char targ[ROOMNAMELEN];
1538 char filename[PATH_MAX];
1539 char save_to[PATH_MAX];
1540 void *attachment = NULL; /* Downloaded attachment */
1541 FILE *dest = NULL; /* Alternate destination other than screen */
1542 int r; /* IPC response code */
1543 static int att_seq = 0; /* Attachment download sequence number */
1544 int rv = 0; /* silence the stupid warn_unused_result warnings */
1551 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1557 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1559 scr_printf("%s\n", cmd);
1561 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1565 if (num_msgs == 0) { /* TODO look at this later */
1566 if (c == LastMessages) return;
1567 scr_printf("*** There are no ");
1568 if (c == NewMessages) scr_printf("new ");
1569 if (c == OldMessages) scr_printf("old ");
1570 scr_printf("messages in this room.\n");
1576 /* this loop cycles through each message... */
1577 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1578 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1579 while (msg_arr[a] == 0L) {
1581 if ((a == num_msgs) || (a == (-1)))
1585 RAGAIN: pagin = ((arcflag == 0)
1587 && (userflags & US_PAGINATOR)) ? 1 : 0;
1589 /* If we're doing a quote, set the screenwidth to 72 */
1591 hold_sw = screenwidth;
1595 /* If printing or archiving, set the screenwidth to 80 */
1597 hold_sw = screenwidth;
1601 /* clear parts list */
1602 free_parts(last_message_parts);
1603 last_message_parts = NULL;
1605 /* now read the message... */
1606 e = read_message(ipc, msg_arr[a], pagin, dest);
1608 /* ...and set the screenwidth back if we have to */
1609 if ((quotflag) || (arcflag)) {
1610 screenwidth = hold_sw;
1612 RMSGREAD: scr_flush();
1613 highest_msg_read = msg_arr[a];
1618 enable_color = hold_color;
1627 enable_color = hold_color;
1630 if (freopen(prtfile, "r", stdin) == NULL) {
1631 /* we probably should handle the error condition here */
1634 stty_ctdl(SB_RESTORE);
1635 ka_system(printcmd);
1636 stty_ctdl(SB_NO_INTR);
1644 } while ((g != f) && (g >= 0));
1645 scr_printf("Message printed.\n");
1647 if (rc_alt_semantics && c == 1) {
1650 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1654 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1655 && (((room_flags & QR_MAILBOX) == 0)
1656 || (rc_force_mail_prompts == 0))) {
1661 color(BRIGHT_WHITE);
1662 scr_printf("%d", num_msgs - a - 1);
1666 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1667 if (rc_url_cmd[0] && num_urls)
1668 keyopt("<U>RLview ");
1669 if (has_images > 0 && !IsEmptyStr(imagecmd))
1670 keyopt("<I>mages ");
1671 keyopt("<?>help -> ");
1675 e = (inkey() & 127);
1677 /* return key same as <N> */ if (e == 10)
1679 /* space key same as <N> */ if (e == 32)
1681 /* del/move for aides only */
1682 if ( (!is_room_aide)
1683 && ((room_flags & QR_MAILBOX) == 0)
1684 && ((room_flags2 & QR2_COLLABDEL) == 0)
1686 if ((e == 'd') || (e == 'm'))
1689 /* print only if available */
1690 if ((e == 'p') && (IsEmptyStr(printcmd)))
1692 /* can't file if not allowed */
1694 && (rc_allow_attachments == 0))
1696 /* link only if browser avail*/
1698 && (IsEmptyStr(rc_url_cmd)))
1701 && (IsEmptyStr(imagecmd) || !has_images))
1703 } while ((e != 'a') && (e != 'n') && (e != 's')
1704 && (e != 'd') && (e != 'm') && (e != 'p')
1705 && (e != 'q') && (e != 'b') && (e != 'h')
1706 && (e != 'r') && (e != 'f') && (e != '?')
1707 && (e != 'u') && (e != 'c') && (e != 'y')
1708 && (e != 'i') && (e != 'o') );
1714 scr_printf("Again");
1717 scr_printf("Delete");
1729 scr_printf("Print");
1732 scr_printf("reply Quoted");
1738 scr_printf("Header");
1741 scr_printf("Reply");
1744 scr_printf("Open attachments");
1750 scr_printf("URL's");
1753 scr_printf("mY next");
1758 scr_printf("? <help>");
1761 if (userflags & US_DISAPPEAR || e == 'i')
1762 scr_printf("\r%79s\r", "");
1767 DONE_QUOTING: switch (e) {
1769 scr_printf("Options available here:\n"
1770 " ? Help (prints this message)\n"
1771 " S Stop reading immediately\n"
1772 " A Again (repeats last message)\n"
1773 " N Next (continue with next message)\n"
1774 " Y My Next (continue with next message you authored)\n"
1775 " B Back (go back to previous message)\n");
1777 || (room_flags & QR_MAILBOX)
1778 || (room_flags2 & QR2_COLLABDEL)
1780 scr_printf(" D Delete this message\n"
1781 " M Move message to another room\n");
1783 scr_printf(" C Copy message to another room\n");
1784 if (!IsEmptyStr(printcmd))
1785 scr_printf(" P Print this message\n");
1787 " Q Reply to this message, quoting portions of it\n"
1788 " H Headers (display message headers only)\n");
1790 scr_printf(" R Reply to this message\n");
1791 if (rc_allow_attachments) {
1792 scr_printf(" O (Open attachments)\n");
1793 scr_printf(" F (save attachments to a File)\n");
1795 if (!IsEmptyStr(rc_url_cmd))
1796 scr_printf(" U (list URL's for display)\n");
1797 if (!IsEmptyStr(imagecmd) && has_images > 0)
1798 scr_printf(" I Image viewer\n");
1803 dest = fopen(prtfile, "w");
1805 hold_color = enable_color;
1810 dest = fopen(temp2, "w");
1812 hold_color = enable_color;
1824 newprompt("Enter target room: ",
1825 targ, ROOMNAMELEN - 1);
1826 if (!IsEmptyStr(targ)) {
1827 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1828 msg_arr[a], targ, cmd);
1829 scr_printf("%s\n", cmd);
1835 if (r / 100 != 2) /* r will be init'ed, FIXME */
1836 goto RMSGREAD; /* the logic here sucks */
1840 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1841 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1842 filename, &attachment, progress, cmd);
1844 scr_printf("%s\n", cmd);
1846 extract_token(filename, cmd, 2, '|', sizeof filename);
1848 * Part 1 won't have a filename; use the
1849 * subject of the message instead. IO
1851 if (IsEmptyStr(filename)) {
1852 strcpy(filename, reply_subject);
1854 if (e == 'o') { /* open attachment */
1855 mkdir(tempdir, 0700);
1856 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1860 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1861 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1864 else { /* save attachment to disk */
1865 destination_directory(save_to, filename);
1866 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1875 scr_printf("*** Delete this message? ");
1877 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1878 scr_printf("%s\n", cmd);
1886 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1889 savedpos = num_msgs;
1890 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1891 num_msgs = savedpos;
1897 image_view(ipc, msg_arr[a]);
1900 { /* hack hack hack */
1901 /* find the next message by me, stay here if we find nothing */
1904 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1906 /* This is repetitively dumb, but that's what computers are for.
1907 We have to load up messages until we find one by us */
1910 struct ctdlipcmessage *msg = NULL;
1912 /* read the header so we can get 'from=' */
1913 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1914 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1915 a = lasta; /* meesa current */
1923 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1927 } /* end for loop */
1928 } /* end read routine */
1934 * View and edit a system message
1936 void edit_system_message(CtdlIPC *ipc, char *which_message)
1940 char write_cmd[SIZ];
1942 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1943 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1944 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1945 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1952 * Verify the message base
1954 void check_message_base(CtdlIPC *ipc)
1957 char *transcript = NULL;
1958 int r; /* IPC response code */
1961 ("Please read the documentation before running this command.\n"
1962 "Having done so, do you still want to check the message base? ");
1966 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1968 scr_printf("%s\n", buf);
1972 while (transcript && !IsEmptyStr(transcript)) {
1974 extract_token(buf, transcript, 0, '\n', sizeof buf);
1975 remove_token(transcript, 0, '\n');
1976 pprintf("%s\n", buf);
1978 if (transcript) free(transcript);
1984 * Loads the contents of a file into memory. Caller must free the allocated
1987 char *load_message_from_file(FILE *src)
1993 fseek(src, 0, SEEK_END);
1997 dest = (char *)calloc(1, i + 1);
2004 g = fread(dest + got, 1, i - got, src);
2007 /* Interrupted system call, keep going */
2010 /* At this point we have either EOF or error */