4 * Citadel/UX 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>
35 #include "citadel_ipc.h"
36 #include "citadel_decls.h"
47 #define MAXWORDBUF SIZ
48 #define NO_REPLY_TO "nobody ... xxxxxx"
51 char reply_subject[SIZ];
55 char text[MAXWORDBUF];
58 void sttybbs(int cmd);
59 int haschar(const char *st, int ch);
60 void getline(char *string, int lim);
61 int file_checksum(char *filename);
62 void progress(unsigned long curr, unsigned long cmax);
64 unsigned long *msg_arr = NULL;
67 char rc_alt_semantics;
68 char rc_reply_extedit;
69 extern char room_name[];
70 extern unsigned room_flags;
71 extern long highest_msg_read;
72 extern struct CtdlServInfo serv_info;
75 extern int screenwidth;
76 extern int screenheight;
77 extern long maxmsgnum;
80 extern char is_room_aide;
81 extern char fullname[];
83 extern unsigned userflags;
84 extern char sigcaught;
85 extern char printcmd[];
86 extern int rc_allow_attachments;
87 extern int rc_display_message_numbers;
88 extern int rc_force_mail_prompts;
89 extern int editor_pid;
90 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
92 char urls[MAXURLS][SIZ];
94 int has_images = 0; /* Current msg has images */
95 struct parts *last_message_parts = NULL; /* Parts from last msg */
97 void ka_sigcatch(int signum)
100 signal(SIGALRM, ka_sigcatch);
101 CtdlIPCNoop(ipc_for_signal_handlers);
106 * server keep-alive version of wait() (needed for external editor)
108 pid_t ka_wait(int *kstatus)
113 signal(SIGALRM, ka_sigcatch);
117 } while (errno == EINTR);
118 signal(SIGALRM, SIG_IGN);
125 * version of system() that uses ka_wait()
127 int ka_system(char *shc)
136 perror("Cannot fork");
138 return ((pid_t) childpid);
142 execlp("/bin/sh", "sh", "-c", shc, NULL);
148 waitpid = ka_wait(&retcode);
149 } while (waitpid != childpid);
159 * add a newline to the buffer...
161 void add_newline(struct cittext *textlist)
166 while (ptr->next != NULL)
169 while (ptr->text[strlen(ptr->text) - 1] == 32)
170 ptr->text[strlen(ptr->text) - 1] = 0;
171 /* strcat(ptr->text,"\n"); */
173 ptr->next = (struct cittext *)
174 malloc(sizeof(struct cittext));
177 strcpy(ptr->text, "");
182 * add a word to the buffer...
184 void add_word(struct cittext *textlist, char *wordbuf)
189 while (ptr->next != NULL)
192 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
193 ptr->next = (struct cittext *)
194 malloc(sizeof(struct cittext));
197 strcpy(ptr->text, "");
200 strcat(ptr->text, wordbuf);
201 strcat(ptr->text, " ");
206 * begin editing of an opened file pointed to by fp
208 void citedit(CtdlIPC *ipc, FILE * fp)
210 int a, prev, finished, b, last_space;
212 struct cittext *textlist = NULL;
214 char wordbuf[MAXWORDBUF];
216 /* first, load the text into the buffer */
218 textlist = (struct cittext *) malloc(sizeof(struct cittext));
219 textlist->next = NULL;
220 strcpy(textlist->text, "");
224 while (a = getc(fp), a >= 0) {
226 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
227 add_word(textlist, wordbuf);
229 if ((prev == 13) || (prev == 10)) {
230 add_word(textlist, "\n");
231 add_newline(textlist);
232 add_word(textlist, "");
235 wordbuf[strlen(wordbuf) + 1] = 0;
236 wordbuf[strlen(wordbuf)] = a;
238 if (strlen(wordbuf) + 3 > screenwidth) {
239 add_word(textlist, wordbuf);
247 prev = (appending ? 13 : (-1));
259 /******* new ***********/
260 if ((a > 32) && (a < 127) && (prev == 13)) {
261 add_word(textlist, "\n");
264 /***********************/
266 if ((a == 32) && (prev == 13)) {
267 add_word(textlist, "\n");
268 add_newline(textlist);
272 if (strlen(wordbuf) > 0) {
273 wordbuf[strlen(wordbuf) - 1] = 0;
278 } else if (a == 23) {
280 wordbuf[strlen(wordbuf) - 1] = 0;
284 } while (strlen(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
285 } else if (a == 13) {
287 if (strlen(wordbuf) == 0)
290 for (b = 0; b < strlen(wordbuf); ++b)
291 if (wordbuf[b] == 32) {
299 add_word(textlist, wordbuf);
304 wordbuf[strlen(wordbuf) + 1] = 0;
305 wordbuf[strlen(wordbuf)] = a;
307 if ((strlen(wordbuf) + 3) > screenwidth) {
309 for (b = 0; b < strlen(wordbuf); ++b)
310 if (wordbuf[b] == 32)
312 if (last_space >= 0) {
313 for (b = 0; b < strlen(wordbuf); ++b)
314 if (wordbuf[b] == 32) {
322 for (b = 0; b < strlen(wordbuf); ++b) {
327 scr_printf("\n%s", wordbuf);
329 add_word(textlist, wordbuf);
335 } while (finished == 0);
338 /* write the buffer back to disk */
340 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
341 fprintf(fp, "%s", ptr->text);
345 ftruncate(fileno(fp), ftell(fp));
347 /* and deallocate the memory we used */
348 while (textlist != NULL) {
349 ptr = textlist->next;
355 /* Read a message from the server
357 int read_message(CtdlIPC *ipc,
358 long num, /* message number */
359 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
360 FILE *dest) /* Destination file, NULL for screen */
367 struct ctdlipcmessage *message = NULL;
368 int r; /* IPC response code */
369 char *converted_text = NULL;
376 int final_line_is_blank = 0;
383 strcpy(reply_to, NO_REPLY_TO);
384 strcpy(reply_subject, "");
386 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
387 (can_do_msg4 ? 4 : 0),
390 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
393 checkpagin(lines_printed, pagin, screenheight);
399 fprintf(dest, "\n ");
403 lines_printed = checkpagin(lines_printed, pagin, screenheight);
407 if (pagin == 1 && !dest) {
411 /* View headers only */
413 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
414 message->nhdr ? "yes" : "no",
415 message->author, message->type,
417 if (strlen(message->subject)) {
418 pprintf("subj=%s\n", message->subject);
420 if (strlen(message->email)) {
421 pprintf("rfca=%s\n", message->email);
423 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
424 message->hnod, message->room,
426 asctime(localtime(&message->time)));
427 if (strlen(message->recipient)) {
428 pprintf("rcpt=%s\n", message->recipient);
430 if (message->attachments) {
433 for (ptr = message->attachments; ptr; ptr = ptr->next) {
434 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
435 ptr->name, ptr->filename, ptr->number,
436 ptr->disposition, ptr->mimetype,
445 if (rc_display_message_numbers) {
447 fprintf(dest, "[#%s] ", message->msgid);
452 scr_printf("#%s", message->msgid);
457 if (nhdr == 1 && !is_room_aide) {
459 fprintf(dest, " ****");
464 fmt_date(now, sizeof now, message->time, 0);
466 fprintf(dest, "%s from %s ", now, message->author);
467 if (strlen(message->email)) {
468 fprintf(dest, "<%s> ", message->email);
472 scr_printf("%s ", now);
476 scr_printf("%s ", message->author);
477 if (strlen(message->email)) {
481 scr_printf("%s", message->email);
486 if (strlen(message->node)) {
487 if ((room_flags & QR_NETWORK)
488 || ((strcasecmp(message->node, serv_info.serv_nodename)
489 && (strcasecmp(message->node, serv_info.serv_fqdn))))) {
490 if (strlen(message->email) == 0) {
492 fprintf(dest, "@%s ", message->node);
496 color(BRIGHT_YELLOW);
497 scr_printf("%s ", message->node);
502 if (strcasecmp(message->hnod, serv_info.serv_humannode)
503 && (strlen(message->hnod)) && (!strlen(message->email))) {
505 fprintf(dest, "(%s) ", message->hnod);
510 scr_printf("%s", message->hnod);
515 if (strcasecmp(message->room, room_name) && (strlen(message->email) == 0)) {
517 fprintf(dest, "in %s> ", message->room);
521 color(BRIGHT_MAGENTA);
522 scr_printf("%s> ", message->room);
525 if (strlen(message->recipient)) {
527 fprintf(dest, "to %s ", message->recipient);
532 scr_printf("%s ", message->recipient);
543 /* Set the reply-to address to an Internet e-mail address if possible
545 if (message->email != NULL) if (strlen(message->email) > 0) {
546 safestrncpy(reply_to, message->email, sizeof reply_to);
549 /* But if we can't do that, set it to a Citadel address.
551 if (!strcmp(reply_to, NO_REPLY_TO)) {
552 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
553 message->author, message->node);
558 lines_printed = checkpagin(lines_printed, pagin, screenheight);
561 if (message->subject != NULL) {
562 safestrncpy(reply_subject, message->subject,
563 sizeof reply_subject);
564 if (strlen(message->subject) > 0) {
566 fprintf(dest, "Subject: %s\n",
570 scr_printf("Subject: ");
572 scr_printf("%s\n", message->subject);
574 lines_printed = checkpagin(lines_printed,
575 pagin, screenheight);
580 if (pagin == 1 && !dest) {
584 /******* end of header output, start of message text output *******/
587 * Convert HTML to plain text, formatting for the actual width
588 * of the client screen.
590 if (!strcasecmp(message->content_type, "text/html")) {
591 converted_text = html_to_ascii(message->text, screenwidth, 0);
592 if (converted_text != NULL) {
594 message->text = converted_text;
599 /* Text/plain is a different type */
600 if (!strcasecmp(message->content_type, "text/plain")) {
605 num_urls = 0; /* Start with a clean slate */
606 searchptr = message->text;
607 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
608 searchptr = strstr(searchptr, "http://");
609 if (searchptr != NULL) {
610 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
611 for (i = 0; i < strlen(urls[num_urls]); i++) {
612 ch = urls[num_urls][i];
613 if (ch == '>' || ch == '\"' || ch == ')' ||
614 ch == ' ' || ch == '\n') {
615 urls[num_urls][i] = 0;
627 if (format_type == 0) {
628 fr = fmout(screenwidth, NULL, message->text, dest,
629 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
631 /* renderer for text/plain */
633 lineptr = message->text;
636 nextline = strchr(lineptr, '\n');
637 if (nextline != NULL) {
640 if (*nextline == 0) nextline = NULL;
643 if (sigcaught == 0) {
644 linelen = strlen(lineptr);
645 if (lineptr[linelen-1] == '\r') {
646 lineptr[--linelen] = 0;
649 fprintf(dest, "%s\n", lineptr);
651 scr_printf("%s\n", lineptr);
652 lines_printed = lines_printed + 1 +
653 (linelen / screenwidth);
655 checkpagin(lines_printed, pagin,
659 if (lineptr[0] == 0) final_line_is_blank = 1;
660 else final_line_is_blank = 0;
665 if (!final_line_is_blank) {
672 lines_printed = checkpagin(lines_printed, pagin, screenheight);
676 /* Enumerate any attachments */
677 if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
680 for (ptr = message->attachments; ptr; ptr = ptr->next) {
681 if ( (!strcasecmp(ptr->disposition, "attachment"))
682 || (!strcasecmp(ptr->disposition, "inline"))) {
685 color(BRIGHT_MAGENTA);
686 scr_printf("%s", ptr->number);
690 scr_printf("%s", ptr->filename);
692 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
693 if (!strncmp(ptr->mimetype, "image/", 6))
699 /* Save the attachments info for later */
700 last_message_parts = message->attachments;
706 if (pagin == 1 && !dest)
713 * replace string function for the built-in editor
715 void replace_string(char *filename, long int startpos)
724 int substitutions = 0;
727 scr_printf("Enter text to be replaced:\n: ");
728 getline(srch_str, (sizeof(srch_str)-1) );
729 if (strlen(srch_str) == 0)
732 scr_printf("Enter text to replace it with:\n: ");
733 getline(rplc_str, (sizeof(rplc_str)-1) );
735 fp = fopen(filename, "r+");
740 fseek(fp, startpos, 0);
742 while (a = getc(fp), a > 0) {
744 buf[strlen(buf) + 1] = 0;
745 buf[strlen(buf)] = a;
746 if (strlen(buf) >= strlen(srch_str)) {
747 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
748 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
749 strcpy(ptr, rplc_str);
753 if (strlen(buf) > 384) {
756 fwrite((char *) buf, 128, 1, fp);
757 strcpy(buf, &buf[128]);
764 fwrite((char *) buf, strlen(buf), 1, fp);
767 truncate(filename, wpos);
768 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
772 * Function to begin composing a new message
774 int client_make_message(CtdlIPC *ipc,
775 char *filename, /* temporary file name */
776 char *recipient, /* NULL if it's not mail */
780 char *subject) /* buffer to store subject line */
787 char *editor_path = NULL;
792 if((mode-2) < MAX_EDITORS && strlen(editor_paths[mode-2]) > 0) {
793 editor_path = editor_paths[mode-2];
794 } else if (strlen(editor_paths[0]) > 0) {
795 editor_path = editor_paths[0];
798 ("*** No editor available, using built-in editor\n");
803 fmt_date(datestr, sizeof datestr, time(NULL), 0);
806 if (room_flags & QR_ANONONLY && !recipient) {
807 snprintf(header, sizeof header, " ****");
810 snprintf(header, sizeof header,
813 (is_anonymous ? "[anonymous]" : fullname)
815 if (strlen(recipient) > 0) {
816 size_t tmp = strlen(header);
817 snprintf(&header[tmp], sizeof header - tmp,
818 " to %s", recipient);
821 scr_printf("%s\n", header);
822 if (subject != NULL) if (strlen(subject) > 0) {
823 scr_printf("Subject: %s\n", subject);
829 scr_printf("(Press ctrl-d when finished)\n");
833 fp = fopen(filename, "r");
835 fmout(screenwidth, fp, NULL, NULL, 0, screenheight, 0, 0);
839 fp = fopen(filename, "w");
841 err_printf("*** Error opening temp file!\n"
843 filename, strerror(errno));
853 fp = fopen(filename, "r+");
855 err_printf("*** Error opening temp file!\n"
857 filename, strerror(errno));
865 fp = fopen(filename, "a");
867 err_printf("*** Error opening temp file!\n"
869 filename, strerror(errno));
889 default: /* allow 2+ modes */
890 e_ex_code = 1; /* start with a failed exit code */
892 cksum = file_checksum(filename);
895 if (editor_pid == 0) {
898 chmod(filename, 0600);
899 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
901 execlp(editor_path, editor_path, filename, NULL);
907 b = ka_wait(&e_ex_code);
908 } while ((b != editor_pid) && (b >= 0));
915 MECR: if (mode >= 2) {
916 if (file_checksum(filename) == cksum) {
917 err_printf("*** Aborted message.\n");
925 b = keymenu("Entry command (? for options)",
926 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
928 "<R>eplace string|<H>old message");
937 scr_printf(" %s from %s", datestr, fullname);
938 if (strlen(recipient) > 0)
939 scr_printf(" to %s", recipient);
941 if (subject != NULL) if (strlen(subject) > 0) {
942 scr_printf("Subject: %s\n", subject);
944 fp = fopen(filename, "r");
946 fmout(screenwidth, fp, NULL, NULL,
947 ((userflags & US_PAGINATOR) ? 1 : 0),
955 replace_string(filename, 0L);
962 if (subject != NULL) {
963 newprompt("Subject: ", subject, 70);
970 MEABT: scr_printf("Are you sure? ");
974 MEABT2: unlink(filename);
981 * Transmit message text to the server.
983 * This loop also implements a "tick" counter that displays the progress, if
984 * we're sending something that will take a long time to transmit.
986 void transmit_message(CtdlIPC *ipc, FILE *fp)
993 fseek(fp, 0L, SEEK_END);
996 lasttick = time(NULL);
998 while (ch = getc(fp), (ch >= 0)) {
1000 if (!strcmp(buf, "000"))
1001 strcpy(buf, ">000");
1002 CtdlIPC_putline(ipc, buf);
1008 if ((ch == 32) && (strlen(buf) > 200)) {
1010 if (!strcmp(buf, "000"))
1011 strcpy(buf, ">000");
1012 CtdlIPC_putline(ipc, buf);
1015 if (strlen(buf) > 250) {
1016 if (!strcmp(buf, "000"))
1017 strcpy(buf, ">000");
1018 CtdlIPC_putline(ipc, buf);
1023 if ((time(NULL) - lasttick) > 2L) {
1024 scr_printf(" %3ld%% completed\r",
1025 ((ftell(fp) * 100L) / msglen));
1027 lasttick = time(NULL);
1031 CtdlIPC_putline(ipc, buf);
1039 * Make sure there's room in msg_arr[] for at least one more.
1041 void check_msg_arr_size(void) {
1042 if ((num_msgs + 1) > msg_arr_size) {
1043 msg_arr_size += 512;
1044 msg_arr = realloc(msg_arr,
1045 ((sizeof(long)) * msg_arr_size) );
1052 * entmsg() - edit and create a message
1053 * returns 0 if message was saved
1055 int entmsg(CtdlIPC *ipc,
1056 int is_reply, /* nonzero if this was a <R>eply command */
1066 struct ctdlipcmessage message;
1067 unsigned long *msgarr = NULL;
1068 int r; /* IPC response code */
1075 strcpy(subject, "");
1078 * First, check to see if we have permission to enter a message in
1079 * this room. The server will return an error code if we can't.
1081 strcpy(message.recipient, "");
1082 strcpy(message.author, "");
1083 strcpy(message.subject, "");
1084 message.text = message.author; /* point to "", changes later */
1085 message.anonymous = 0;
1086 message.type = mode;
1087 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1089 if (r / 100 != 2 && r / 10 != 57) {
1090 scr_printf("%s\n", buf);
1094 /* Error code 570 is special. It means that we CAN enter a message
1095 * in this room, but a recipient needs to be specified.
1101 /* If the user is a dumbass, tell them how to type. */
1102 if ((userflags & US_EXPERT) == 0) {
1103 formout(ipc, "entermsg");
1106 /* Handle the selection of a recipient, if necessary. */
1108 if (need_recp == 1) {
1111 strcpy(buf, reply_to);
1113 scr_printf("Enter recipient: ");
1114 getline(buf, (SIZ-100) );
1115 if (strlen(buf) == 0)
1119 strcpy(buf, "sysop");
1121 strcpy(message.recipient, buf);
1124 if (strlen(reply_subject) > 0) {
1125 if (!strncasecmp(reply_subject,
1127 strcpy(message.subject, reply_subject);
1130 snprintf(message.subject,
1131 sizeof message.subject,
1138 if (room_flags & QR_ANONOPT) {
1139 scr_printf("Anonymous (Y/N)? ");
1141 message.anonymous = 1;
1144 /* If it's mail, we've got to check the validity of the recipient... */
1145 if (strlen(message.recipient) > 0) {
1146 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1148 scr_printf("%s\n", buf);
1153 /* Learn the number of the newest message in in the room, so we can
1154 * tell upon saving whether someone else has posted too.
1157 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1159 scr_printf("%s\n", buf);
1161 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1165 /* Now compose the message... */
1166 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject) != 0) {
1170 /* Reopen the temp file that was created, so we can send it */
1171 fp = fopen(temp, "r");
1173 /* Yes, unlink it now, so it doesn't stick around if we crash */
1176 if (!fp || !(message.text = load_message_from_file(fp))) {
1177 err_printf("*** Internal error while trying to save message!\n"
1179 temp, strerror(errno));
1185 /* Transmit message to the server */
1186 r = CtdlIPCPostMessage(ipc, 1, &message, buf);
1188 scr_printf("%s\n", buf);
1192 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1194 if (msgarr) free(msgarr);
1196 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1198 scr_printf("%s\n", buf);
1200 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1204 /* get new highest message number in room to set lrp for goto... */
1205 maxmsgnum = msgarr[num_msgs - 1];
1207 /* now see if anyone else has posted in here */
1209 for (a = 0; a < num_msgs; ++a) {
1210 if (msgarr[a] > highmsg) {
1214 if (msgarr) free(msgarr);
1217 /* In the Mail> room, this algorithm always counts one message
1218 * higher than in public rooms, so we decrement it by one.
1225 scr_printf("*** 1 additional message has been entered "
1226 "in this room by another user.\n");
1229 scr_printf("*** %d additional messages have been entered "
1230 "in this room by other users.\n", b);
1237 * Do editing on a quoted file
1239 void process_quote(void)
1241 FILE *qfile, *tfile;
1243 int line, qstart, qend;
1245 /* Unlink the second temp file as soon as it's opened, so it'll get
1246 * deleted even if the program dies
1248 qfile = fopen(temp2, "r");
1251 /* Display the quotable text with line numbers added */
1253 fgets(buf, 128, qfile);
1254 while (fgets(buf, 128, qfile) != NULL) {
1255 scr_printf("%2d %s", ++line, buf);
1257 scr_printf("Begin quoting at [ 1] : ");
1259 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1260 scr_printf(" End quoting at [%d] : ", line);
1262 qend = (buf[0] == 0) ? (line) : atoi(buf);
1265 fgets(buf, 128, qfile);
1266 tfile = fopen(temp, "w");
1267 while (fgets(buf, 128, qfile) != NULL) {
1268 if ((++line >= qstart) && (line <= qend))
1269 fprintf(tfile, " >%s", buf);
1271 fprintf(tfile, " \n");
1280 * List the URL's which were embedded in the previous message
1282 void list_urls(CtdlIPC *ipc)
1287 if (num_urls == 0) {
1288 scr_printf("There were no URL's in the previous message.\n\n");
1292 for (i = 0; i < num_urls; ++i) {
1293 scr_printf("%3d %s\n", i + 1, urls[i]);
1296 if ((i = num_urls) != 1)
1297 i = intprompt("Display which one", 1, 1, num_urls);
1299 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1305 * View an image attached to a message
1307 void image_view(CtdlIPC *ipc, unsigned long msg)
1309 char selected_part[SIZ];
1310 struct parts *ptr = last_message_parts;
1314 /* List available parts */
1315 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1316 if ((!strcasecmp(ptr->disposition, "attachment")
1317 || !strcasecmp(ptr->disposition, "inline"))
1318 && !strncmp(ptr->mimetype, "image/", 6)) {
1320 scr_printf("Part ");
1321 color(BRIGHT_MAGENTA);
1322 scr_printf("%s", ptr->number);
1325 strncpy(selected_part, ptr->number, SIZ-1);
1330 scr_printf("%s", ptr->filename);
1332 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
1338 strprompt("View which part (0 when done)", selected_part, SIZ-1);
1339 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1340 if ((!strcasecmp(ptr->disposition, "attachment")
1341 || !strcasecmp(ptr->disposition, "inline"))
1342 && !strncmp(ptr->mimetype, "image/", 6)
1343 && !strcmp(ptr->number, selected_part)) {
1346 void *file = NULL; /* The downloaded file */
1351 r = CtdlIPCAttachmentDownload(ipc, msg, selected_part, &file, progress, buf);
1353 scr_printf("%s\n", buf);
1357 len = (size_t)extract_long(buf, 0);
1360 strcpy(tmp, tmpnam(NULL));
1361 save_buffer(file, len, tmp);
1363 snprintf(buf, sizeof buf, imagecmd, tmp);
1374 * Read the messages in the current room
1376 void readmsgs(CtdlIPC *ipc,
1377 enum MessageList c, /* see listing in citadel_ipc.h */
1378 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1379 int q /* Number of msgs to read (if c==3) */
1381 int a, b, e, f, g, start;
1387 char prtfile[PATH_MAX];
1390 char targ[ROOMNAMELEN];
1391 char filename[PATH_MAX];
1392 char save_to[PATH_MAX];
1393 void *attachment = NULL; /* Downloaded attachment */
1394 FILE *dest = NULL; /* Alternate destination other than screen */
1395 int r; /* IPC response code */
1402 strcpy(prtfile, tmpnam(NULL));
1408 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1410 scr_printf("%s\n", cmd);
1412 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1416 if (num_msgs == 0) { /* TODO look at this later */
1417 if (c == LastMessages) return;
1418 scr_printf("*** There are no ");
1419 if (c == NewMessages) scr_printf("new ");
1420 if (c == OldMessages) scr_printf("old ");
1421 scr_printf("messages in this room.\n");
1427 /* this loop cycles through each message... */
1428 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1429 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1430 while (msg_arr[a] == 0L) {
1432 if ((a == num_msgs) || (a == (-1)))
1436 RAGAIN: pagin = ((arcflag == 0)
1438 && (userflags & US_PAGINATOR)) ? 1 : 0;
1440 /* If we're doing a quote, set the screenwidth to 72 */
1442 hold_sw = screenwidth;
1446 /* If printing or archiving, set the screenwidth to 80 */
1448 hold_sw = screenwidth;
1452 /* clear parts list */
1453 /* FIXME free() the old parts list */
1454 last_message_parts = NULL;
1456 /* now read the message... */
1457 e = read_message(ipc, msg_arr[a], pagin, dest);
1459 /* ...and set the screenwidth back if we have to */
1460 if ((quotflag) || (arcflag)) {
1461 screenwidth = hold_sw;
1463 RMSGREAD: scr_flush();
1464 highest_msg_read = msg_arr[a];
1469 enable_color = hold_color;
1476 enable_color = hold_color;
1479 freopen(prtfile, "r", stdin);
1481 sttybbs(SB_RESTORE);
1482 ka_system(printcmd);
1483 sttybbs(SB_NO_INTR);
1491 } while ((g != f) && (g >= 0));
1492 scr_printf("Message printed.\n");
1494 if (rc_alt_semantics && c == 1) {
1497 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1501 if (((userflags & US_NOPROMPT) || (e == 2))
1502 && (((room_flags & QR_MAILBOX) == 0)
1503 || (rc_force_mail_prompts == 0))) {
1508 color(BRIGHT_WHITE);
1509 scr_printf("%d", num_msgs - a - 1);
1513 keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top m<Y> next ");
1514 if (rc_url_cmd[0] && num_urls)
1515 keyopt("<U>RLview ");
1516 if (has_images > 0 && strlen(imagecmd) > 0)
1517 keyopt("<I>mages ");
1518 keyopt("<?>help -> ");
1522 e = (inkey() & 127);
1524 /* return key same as <N> */ if (e == 10)
1526 /* space key same as <N> */ if (e == 32)
1528 /* del/move for aides only */
1530 && ((room_flags & QR_MAILBOX) ==
1532 if ((e == 'd') || (e == 'm'))
1535 /* print only if available */
1536 if ((e == 'p') && (strlen(printcmd) == 0))
1538 /* can't file if not allowed */
1540 && (rc_allow_attachments == 0))
1542 /* link only if browser avail*/
1544 && (strlen(rc_url_cmd) == 0))
1547 && (!strlen(imagecmd) || !has_images))
1549 } while ((e != 'a') && (e != 'n') && (e != 's')
1550 && (e != 'd') && (e != 'm') && (e != 'p')
1551 && (e != 'q') && (e != 'b') && (e != 'h')
1552 && (e != 'r') && (e != 'f') && (e != '?')
1553 && (e != 'u') && (e != 'c') && (e != 'y')
1560 scr_printf("Again");
1563 scr_printf("Delete");
1575 scr_printf("Print");
1578 scr_printf("Quote");
1584 scr_printf("Header");
1587 scr_printf("Reply");
1593 scr_printf("URL's");
1596 scr_printf("mY next");
1599 scr_printf("Images");
1602 scr_printf("? <help>");
1605 if (userflags & US_DISAPPEAR)
1606 scr_printf("\r%79s\r", "");
1613 scr_printf("Options available here:\n"
1614 " ? Help (prints this message)\n"
1615 " S Stop reading immediately\n"
1616 " A Again (repeats last message)\n"
1617 " N Next (continue with next message)\n"
1618 " Y My Next (continue with next message you authored)\n"
1619 " B Back (go back to previous message)\n");
1621 || (room_flags & QR_MAILBOX)) {
1622 scr_printf(" D Delete this message\n"
1623 " M Move message to another room\n");
1625 scr_printf(" C Copy message to another room\n");
1626 if (strlen(printcmd) > 0)
1627 scr_printf(" P Print this message\n");
1629 " Q Quote portions of this message for your next post\n"
1630 " H Headers (display message headers only)\n");
1632 scr_printf(" R Reply to this message\n");
1633 if (rc_allow_attachments)
1635 (" F (save attachments to a file)\n");
1636 if (strlen(rc_url_cmd) > 0)
1637 scr_printf(" U (list URL's for display)\n");
1638 if (strlen(imagecmd) > 0 && has_images > 0)
1639 scr_printf(" I Image viewer\n");
1644 dest = fopen(prtfile, "w");
1646 hold_color = enable_color;
1651 dest = fopen(temp2, "w");
1653 hold_color = enable_color;
1665 newprompt("Enter target room: ",
1666 targ, ROOMNAMELEN - 1);
1667 if (strlen(targ) > 0) {
1668 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1669 msg_arr[a], targ, cmd);
1670 scr_printf("%s\n", cmd);
1676 if (r / 100 != 2) /* r will be init'ed, FIXME */
1677 goto RMSGREAD; /* the logic here sucks */
1680 newprompt("Which section? ", filename,
1681 ((sizeof filename) - 1));
1682 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1683 filename, &attachment, progress, cmd);
1685 scr_printf("%s\n", cmd);
1687 extract(filename, cmd, 2);
1689 * Part 1 won't have a filename; use the
1690 * subject of the message instead. IO
1692 if (!strlen(filename))
1693 strcpy(filename, reply_subject);
1694 destination_directory(save_to, filename);
1695 save_buffer(attachment,
1696 extract_unsigned_long(cmd, 0),
1705 scr_printf("*** Delete this message? ");
1707 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1708 scr_printf("%s\n", cmd);
1716 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1719 savedpos = num_msgs;
1720 entmsg(ipc, 1, (rc_reply_extedit ? 2 : 0));
1721 num_msgs = savedpos;
1727 image_view(ipc, msg_arr[a]);
1730 { /* hack hack hack */
1731 /* find the next message by me, stay here if we find nothing */
1734 for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1736 /* This is repetitively dumb, but that's what computers are for.
1737 We have to load up messages until we find one by us */
1741 /* read the header so we can get 'from=' */
1742 snprintf(buf, sizeof buf, "MSG0 %ld|1", msg_arr[finda]);
1743 CtdlIPC_putline(ipc, buf);
1744 CtdlIPC_getline(ipc, buf);
1745 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"))
1747 if ((!strncasecmp(buf, "from=", 5)) && (finda != a)) /* Skip current message. */
1749 if (strcasecmp(buf+5, fullname) == 0)
1751 a = lasta; /* meesa current */
1756 /* we are now in synch with the server */
1759 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1763 } /* end for loop */
1764 } /* end read routine */
1770 * View and edit a system message
1772 void edit_system_message(CtdlIPC *ipc, char *which_message)
1776 char write_cmd[SIZ];
1778 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1779 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1780 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1781 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1788 * Verify the message base
1790 void check_message_base(CtdlIPC *ipc)
1793 char *transcript = NULL;
1794 int r; /* IPC response code */
1797 ("Please read the documentation before running this command.\n"
1798 "Having done so, do you still want to check the message base? ");
1802 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1804 scr_printf("%s\n", buf);
1808 while (transcript && strlen(transcript)) {
1810 extract_token(buf, transcript, 0, '\n');
1811 remove_token(transcript, 0, '\n');
1812 pprintf("%s\n", buf);
1814 if (transcript) free(transcript);
1820 * Loads the contents of a file into memory. Caller must free the allocated
1823 char *load_message_from_file(FILE *src)
1829 fseek(src, 0, SEEK_END);
1833 dest = (char *)calloc(1, i + 1);
1840 g = fread(dest + got, 1, i - got, src);
1843 /* Interrupted system call, keep going */
1846 /* At this point we have either EOF or error */