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>
39 #include "citadel_ipc.h"
40 #include "citadel_decls.h"
51 #define MAXWORDBUF SIZ
52 #define NO_REPLY_TO "nobody ... xxxxxx"
55 char reply_subject[SIZ];
59 char text[MAXWORDBUF];
62 void stty_ctdl(int cmd);
63 int haschar(const char *st, int ch);
64 void ctdl_getline(char *string, int lim);
65 int file_checksum(char *filename);
66 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
68 unsigned long *msg_arr = NULL;
71 char rc_alt_semantics;
72 extern char room_name[];
73 extern unsigned room_flags;
74 extern long highest_msg_read;
77 extern int screenwidth;
78 extern int screenheight;
79 extern long maxmsgnum;
82 extern char is_room_aide;
83 extern char fullname[];
85 extern unsigned userflags;
86 extern char sigcaught;
87 extern char printcmd[];
88 extern int rc_allow_attachments;
89 extern int rc_display_message_numbers;
90 extern int rc_force_mail_prompts;
91 extern int editor_pid;
92 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
94 char urls[MAXURLS][SIZ];
96 int has_images = 0; /* Current msg has images */
97 struct parts *last_message_parts = NULL; /* Parts from last msg */
101 void ka_sigcatch(int signum)
104 signal(SIGALRM, ka_sigcatch);
105 CtdlIPCNoop(ipc_for_signal_handlers);
110 * server keep-alive version of wait() (needed for external editor)
112 pid_t ka_wait(int *kstatus)
117 signal(SIGALRM, ka_sigcatch);
121 } while (errno == EINTR);
122 signal(SIGALRM, SIG_IGN);
129 * version of system() that uses ka_wait()
131 int ka_system(char *shc)
140 perror("Cannot fork");
142 return ((pid_t) childpid);
146 execlp("/bin/sh", "sh", "-c", shc, NULL);
152 waitpid = ka_wait(&retcode);
153 } while (waitpid != childpid);
163 * add a newline to the buffer...
165 void add_newline(struct cittext *textlist)
170 while (ptr->next != NULL)
173 while (ptr->text[strlen(ptr->text) - 1] == 32)
174 ptr->text[strlen(ptr->text) - 1] = 0;
175 /* strcat(ptr->text,"\n"); */
177 ptr->next = (struct cittext *)
178 malloc(sizeof(struct cittext));
181 strcpy(ptr->text, "");
186 * add a word to the buffer...
188 void add_word(struct cittext *textlist, char *wordbuf)
193 while (ptr->next != NULL)
196 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
197 ptr->next = (struct cittext *)
198 malloc(sizeof(struct cittext));
201 strcpy(ptr->text, "");
204 strcat(ptr->text, wordbuf);
205 strcat(ptr->text, " ");
210 * begin editing of an opened file pointed to by fp
212 void citedit(CtdlIPC *ipc, FILE * fp)
214 int a, prev, finished, b, last_space;
216 struct cittext *textlist = NULL;
218 char wordbuf[MAXWORDBUF];
220 /* first, load the text into the buffer */
222 textlist = (struct cittext *) malloc(sizeof(struct cittext));
223 textlist->next = NULL;
224 strcpy(textlist->text, "");
228 while (a = getc(fp), a >= 0) {
230 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
231 add_word(textlist, wordbuf);
233 if ((prev == 13) || (prev == 10)) {
234 add_word(textlist, "\n");
235 add_newline(textlist);
236 add_word(textlist, "");
239 wordbuf[strlen(wordbuf) + 1] = 0;
240 wordbuf[strlen(wordbuf)] = a;
242 if (strlen(wordbuf) + 3 > screenwidth) {
243 add_word(textlist, wordbuf);
251 prev = (appending ? 13 : (-1));
263 if ((a != 32) && (prev == 13)) {
264 add_word(textlist, "\n");
268 if ((a == 32) && (prev == 13)) {
269 add_word(textlist, "\n");
270 add_newline(textlist);
274 if (strlen(wordbuf) > 0) {
275 wordbuf[strlen(wordbuf) - 1] = 0;
280 } else if (a == 23) {
282 wordbuf[strlen(wordbuf) - 1] = 0;
286 } while (strlen(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
287 } else if (a == 13) {
289 if (strlen(wordbuf) == 0)
292 for (b = 0; b < strlen(wordbuf); ++b)
293 if (wordbuf[b] == 32) {
301 add_word(textlist, wordbuf);
306 wordbuf[strlen(wordbuf) + 1] = 0;
307 wordbuf[strlen(wordbuf)] = a;
309 if ((strlen(wordbuf) + 3) > screenwidth) {
311 for (b = 0; b < strlen(wordbuf); ++b)
312 if (wordbuf[b] == 32)
314 if (last_space >= 0) {
315 for (b = 0; b < strlen(wordbuf); ++b)
316 if (wordbuf[b] == 32) {
324 for (b = 0; b < strlen(wordbuf); ++b) {
329 scr_printf("\n%s", wordbuf);
331 add_word(textlist, wordbuf);
337 } while (finished == 0);
340 /* write the buffer back to disk */
342 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
343 fprintf(fp, "%s", ptr->text);
347 ftruncate(fileno(fp), ftell(fp));
349 /* and deallocate the memory we used */
350 while (textlist != NULL) {
351 ptr = textlist->next;
359 * Free the struct parts
361 void free_parts(struct parts *p)
363 struct parts *a_part = p;
369 a_part = a_part->next;
376 * Read a message from the server
378 int read_message(CtdlIPC *ipc,
379 long num, /* message number */
380 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
381 FILE *dest) /* Destination file, NULL for screen */
388 struct ctdlipcmessage *message = NULL;
389 int r; /* IPC response code */
390 char *converted_text = NULL;
397 int final_line_is_blank = 0;
404 strcpy(reply_to, NO_REPLY_TO);
405 strcpy(reply_subject, "");
407 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
408 (can_do_msg4 ? 4 : 0),
411 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
414 checkpagin(lines_printed, pagin, screenheight);
420 fprintf(dest, "\n ");
424 lines_printed = checkpagin(lines_printed, pagin, screenheight);
428 if (pagin == 1 && !dest) {
432 /* View headers only */
434 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
435 message->nhdr ? "yes" : "no",
436 message->author, message->type,
438 if (strlen(message->subject)) {
439 pprintf("subj=%s\n", message->subject);
441 if (strlen(message->email)) {
442 pprintf("rfca=%s\n", message->email);
444 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
445 message->hnod, message->room,
447 asctime(localtime(&message->time)));
448 if (strlen(message->recipient)) {
449 pprintf("rcpt=%s\n", message->recipient);
451 if (message->attachments) {
454 for (ptr = message->attachments; ptr; ptr = ptr->next) {
455 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
456 ptr->name, ptr->filename, ptr->number,
457 ptr->disposition, ptr->mimetype,
466 if (rc_display_message_numbers) {
468 fprintf(dest, "[#%s] ", message->msgid);
473 scr_printf("#%s", message->msgid);
478 if (nhdr == 1 && !is_room_aide) {
480 fprintf(dest, " ****");
485 fmt_date(now, sizeof now, message->time, 0);
487 fprintf(dest, "%s from %s ", now, message->author);
488 if (strlen(message->email)) {
489 fprintf(dest, "<%s> ", message->email);
493 scr_printf("%s ", now);
497 scr_printf("%s ", message->author);
498 if (strlen(message->email)) {
502 scr_printf("%s", message->email);
507 if (strlen(message->node)) {
508 if ((room_flags & QR_NETWORK)
509 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
510 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
511 if (strlen(message->email) == 0) {
513 fprintf(dest, "@%s ", message->node);
517 color(BRIGHT_YELLOW);
518 scr_printf("%s ", message->node);
523 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
524 && (strlen(message->hnod)) && (!strlen(message->email))) {
526 fprintf(dest, "(%s) ", message->hnod);
531 scr_printf("%s", message->hnod);
536 if (strcasecmp(message->room, room_name) && (strlen(message->email) == 0)) {
538 fprintf(dest, "in %s> ", message->room);
542 color(BRIGHT_MAGENTA);
543 scr_printf("%s> ", message->room);
546 if (strlen(message->recipient)) {
548 fprintf(dest, "to %s ", message->recipient);
553 scr_printf("%s ", message->recipient);
564 /* Set the reply-to address to an Internet e-mail address if possible
566 if (message->email != NULL) if (strlen(message->email) > 0) {
567 if (strlen(message->author) > 0) {
568 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
571 safestrncpy(reply_to, message->email, sizeof reply_to);
575 /* But if we can't do that, set it to a Citadel address.
577 if (!strcmp(reply_to, NO_REPLY_TO)) {
578 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
579 message->author, message->node);
584 lines_printed = checkpagin(lines_printed, pagin, screenheight);
587 if (message->subject != NULL) {
588 safestrncpy(reply_subject, message->subject,
589 sizeof reply_subject);
590 if (strlen(message->subject) > 0) {
592 fprintf(dest, "Subject: %s\n",
596 scr_printf("Subject: ");
598 scr_printf("%s\n", message->subject);
600 lines_printed = checkpagin(lines_printed,
601 pagin, screenheight);
606 if (pagin == 1 && !dest) {
610 /******* end of header output, start of message text output *******/
613 * Convert HTML to plain text, formatting for the actual width
614 * of the client screen.
616 if (!strcasecmp(message->content_type, "text/html")) {
617 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
618 if (converted_text != NULL) {
620 message->text = converted_text;
625 /* Text/plain is a different type */
626 if (!strcasecmp(message->content_type, "text/plain")) {
631 num_urls = 0; /* Start with a clean slate */
632 searchptr = message->text;
633 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
634 searchptr = strstr(searchptr, "http://");
635 if (searchptr != NULL) {
636 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
637 for (i = 0; i < strlen(urls[num_urls]); i++) {
638 ch = urls[num_urls][i];
639 if (ch == '>' || ch == '\"' || ch == ')' ||
640 ch == ' ' || ch == '\n') {
641 urls[num_urls][i] = 0;
653 if (format_type == 0) {
654 fr = fmout(screenwidth, NULL, message->text, dest,
655 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
657 /* renderer for text/plain */
659 lineptr = message->text;
662 nextline = strchr(lineptr, '\n');
663 if (nextline != NULL) {
666 if (*nextline == 0) nextline = NULL;
669 if (sigcaught == 0) {
670 linelen = strlen(lineptr);
671 if (lineptr[linelen-1] == '\r') {
672 lineptr[--linelen] = 0;
675 fprintf(dest, "%s\n", lineptr);
677 scr_printf("%s\n", lineptr);
678 lines_printed = lines_printed + 1 +
679 (linelen / screenwidth);
681 checkpagin(lines_printed, pagin,
685 if (lineptr[0] == 0) final_line_is_blank = 1;
686 else final_line_is_blank = 0;
691 if (!final_line_is_blank) {
698 lines_printed = checkpagin(lines_printed, pagin, screenheight);
702 /* Enumerate any attachments */
703 if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
706 for (ptr = message->attachments; ptr; ptr = ptr->next) {
707 if ( (!strcasecmp(ptr->disposition, "attachment"))
708 || (!strcasecmp(ptr->disposition, "inline"))
709 || (!strcasecmp(ptr->disposition, ""))
711 if ( (strcasecmp(ptr->number, message->mime_chosen))
712 && (strlen(ptr->mimetype) > 0)
716 color(BRIGHT_MAGENTA);
717 pprintf("%s", ptr->number);
721 pprintf("%s", ptr->filename);
723 pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
724 if (!strncmp(ptr->mimetype, "image/", 6)) {
732 /* Save the attachments info for later */
733 last_message_parts = message->attachments;
739 if (pagin == 1 && !dest)
746 * replace string function for the built-in editor
748 void replace_string(char *filename, long int startpos)
757 int substitutions = 0;
760 scr_printf("Enter text to be replaced:\n: ");
761 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
762 if (strlen(srch_str) == 0)
765 scr_printf("Enter text to replace it with:\n: ");
766 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
768 fp = fopen(filename, "r+");
773 fseek(fp, startpos, 0);
775 while (a = getc(fp), a > 0) {
777 buf[strlen(buf) + 1] = 0;
778 buf[strlen(buf)] = a;
779 if (strlen(buf) >= strlen(srch_str)) {
780 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
781 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
782 strcpy(ptr, rplc_str);
786 if (strlen(buf) > 384) {
789 fwrite((char *) buf, 128, 1, fp);
790 strcpy(buf, &buf[128]);
797 fwrite((char *) buf, strlen(buf), 1, fp);
800 truncate(filename, wpos);
801 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
805 * Function to begin composing a new message
807 int client_make_message(CtdlIPC *ipc,
808 char *filename, /* temporary file name */
809 char *recipient, /* NULL if it's not mail */
813 char *subject) /* buffer to store subject line */
820 char *editor_path = NULL;
825 if((mode-2) < MAX_EDITORS && strlen(editor_paths[mode-2]) > 0) {
826 editor_path = editor_paths[mode-2];
827 } else if (strlen(editor_paths[0]) > 0) {
828 editor_path = editor_paths[0];
830 err_printf("*** No editor available, "
831 "using built-in editor\n");
836 fmt_date(datestr, sizeof datestr, time(NULL), 0);
839 if (room_flags & QR_ANONONLY && !recipient) {
840 snprintf(header, sizeof header, " ****");
843 snprintf(header, sizeof header,
846 (is_anonymous ? "[anonymous]" : fullname)
848 if (strlen(recipient) > 0) {
849 size_t tmp = strlen(header);
850 snprintf(&header[tmp], sizeof header - tmp,
851 " to %s", recipient);
854 scr_printf("%s\n", header);
855 if (subject != NULL) if (strlen(subject) > 0) {
856 scr_printf("Subject: %s\n", subject);
862 scr_printf("(Press ctrl-d when finished)\n");
866 fp = fopen(filename, "r");
868 fmout(screenwidth, fp, NULL, NULL, 0,
873 fp = fopen(filename, "w");
875 err_printf("*** Error opening temp file!\n"
877 filename, strerror(errno));
887 fp = fopen(filename, "r+");
889 err_printf("*** Error opening temp file!\n"
891 filename, strerror(errno));
899 fp = fopen(filename, "a");
901 err_printf("*** Error opening temp file!\n"
903 filename, strerror(errno));
923 default: /* allow 2+ modes */
924 e_ex_code = 1; /* start with a failed exit code */
926 stty_ctdl(SB_RESTORE);
928 cksum = file_checksum(filename);
929 if (editor_pid == 0) {
932 chmod(filename, 0600);
933 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
935 execlp(editor_path, editor_path, filename, NULL);
941 b = ka_wait(&e_ex_code);
942 } while ((b != editor_pid) && (b >= 0));
949 MECR: if (mode >= 2) {
950 if (file_checksum(filename) == cksum) {
951 err_printf("*** Aborted message.\n");
954 if (e_ex_code == 0) {
960 b = keymenu("Entry command (? for options)",
961 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
963 "<R>eplace string|<H>old message");
965 if (b == 'a') goto MEABT;
966 if (b == 'c') goto ME1;
967 if (b == 's') goto MEFIN;
969 scr_printf(" %s from %s", datestr, fullname);
970 if (strlen(recipient) > 0) {
971 scr_printf(" to %s", recipient);
974 if (subject != NULL) if (strlen(subject) > 0) {
975 scr_printf("Subject: %s\n", subject);
977 fp = fopen(filename, "r");
979 fmout(screenwidth, fp, NULL, NULL,
980 ((userflags & US_PAGINATOR) ? 1 : 0),
988 replace_string(filename, 0L);
995 if (subject != NULL) {
996 newprompt("Subject: ", subject, 70);
1003 MEABT: scr_printf("Are you sure? ");
1007 MEABT2: unlink(filename);
1014 * Transmit message text to the server.
1016 * This loop also implements a "tick" counter that displays the progress, if
1017 * we're sending something that will take a long time to transmit.
1019 void transmit_message(CtdlIPC *ipc, FILE *fp)
1026 fseek(fp, 0L, SEEK_END);
1029 lasttick = time(NULL);
1031 while (ch = getc(fp), (ch >= 0)) {
1033 if (!strcmp(buf, "000"))
1034 strcpy(buf, ">000");
1035 CtdlIPC_putline(ipc, buf);
1041 if ((ch == 32) && (strlen(buf) > 200)) {
1043 if (!strcmp(buf, "000"))
1044 strcpy(buf, ">000");
1045 CtdlIPC_putline(ipc, buf);
1048 if (strlen(buf) > 250) {
1049 if (!strcmp(buf, "000"))
1050 strcpy(buf, ">000");
1051 CtdlIPC_putline(ipc, buf);
1056 if ((time(NULL) - lasttick) > 2L) {
1057 scr_printf(" %3ld%% completed\r",
1058 ((ftell(fp) * 100L) / msglen));
1060 lasttick = time(NULL);
1064 CtdlIPC_putline(ipc, buf);
1072 * Make sure there's room in msg_arr[] for at least one more.
1074 void check_msg_arr_size(void) {
1075 if ((num_msgs + 1) > msg_arr_size) {
1076 msg_arr_size += 512;
1077 msg_arr = realloc(msg_arr,
1078 ((sizeof(long)) * msg_arr_size) );
1084 * break_big_lines() - break up lines that are >1024 characters
1085 * otherwise the server will truncate
1087 void break_big_lines(char *msg) {
1096 while (strlen(ptr) > 1000) {
1097 break_here = strchr(&ptr[900], ' ');
1098 if ((break_here == NULL) || (break_here > &ptr[999])) {
1099 break_here = &ptr[999];
1108 * entmsg() - edit and create a message
1109 * returns 0 if message was saved
1111 int entmsg(CtdlIPC *ipc,
1112 int is_reply, /* nonzero if this was a <R>eply command */
1114 int masquerade /* prompt for a non-default display name? */
1123 struct ctdlipcmessage message;
1124 unsigned long *msgarr = NULL;
1125 int r; /* IPC response code */
1132 strcpy(subject, "");
1135 * First, check to see if we have permission to enter a message in
1136 * this room. The server will return an error code if we can't.
1138 strcpy(message.recipient, "");
1139 strcpy(message.author, "");
1140 strcpy(message.subject, "");
1141 message.text = ""; /* point to "", changes later */
1142 message.anonymous = 0;
1143 message.type = mode;
1146 newprompt("Display name for this message: ", message.author, 40);
1149 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1151 if (r / 100 != 2 && r / 10 != 57) {
1152 scr_printf("%s\n", buf);
1156 /* Error code 570 is special. It means that we CAN enter a message
1157 * in this room, but a recipient needs to be specified.
1164 /* If the user is a dumbass, tell them how to type. */
1165 if ((userflags & US_EXPERT) == 0) {
1166 formout(ipc, "entermsg");
1169 /* Handle the selection of a recipient, if necessary. */
1171 if (need_recp == 1) {
1174 strcpy(buf, reply_to);
1176 scr_printf("Enter recipient: ");
1177 ctdl_getline(buf, (SIZ-100) );
1178 if (strlen(buf) == 0)
1182 strcpy(buf, "sysop");
1184 strcpy(message.recipient, buf);
1187 if (strlen(reply_subject) > 0) {
1188 if (!strncasecmp(reply_subject,
1190 strcpy(message.subject, reply_subject);
1193 snprintf(message.subject,
1194 sizeof message.subject,
1201 if (room_flags & QR_ANONOPT) {
1202 scr_printf("Anonymous (Y/N)? ");
1204 message.anonymous = 1;
1207 /* If it's mail, we've got to check the validity of the recipient... */
1208 if (strlen(message.recipient) > 0) {
1209 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1211 scr_printf("%s\n", buf);
1216 /* Learn the number of the newest message in in the room, so we can
1217 * tell upon saving whether someone else has posted too.
1220 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1222 scr_printf("%s\n", buf);
1224 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1228 /* Now compose the message... */
1229 if (client_make_message(ipc, temp, message.recipient,
1230 message.anonymous, 0, c, message.subject) != 0) {
1234 /* Reopen the temp file that was created, so we can send it */
1235 fp = fopen(temp, "r");
1237 if (!fp || !(message.text = load_message_from_file(fp))) {
1238 err_printf("*** Internal error while trying to save message!\n"
1240 temp, strerror(errno));
1247 /* Break lines that are >1024 characters, otherwise the server
1248 * will truncate them.
1250 break_big_lines(message.text);
1252 /* Transmit message to the server */
1253 r = CtdlIPCPostMessage(ipc, 1, &message, buf);
1255 scr_printf("%s\n", buf);
1259 /* Yes, unlink it now, so it doesn't stick around if we crash */
1262 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1264 if (msgarr) free(msgarr);
1266 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1268 scr_printf("%s\n", buf);
1270 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1274 /* get new highest message number in room to set lrp for goto... */
1275 maxmsgnum = msgarr[num_msgs - 1];
1277 /* now see if anyone else has posted in here */
1279 for (a = 0; a < num_msgs; ++a) {
1280 if (msgarr[a] > highmsg) {
1284 if (msgarr) free(msgarr);
1287 /* In the Mail> room, this algorithm always counts one message
1288 * higher than in public rooms, so we decrement it by one.
1295 scr_printf("*** 1 additional message has been entered "
1296 "in this room by another user.\n");
1299 scr_printf("*** %d additional messages have been entered "
1300 "in this room by other users.\n", b);
1308 * Do editing on a quoted file
1310 void process_quote(void)
1312 FILE *qfile, *tfile;
1314 int line, qstart, qend;
1316 /* Unlink the second temp file as soon as it's opened, so it'll get
1317 * deleted even if the program dies
1319 qfile = fopen(temp2, "r");
1322 /* Display the quotable text with line numbers added */
1324 fgets(buf, 128, qfile);
1325 while (fgets(buf, 128, qfile) != NULL) {
1326 scr_printf("%2d %s", ++line, buf);
1328 scr_printf("Begin quoting at [ 1] : ");
1329 ctdl_getline(buf, 3);
1330 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1331 scr_printf(" End quoting at [%d] : ", line);
1332 ctdl_getline(buf, 3);
1333 qend = (buf[0] == 0) ? (line) : atoi(buf);
1336 fgets(buf, 128, qfile);
1337 tfile = fopen(temp, "w");
1338 while (fgets(buf, 128, qfile) != NULL) {
1339 if ((++line >= qstart) && (line <= qend))
1340 fprintf(tfile, " >%s", buf);
1342 fprintf(tfile, " \n");
1351 * List the URL's which were embedded in the previous message
1353 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 */
1528 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1534 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1536 scr_printf("%s\n", cmd);
1538 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1542 if (num_msgs == 0) { /* TODO look at this later */
1543 if (c == LastMessages) return;
1544 scr_printf("*** There are no ");
1545 if (c == NewMessages) scr_printf("new ");
1546 if (c == OldMessages) scr_printf("old ");
1547 scr_printf("messages in this room.\n");
1553 /* this loop cycles through each message... */
1554 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1555 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1556 while (msg_arr[a] == 0L) {
1558 if ((a == num_msgs) || (a == (-1)))
1562 RAGAIN: pagin = ((arcflag == 0)
1564 && (userflags & US_PAGINATOR)) ? 1 : 0;
1566 /* If we're doing a quote, set the screenwidth to 72 */
1568 hold_sw = screenwidth;
1572 /* If printing or archiving, set the screenwidth to 80 */
1574 hold_sw = screenwidth;
1578 /* clear parts list */
1579 free_parts(last_message_parts);
1580 last_message_parts = NULL;
1582 /* now read the message... */
1583 e = read_message(ipc, msg_arr[a], pagin, dest);
1585 /* ...and set the screenwidth back if we have to */
1586 if ((quotflag) || (arcflag)) {
1587 screenwidth = hold_sw;
1589 RMSGREAD: scr_flush();
1590 highest_msg_read = msg_arr[a];
1595 enable_color = hold_color;
1602 enable_color = hold_color;
1605 freopen(prtfile, "r", stdin);
1607 stty_ctdl(SB_RESTORE);
1608 ka_system(printcmd);
1609 stty_ctdl(SB_NO_INTR);
1617 } while ((g != f) && (g >= 0));
1618 scr_printf("Message printed.\n");
1620 if (rc_alt_semantics && c == 1) {
1623 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1627 if (((userflags & US_NOPROMPT) || (e == 2))
1628 && (((room_flags & QR_MAILBOX) == 0)
1629 || (rc_force_mail_prompts == 0))) {
1634 color(BRIGHT_WHITE);
1635 scr_printf("%d", num_msgs - a - 1);
1639 keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1640 if (rc_url_cmd[0] && num_urls)
1641 keyopt("<U>RLview ");
1642 if (has_images > 0 && strlen(imagecmd) > 0)
1643 keyopt("<I>mages ");
1644 keyopt("<?>help -> ");
1648 e = (inkey() & 127);
1650 /* return key same as <N> */ if (e == 10)
1652 /* space key same as <N> */ if (e == 32)
1654 /* del/move for aides only */
1656 && ((room_flags & QR_MAILBOX) ==
1658 if ((e == 'd') || (e == 'm'))
1661 /* print only if available */
1662 if ((e == 'p') && (strlen(printcmd) == 0))
1664 /* can't file if not allowed */
1666 && (rc_allow_attachments == 0))
1668 /* link only if browser avail*/
1670 && (strlen(rc_url_cmd) == 0))
1673 && (!strlen(imagecmd) || !has_images))
1675 } while ((e != 'a') && (e != 'n') && (e != 's')
1676 && (e != 'd') && (e != 'm') && (e != 'p')
1677 && (e != 'q') && (e != 'b') && (e != 'h')
1678 && (e != 'r') && (e != 'f') && (e != '?')
1679 && (e != 'u') && (e != 'c') && (e != 'y')
1686 scr_printf("Again");
1689 scr_printf("Delete");
1701 scr_printf("Print");
1704 scr_printf("Quote");
1710 scr_printf("Header");
1713 scr_printf("Reply");
1719 scr_printf("URL's");
1722 scr_printf("mY next");
1727 scr_printf("? <help>");
1730 if (userflags & US_DISAPPEAR || e == 'i')
1731 scr_printf("\r%79s\r", "");
1738 scr_printf("Options available here:\n"
1739 " ? Help (prints this message)\n"
1740 " S Stop reading immediately\n"
1741 " A Again (repeats last message)\n"
1742 " N Next (continue with next message)\n"
1743 " Y My Next (continue with next message you authored)\n"
1744 " B Back (go back to previous message)\n");
1746 || (room_flags & QR_MAILBOX)) {
1747 scr_printf(" D Delete this message\n"
1748 " M Move message to another room\n");
1750 scr_printf(" C Copy message to another room\n");
1751 if (strlen(printcmd) > 0)
1752 scr_printf(" P Print this message\n");
1754 " Q Quote portions of this message for your next post\n"
1755 " H Headers (display message headers only)\n");
1757 scr_printf(" R Reply to this message\n");
1758 if (rc_allow_attachments)
1760 (" F (save attachments to a file)\n");
1761 if (strlen(rc_url_cmd) > 0)
1762 scr_printf(" U (list URL's for display)\n");
1763 if (strlen(imagecmd) > 0 && has_images > 0)
1764 scr_printf(" I Image viewer\n");
1769 dest = fopen(prtfile, "w");
1771 hold_color = enable_color;
1776 dest = fopen(temp2, "w");
1778 hold_color = enable_color;
1790 newprompt("Enter target room: ",
1791 targ, ROOMNAMELEN - 1);
1792 if (strlen(targ) > 0) {
1793 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1794 msg_arr[a], targ, cmd);
1795 scr_printf("%s\n", cmd);
1801 if (r / 100 != 2) /* r will be init'ed, FIXME */
1802 goto RMSGREAD; /* the logic here sucks */
1805 newprompt("Which section? ", filename,
1806 ((sizeof filename) - 1));
1807 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1808 filename, &attachment, progress, cmd);
1810 scr_printf("%s\n", cmd);
1812 extract_token(filename, cmd, 2, '|', sizeof filename);
1814 * Part 1 won't have a filename; use the
1815 * subject of the message instead. IO
1817 if (!strlen(filename))
1818 strcpy(filename, reply_subject);
1819 destination_directory(save_to, filename);
1820 save_buffer(attachment,
1821 extract_unsigned_long(cmd, 0),
1830 scr_printf("*** Delete this message? ");
1832 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1833 scr_printf("%s\n", cmd);
1841 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1844 savedpos = num_msgs;
1845 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1846 num_msgs = savedpos;
1852 image_view(ipc, msg_arr[a]);
1855 { /* hack hack hack */
1856 /* find the next message by me, stay here if we find nothing */
1859 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1861 /* This is repetitively dumb, but that's what computers are for.
1862 We have to load up messages until we find one by us */
1865 struct ctdlipcmessage *msg = NULL;
1867 /* read the header so we can get 'from=' */
1868 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1869 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1870 a = lasta; /* meesa current */
1878 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1882 } /* end for loop */
1883 } /* end read routine */
1889 * View and edit a system message
1891 void edit_system_message(CtdlIPC *ipc, char *which_message)
1895 char write_cmd[SIZ];
1897 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1898 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1899 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1900 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1907 * Verify the message base
1909 void check_message_base(CtdlIPC *ipc)
1912 char *transcript = NULL;
1913 int r; /* IPC response code */
1916 ("Please read the documentation before running this command.\n"
1917 "Having done so, do you still want to check the message base? ");
1921 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1923 scr_printf("%s\n", buf);
1927 while (transcript && strlen(transcript)) {
1929 extract_token(buf, transcript, 0, '\n', sizeof buf);
1930 remove_token(transcript, 0, '\n');
1931 pprintf("%s\n", buf);
1933 if (transcript) free(transcript);
1939 * Loads the contents of a file into memory. Caller must free the allocated
1942 char *load_message_from_file(FILE *src)
1948 fseek(src, 0, SEEK_END);
1952 dest = (char *)calloc(1, i + 1);
1959 g = fread(dest + got, 1, i - got, src);
1962 /* Interrupted system call, keep going */
1965 /* At this point we have either EOF or error */