4 * Citadel message support routines
5 * see copyright.txt for copyright information
11 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
38 #include <libcitadel.h>
40 #include "citadel_ipc.h"
41 #include "citadel_decls.h"
50 #define MAXWORDBUF SIZ
51 #define NO_REPLY_TO "nobody ... xxxxxx"
54 char reply_subject[SIZ];
55 char reply_references[SIZ];
56 char reply_inreplyto[SIZ];
60 char text[MAXWORDBUF];
63 void stty_ctdl(int cmd);
64 int haschar(const char *st, int ch);
65 void ctdl_getline(char *string, int lim);
66 int file_checksum(char *filename);
67 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
69 unsigned long *msg_arr = NULL;
72 char rc_alt_semantics;
73 extern char room_name[];
74 extern char tempdir[];
75 extern unsigned room_flags;
76 extern unsigned room_flags2;
77 extern long highest_msg_read;
80 extern int screenwidth;
81 extern int screenheight;
82 extern long maxmsgnum;
85 extern char is_room_aide;
86 extern char fullname[];
88 extern unsigned userflags;
89 extern char sigcaught;
90 extern char printcmd[];
91 extern int rc_allow_attachments;
92 extern int rc_display_message_numbers;
93 extern int rc_force_mail_prompts;
94 extern int editor_pid;
95 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
97 char urls[MAXURLS][SIZ];
99 int has_images = 0; /* Current msg has images */
100 struct parts *last_message_parts = NULL; /* Parts from last msg */
104 void ka_sigcatch(int signum)
107 signal(SIGALRM, ka_sigcatch);
108 CtdlIPCNoop(ipc_for_signal_handlers);
113 * server keep-alive version of wait() (needed for external editor)
115 pid_t ka_wait(int *kstatus)
120 signal(SIGALRM, ka_sigcatch);
124 } while (errno == EINTR);
125 signal(SIGALRM, SIG_IGN);
132 * version of system() that uses ka_wait()
134 int ka_system(char *shc)
143 perror("Cannot fork");
145 return ((pid_t) childpid);
149 execlp("/bin/sh", "sh", "-c", shc, NULL);
155 waitpid = ka_wait(&retcode);
156 } while (waitpid != childpid);
166 * add a newline to the buffer...
168 void add_newline(struct cittext *textlist)
173 while (ptr->next != NULL)
176 while (ptr->text[strlen(ptr->text) - 1] == 32)
177 ptr->text[strlen(ptr->text) - 1] = 0;
178 /* strcat(ptr->text,"\n"); */
180 ptr->next = (struct cittext *)
181 malloc(sizeof(struct cittext));
184 strcpy(ptr->text, "");
189 * add a word to the buffer...
191 void add_word(struct cittext *textlist, char *wordbuf)
196 while (ptr->next != NULL)
199 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
200 ptr->next = (struct cittext *)
201 malloc(sizeof(struct cittext));
204 strcpy(ptr->text, "");
207 strcat(ptr->text, wordbuf);
208 strcat(ptr->text, " ");
213 * begin editing of an opened file pointed to by fp
215 void citedit(CtdlIPC *ipc, FILE * fp)
217 int a, prev, finished, b, last_space;
219 struct cittext *textlist = NULL;
221 char wordbuf[MAXWORDBUF];
223 /* first, load the text into the buffer */
225 textlist = (struct cittext *) malloc(sizeof(struct cittext));
226 textlist->next = NULL;
227 strcpy(textlist->text, "");
231 while (a = getc(fp), a >= 0) {
233 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
234 add_word(textlist, wordbuf);
236 if ((prev == 13) || (prev == 10)) {
237 add_word(textlist, "\n");
238 add_newline(textlist);
239 add_word(textlist, "");
242 wordbuf[strlen(wordbuf) + 1] = 0;
243 wordbuf[strlen(wordbuf)] = a;
245 if (strlen(wordbuf) + 3 > screenwidth) {
246 add_word(textlist, wordbuf);
254 prev = (appending ? 13 : (-1));
266 if ((a != 32) && (prev == 13)) {
267 add_word(textlist, "\n");
271 if ((a == 32) && (prev == 13)) {
272 add_word(textlist, "\n");
273 add_newline(textlist);
277 if (!IsEmptyStr(wordbuf)) {
278 wordbuf[strlen(wordbuf) - 1] = 0;
283 } else if (a == 23) {
285 wordbuf[strlen(wordbuf) - 1] = 0;
289 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
290 } else if (a == 13) {
292 if (IsEmptyStr(wordbuf))
295 for (b = 0; b < strlen(wordbuf); ++b)
296 if (wordbuf[b] == 32) {
304 add_word(textlist, wordbuf);
309 wordbuf[strlen(wordbuf) + 1] = 0;
310 wordbuf[strlen(wordbuf)] = a;
312 if ((strlen(wordbuf) + 3) > screenwidth) {
314 for (b = 0; b < strlen(wordbuf); ++b)
315 if (wordbuf[b] == 32)
317 if (last_space >= 0) {
318 for (b = 0; b < strlen(wordbuf); ++b)
319 if (wordbuf[b] == 32) {
327 for (b = 0; b < strlen(wordbuf); ++b) {
332 scr_printf("\n%s", wordbuf);
334 add_word(textlist, wordbuf);
340 } while (finished == 0);
343 /* write the buffer back to disk */
345 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
346 fprintf(fp, "%s", ptr->text);
350 ftruncate(fileno(fp), ftell(fp));
352 /* and deallocate the memory we used */
353 while (textlist != NULL) {
354 ptr = textlist->next;
362 * Free the struct parts
364 void free_parts(struct parts *p)
366 struct parts *a_part = p;
372 a_part = a_part->next;
379 * Read a message from the server
381 int read_message(CtdlIPC *ipc,
382 long num, /* message number */
383 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
384 FILE *dest) /* Destination file, NULL for screen */
391 struct ctdlipcmessage *message = NULL;
392 int r; /* IPC response code */
393 char *converted_text = NULL;
400 int final_line_is_blank = 0;
407 strcpy(reply_to, NO_REPLY_TO);
408 strcpy(reply_subject, "");
409 strcpy(reply_references, "");
410 strcpy(reply_inreplyto, "");
412 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
414 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
416 lines_printed = checkpagin(lines_printed, pagin, screenheight);
419 free_parts(message->attachments);
425 fprintf(dest, "\n ");
429 lines_printed = checkpagin(lines_printed, pagin, screenheight);
433 if (pagin == 1 && !dest) {
437 /* View headers only */
439 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
440 message->nhdr ? "yes" : "no",
441 message->author, message->type,
443 if (!IsEmptyStr(message->subject)) {
444 pprintf("subj=%s\n", message->subject);
446 if (!IsEmptyStr(message->email)) {
447 pprintf("rfca=%s\n", message->email);
449 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
450 message->hnod, message->room,
452 asctime(localtime(&message->time)));
453 if (!IsEmptyStr(message->recipient)) {
454 pprintf("rcpt=%s\n", message->recipient);
456 if (message->attachments) {
459 for (ptr = message->attachments; ptr; ptr = ptr->next) {
460 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
461 ptr->name, ptr->filename, ptr->number,
462 ptr->disposition, ptr->mimetype,
469 free_parts(message->attachments);
474 if (rc_display_message_numbers) {
476 fprintf(dest, "[#%s] ", message->msgid);
481 scr_printf("#%s", message->msgid);
486 if (nhdr == 1 && !is_room_aide) {
488 fprintf(dest, " ****");
493 fmt_date(now, sizeof now, message->time, 0);
495 fprintf(dest, "%s from %s ", now, message->author);
496 if (!IsEmptyStr(message->email)) {
497 fprintf(dest, "<%s> ", message->email);
501 scr_printf("%s ", now);
505 scr_printf("%s ", message->author);
506 if (!IsEmptyStr(message->email)) {
510 scr_printf("%s", message->email);
515 if (!IsEmptyStr(message->node)) {
516 if ((room_flags & QR_NETWORK)
517 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
518 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
519 if (IsEmptyStr(message->email)) {
521 fprintf(dest, "@%s ", message->node);
525 color(BRIGHT_YELLOW);
526 scr_printf("%s ", message->node);
531 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
532 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
534 fprintf(dest, "(%s) ", message->hnod);
539 scr_printf("%s", message->hnod);
544 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
546 fprintf(dest, "in %s> ", message->room);
550 color(BRIGHT_MAGENTA);
551 scr_printf("%s> ", message->room);
554 if (!IsEmptyStr(message->recipient)) {
556 fprintf(dest, "to %s ", message->recipient);
561 scr_printf("%s ", message->recipient);
572 /* Set the reply-to address to an Internet e-mail address if possible
574 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
575 if (!IsEmptyStr(message->author)) {
576 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
579 safestrncpy(reply_to, message->email, sizeof reply_to);
583 /* But if we can't do that, set it to a Citadel address.
585 if (!strcmp(reply_to, NO_REPLY_TO)) {
586 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
587 message->author, message->node);
592 lines_printed = checkpagin(lines_printed, pagin, screenheight);
596 if (message->msgid != NULL) {
597 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
600 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
601 safestrncpy(reply_references, message->references, sizeof reply_references);
604 if (message->subject != NULL) {
605 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
606 if (!IsEmptyStr(message->subject)) {
608 fprintf(dest, "Subject: %s\n",
612 scr_printf("Subject: ");
614 scr_printf("%s\n", message->subject);
616 lines_printed = checkpagin(lines_printed,
617 pagin, screenheight);
622 if (pagin == 1 && !dest) {
626 /******* end of header output, start of message text output *******/
629 * Convert HTML to plain text, formatting for the actual width
630 * of the client screen.
632 if (!strcasecmp(message->content_type, "text/html")) {
633 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
634 if (converted_text != NULL) {
636 message->text = converted_text;
641 /* Text/plain is a different type */
642 if (!strcasecmp(message->content_type, "text/plain")) {
647 num_urls = 0; /* Start with a clean slate */
648 searchptr = message->text;
649 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
650 searchptr = strstr(searchptr, "http://");
651 if (searchptr != NULL) {
652 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
653 for (i = 0; i < strlen(urls[num_urls]); i++) {
654 ch = urls[num_urls][i];
655 if (ch == '>' || ch == '\"' || ch == ')' ||
656 ch == ' ' || ch == '\n') {
657 urls[num_urls][i] = 0;
669 if (format_type == 0) {
670 fr = fmout(screenwidth, NULL, message->text, dest,
671 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
673 /* renderer for text/plain */
675 lineptr = message->text;
678 nextline = strchr(lineptr, '\n');
679 if (nextline != NULL) {
682 if (*nextline == 0) nextline = NULL;
685 if (sigcaught == 0) {
686 linelen = strlen(lineptr);
687 if (linelen && (lineptr[linelen-1] == '\r')) {
688 lineptr[--linelen] = 0;
691 fprintf(dest, "%s\n", lineptr);
693 scr_printf("%s\n", lineptr);
694 lines_printed = lines_printed + 1 +
695 (linelen / screenwidth);
697 checkpagin(lines_printed, pagin,
701 if (lineptr[0] == 0) final_line_is_blank = 1;
702 else final_line_is_blank = 0;
707 if (!final_line_is_blank) {
714 lines_printed = checkpagin(lines_printed, pagin, screenheight);
719 /* Enumerate any attachments */
720 if ( (pagin == 1) && (message->attachments) ) {
723 for (ptr = message->attachments; ptr; ptr = ptr->next) {
724 if ( (!strcasecmp(ptr->disposition, "attachment"))
725 || (!strcasecmp(ptr->disposition, "inline"))
726 || (!strcasecmp(ptr->disposition, ""))
728 if ( (strcasecmp(ptr->number, message->mime_chosen))
729 && (!IsEmptyStr(ptr->mimetype))
733 color(BRIGHT_MAGENTA);
734 pprintf("%s", ptr->number);
738 pprintf("%s", ptr->filename);
740 pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
741 if (!strncmp(ptr->mimetype, "image/", 6)) {
749 /* Save the attachments info for later */
750 last_message_parts = message->attachments;
756 if (pagin == 1 && !dest)
763 * replace string function for the built-in editor
765 void replace_string(char *filename, long int startpos)
774 int substitutions = 0;
777 scr_printf("Enter text to be replaced:\n: ");
778 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
779 if (IsEmptyStr(srch_str))
782 scr_printf("Enter text to replace it with:\n: ");
783 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
785 fp = fopen(filename, "r+");
790 fseek(fp, startpos, 0);
792 while (a = getc(fp), a > 0) {
794 buf[strlen(buf) + 1] = 0;
795 buf[strlen(buf)] = a;
796 if (strlen(buf) >= strlen(srch_str)) {
797 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
798 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
799 strcpy(ptr, rplc_str);
803 if (strlen(buf) > 384) {
806 fwrite((char *) buf, 128, 1, fp);
807 strcpy(buf, &buf[128]);
813 if (!IsEmptyStr(buf))
814 fwrite((char *) buf, strlen(buf), 1, fp);
817 truncate(filename, wpos);
818 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
822 * Function to begin composing a new message
824 int client_make_message(CtdlIPC *ipc,
825 char *filename, /* temporary file name */
826 char *recipient, /* NULL if it's not mail */
830 char *subject, /* buffer to store subject line */
831 int subject_required)
838 char *editor_path = NULL;
843 if((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2])) {
844 editor_path = editor_paths[mode-2];
845 } else if (!IsEmptyStr(editor_paths[0])) {
846 editor_path = editor_paths[0];
848 err_printf("*** No editor available, "
849 "using built-in editor\n");
854 fmt_date(datestr, sizeof datestr, time(NULL), 0);
857 if (room_flags & QR_ANONONLY && !recipient) {
858 snprintf(header, sizeof header, " ****");
861 snprintf(header, sizeof header,
864 (is_anonymous ? "[anonymous]" : fullname)
866 if (!IsEmptyStr(recipient)) {
867 size_t tmp = strlen(header);
868 snprintf(&header[tmp], sizeof header - tmp,
869 " to %s", recipient);
872 scr_printf("%s\n", header);
873 if (subject != NULL) if (!IsEmptyStr(subject)) {
874 scr_printf("Subject: %s\n", subject);
877 if ( (subject_required) && (IsEmptyStr(subject)) ) {
878 newprompt("Subject: ", subject, 70);
884 scr_printf("(Press ctrl-d when finished)\n");
888 fp = fopen(filename, "r");
890 fmout(screenwidth, fp, NULL, NULL, 0,
895 fp = fopen(filename, "w");
897 err_printf("*** Error opening temp file!\n"
899 filename, strerror(errno));
909 fp = fopen(filename, "r+");
911 err_printf("*** Error opening temp file!\n"
913 filename, strerror(errno));
921 fp = fopen(filename, "a");
923 err_printf("*** Error opening temp file!\n"
925 filename, strerror(errno));
945 default: /* allow 2+ modes */
946 e_ex_code = 1; /* start with a failed exit code */
948 stty_ctdl(SB_RESTORE);
950 cksum = file_checksum(filename);
951 if (editor_pid == 0) {
954 chmod(filename, 0600);
955 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
957 execlp(editor_path, editor_path, filename, NULL);
963 b = ka_wait(&e_ex_code);
964 } while ((b != editor_pid) && (b >= 0));
971 MECR: if (mode >= 2) {
972 if (file_checksum(filename) == cksum) {
973 err_printf("*** Aborted message.\n");
976 if (e_ex_code == 0) {
982 b = keymenu("Entry command (? for options)",
983 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
985 "<R>eplace string|<H>old message");
987 if (b == 'a') goto MEABT;
988 if (b == 'c') goto ME1;
989 if (b == 's') goto MEFIN;
991 scr_printf(" %s from %s", datestr, fullname);
992 if (!IsEmptyStr(recipient)) {
993 scr_printf(" to %s", recipient);
996 if (subject != NULL) if (!IsEmptyStr(subject)) {
997 scr_printf("Subject: %s\n", subject);
999 fp = fopen(filename, "r");
1001 fmout(screenwidth, fp, NULL, NULL,
1002 ((userflags & US_PAGINATOR) ? 1 : 0),
1003 screenheight, 0, 0);
1010 replace_string(filename, 0L);
1017 if (subject != NULL) {
1018 newprompt("Subject: ", subject, 70);
1025 MEABT: scr_printf("Are you sure? ");
1029 MEABT2: unlink(filename);
1035 * Make sure there's room in msg_arr[] for at least one more.
1037 void check_msg_arr_size(void) {
1038 if ((num_msgs + 1) > msg_arr_size) {
1039 msg_arr_size += 512;
1040 msg_arr = realloc(msg_arr,
1041 ((sizeof(long)) * msg_arr_size) );
1047 * break_big_lines() - break up lines that are >1024 characters
1048 * otherwise the server will truncate
1050 void break_big_lines(char *msg) {
1059 while (strlen(ptr) > 1000) {
1060 break_here = strchr(&ptr[900], ' ');
1061 if ((break_here == NULL) || (break_here > &ptr[999])) {
1062 break_here = &ptr[999];
1071 * entmsg() - edit and create a message
1072 * returns 0 if message was saved
1074 int entmsg(CtdlIPC *ipc,
1075 int is_reply, /* nonzero if this was a <R>eply command */
1077 int masquerade /* prompt for a non-default display name? */
1086 struct ctdlipcmessage message;
1087 unsigned long *msgarr = NULL;
1088 int r; /* IPC response code */
1089 int subject_required = 0;
1096 strcpy(subject, "");
1099 * First, check to see if we have permission to enter a message in
1100 * this room. The server will return an error code if we can't.
1102 strcpy(message.recipient, "");
1103 strcpy(message.author, "");
1104 strcpy(message.subject, "");
1105 strcpy(message.references, "");
1106 message.text = ""; /* point to "", changes later */
1107 message.anonymous = 0;
1108 message.type = mode;
1111 newprompt("Display name for this message: ", message.author, 40);
1114 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1116 if (r / 100 != 2 && r / 10 != 57) {
1117 scr_printf("%s\n", buf);
1121 /* Error code 570 is special. It means that we CAN enter a message
1122 * in this room, but a recipient needs to be specified.
1129 /* If the user is a dumbass, tell them how to type. */
1130 if ((userflags & US_EXPERT) == 0) {
1131 formout(ipc, "entermsg");
1134 /* Handle the selection of a recipient, if necessary. */
1136 if (need_recp == 1) {
1139 strcpy(buf, reply_to);
1141 scr_printf("Enter recipient: ");
1142 ctdl_getline(buf, (SIZ-100) );
1143 if (IsEmptyStr(buf))
1147 strcpy(buf, "sysop");
1149 strcpy(message.recipient, buf);
1153 if (!IsEmptyStr(reply_subject)) {
1154 if (!strncasecmp(reply_subject,
1156 strcpy(message.subject, reply_subject);
1159 snprintf(message.subject,
1160 sizeof message.subject,
1166 /* Trim down excessively long lists of thread references. We eliminate the
1167 * second one in the list so that the thread root remains intact.
1169 int rrtok = num_tokens(reply_references, '|');
1170 int rrlen = strlen(reply_references);
1171 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1172 remove_token(reply_references, 1, '|');
1175 snprintf(message.references, sizeof message.references, "%s%s%s",
1177 (IsEmptyStr(reply_references) ? "" : "|"),
1182 if (room_flags & QR_ANONOPT) {
1183 scr_printf("Anonymous (Y/N)? ");
1185 message.anonymous = 1;
1188 /* If it's mail, we've got to check the validity of the recipient... */
1189 if (!IsEmptyStr(message.recipient)) {
1190 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1192 scr_printf("%s\n", buf);
1197 /* Learn the number of the newest message in in the room, so we can
1198 * tell upon saving whether someone else has posted too.
1201 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1203 scr_printf("%s\n", buf);
1205 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1209 /* Now compose the message... */
1210 if (client_make_message(ipc, temp, message.recipient,
1211 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1212 if (msgarr) free(msgarr);
1216 /* Reopen the temp file that was created, so we can send it */
1217 fp = fopen(temp, "r");
1219 if (!fp || !(message.text = load_message_from_file(fp))) {
1220 err_printf("*** Internal error while trying to save message!\n"
1222 temp, strerror(errno));
1229 /* Break lines that are >1024 characters, otherwise the server
1230 * will truncate them.
1232 break_big_lines(message.text);
1234 /* Transmit message to the server */
1235 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1237 scr_printf("%s\n", buf);
1241 /* Yes, unlink it now, so it doesn't stick around if we crash */
1244 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1246 if (msgarr) free(msgarr);
1248 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1250 scr_printf("%s\n", buf);
1252 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1256 /* get new highest message number in room to set lrp for goto... */
1257 maxmsgnum = msgarr[num_msgs - 1];
1259 /* now see if anyone else has posted in here */
1261 for (a = 0; a < num_msgs; ++a) {
1262 if (msgarr[a] > highmsg) {
1266 if (msgarr) free(msgarr);
1269 /* In the Mail> room, this algorithm always counts one message
1270 * higher than in public rooms, so we decrement it by one.
1277 scr_printf("*** 1 additional message has been entered "
1278 "in this room by another user.\n");
1281 scr_printf("*** %d additional messages have been entered "
1282 "in this room by other users.\n", b);
1290 * Do editing on a quoted file
1292 void process_quote(void)
1294 FILE *qfile, *tfile;
1296 int line, qstart, qend;
1298 /* Unlink the second temp file as soon as it's opened, so it'll get
1299 * deleted even if the program dies
1301 qfile = fopen(temp2, "r");
1304 /* Display the quotable text with line numbers added */
1306 fgets(buf, 128, qfile);
1307 while (fgets(buf, 128, qfile) != NULL) {
1308 scr_printf("%3d %s", ++line, buf);
1310 scr_printf("Begin quoting at [1] : ");
1311 ctdl_getline(buf, 4);
1312 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1313 scr_printf(" End quoting at [%d] : ", line);
1314 ctdl_getline(buf, 4);
1315 qend = (buf[0] == 0) ? (line) : atoi(buf);
1318 fgets(buf, 128, qfile);
1319 tfile = fopen(temp, "w");
1320 while (fgets(buf, 128, qfile) != NULL) {
1321 if ((++line >= qstart) && (line <= qend))
1322 fprintf(tfile, " >%s", buf);
1324 fprintf(tfile, " \n");
1333 * List the URL's which were embedded in the previous message
1335 void list_urls(CtdlIPC *ipc)
1340 if (num_urls == 0) {
1341 scr_printf("There were no URL's in the previous message.\n\n");
1345 for (i = 0; i < num_urls; ++i) {
1346 scr_printf("%3d %s\n", i + 1, urls[i]);
1349 if ((i = num_urls) != 1)
1350 i = intprompt("Display which one", 1, 1, num_urls);
1352 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1359 * Run image viewer in background
1361 int do_image_view(const char *filename)
1366 snprintf(cmd, sizeof cmd, imagecmd, filename);
1373 if (childpid == 0) {
1375 pid_t grandchildpid;
1377 grandchildpid = fork();
1378 if (grandchildpid < 0) {
1379 return grandchildpid;
1382 if (grandchildpid == 0) {
1387 nullfd = open("/dev/null", O_WRONLY);
1394 retcode = system(cmd);
1404 if (grandchildpid > 0) {
1412 waitpid(childpid, &retcode, 0);
1421 * View an image attached to a message
1423 void image_view(CtdlIPC *ipc, unsigned long msg)
1425 struct parts *ptr = last_message_parts;
1429 /* Run through available parts */
1430 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1431 if ((!strcasecmp(ptr->disposition, "attachment")
1432 || !strcasecmp(ptr->disposition, "inline"))
1433 && !strncmp(ptr->mimetype, "image/", 6)) {
1436 strcpy(part, ptr->number);
1443 strprompt("View which part (0 when done)", part, SIZ-1);
1445 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1446 if ((!strcasecmp(ptr->disposition, "attachment")
1447 || !strcasecmp(ptr->disposition, "inline"))
1448 && !strncmp(ptr->mimetype, "image/", 6)
1449 && !strcasecmp(ptr->number, part)) {
1452 void *file = NULL; /* The downloaded file */
1457 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1459 scr_printf("%s\n", buf);
1463 len = (size_t)extract_long(buf, 0);
1464 progress(ipc, len, len);
1466 CtdlMakeTempFileName(tmp, sizeof tmp);
1467 strcat(tmp, ptr->filename);
1468 save_buffer(file, len, tmp);
1482 * Read the messages in the current room
1484 void readmsgs(CtdlIPC *ipc,
1485 enum MessageList c, /* see listing in citadel_ipc.h */
1486 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1487 int q /* Number of msgs to read (if c==3) */
1489 int a, b, e, f, g, start;
1495 char prtfile[PATH_MAX];
1498 char targ[ROOMNAMELEN];
1499 char filename[PATH_MAX];
1500 char save_to[PATH_MAX];
1501 void *attachment = NULL; /* Downloaded attachment */
1502 FILE *dest = NULL; /* Alternate destination other than screen */
1503 int r; /* IPC response code */
1504 static int att_seq = 0; /* Attachment download sequence number */
1511 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1517 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1519 scr_printf("%s\n", cmd);
1521 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1525 if (num_msgs == 0) { /* TODO look at this later */
1526 if (c == LastMessages) return;
1527 scr_printf("*** There are no ");
1528 if (c == NewMessages) scr_printf("new ");
1529 if (c == OldMessages) scr_printf("old ");
1530 scr_printf("messages in this room.\n");
1536 /* this loop cycles through each message... */
1537 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1538 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1539 while (msg_arr[a] == 0L) {
1541 if ((a == num_msgs) || (a == (-1)))
1545 RAGAIN: pagin = ((arcflag == 0)
1547 && (userflags & US_PAGINATOR)) ? 1 : 0;
1549 /* If we're doing a quote, set the screenwidth to 72 */
1551 hold_sw = screenwidth;
1555 /* If printing or archiving, set the screenwidth to 80 */
1557 hold_sw = screenwidth;
1561 /* clear parts list */
1562 free_parts(last_message_parts);
1563 last_message_parts = NULL;
1565 /* now read the message... */
1566 e = read_message(ipc, msg_arr[a], pagin, dest);
1568 /* ...and set the screenwidth back if we have to */
1569 if ((quotflag) || (arcflag)) {
1570 screenwidth = hold_sw;
1572 RMSGREAD: scr_flush();
1573 highest_msg_read = msg_arr[a];
1578 enable_color = hold_color;
1587 enable_color = hold_color;
1590 freopen(prtfile, "r", stdin);
1592 stty_ctdl(SB_RESTORE);
1593 ka_system(printcmd);
1594 stty_ctdl(SB_NO_INTR);
1602 } while ((g != f) && (g >= 0));
1603 scr_printf("Message printed.\n");
1605 if (rc_alt_semantics && c == 1) {
1608 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1612 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1613 && (((room_flags & QR_MAILBOX) == 0)
1614 || (rc_force_mail_prompts == 0))) {
1619 color(BRIGHT_WHITE);
1620 scr_printf("%d", num_msgs - a - 1);
1624 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1625 if (rc_url_cmd[0] && num_urls)
1626 keyopt("<U>RLview ");
1627 if (has_images > 0 && !IsEmptyStr(imagecmd))
1628 keyopt("<I>mages ");
1629 keyopt("<?>help -> ");
1633 e = (inkey() & 127);
1635 /* return key same as <N> */ if (e == 10)
1637 /* space key same as <N> */ if (e == 32)
1639 /* del/move for aides only */
1640 if ( (!is_room_aide)
1641 && ((room_flags & QR_MAILBOX) == 0)
1642 && ((room_flags2 & QR2_COLLABDEL) == 0)
1644 if ((e == 'd') || (e == 'm'))
1647 /* print only if available */
1648 if ((e == 'p') && (IsEmptyStr(printcmd)))
1650 /* can't file if not allowed */
1652 && (rc_allow_attachments == 0))
1654 /* link only if browser avail*/
1656 && (IsEmptyStr(rc_url_cmd)))
1659 && (IsEmptyStr(imagecmd) || !has_images))
1661 } while ((e != 'a') && (e != 'n') && (e != 's')
1662 && (e != 'd') && (e != 'm') && (e != 'p')
1663 && (e != 'q') && (e != 'b') && (e != 'h')
1664 && (e != 'r') && (e != 'f') && (e != '?')
1665 && (e != 'u') && (e != 'c') && (e != 'y')
1666 && (e != 'i') && (e != 'o') );
1672 scr_printf("Again");
1675 scr_printf("Delete");
1687 scr_printf("Print");
1690 scr_printf("reply Quoted");
1696 scr_printf("Header");
1699 scr_printf("Reply");
1702 scr_printf("Open attachments");
1708 scr_printf("URL's");
1711 scr_printf("mY next");
1716 scr_printf("? <help>");
1719 if (userflags & US_DISAPPEAR || e == 'i')
1720 scr_printf("\r%79s\r", "");
1725 DONE_QUOTING: switch (e) {
1727 scr_printf("Options available here:\n"
1728 " ? Help (prints this message)\n"
1729 " S Stop reading immediately\n"
1730 " A Again (repeats last message)\n"
1731 " N Next (continue with next message)\n"
1732 " Y My Next (continue with next message you authored)\n"
1733 " B Back (go back to previous message)\n");
1735 || (room_flags & QR_MAILBOX)
1736 || (room_flags2 & QR2_COLLABDEL)
1738 scr_printf(" D Delete this message\n"
1739 " M Move message to another room\n");
1741 scr_printf(" C Copy message to another room\n");
1742 if (!IsEmptyStr(printcmd))
1743 scr_printf(" P Print this message\n");
1745 " Q Reply to this message, quoting portions of it\n"
1746 " H Headers (display message headers only)\n");
1748 scr_printf(" R Reply to this message\n");
1749 if (rc_allow_attachments) {
1750 scr_printf(" O (Open attachments)\n");
1751 scr_printf(" F (save attachments to a File)\n");
1753 if (!IsEmptyStr(rc_url_cmd))
1754 scr_printf(" U (list URL's for display)\n");
1755 if (!IsEmptyStr(imagecmd) && has_images > 0)
1756 scr_printf(" I Image viewer\n");
1761 dest = fopen(prtfile, "w");
1763 hold_color = enable_color;
1768 dest = fopen(temp2, "w");
1770 hold_color = enable_color;
1782 newprompt("Enter target room: ",
1783 targ, ROOMNAMELEN - 1);
1784 if (!IsEmptyStr(targ)) {
1785 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1786 msg_arr[a], targ, cmd);
1787 scr_printf("%s\n", cmd);
1793 if (r / 100 != 2) /* r will be init'ed, FIXME */
1794 goto RMSGREAD; /* the logic here sucks */
1798 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1799 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1800 filename, &attachment, progress, cmd);
1802 scr_printf("%s\n", cmd);
1804 extract_token(filename, cmd, 2, '|', sizeof filename);
1806 * Part 1 won't have a filename; use the
1807 * subject of the message instead. IO
1809 if (IsEmptyStr(filename)) {
1810 strcpy(filename, reply_subject);
1812 if (e == 'o') { /* open attachment */
1813 mkdir(tempdir, 0700);
1814 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1818 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1819 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1822 else { /* save attachment to disk */
1823 destination_directory(save_to, filename);
1824 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1833 scr_printf("*** Delete this message? ");
1835 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1836 scr_printf("%s\n", cmd);
1844 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1847 savedpos = num_msgs;
1848 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1849 num_msgs = savedpos;
1855 image_view(ipc, msg_arr[a]);
1858 { /* hack hack hack */
1859 /* find the next message by me, stay here if we find nothing */
1862 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1864 /* This is repetitively dumb, but that's what computers are for.
1865 We have to load up messages until we find one by us */
1868 struct ctdlipcmessage *msg = NULL;
1870 /* read the header so we can get 'from=' */
1871 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1872 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1873 a = lasta; /* meesa current */
1881 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1885 } /* end for loop */
1886 } /* end read routine */
1892 * View and edit a system message
1894 void edit_system_message(CtdlIPC *ipc, char *which_message)
1898 char write_cmd[SIZ];
1900 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1901 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1902 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1903 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1910 * Verify the message base
1912 void check_message_base(CtdlIPC *ipc)
1915 char *transcript = NULL;
1916 int r; /* IPC response code */
1919 ("Please read the documentation before running this command.\n"
1920 "Having done so, do you still want to check the message base? ");
1924 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1926 scr_printf("%s\n", buf);
1930 while (transcript && !IsEmptyStr(transcript)) {
1932 extract_token(buf, transcript, 0, '\n', sizeof buf);
1933 remove_token(transcript, 0, '\n');
1934 pprintf("%s\n", buf);
1936 if (transcript) free(transcript);
1942 * Loads the contents of a file into memory. Caller must free the allocated
1945 char *load_message_from_file(FILE *src)
1951 fseek(src, 0, SEEK_END);
1955 dest = (char *)calloc(1, i + 1);
1962 g = fread(dest + got, 1, i - got, src);
1965 /* Interrupted system call, keep going */
1968 /* At this point we have either EOF or error */