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 sttybbs(int cmd);
63 int haschar(const char *st, int ch);
64 void 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 char rc_reply_extedit;
73 extern char room_name[];
74 extern unsigned room_flags;
75 extern long highest_msg_read;
78 extern int screenwidth;
79 extern int screenheight;
80 extern long maxmsgnum;
83 extern char is_room_aide;
84 extern char fullname[];
86 extern unsigned userflags;
87 extern char sigcaught;
88 extern char printcmd[];
89 extern int rc_allow_attachments;
90 extern int rc_display_message_numbers;
91 extern int rc_force_mail_prompts;
92 extern int editor_pid;
93 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
95 char urls[MAXURLS][SIZ];
97 int has_images = 0; /* Current msg has images */
98 struct parts *last_message_parts = NULL; /* Parts from last msg */
102 void ka_sigcatch(int signum)
105 signal(SIGALRM, ka_sigcatch);
106 CtdlIPCNoop(ipc_for_signal_handlers);
111 * server keep-alive version of wait() (needed for external editor)
113 pid_t ka_wait(int *kstatus)
118 signal(SIGALRM, ka_sigcatch);
122 } while (errno == EINTR);
123 signal(SIGALRM, SIG_IGN);
130 * version of system() that uses ka_wait()
132 int ka_system(char *shc)
141 perror("Cannot fork");
143 return ((pid_t) childpid);
147 execlp("/bin/sh", "sh", "-c", shc, NULL);
153 waitpid = ka_wait(&retcode);
154 } while (waitpid != childpid);
164 * add a newline to the buffer...
166 void add_newline(struct cittext *textlist)
171 while (ptr->next != NULL)
174 while (ptr->text[strlen(ptr->text) - 1] == 32)
175 ptr->text[strlen(ptr->text) - 1] = 0;
176 /* strcat(ptr->text,"\n"); */
178 ptr->next = (struct cittext *)
179 malloc(sizeof(struct cittext));
182 strcpy(ptr->text, "");
187 * add a word to the buffer...
189 void add_word(struct cittext *textlist, char *wordbuf)
194 while (ptr->next != NULL)
197 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
198 ptr->next = (struct cittext *)
199 malloc(sizeof(struct cittext));
202 strcpy(ptr->text, "");
205 strcat(ptr->text, wordbuf);
206 strcat(ptr->text, " ");
211 * begin editing of an opened file pointed to by fp
213 void citedit(CtdlIPC *ipc, FILE * fp)
215 int a, prev, finished, b, last_space;
217 struct cittext *textlist = NULL;
219 char wordbuf[MAXWORDBUF];
221 /* first, load the text into the buffer */
223 textlist = (struct cittext *) malloc(sizeof(struct cittext));
224 textlist->next = NULL;
225 strcpy(textlist->text, "");
229 while (a = getc(fp), a >= 0) {
231 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
232 add_word(textlist, wordbuf);
234 if ((prev == 13) || (prev == 10)) {
235 add_word(textlist, "\n");
236 add_newline(textlist);
237 add_word(textlist, "");
240 wordbuf[strlen(wordbuf) + 1] = 0;
241 wordbuf[strlen(wordbuf)] = a;
243 if (strlen(wordbuf) + 3 > screenwidth) {
244 add_word(textlist, wordbuf);
252 prev = (appending ? 13 : (-1));
264 /******* new ***********/
265 if ((a > 32) && (a < 127) && (prev == 13)) {
266 add_word(textlist, "\n");
269 /***********************/
271 if ((a == 32) && (prev == 13)) {
272 add_word(textlist, "\n");
273 add_newline(textlist);
277 if (strlen(wordbuf) > 0) {
278 wordbuf[strlen(wordbuf) - 1] = 0;
283 } else if (a == 23) {
285 wordbuf[strlen(wordbuf) - 1] = 0;
289 } while (strlen(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
290 } else if (a == 13) {
292 if (strlen(wordbuf) == 0)
295 for (b = 0; b < strlen(wordbuf); ++b)
296 if (wordbuf[b] == 32) {
304 add_word(textlist, wordbuf);
309 wordbuf[strlen(wordbuf) + 1] = 0;
310 wordbuf[strlen(wordbuf)] = a;
312 if ((strlen(wordbuf) + 3) > screenwidth) {
314 for (b = 0; b < strlen(wordbuf); ++b)
315 if (wordbuf[b] == 32)
317 if (last_space >= 0) {
318 for (b = 0; b < strlen(wordbuf); ++b)
319 if (wordbuf[b] == 32) {
327 for (b = 0; b < strlen(wordbuf); ++b) {
332 scr_printf("\n%s", wordbuf);
334 add_word(textlist, wordbuf);
340 } while (finished == 0);
343 /* write the buffer back to disk */
345 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
346 fprintf(fp, "%s", ptr->text);
350 ftruncate(fileno(fp), ftell(fp));
352 /* and deallocate the memory we used */
353 while (textlist != NULL) {
354 ptr = textlist->next;
362 * Free the struct parts
364 void free_parts(struct parts *p)
366 struct parts *a_part = p;
372 a_part = a_part->next;
379 * Read a message from the server
381 int read_message(CtdlIPC *ipc,
382 long num, /* message number */
383 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
384 FILE *dest) /* Destination file, NULL for screen */
391 struct ctdlipcmessage *message = NULL;
392 int r; /* IPC response code */
393 char *converted_text = NULL;
400 int final_line_is_blank = 0;
407 strcpy(reply_to, NO_REPLY_TO);
408 strcpy(reply_subject, "");
410 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
411 (can_do_msg4 ? 4 : 0),
414 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
417 checkpagin(lines_printed, pagin, screenheight);
423 fprintf(dest, "\n ");
427 lines_printed = checkpagin(lines_printed, pagin, screenheight);
431 if (pagin == 1 && !dest) {
435 /* View headers only */
437 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
438 message->nhdr ? "yes" : "no",
439 message->author, message->type,
441 if (strlen(message->subject)) {
442 pprintf("subj=%s\n", message->subject);
444 if (strlen(message->email)) {
445 pprintf("rfca=%s\n", message->email);
447 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
448 message->hnod, message->room,
450 asctime(localtime(&message->time)));
451 if (strlen(message->recipient)) {
452 pprintf("rcpt=%s\n", message->recipient);
454 if (message->attachments) {
457 for (ptr = message->attachments; ptr; ptr = ptr->next) {
458 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
459 ptr->name, ptr->filename, ptr->number,
460 ptr->disposition, ptr->mimetype,
469 if (rc_display_message_numbers) {
471 fprintf(dest, "[#%s] ", message->msgid);
476 scr_printf("#%s", message->msgid);
481 if (nhdr == 1 && !is_room_aide) {
483 fprintf(dest, " ****");
488 fmt_date(now, sizeof now, message->time, 0);
490 fprintf(dest, "%s from %s ", now, message->author);
491 if (strlen(message->email)) {
492 fprintf(dest, "<%s> ", message->email);
496 scr_printf("%s ", now);
500 scr_printf("%s ", message->author);
501 if (strlen(message->email)) {
505 scr_printf("%s", message->email);
510 if (strlen(message->node)) {
511 if ((room_flags & QR_NETWORK)
512 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
513 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
514 if (strlen(message->email) == 0) {
516 fprintf(dest, "@%s ", message->node);
520 color(BRIGHT_YELLOW);
521 scr_printf("%s ", message->node);
526 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
527 && (strlen(message->hnod)) && (!strlen(message->email))) {
529 fprintf(dest, "(%s) ", message->hnod);
534 scr_printf("%s", message->hnod);
539 if (strcasecmp(message->room, room_name) && (strlen(message->email) == 0)) {
541 fprintf(dest, "in %s> ", message->room);
545 color(BRIGHT_MAGENTA);
546 scr_printf("%s> ", message->room);
549 if (strlen(message->recipient)) {
551 fprintf(dest, "to %s ", message->recipient);
556 scr_printf("%s ", message->recipient);
567 /* Set the reply-to address to an Internet e-mail address if possible
569 if (message->email != NULL) if (strlen(message->email) > 0) {
570 safestrncpy(reply_to, message->email, sizeof reply_to);
573 /* But if we can't do that, set it to a Citadel address.
575 if (!strcmp(reply_to, NO_REPLY_TO)) {
576 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
577 message->author, message->node);
582 lines_printed = checkpagin(lines_printed, pagin, screenheight);
585 if (message->subject != NULL) {
586 safestrncpy(reply_subject, message->subject,
587 sizeof reply_subject);
588 if (strlen(message->subject) > 0) {
590 fprintf(dest, "Subject: %s\n",
594 scr_printf("Subject: ");
596 scr_printf("%s\n", message->subject);
598 lines_printed = checkpagin(lines_printed,
599 pagin, screenheight);
604 if (pagin == 1 && !dest) {
608 /******* end of header output, start of message text output *******/
611 * Convert HTML to plain text, formatting for the actual width
612 * of the client screen.
614 if (!strcasecmp(message->content_type, "text/html")) {
615 converted_text = html_to_ascii(message->text, screenwidth, 0);
616 if (converted_text != NULL) {
618 message->text = converted_text;
623 /* Text/plain is a different type */
624 if (!strcasecmp(message->content_type, "text/plain")) {
629 num_urls = 0; /* Start with a clean slate */
630 searchptr = message->text;
631 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
632 searchptr = strstr(searchptr, "http://");
633 if (searchptr != NULL) {
634 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
635 for (i = 0; i < strlen(urls[num_urls]); i++) {
636 ch = urls[num_urls][i];
637 if (ch == '>' || ch == '\"' || ch == ')' ||
638 ch == ' ' || ch == '\n') {
639 urls[num_urls][i] = 0;
651 if (format_type == 0) {
652 fr = fmout(screenwidth, NULL, message->text, dest,
653 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
655 /* renderer for text/plain */
657 lineptr = message->text;
660 nextline = strchr(lineptr, '\n');
661 if (nextline != NULL) {
664 if (*nextline == 0) nextline = NULL;
667 if (sigcaught == 0) {
668 linelen = strlen(lineptr);
669 if (lineptr[linelen-1] == '\r') {
670 lineptr[--linelen] = 0;
673 fprintf(dest, "%s\n", lineptr);
675 scr_printf("%s\n", lineptr);
676 lines_printed = lines_printed + 1 +
677 (linelen / screenwidth);
679 checkpagin(lines_printed, pagin,
683 if (lineptr[0] == 0) final_line_is_blank = 1;
684 else final_line_is_blank = 0;
689 if (!final_line_is_blank) {
696 lines_printed = checkpagin(lines_printed, pagin, screenheight);
700 /* Enumerate any attachments */
701 if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
704 for (ptr = message->attachments; ptr; ptr = ptr->next) {
705 if ( (!strcasecmp(ptr->disposition, "attachment"))
706 || (!strcasecmp(ptr->disposition, "inline"))) {
709 color(BRIGHT_MAGENTA);
710 pprintf("%s", ptr->number);
714 pprintf("%s", ptr->filename);
716 pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
717 if (!strncmp(ptr->mimetype, "image/", 6))
723 /* Save the attachments info for later */
724 last_message_parts = message->attachments;
730 if (pagin == 1 && !dest)
737 * replace string function for the built-in editor
739 void replace_string(char *filename, long int startpos)
748 int substitutions = 0;
751 scr_printf("Enter text to be replaced:\n: ");
752 getline(srch_str, (sizeof(srch_str)-1) );
753 if (strlen(srch_str) == 0)
756 scr_printf("Enter text to replace it with:\n: ");
757 getline(rplc_str, (sizeof(rplc_str)-1) );
759 fp = fopen(filename, "r+");
764 fseek(fp, startpos, 0);
766 while (a = getc(fp), a > 0) {
768 buf[strlen(buf) + 1] = 0;
769 buf[strlen(buf)] = a;
770 if (strlen(buf) >= strlen(srch_str)) {
771 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
772 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
773 strcpy(ptr, rplc_str);
777 if (strlen(buf) > 384) {
780 fwrite((char *) buf, 128, 1, fp);
781 strcpy(buf, &buf[128]);
788 fwrite((char *) buf, strlen(buf), 1, fp);
791 truncate(filename, wpos);
792 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
796 * Function to begin composing a new message
798 int client_make_message(CtdlIPC *ipc,
799 char *filename, /* temporary file name */
800 char *recipient, /* NULL if it's not mail */
804 char *subject) /* buffer to store subject line */
811 char *editor_path = NULL;
816 if((mode-2) < MAX_EDITORS && strlen(editor_paths[mode-2]) > 0) {
817 editor_path = editor_paths[mode-2];
818 } else if (strlen(editor_paths[0]) > 0) {
819 editor_path = editor_paths[0];
821 err_printf("*** No editor available, "
822 "using built-in editor\n");
827 fmt_date(datestr, sizeof datestr, time(NULL), 0);
830 if (room_flags & QR_ANONONLY && !recipient) {
831 snprintf(header, sizeof header, " ****");
834 snprintf(header, sizeof header,
837 (is_anonymous ? "[anonymous]" : fullname)
839 if (strlen(recipient) > 0) {
840 size_t tmp = strlen(header);
841 snprintf(&header[tmp], sizeof header - tmp,
842 " to %s", recipient);
845 scr_printf("%s\n", header);
846 if (subject != NULL) if (strlen(subject) > 0) {
847 scr_printf("Subject: %s\n", subject);
853 scr_printf("(Press ctrl-d when finished)\n");
857 fp = fopen(filename, "r");
859 fmout(screenwidth, fp, NULL, NULL, 0,
864 fp = fopen(filename, "w");
866 err_printf("*** Error opening temp file!\n"
868 filename, strerror(errno));
878 fp = fopen(filename, "r+");
880 err_printf("*** Error opening temp file!\n"
882 filename, strerror(errno));
890 fp = fopen(filename, "a");
892 err_printf("*** Error opening temp file!\n"
894 filename, strerror(errno));
914 default: /* allow 2+ modes */
915 e_ex_code = 1; /* start with a failed exit code */
919 cksum = file_checksum(filename);
920 if (editor_pid == 0) {
923 chmod(filename, 0600);
924 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
926 execlp(editor_path, editor_path, filename, NULL);
932 b = ka_wait(&e_ex_code);
933 } while ((b != editor_pid) && (b >= 0));
940 MECR: if (mode >= 2) {
941 if (file_checksum(filename) == cksum) {
942 err_printf("*** Aborted message.\n");
950 b = keymenu("Entry command (? for options)",
951 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
953 "<R>eplace string|<H>old message");
962 scr_printf(" %s from %s", datestr, fullname);
963 if (strlen(recipient) > 0)
964 scr_printf(" to %s", recipient);
966 if (subject != NULL) if (strlen(subject) > 0) {
967 scr_printf("Subject: %s\n", subject);
969 fp = fopen(filename, "r");
971 fmout(screenwidth, fp, NULL, NULL,
972 ((userflags & US_PAGINATOR) ? 1 : 0),
980 replace_string(filename, 0L);
987 if (subject != NULL) {
988 newprompt("Subject: ", subject, 70);
995 MEABT: scr_printf("Are you sure? ");
999 MEABT2: unlink(filename);
1006 * Transmit message text to the server.
1008 * This loop also implements a "tick" counter that displays the progress, if
1009 * we're sending something that will take a long time to transmit.
1011 void transmit_message(CtdlIPC *ipc, FILE *fp)
1018 fseek(fp, 0L, SEEK_END);
1021 lasttick = time(NULL);
1023 while (ch = getc(fp), (ch >= 0)) {
1025 if (!strcmp(buf, "000"))
1026 strcpy(buf, ">000");
1027 CtdlIPC_putline(ipc, buf);
1033 if ((ch == 32) && (strlen(buf) > 200)) {
1035 if (!strcmp(buf, "000"))
1036 strcpy(buf, ">000");
1037 CtdlIPC_putline(ipc, buf);
1040 if (strlen(buf) > 250) {
1041 if (!strcmp(buf, "000"))
1042 strcpy(buf, ">000");
1043 CtdlIPC_putline(ipc, buf);
1048 if ((time(NULL) - lasttick) > 2L) {
1049 scr_printf(" %3ld%% completed\r",
1050 ((ftell(fp) * 100L) / msglen));
1052 lasttick = time(NULL);
1056 CtdlIPC_putline(ipc, buf);
1064 * Make sure there's room in msg_arr[] for at least one more.
1066 void check_msg_arr_size(void) {
1067 if ((num_msgs + 1) > msg_arr_size) {
1068 msg_arr_size += 512;
1069 msg_arr = realloc(msg_arr,
1070 ((sizeof(long)) * msg_arr_size) );
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 */
1091 struct ctdlipcmessage message;
1092 unsigned long *msgarr = NULL;
1093 int r; /* IPC response code */
1100 strcpy(subject, "");
1103 * First, check to see if we have permission to enter a message in
1104 * this room. The server will return an error code if we can't.
1106 strcpy(message.recipient, "");
1107 strcpy(message.author, "");
1108 strcpy(message.subject, "");
1109 message.text = message.author; /* point to "", changes later */
1110 message.anonymous = 0;
1111 message.type = mode;
1112 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1114 if (r / 100 != 2 && r / 10 != 57) {
1115 scr_printf("%s\n", buf);
1119 /* Error code 570 is special. It means that we CAN enter a message
1120 * in this room, but a recipient needs to be specified.
1126 /* If the user is a dumbass, tell them how to type. */
1127 if ((userflags & US_EXPERT) == 0) {
1128 formout(ipc, "entermsg");
1131 /* Handle the selection of a recipient, if necessary. */
1133 if (need_recp == 1) {
1136 strcpy(buf, reply_to);
1138 scr_printf("Enter recipient: ");
1139 getline(buf, (SIZ-100) );
1140 if (strlen(buf) == 0)
1144 strcpy(buf, "sysop");
1146 strcpy(message.recipient, buf);
1149 if (strlen(reply_subject) > 0) {
1150 if (!strncasecmp(reply_subject,
1152 strcpy(message.subject, reply_subject);
1155 snprintf(message.subject,
1156 sizeof message.subject,
1163 if (room_flags & QR_ANONOPT) {
1164 scr_printf("Anonymous (Y/N)? ");
1166 message.anonymous = 1;
1169 /* If it's mail, we've got to check the validity of the recipient... */
1170 if (strlen(message.recipient) > 0) {
1171 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1173 scr_printf("%s\n", buf);
1178 /* Learn the number of the newest message in in the room, so we can
1179 * tell upon saving whether someone else has posted too.
1182 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1184 scr_printf("%s\n", buf);
1186 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1190 /* Now compose the message... */
1191 if (client_make_message(ipc, temp, message.recipient,
1192 message.anonymous, 0, c, message.subject) != 0) {
1196 /* Reopen the temp file that was created, so we can send it */
1197 fp = fopen(temp, "r");
1199 if (!fp || !(message.text = load_message_from_file(fp))) {
1200 err_printf("*** Internal error while trying to save message!\n"
1202 temp, strerror(errno));
1209 /* Transmit message to the server */
1210 r = CtdlIPCPostMessage(ipc, 1, &message, buf);
1212 scr_printf("%s\n", buf);
1216 /* Yes, unlink it now, so it doesn't stick around if we crash */
1219 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1221 if (msgarr) free(msgarr);
1223 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1225 scr_printf("%s\n", buf);
1227 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1231 /* get new highest message number in room to set lrp for goto... */
1232 maxmsgnum = msgarr[num_msgs - 1];
1234 /* now see if anyone else has posted in here */
1236 for (a = 0; a < num_msgs; ++a) {
1237 if (msgarr[a] > highmsg) {
1241 if (msgarr) free(msgarr);
1244 /* In the Mail> room, this algorithm always counts one message
1245 * higher than in public rooms, so we decrement it by one.
1252 scr_printf("*** 1 additional message has been entered "
1253 "in this room by another user.\n");
1256 scr_printf("*** %d additional messages have been entered "
1257 "in this room by other users.\n", b);
1264 * Do editing on a quoted file
1266 void process_quote(void)
1268 FILE *qfile, *tfile;
1270 int line, qstart, qend;
1272 /* Unlink the second temp file as soon as it's opened, so it'll get
1273 * deleted even if the program dies
1275 qfile = fopen(temp2, "r");
1278 /* Display the quotable text with line numbers added */
1280 fgets(buf, 128, qfile);
1281 while (fgets(buf, 128, qfile) != NULL) {
1282 scr_printf("%2d %s", ++line, buf);
1284 scr_printf("Begin quoting at [ 1] : ");
1286 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1287 scr_printf(" End quoting at [%d] : ", line);
1289 qend = (buf[0] == 0) ? (line) : atoi(buf);
1292 fgets(buf, 128, qfile);
1293 tfile = fopen(temp, "w");
1294 while (fgets(buf, 128, qfile) != NULL) {
1295 if ((++line >= qstart) && (line <= qend))
1296 fprintf(tfile, " >%s", buf);
1298 fprintf(tfile, " \n");
1307 * List the URL's which were embedded in the previous message
1309 void list_urls(CtdlIPC *ipc)
1314 if (num_urls == 0) {
1315 scr_printf("There were no URL's in the previous message.\n\n");
1319 for (i = 0; i < num_urls; ++i) {
1320 scr_printf("%3d %s\n", i + 1, urls[i]);
1323 if ((i = num_urls) != 1)
1324 i = intprompt("Display which one", 1, 1, num_urls);
1326 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1333 * Run image viewer in background
1335 int do_image_view(const char *filename)
1340 snprintf(cmd, sizeof cmd, imagecmd, filename);
1347 if (childpid == 0) {
1349 pid_t grandchildpid;
1351 grandchildpid = fork();
1352 if (grandchildpid < 0) {
1353 return grandchildpid;
1356 if (grandchildpid == 0) {
1361 nullfd = open("/dev/null", O_WRONLY);
1368 retcode = system(cmd);
1378 if (grandchildpid > 0) {
1386 waitpid(childpid, &retcode, 0);
1395 * View an image attached to a message
1397 void image_view(CtdlIPC *ipc, unsigned long msg)
1399 struct parts *ptr = last_message_parts;
1403 /* Run through available parts */
1404 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1405 if ((!strcasecmp(ptr->disposition, "attachment")
1406 || !strcasecmp(ptr->disposition, "inline"))
1407 && !strncmp(ptr->mimetype, "image/", 6)) {
1410 strcpy(part, ptr->number);
1417 strprompt("View which part (0 when done)", part, SIZ-1);
1419 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1420 if ((!strcasecmp(ptr->disposition, "attachment")
1421 || !strcasecmp(ptr->disposition, "inline"))
1422 && !strncmp(ptr->mimetype, "image/", 6)
1423 && !strcasecmp(ptr->number, part)) {
1426 void *file = NULL; /* The downloaded file */
1431 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1433 scr_printf("%s\n", buf);
1437 len = (size_t)extract_long(buf, 0);
1438 progress(ipc, len, len);
1440 snprintf(tmp, sizeof tmp, "%s.%s",
1443 save_buffer(file, len, tmp);
1457 * Read the messages in the current room
1459 void readmsgs(CtdlIPC *ipc,
1460 enum MessageList c, /* see listing in citadel_ipc.h */
1461 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1462 int q /* Number of msgs to read (if c==3) */
1464 int a, b, e, f, g, start;
1470 char prtfile[PATH_MAX];
1473 char targ[ROOMNAMELEN];
1474 char filename[PATH_MAX];
1475 char save_to[PATH_MAX];
1476 void *attachment = NULL; /* Downloaded attachment */
1477 FILE *dest = NULL; /* Alternate destination other than screen */
1478 int r; /* IPC response code */
1485 strcpy(prtfile, tmpnam(NULL));
1491 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1493 scr_printf("%s\n", cmd);
1495 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1499 if (num_msgs == 0) { /* TODO look at this later */
1500 if (c == LastMessages) return;
1501 scr_printf("*** There are no ");
1502 if (c == NewMessages) scr_printf("new ");
1503 if (c == OldMessages) scr_printf("old ");
1504 scr_printf("messages in this room.\n");
1510 /* this loop cycles through each message... */
1511 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1512 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1513 while (msg_arr[a] == 0L) {
1515 if ((a == num_msgs) || (a == (-1)))
1519 RAGAIN: pagin = ((arcflag == 0)
1521 && (userflags & US_PAGINATOR)) ? 1 : 0;
1523 /* If we're doing a quote, set the screenwidth to 72 */
1525 hold_sw = screenwidth;
1529 /* If printing or archiving, set the screenwidth to 80 */
1531 hold_sw = screenwidth;
1535 /* clear parts list */
1536 free_parts(last_message_parts);
1537 last_message_parts = NULL;
1539 /* now read the message... */
1540 e = read_message(ipc, msg_arr[a], pagin, dest);
1542 /* ...and set the screenwidth back if we have to */
1543 if ((quotflag) || (arcflag)) {
1544 screenwidth = hold_sw;
1546 RMSGREAD: scr_flush();
1547 highest_msg_read = msg_arr[a];
1552 enable_color = hold_color;
1559 enable_color = hold_color;
1562 freopen(prtfile, "r", stdin);
1564 sttybbs(SB_RESTORE);
1565 ka_system(printcmd);
1566 sttybbs(SB_NO_INTR);
1574 } while ((g != f) && (g >= 0));
1575 scr_printf("Message printed.\n");
1577 if (rc_alt_semantics && c == 1) {
1580 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1584 if (((userflags & US_NOPROMPT) || (e == 2))
1585 && (((room_flags & QR_MAILBOX) == 0)
1586 || (rc_force_mail_prompts == 0))) {
1591 color(BRIGHT_WHITE);
1592 scr_printf("%d", num_msgs - a - 1);
1596 keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1597 if (rc_url_cmd[0] && num_urls)
1598 keyopt("<U>RLview ");
1599 if (has_images > 0 && strlen(imagecmd) > 0)
1600 keyopt("<I>mages ");
1601 keyopt("<?>help -> ");
1605 e = (inkey() & 127);
1607 /* return key same as <N> */ if (e == 10)
1609 /* space key same as <N> */ if (e == 32)
1611 /* del/move for aides only */
1613 && ((room_flags & QR_MAILBOX) ==
1615 if ((e == 'd') || (e == 'm'))
1618 /* print only if available */
1619 if ((e == 'p') && (strlen(printcmd) == 0))
1621 /* can't file if not allowed */
1623 && (rc_allow_attachments == 0))
1625 /* link only if browser avail*/
1627 && (strlen(rc_url_cmd) == 0))
1630 && (!strlen(imagecmd) || !has_images))
1632 } while ((e != 'a') && (e != 'n') && (e != 's')
1633 && (e != 'd') && (e != 'm') && (e != 'p')
1634 && (e != 'q') && (e != 'b') && (e != 'h')
1635 && (e != 'r') && (e != 'f') && (e != '?')
1636 && (e != 'u') && (e != 'c') && (e != 'y')
1643 scr_printf("Again");
1646 scr_printf("Delete");
1658 scr_printf("Print");
1661 scr_printf("Quote");
1667 scr_printf("Header");
1670 scr_printf("Reply");
1676 scr_printf("URL's");
1679 scr_printf("mY next");
1684 scr_printf("? <help>");
1687 if (userflags & US_DISAPPEAR || e == 'i')
1688 scr_printf("\r%79s\r", "");
1695 scr_printf("Options available here:\n"
1696 " ? Help (prints this message)\n"
1697 " S Stop reading immediately\n"
1698 " A Again (repeats last message)\n"
1699 " N Next (continue with next message)\n"
1700 " Y My Next (continue with next message you authored)\n"
1701 " B Back (go back to previous message)\n");
1703 || (room_flags & QR_MAILBOX)) {
1704 scr_printf(" D Delete this message\n"
1705 " M Move message to another room\n");
1707 scr_printf(" C Copy message to another room\n");
1708 if (strlen(printcmd) > 0)
1709 scr_printf(" P Print this message\n");
1711 " Q Quote portions of this message for your next post\n"
1712 " H Headers (display message headers only)\n");
1714 scr_printf(" R Reply to this message\n");
1715 if (rc_allow_attachments)
1717 (" F (save attachments to a file)\n");
1718 if (strlen(rc_url_cmd) > 0)
1719 scr_printf(" U (list URL's for display)\n");
1720 if (strlen(imagecmd) > 0 && has_images > 0)
1721 scr_printf(" I Image viewer\n");
1726 dest = fopen(prtfile, "w");
1728 hold_color = enable_color;
1733 dest = fopen(temp2, "w");
1735 hold_color = enable_color;
1747 newprompt("Enter target room: ",
1748 targ, ROOMNAMELEN - 1);
1749 if (strlen(targ) > 0) {
1750 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1751 msg_arr[a], targ, cmd);
1752 scr_printf("%s\n", cmd);
1758 if (r / 100 != 2) /* r will be init'ed, FIXME */
1759 goto RMSGREAD; /* the logic here sucks */
1762 newprompt("Which section? ", filename,
1763 ((sizeof filename) - 1));
1764 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1765 filename, &attachment, progress, cmd);
1767 scr_printf("%s\n", cmd);
1769 extract_token(filename, cmd, 2, '|', sizeof filename);
1771 * Part 1 won't have a filename; use the
1772 * subject of the message instead. IO
1774 if (!strlen(filename))
1775 strcpy(filename, reply_subject);
1776 destination_directory(save_to, filename);
1777 save_buffer(attachment,
1778 extract_unsigned_long(cmd, 0),
1787 scr_printf("*** Delete this message? ");
1789 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1790 scr_printf("%s\n", cmd);
1798 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1801 savedpos = num_msgs;
1802 entmsg(ipc, 1, (rc_reply_extedit ? 2 : 0));
1803 num_msgs = savedpos;
1809 image_view(ipc, msg_arr[a]);
1812 { /* hack hack hack */
1813 /* find the next message by me, stay here if we find nothing */
1816 for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1818 /* This is repetitively dumb, but that's what computers are for.
1819 We have to load up messages until we find one by us */
1822 struct ctdlipcmessage *msg = NULL;
1824 /* read the header so we can get 'from=' */
1825 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1826 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1827 a = lasta; /* meesa current */
1835 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1839 } /* end for loop */
1840 } /* end read routine */
1846 * View and edit a system message
1848 void edit_system_message(CtdlIPC *ipc, char *which_message)
1852 char write_cmd[SIZ];
1854 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1855 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1856 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1857 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1864 * Verify the message base
1866 void check_message_base(CtdlIPC *ipc)
1869 char *transcript = NULL;
1870 int r; /* IPC response code */
1873 ("Please read the documentation before running this command.\n"
1874 "Having done so, do you still want to check the message base? ");
1878 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1880 scr_printf("%s\n", buf);
1884 while (transcript && strlen(transcript)) {
1886 extract_token(buf, transcript, 0, '\n', sizeof buf);
1887 remove_token(transcript, 0, '\n');
1888 pprintf("%s\n", buf);
1890 if (transcript) free(transcript);
1896 * Loads the contents of a file into memory. Caller must free the allocated
1899 char *load_message_from_file(FILE *src)
1905 fseek(src, 0, SEEK_END);
1909 dest = (char *)calloc(1, i + 1);
1916 g = fread(dest + got, 1, i - got, src);
1919 /* Interrupted system call, keep going */
1922 /* At this point we have either EOF or error */