4 * Citadel message support routines
5 * see COPYING 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;
78 extern long highest_msg_read;
81 extern int screenwidth;
82 extern int screenheight;
83 extern long maxmsgnum;
86 extern char is_room_aide;
87 extern char fullname[];
89 extern unsigned userflags;
90 extern char sigcaught;
91 extern char printcmd[];
92 extern int rc_allow_attachments;
93 extern int rc_display_message_numbers;
94 extern int rc_force_mail_prompts;
95 extern int editor_pid;
96 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
98 char urls[MAXURLS][SIZ];
100 int has_images = 0; /* Current msg has images */
101 struct parts *last_message_parts = NULL; /* Parts from last msg */
105 void ka_sigcatch(int signum)
108 signal(SIGALRM, ka_sigcatch);
109 CtdlIPCNoop(ipc_for_signal_handlers);
114 * server keep-alive version of wait() (needed for external editor)
116 pid_t ka_wait(int *kstatus)
121 signal(SIGALRM, ka_sigcatch);
125 } while (errno == EINTR);
126 signal(SIGALRM, SIG_IGN);
133 * version of system() that uses ka_wait()
135 int ka_system(char *shc)
144 perror("Cannot fork");
146 return ((pid_t) childpid);
150 execlp("/bin/sh", "sh", "-c", shc, NULL);
156 waitpid = ka_wait(&retcode);
157 } while (waitpid != childpid);
167 * add a newline to the buffer...
169 void add_newline(struct cittext *textlist)
174 while (ptr->next != NULL)
177 while (ptr->text[strlen(ptr->text) - 1] == 32)
178 ptr->text[strlen(ptr->text) - 1] = 0;
179 /* strcat(ptr->text,"\n"); */
181 ptr->next = (struct cittext *)
182 malloc(sizeof(struct cittext));
185 strcpy(ptr->text, "");
190 * add a word to the buffer...
192 void add_word(struct cittext *textlist, char *wordbuf)
197 while (ptr->next != NULL)
200 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
201 ptr->next = (struct cittext *)
202 malloc(sizeof(struct cittext));
205 strcpy(ptr->text, "");
208 strcat(ptr->text, wordbuf);
209 strcat(ptr->text, " ");
214 * begin editing of an opened file pointed to by fp
216 void citedit(CtdlIPC *ipc, FILE * fp)
218 int a, prev, finished, b, last_space;
220 struct cittext *textlist = NULL;
222 char wordbuf[MAXWORDBUF];
225 /* first, load the text into the buffer */
227 textlist = (struct cittext *) malloc(sizeof(struct cittext));
228 textlist->next = NULL;
229 strcpy(textlist->text, "");
233 while (a = getc(fp), a >= 0) {
235 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
236 add_word(textlist, wordbuf);
238 if ((prev == 13) || (prev == 10)) {
239 add_word(textlist, "\n");
240 add_newline(textlist);
241 add_word(textlist, "");
244 wordbuf[strlen(wordbuf) + 1] = 0;
245 wordbuf[strlen(wordbuf)] = a;
247 if (strlen(wordbuf) + 3 > screenwidth) {
248 add_word(textlist, wordbuf);
256 prev = (appending ? 13 : (-1));
268 if ((a != 32) && (prev == 13)) {
269 add_word(textlist, "\n");
273 if ((a == 32) && (prev == 13)) {
274 add_word(textlist, "\n");
275 add_newline(textlist);
279 if (!IsEmptyStr(wordbuf)) {
280 wordbuf[strlen(wordbuf) - 1] = 0;
285 } else if (a == 23) {
287 wordbuf[strlen(wordbuf) - 1] = 0;
291 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
292 } else if (a == 13) {
294 if (IsEmptyStr(wordbuf))
297 for (b = 0; b < strlen(wordbuf); ++b)
298 if (wordbuf[b] == 32) {
306 add_word(textlist, wordbuf);
311 wordbuf[strlen(wordbuf) + 1] = 0;
312 wordbuf[strlen(wordbuf)] = a;
314 if ((strlen(wordbuf) + 3) > screenwidth) {
316 for (b = 0; b < strlen(wordbuf); ++b)
317 if (wordbuf[b] == 32)
319 if (last_space >= 0) {
320 for (b = 0; b < strlen(wordbuf); ++b)
321 if (wordbuf[b] == 32) {
329 for (b = 0; b < strlen(wordbuf); ++b) {
334 scr_printf("\n%s", wordbuf);
336 add_word(textlist, wordbuf);
342 } while (finished == 0);
345 /* write the buffer back to disk */
347 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
348 fprintf(fp, "%s", ptr->text);
352 rv = ftruncate(fileno(fp), ftell(fp));
354 /* and deallocate the memory we used */
355 while (textlist != NULL) {
356 ptr = textlist->next;
364 * Free the struct parts
366 void free_parts(struct parts *p)
368 struct parts *a_part = p;
374 a_part = a_part->next;
381 * Read a message from the server
383 int read_message(CtdlIPC *ipc,
384 long num, /* message number */
385 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
386 FILE *dest) /* Destination file, NULL for screen */
393 struct ctdlipcmessage *message = NULL;
394 int r; /* IPC response code */
395 char *converted_text = NULL;
402 int final_line_is_blank = 0;
409 strcpy(reply_to, NO_REPLY_TO);
410 strcpy(reply_subject, "");
411 strcpy(reply_references, "");
412 strcpy(reply_inreplyto, "");
414 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
416 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
418 lines_printed = checkpagin(lines_printed, pagin, screenheight);
421 free_parts(message->attachments);
427 fprintf(dest, "\n ");
431 lines_printed = checkpagin(lines_printed, pagin, screenheight);
435 if (pagin == 1 && !dest) {
439 /* View headers only */
441 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
442 message->nhdr ? "yes" : "no",
443 message->author, message->type,
445 if (!IsEmptyStr(message->subject)) {
446 pprintf("subj=%s\n", message->subject);
448 if (!IsEmptyStr(message->email)) {
449 pprintf("rfca=%s\n", message->email);
451 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
452 message->hnod, message->room,
454 asctime(localtime(&message->time)));
455 if (!IsEmptyStr(message->recipient)) {
456 pprintf("rcpt=%s\n", message->recipient);
458 if (message->attachments) {
461 for (ptr = message->attachments; ptr; ptr = ptr->next) {
462 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
463 ptr->name, ptr->filename, ptr->number,
464 ptr->disposition, ptr->mimetype,
471 free_parts(message->attachments);
476 if (rc_display_message_numbers) {
478 fprintf(dest, "[#%s] ", message->msgid);
483 scr_printf("#%s", message->msgid);
488 if (nhdr == 1 && !is_room_aide) {
490 fprintf(dest, " ****");
495 fmt_date(now, sizeof now, message->time, 0);
497 fprintf(dest, "%s from %s ", now, message->author);
498 if (!IsEmptyStr(message->email)) {
499 fprintf(dest, "<%s> ", message->email);
503 scr_printf("%s ", now);
507 scr_printf("%s ", message->author);
508 if (!IsEmptyStr(message->email)) {
512 scr_printf("%s", message->email);
517 if (!IsEmptyStr(message->node)) {
518 if ((room_flags & QR_NETWORK)
519 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
520 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
521 if (IsEmptyStr(message->email)) {
523 fprintf(dest, "@%s ", message->node);
527 color(BRIGHT_YELLOW);
528 scr_printf("%s ", message->node);
533 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
534 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
536 fprintf(dest, "(%s) ", message->hnod);
541 scr_printf("%s", message->hnod);
546 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
548 fprintf(dest, "in %s> ", message->room);
552 color(BRIGHT_MAGENTA);
553 scr_printf("%s> ", message->room);
556 if (!IsEmptyStr(message->recipient)) {
558 fprintf(dest, "to %s ", message->recipient);
563 scr_printf("%s ", message->recipient);
574 /* Set the reply-to address to an Internet e-mail address if possible
576 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
577 if (!IsEmptyStr(message->author)) {
578 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
581 safestrncpy(reply_to, message->email, sizeof reply_to);
585 /* But if we can't do that, set it to a Citadel address.
587 if (!strcmp(reply_to, NO_REPLY_TO)) {
588 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
589 message->author, message->node);
594 lines_printed = checkpagin(lines_printed, pagin, screenheight);
598 if (message->msgid != NULL) {
599 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
602 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
603 safestrncpy(reply_references, message->references, sizeof reply_references);
606 if (message->subject != NULL) {
607 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
608 if (!IsEmptyStr(message->subject)) {
610 fprintf(dest, "Subject: %s\n",
614 scr_printf("Subject: ");
616 scr_printf("%s\n", message->subject);
618 lines_printed = checkpagin(lines_printed,
619 pagin, screenheight);
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,
673 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
675 /* renderer for text/plain */
677 lineptr = message->text;
680 nextline = strchr(lineptr, '\n');
681 if (nextline != NULL) {
684 if (*nextline == 0) nextline = NULL;
687 if (sigcaught == 0) {
688 linelen = strlen(lineptr);
689 if (linelen && (lineptr[linelen-1] == '\r')) {
690 lineptr[--linelen] = 0;
693 fprintf(dest, "%s\n", lineptr);
695 scr_printf("%s\n", lineptr);
696 lines_printed = lines_printed + 1 +
697 (linelen / screenwidth);
699 checkpagin(lines_printed, pagin,
703 if (lineptr[0] == 0) final_line_is_blank = 1;
704 else final_line_is_blank = 0;
709 if (!final_line_is_blank) {
716 lines_printed = checkpagin(lines_printed, pagin, screenheight);
721 /* Enumerate any attachments */
722 if ( (pagin == 1) && (message->attachments) ) {
725 for (ptr = message->attachments; ptr; ptr = ptr->next) {
726 if ( (!strcasecmp(ptr->disposition, "attachment"))
727 || (!strcasecmp(ptr->disposition, "inline"))
728 || (!strcasecmp(ptr->disposition, ""))
730 if ( (strcasecmp(ptr->number, message->mime_chosen))
731 && (!IsEmptyStr(ptr->mimetype))
735 color(BRIGHT_MAGENTA);
736 pprintf("%s", ptr->number);
740 pprintf("%s", ptr->filename);
742 pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
743 if (!strncmp(ptr->mimetype, "image/", 6)) {
751 /* Save the attachments info for later */
752 last_message_parts = message->attachments;
758 if (pagin == 1 && !dest)
765 * replace string function for the built-in editor
767 void replace_string(char *filename, long int startpos)
776 int substitutions = 0;
780 scr_printf("Enter text to be replaced:\n: ");
781 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
782 if (IsEmptyStr(srch_str)) {
786 scr_printf("Enter text to replace it with:\n: ");
787 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
789 fp = fopen(filename, "r+");
795 fseek(fp, startpos, 0);
797 while (a = getc(fp), a > 0) {
799 buf[strlen(buf) + 1] = 0;
800 buf[strlen(buf)] = a;
801 if (strlen(buf) >= strlen(srch_str)) {
802 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
803 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
804 strcpy(ptr, rplc_str);
808 if (strlen(buf) > 384) {
811 rv = fwrite((char *) buf, 128, 1, fp);
812 strcpy(buf, &buf[128]);
818 if (!IsEmptyStr(buf)) {
819 rv = fwrite((char *) buf, strlen(buf), 1, fp);
823 rv = truncate(filename, wpos);
824 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
828 * Function to begin composing a new message
830 int client_make_message(CtdlIPC *ipc,
831 char *filename, /* temporary file name */
832 char *recipient, /* NULL if it's not mail */
836 char *subject, /* buffer to store subject line */
837 int subject_required)
844 char *editor_path = NULL;
849 if((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2])) {
850 editor_path = editor_paths[mode-2];
851 } else if (!IsEmptyStr(editor_paths[0])) {
852 editor_path = editor_paths[0];
854 err_printf("*** No editor available, "
855 "using built-in editor\n");
860 fmt_date(datestr, sizeof datestr, time(NULL), 0);
863 if (room_flags & QR_ANONONLY && !recipient) {
864 snprintf(header, sizeof header, " ****");
867 snprintf(header, sizeof header,
870 (is_anonymous ? "[anonymous]" : fullname)
872 if (!IsEmptyStr(recipient)) {
873 size_t tmp = strlen(header);
874 snprintf(&header[tmp], sizeof header - tmp,
875 " to %s", recipient);
878 scr_printf("%s\n", header);
879 if (subject != NULL) if (!IsEmptyStr(subject)) {
880 scr_printf("Subject: %s\n", subject);
883 if ( (subject_required) && (IsEmptyStr(subject)) ) {
884 newprompt("Subject: ", subject, 70);
890 scr_printf("(Press ctrl-d when finished)\n");
894 fp = fopen(filename, "r");
896 fmout(screenwidth, fp, NULL, NULL, 0,
901 fp = fopen(filename, "w");
903 err_printf("*** Error opening temp file!\n"
905 filename, strerror(errno));
915 fp = fopen(filename, "r+");
917 err_printf("*** Error opening temp file!\n"
919 filename, strerror(errno));
927 fp = fopen(filename, "a");
929 err_printf("*** Error opening temp file!\n"
931 filename, strerror(errno));
951 default: /* allow 2+ modes */
952 e_ex_code = 1; /* start with a failed exit code */
954 stty_ctdl(SB_RESTORE);
956 cksum = file_checksum(filename);
957 if (editor_pid == 0) {
960 chmod(filename, 0600);
961 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
963 execlp(editor_path, editor_path, filename, NULL);
969 b = ka_wait(&e_ex_code);
970 } while ((b != editor_pid) && (b >= 0));
977 MECR: if (mode >= 2) {
978 if (file_checksum(filename) == cksum) {
979 err_printf("*** Aborted message.\n");
982 if (e_ex_code == 0) {
988 b = keymenu("Entry command (? for options)",
989 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
991 "<R>eplace string|<H>old message");
993 if (b == 'a') goto MEABT;
994 if (b == 'c') goto ME1;
995 if (b == 's') goto MEFIN;
997 scr_printf(" %s from %s", datestr, fullname);
998 if (!IsEmptyStr(recipient)) {
999 scr_printf(" to %s", recipient);
1002 if (subject != NULL) if (!IsEmptyStr(subject)) {
1003 scr_printf("Subject: %s\n", subject);
1005 fp = fopen(filename, "r");
1007 fmout(screenwidth, fp, NULL, NULL,
1008 ((userflags & US_PAGINATOR) ? 1 : 0),
1009 screenheight, 0, 0);
1016 replace_string(filename, 0L);
1023 if (subject != NULL) {
1024 newprompt("Subject: ", subject, 70);
1031 MEABT: scr_printf("Are you sure? ");
1035 MEABT2: unlink(filename);
1041 * Make sure there's room in msg_arr[] for at least one more.
1043 void check_msg_arr_size(void) {
1044 if ((num_msgs + 1) > msg_arr_size) {
1045 msg_arr_size += 512;
1046 msg_arr = realloc(msg_arr,
1047 ((sizeof(long)) * msg_arr_size) );
1053 * break_big_lines() - break up lines that are >1024 characters
1054 * otherwise the server will truncate
1056 void break_big_lines(char *msg) {
1065 while (strlen(ptr) > 1000) {
1066 break_here = strchr(&ptr[900], ' ');
1067 if ((break_here == NULL) || (break_here > &ptr[999])) {
1068 break_here = &ptr[999];
1077 * entmsg() - edit and create a message
1078 * returns 0 if message was saved
1080 int entmsg(CtdlIPC *ipc,
1081 int is_reply, /* nonzero if this was a <R>eply command */
1083 int masquerade /* prompt for a non-default display name? */
1092 struct ctdlipcmessage message;
1093 unsigned long *msgarr = NULL;
1094 int r; /* IPC response code */
1095 int subject_required = 0;
1098 scr_printf("You may not enter messages in this type of room.\n");
1109 strcpy(subject, "");
1112 * First, check to see if we have permission to enter a message in
1113 * this room. The server will return an error code if we can't.
1115 strcpy(message.recipient, "");
1116 strcpy(message.author, "");
1117 strcpy(message.subject, "");
1118 strcpy(message.references, "");
1119 message.text = ""; /* point to "", changes later */
1120 message.anonymous = 0;
1121 message.type = mode;
1124 newprompt("Display name for this message: ", message.author, 40);
1127 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1129 if (r / 100 != 2 && r / 10 != 57) {
1130 scr_printf("%s\n", buf);
1134 /* Error code 570 is special. It means that we CAN enter a message
1135 * in this room, but a recipient needs to be specified.
1142 /* If the user is a dumbass, tell them how to type. */
1143 if ((userflags & US_EXPERT) == 0) {
1144 formout(ipc, "entermsg");
1147 /* Handle the selection of a recipient, if necessary. */
1149 if (need_recp == 1) {
1152 strcpy(buf, reply_to);
1154 scr_printf("Enter recipient: ");
1155 ctdl_getline(buf, (SIZ-100) );
1156 if (IsEmptyStr(buf))
1160 strcpy(buf, "sysop");
1162 strcpy(message.recipient, buf);
1166 if (!IsEmptyStr(reply_subject)) {
1167 if (!strncasecmp(reply_subject,
1169 strcpy(message.subject, reply_subject);
1172 snprintf(message.subject,
1173 sizeof message.subject,
1179 /* Trim down excessively long lists of thread references. We eliminate the
1180 * second one in the list so that the thread root remains intact.
1182 int rrtok = num_tokens(reply_references, '|');
1183 int rrlen = strlen(reply_references);
1184 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1185 remove_token(reply_references, 1, '|');
1188 snprintf(message.references, sizeof message.references, "%s%s%s",
1190 (IsEmptyStr(reply_references) ? "" : "|"),
1195 if (room_flags & QR_ANONOPT) {
1196 scr_printf("Anonymous (Y/N)? ");
1198 message.anonymous = 1;
1201 /* If it's mail, we've got to check the validity of the recipient... */
1202 if (!IsEmptyStr(message.recipient)) {
1203 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1205 scr_printf("%s\n", buf);
1210 /* Learn the number of the newest message in in the room, so we can
1211 * tell upon saving whether someone else has posted too.
1214 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1216 scr_printf("%s\n", buf);
1218 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1222 /* Now compose the message... */
1223 if (client_make_message(ipc, temp, message.recipient,
1224 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1225 if (msgarr) free(msgarr);
1229 /* Reopen the temp file that was created, so we can send it */
1230 fp = fopen(temp, "r");
1232 if (!fp || !(message.text = load_message_from_file(fp))) {
1233 err_printf("*** Internal error while trying to save message!\n"
1235 temp, strerror(errno));
1242 /* Break lines that are >1024 characters, otherwise the server
1243 * will truncate them.
1245 break_big_lines(message.text);
1247 /* Transmit message to the server */
1248 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1250 scr_printf("%s\n", buf);
1254 /* Yes, unlink it now, so it doesn't stick around if we crash */
1257 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1259 if (msgarr) free(msgarr);
1261 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1263 scr_printf("%s\n", buf);
1265 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1269 /* get new highest message number in room to set lrp for goto... */
1270 maxmsgnum = msgarr[num_msgs - 1];
1272 /* now see if anyone else has posted in here */
1274 for (a = 0; a < num_msgs; ++a) {
1275 if (msgarr[a] > highmsg) {
1279 if (msgarr) free(msgarr);
1282 /* In the Mail> room, this algorithm always counts one message
1283 * higher than in public rooms, so we decrement it by one.
1290 scr_printf("*** 1 additional message has been entered "
1291 "in this room by another user.\n");
1294 scr_printf("*** %d additional messages have been entered "
1295 "in this room by other users.\n", b);
1303 * Do editing on a quoted file
1305 void process_quote(void)
1307 FILE *qfile, *tfile;
1309 int line, qstart, qend;
1311 /* Unlink the second temp file as soon as it's opened, so it'll get
1312 * deleted even if the program dies
1314 qfile = fopen(temp2, "r");
1317 /* Display the quotable text with line numbers added */
1319 if (fgets(buf, 128, qfile) == NULL) {
1320 /* we're skipping a line here */
1322 while (fgets(buf, 128, qfile) != NULL) {
1323 scr_printf("%3d %s", ++line, buf);
1325 scr_printf("Begin quoting at [1] : ");
1326 ctdl_getline(buf, 4);
1327 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1328 scr_printf(" End quoting at [%d] : ", line);
1329 ctdl_getline(buf, 4);
1330 qend = (buf[0] == 0) ? (line) : atoi(buf);
1333 if (fgets(buf, 128, qfile) == NULL) {
1334 /* we're skipping a line here */
1336 tfile = fopen(temp, "w");
1337 while (fgets(buf, 128, qfile) != NULL) {
1338 if ((++line >= qstart) && (line <= qend))
1339 fprintf(tfile, " >%s", buf);
1341 fprintf(tfile, " \n");
1350 * List the URL's which were embedded in the previous message
1352 void list_urls(CtdlIPC *ipc)
1358 if (num_urls == 0) {
1359 scr_printf("There were no URL's in the previous message.\n\n");
1363 for (i = 0; i < num_urls; ++i) {
1364 scr_printf("%3d %s\n", i + 1, urls[i]);
1367 if ((i = num_urls) != 1)
1368 i = intprompt("Display which one", 1, 1, num_urls);
1370 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1377 * Run image viewer in background
1379 int do_image_view(const char *filename)
1384 snprintf(cmd, sizeof cmd, imagecmd, filename);
1391 if (childpid == 0) {
1393 pid_t grandchildpid;
1395 grandchildpid = fork();
1396 if (grandchildpid < 0) {
1397 return grandchildpid;
1400 if (grandchildpid == 0) {
1405 nullfd = open("/dev/null", O_WRONLY);
1412 retcode = system(cmd);
1422 if (grandchildpid > 0) {
1430 waitpid(childpid, &retcode, 0);
1439 * View an image attached to a message
1441 void image_view(CtdlIPC *ipc, unsigned long msg)
1443 struct parts *ptr = last_message_parts;
1447 /* Run through available parts */
1448 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1449 if ((!strcasecmp(ptr->disposition, "attachment")
1450 || !strcasecmp(ptr->disposition, "inline"))
1451 && !strncmp(ptr->mimetype, "image/", 6)) {
1454 strcpy(part, ptr->number);
1461 strprompt("View which part (0 when done)", part, SIZ-1);
1463 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1464 if ((!strcasecmp(ptr->disposition, "attachment")
1465 || !strcasecmp(ptr->disposition, "inline"))
1466 && !strncmp(ptr->mimetype, "image/", 6)
1467 && !strcasecmp(ptr->number, part)) {
1470 void *file = NULL; /* The downloaded file */
1475 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1477 scr_printf("%s\n", buf);
1481 len = (size_t)extract_long(buf, 0);
1482 progress(ipc, len, len);
1484 CtdlMakeTempFileName(tmp, sizeof tmp);
1485 strcat(tmp, ptr->filename);
1486 save_buffer(file, len, tmp);
1500 * Read the messages in the current room
1502 void readmsgs(CtdlIPC *ipc,
1503 enum MessageList c, /* see listing in citadel_ipc.h */
1504 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1505 int q /* Number of msgs to read (if c==3) */
1507 int a, b, e, f, g, start;
1513 char prtfile[PATH_MAX];
1516 char targ[ROOMNAMELEN];
1517 char filename[PATH_MAX];
1518 char save_to[PATH_MAX];
1519 void *attachment = NULL; /* Downloaded attachment */
1520 FILE *dest = NULL; /* Alternate destination other than screen */
1521 int r; /* IPC response code */
1522 static int att_seq = 0; /* Attachment download sequence number */
1523 int rv = 0; /* silence the stupid warn_unused_result warnings */
1530 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1536 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1538 scr_printf("%s\n", cmd);
1540 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1544 if (num_msgs == 0) { /* TODO look at this later */
1545 if (c == LastMessages) return;
1546 scr_printf("*** There are no ");
1547 if (c == NewMessages) scr_printf("new ");
1548 if (c == OldMessages) scr_printf("old ");
1549 scr_printf("messages in this room.\n");
1555 /* this loop cycles through each message... */
1556 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1557 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1558 while (msg_arr[a] == 0L) {
1560 if ((a == num_msgs) || (a == (-1)))
1564 RAGAIN: pagin = ((arcflag == 0)
1566 && (userflags & US_PAGINATOR)) ? 1 : 0;
1568 /* If we're doing a quote, set the screenwidth to 72 */
1570 hold_sw = screenwidth;
1574 /* If printing or archiving, set the screenwidth to 80 */
1576 hold_sw = screenwidth;
1580 /* clear parts list */
1581 free_parts(last_message_parts);
1582 last_message_parts = NULL;
1584 /* now read the message... */
1585 e = read_message(ipc, msg_arr[a], pagin, dest);
1587 /* ...and set the screenwidth back if we have to */
1588 if ((quotflag) || (arcflag)) {
1589 screenwidth = hold_sw;
1591 RMSGREAD: scr_flush();
1592 highest_msg_read = msg_arr[a];
1597 enable_color = hold_color;
1606 enable_color = hold_color;
1609 if (freopen(prtfile, "r", stdin) == NULL) {
1610 /* we probably should handle the error condition here */
1613 stty_ctdl(SB_RESTORE);
1614 ka_system(printcmd);
1615 stty_ctdl(SB_NO_INTR);
1623 } while ((g != f) && (g >= 0));
1624 scr_printf("Message printed.\n");
1626 if (rc_alt_semantics && c == 1) {
1629 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1633 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1634 && (((room_flags & QR_MAILBOX) == 0)
1635 || (rc_force_mail_prompts == 0))) {
1640 color(BRIGHT_WHITE);
1641 scr_printf("%d", num_msgs - a - 1);
1645 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1646 if (rc_url_cmd[0] && num_urls)
1647 keyopt("<U>RLview ");
1648 if (has_images > 0 && !IsEmptyStr(imagecmd))
1649 keyopt("<I>mages ");
1650 keyopt("<?>help -> ");
1654 e = (inkey() & 127);
1656 /* return key same as <N> */ if (e == 10)
1658 /* space key same as <N> */ if (e == 32)
1660 /* del/move for aides only */
1661 if ( (!is_room_aide)
1662 && ((room_flags & QR_MAILBOX) == 0)
1663 && ((room_flags2 & QR2_COLLABDEL) == 0)
1665 if ((e == 'd') || (e == 'm'))
1668 /* print only if available */
1669 if ((e == 'p') && (IsEmptyStr(printcmd)))
1671 /* can't file if not allowed */
1673 && (rc_allow_attachments == 0))
1675 /* link only if browser avail*/
1677 && (IsEmptyStr(rc_url_cmd)))
1680 && (IsEmptyStr(imagecmd) || !has_images))
1682 } while ((e != 'a') && (e != 'n') && (e != 's')
1683 && (e != 'd') && (e != 'm') && (e != 'p')
1684 && (e != 'q') && (e != 'b') && (e != 'h')
1685 && (e != 'r') && (e != 'f') && (e != '?')
1686 && (e != 'u') && (e != 'c') && (e != 'y')
1687 && (e != 'i') && (e != 'o') );
1693 scr_printf("Again");
1696 scr_printf("Delete");
1708 scr_printf("Print");
1711 scr_printf("reply Quoted");
1717 scr_printf("Header");
1720 scr_printf("Reply");
1723 scr_printf("Open attachments");
1729 scr_printf("URL's");
1732 scr_printf("mY next");
1737 scr_printf("? <help>");
1740 if (userflags & US_DISAPPEAR || e == 'i')
1741 scr_printf("\r%79s\r", "");
1746 DONE_QUOTING: switch (e) {
1748 scr_printf("Options available here:\n"
1749 " ? Help (prints this message)\n"
1750 " S Stop reading immediately\n"
1751 " A Again (repeats last message)\n"
1752 " N Next (continue with next message)\n"
1753 " Y My Next (continue with next message you authored)\n"
1754 " B Back (go back to previous message)\n");
1756 || (room_flags & QR_MAILBOX)
1757 || (room_flags2 & QR2_COLLABDEL)
1759 scr_printf(" D Delete this message\n"
1760 " M Move message to another room\n");
1762 scr_printf(" C Copy message to another room\n");
1763 if (!IsEmptyStr(printcmd))
1764 scr_printf(" P Print this message\n");
1766 " Q Reply to this message, quoting portions of it\n"
1767 " H Headers (display message headers only)\n");
1769 scr_printf(" R Reply to this message\n");
1770 if (rc_allow_attachments) {
1771 scr_printf(" O (Open attachments)\n");
1772 scr_printf(" F (save attachments to a File)\n");
1774 if (!IsEmptyStr(rc_url_cmd))
1775 scr_printf(" U (list URL's for display)\n");
1776 if (!IsEmptyStr(imagecmd) && has_images > 0)
1777 scr_printf(" I Image viewer\n");
1782 dest = fopen(prtfile, "w");
1784 hold_color = enable_color;
1789 dest = fopen(temp2, "w");
1791 hold_color = enable_color;
1803 newprompt("Enter target room: ",
1804 targ, ROOMNAMELEN - 1);
1805 if (!IsEmptyStr(targ)) {
1806 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1807 msg_arr[a], targ, cmd);
1808 scr_printf("%s\n", cmd);
1814 if (r / 100 != 2) /* r will be init'ed, FIXME */
1815 goto RMSGREAD; /* the logic here sucks */
1819 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1820 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1821 filename, &attachment, progress, cmd);
1823 scr_printf("%s\n", cmd);
1825 extract_token(filename, cmd, 2, '|', sizeof filename);
1827 * Part 1 won't have a filename; use the
1828 * subject of the message instead. IO
1830 if (IsEmptyStr(filename)) {
1831 strcpy(filename, reply_subject);
1833 if (e == 'o') { /* open attachment */
1834 mkdir(tempdir, 0700);
1835 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1839 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1840 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1843 else { /* save attachment to disk */
1844 destination_directory(save_to, filename);
1845 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1854 scr_printf("*** Delete this message? ");
1856 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1857 scr_printf("%s\n", cmd);
1865 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1868 savedpos = num_msgs;
1869 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1870 num_msgs = savedpos;
1876 image_view(ipc, msg_arr[a]);
1879 { /* hack hack hack */
1880 /* find the next message by me, stay here if we find nothing */
1883 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1885 /* This is repetitively dumb, but that's what computers are for.
1886 We have to load up messages until we find one by us */
1889 struct ctdlipcmessage *msg = NULL;
1891 /* read the header so we can get 'from=' */
1892 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1893 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1894 a = lasta; /* meesa current */
1902 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1906 } /* end for loop */
1907 } /* end read routine */
1913 * View and edit a system message
1915 void edit_system_message(CtdlIPC *ipc, char *which_message)
1919 char write_cmd[SIZ];
1921 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1922 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1923 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1924 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1931 * Verify the message base
1933 void check_message_base(CtdlIPC *ipc)
1936 char *transcript = NULL;
1937 int r; /* IPC response code */
1940 ("Please read the documentation before running this command.\n"
1941 "Having done so, do you still want to check the message base? ");
1945 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1947 scr_printf("%s\n", buf);
1951 while (transcript && !IsEmptyStr(transcript)) {
1953 extract_token(buf, transcript, 0, '\n', sizeof buf);
1954 remove_token(transcript, 0, '\n');
1955 pprintf("%s\n", buf);
1957 if (transcript) free(transcript);
1963 * Loads the contents of a file into memory. Caller must free the allocated
1966 char *load_message_from_file(FILE *src)
1972 fseek(src, 0, SEEK_END);
1976 dest = (char *)calloc(1, i + 1);
1983 g = fread(dest + got, 1, i - got, src);
1986 /* Interrupted system call, keep going */
1989 /* At this point we have either EOF or error */