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 unsigned room_flags2;
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 if ((a != 32) && (prev == 13)) {
265 add_word(textlist, "\n");
269 if ((a == 32) && (prev == 13)) {
270 add_word(textlist, "\n");
271 add_newline(textlist);
275 if (!IsEmptyStr(wordbuf)) {
276 wordbuf[strlen(wordbuf) - 1] = 0;
281 } else if (a == 23) {
283 wordbuf[strlen(wordbuf) - 1] = 0;
287 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
288 } else if (a == 13) {
290 if (IsEmptyStr(wordbuf))
293 for (b = 0; b < strlen(wordbuf); ++b)
294 if (wordbuf[b] == 32) {
302 add_word(textlist, wordbuf);
307 wordbuf[strlen(wordbuf) + 1] = 0;
308 wordbuf[strlen(wordbuf)] = a;
310 if ((strlen(wordbuf) + 3) > screenwidth) {
312 for (b = 0; b < strlen(wordbuf); ++b)
313 if (wordbuf[b] == 32)
315 if (last_space >= 0) {
316 for (b = 0; b < strlen(wordbuf); ++b)
317 if (wordbuf[b] == 32) {
325 for (b = 0; b < strlen(wordbuf); ++b) {
330 scr_printf("\n%s", wordbuf);
332 add_word(textlist, wordbuf);
338 } while (finished == 0);
341 /* write the buffer back to disk */
343 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
344 fprintf(fp, "%s", ptr->text);
348 ftruncate(fileno(fp), ftell(fp));
350 /* and deallocate the memory we used */
351 while (textlist != NULL) {
352 ptr = textlist->next;
360 * Free the struct parts
362 void free_parts(struct parts *p)
364 struct parts *a_part = p;
370 a_part = a_part->next;
377 * Read a message from the server
379 int read_message(CtdlIPC *ipc,
380 long num, /* message number */
381 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
382 FILE *dest) /* Destination file, NULL for screen */
389 struct ctdlipcmessage *message = NULL;
390 int r; /* IPC response code */
391 char *converted_text = NULL;
398 int final_line_is_blank = 0;
405 strcpy(reply_to, NO_REPLY_TO);
406 strcpy(reply_subject, "");
408 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
409 (can_do_msg4 ? 4 : 0),
412 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
414 lines_printed = checkpagin(lines_printed, pagin, screenheight);
417 free_parts(message->attachments);
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 (!IsEmptyStr(message->subject)) {
442 pprintf("subj=%s\n", message->subject);
444 if (!IsEmptyStr(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 (!IsEmptyStr(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,
467 free_parts(message->attachments);
472 if (rc_display_message_numbers) {
474 fprintf(dest, "[#%s] ", message->msgid);
479 scr_printf("#%s", message->msgid);
484 if (nhdr == 1 && !is_room_aide) {
486 fprintf(dest, " ****");
491 fmt_date(now, sizeof now, message->time, 0);
493 fprintf(dest, "%s from %s ", now, message->author);
494 if (!IsEmptyStr(message->email)) {
495 fprintf(dest, "<%s> ", message->email);
499 scr_printf("%s ", now);
503 scr_printf("%s ", message->author);
504 if (!IsEmptyStr(message->email)) {
508 scr_printf("%s", message->email);
513 if (!IsEmptyStr(message->node)) {
514 if ((room_flags & QR_NETWORK)
515 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
516 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
517 if (IsEmptyStr(message->email)) {
519 fprintf(dest, "@%s ", message->node);
523 color(BRIGHT_YELLOW);
524 scr_printf("%s ", message->node);
529 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
530 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
532 fprintf(dest, "(%s) ", message->hnod);
537 scr_printf("%s", message->hnod);
542 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
544 fprintf(dest, "in %s> ", message->room);
548 color(BRIGHT_MAGENTA);
549 scr_printf("%s> ", message->room);
552 if (!IsEmptyStr(message->recipient)) {
554 fprintf(dest, "to %s ", message->recipient);
559 scr_printf("%s ", message->recipient);
570 /* Set the reply-to address to an Internet e-mail address if possible
572 if (message->email != NULL) if (!IsEmptyStr(message->email)) {
573 if (!IsEmptyStr(message->author)) {
574 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
577 safestrncpy(reply_to, message->email, sizeof reply_to);
581 /* But if we can't do that, set it to a Citadel address.
583 if (!strcmp(reply_to, NO_REPLY_TO)) {
584 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
585 message->author, message->node);
590 lines_printed = checkpagin(lines_printed, pagin, screenheight);
593 if (message->subject != NULL) {
594 safestrncpy(reply_subject, message->subject,
595 sizeof reply_subject);
596 if (!IsEmptyStr(message->subject)) {
598 fprintf(dest, "Subject: %s\n",
602 scr_printf("Subject: ");
604 scr_printf("%s\n", message->subject);
606 lines_printed = checkpagin(lines_printed,
607 pagin, screenheight);
612 if (pagin == 1 && !dest) {
616 /******* end of header output, start of message text output *******/
619 * Convert HTML to plain text, formatting for the actual width
620 * of the client screen.
622 if (!strcasecmp(message->content_type, "text/html")) {
623 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
624 if (converted_text != NULL) {
626 message->text = converted_text;
631 /* Text/plain is a different type */
632 if (!strcasecmp(message->content_type, "text/plain")) {
637 num_urls = 0; /* Start with a clean slate */
638 searchptr = message->text;
639 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
640 searchptr = strstr(searchptr, "http://");
641 if (searchptr != NULL) {
642 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
643 for (i = 0; i < strlen(urls[num_urls]); i++) {
644 ch = urls[num_urls][i];
645 if (ch == '>' || ch == '\"' || ch == ')' ||
646 ch == ' ' || ch == '\n') {
647 urls[num_urls][i] = 0;
659 if (format_type == 0) {
660 fr = fmout(screenwidth, NULL, message->text, dest,
661 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
663 /* renderer for text/plain */
665 lineptr = message->text;
668 nextline = strchr(lineptr, '\n');
669 if (nextline != NULL) {
672 if (*nextline == 0) nextline = NULL;
675 if (sigcaught == 0) {
676 linelen = strlen(lineptr);
677 if (linelen && (lineptr[linelen-1] == '\r')) {
678 lineptr[--linelen] = 0;
681 fprintf(dest, "%s\n", lineptr);
683 scr_printf("%s\n", lineptr);
684 lines_printed = lines_printed + 1 +
685 (linelen / screenwidth);
687 checkpagin(lines_printed, pagin,
691 if (lineptr[0] == 0) final_line_is_blank = 1;
692 else final_line_is_blank = 0;
697 if (!final_line_is_blank) {
704 lines_printed = checkpagin(lines_printed, pagin, screenheight);
709 /* Enumerate any attachments */
710 if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
713 for (ptr = message->attachments; ptr; ptr = ptr->next) {
714 if ( (!strcasecmp(ptr->disposition, "attachment"))
715 || (!strcasecmp(ptr->disposition, "inline"))
716 || (!strcasecmp(ptr->disposition, ""))
718 if ( (strcasecmp(ptr->number, message->mime_chosen))
719 && (!IsEmptyStr(ptr->mimetype))
723 color(BRIGHT_MAGENTA);
724 pprintf("%s", ptr->number);
728 pprintf("%s", ptr->filename);
730 pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
731 if (!strncmp(ptr->mimetype, "image/", 6)) {
739 /* Save the attachments info for later */
740 last_message_parts = message->attachments;
746 if (pagin == 1 && !dest)
753 * replace string function for the built-in editor
755 void replace_string(char *filename, long int startpos)
764 int substitutions = 0;
767 scr_printf("Enter text to be replaced:\n: ");
768 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
769 if (IsEmptyStr(srch_str))
772 scr_printf("Enter text to replace it with:\n: ");
773 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
775 fp = fopen(filename, "r+");
780 fseek(fp, startpos, 0);
782 while (a = getc(fp), a > 0) {
784 buf[strlen(buf) + 1] = 0;
785 buf[strlen(buf)] = a;
786 if (strlen(buf) >= strlen(srch_str)) {
787 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
788 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
789 strcpy(ptr, rplc_str);
793 if (strlen(buf) > 384) {
796 fwrite((char *) buf, 128, 1, fp);
797 strcpy(buf, &buf[128]);
803 if (!IsEmptyStr(buf))
804 fwrite((char *) buf, strlen(buf), 1, fp);
807 truncate(filename, wpos);
808 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
812 * Function to begin composing a new message
814 int client_make_message(CtdlIPC *ipc,
815 char *filename, /* temporary file name */
816 char *recipient, /* NULL if it's not mail */
820 char *subject, /* buffer to store subject line */
821 int subject_required)
828 char *editor_path = NULL;
833 if((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2])) {
834 editor_path = editor_paths[mode-2];
835 } else if (!IsEmptyStr(editor_paths[0])) {
836 editor_path = editor_paths[0];
838 err_printf("*** No editor available, "
839 "using built-in editor\n");
844 fmt_date(datestr, sizeof datestr, time(NULL), 0);
847 if (room_flags & QR_ANONONLY && !recipient) {
848 snprintf(header, sizeof header, " ****");
851 snprintf(header, sizeof header,
854 (is_anonymous ? "[anonymous]" : fullname)
856 if (!IsEmptyStr(recipient)) {
857 size_t tmp = strlen(header);
858 snprintf(&header[tmp], sizeof header - tmp,
859 " to %s", recipient);
862 scr_printf("%s\n", header);
863 if (subject != NULL) if (!IsEmptyStr(subject)) {
864 scr_printf("Subject: %s\n", subject);
867 if ( (subject_required) && (IsEmptyStr(subject)) ) {
868 newprompt("Subject: ", subject, 70);
874 scr_printf("(Press ctrl-d when finished)\n");
878 fp = fopen(filename, "r");
880 fmout(screenwidth, fp, NULL, NULL, 0,
885 fp = fopen(filename, "w");
887 err_printf("*** Error opening temp file!\n"
889 filename, strerror(errno));
899 fp = fopen(filename, "r+");
901 err_printf("*** Error opening temp file!\n"
903 filename, strerror(errno));
911 fp = fopen(filename, "a");
913 err_printf("*** Error opening temp file!\n"
915 filename, strerror(errno));
935 default: /* allow 2+ modes */
936 e_ex_code = 1; /* start with a failed exit code */
938 stty_ctdl(SB_RESTORE);
940 cksum = file_checksum(filename);
941 if (editor_pid == 0) {
944 chmod(filename, 0600);
945 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
947 execlp(editor_path, editor_path, filename, NULL);
953 b = ka_wait(&e_ex_code);
954 } while ((b != editor_pid) && (b >= 0));
961 MECR: if (mode >= 2) {
962 if (file_checksum(filename) == cksum) {
963 err_printf("*** Aborted message.\n");
966 if (e_ex_code == 0) {
972 b = keymenu("Entry command (? for options)",
973 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
975 "<R>eplace string|<H>old message");
977 if (b == 'a') goto MEABT;
978 if (b == 'c') goto ME1;
979 if (b == 's') goto MEFIN;
981 scr_printf(" %s from %s", datestr, fullname);
982 if (!IsEmptyStr(recipient)) {
983 scr_printf(" to %s", recipient);
986 if (subject != NULL) if (!IsEmptyStr(subject)) {
987 scr_printf("Subject: %s\n", subject);
989 fp = fopen(filename, "r");
991 fmout(screenwidth, fp, NULL, NULL,
992 ((userflags & US_PAGINATOR) ? 1 : 0),
1000 replace_string(filename, 0L);
1007 if (subject != NULL) {
1008 newprompt("Subject: ", subject, 70);
1015 MEABT: scr_printf("Are you sure? ");
1019 MEABT2: unlink(filename);
1026 * Transmit message text to the server.
1028 * This loop also implements a "tick" counter that displays the progress, if
1029 * we're sending something that will take a long time to transmit.
1031 void transmit_message(CtdlIPC *ipc, FILE *fp)
1038 fseek(fp, 0L, SEEK_END);
1041 lasttick = time(NULL);
1043 while (ch = getc(fp), (ch >= 0)) {
1045 if (!strcmp(buf, "000"))
1046 strcpy(buf, ">000");
1047 CtdlIPC_putline(ipc, buf);
1053 if ((ch == 32) && (strlen(buf) > 200)) {
1055 if (!strcmp(buf, "000"))
1056 strcpy(buf, ">000");
1057 CtdlIPC_putline(ipc, buf);
1060 if (strlen(buf) > 250) {
1061 if (!strcmp(buf, "000"))
1062 strcpy(buf, ">000");
1063 CtdlIPC_putline(ipc, buf);
1068 if ((time(NULL) - lasttick) > 2L) {
1069 scr_printf(" %3ld%% completed\r",
1070 ((ftell(fp) * 100L) / msglen));
1072 lasttick = time(NULL);
1076 CtdlIPC_putline(ipc, buf);
1084 * Make sure there's room in msg_arr[] for at least one more.
1086 void check_msg_arr_size(void) {
1087 if ((num_msgs + 1) > msg_arr_size) {
1088 msg_arr_size += 512;
1089 msg_arr = realloc(msg_arr,
1090 ((sizeof(long)) * msg_arr_size) );
1096 * break_big_lines() - break up lines that are >1024 characters
1097 * otherwise the server will truncate
1099 void break_big_lines(char *msg) {
1108 while (strlen(ptr) > 1000) {
1109 break_here = strchr(&ptr[900], ' ');
1110 if ((break_here == NULL) || (break_here > &ptr[999])) {
1111 break_here = &ptr[999];
1120 * entmsg() - edit and create a message
1121 * returns 0 if message was saved
1123 int entmsg(CtdlIPC *ipc,
1124 int is_reply, /* nonzero if this was a <R>eply command */
1126 int masquerade /* prompt for a non-default display name? */
1135 struct ctdlipcmessage message;
1136 unsigned long *msgarr = NULL;
1137 int r; /* IPC response code */
1138 int subject_required = 0;
1145 strcpy(subject, "");
1148 * First, check to see if we have permission to enter a message in
1149 * this room. The server will return an error code if we can't.
1151 strcpy(message.recipient, "");
1152 strcpy(message.author, "");
1153 strcpy(message.subject, "");
1154 message.text = ""; /* point to "", changes later */
1155 message.anonymous = 0;
1156 message.type = mode;
1159 newprompt("Display name for this message: ", message.author, 40);
1162 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1164 if (r / 100 != 2 && r / 10 != 57) {
1165 scr_printf("%s\n", buf);
1169 /* Error code 570 is special. It means that we CAN enter a message
1170 * in this room, but a recipient needs to be specified.
1177 /* If the user is a dumbass, tell them how to type. */
1178 if ((userflags & US_EXPERT) == 0) {
1179 formout(ipc, "entermsg");
1182 /* Handle the selection of a recipient, if necessary. */
1184 if (need_recp == 1) {
1187 strcpy(buf, reply_to);
1189 scr_printf("Enter recipient: ");
1190 ctdl_getline(buf, (SIZ-100) );
1191 if (IsEmptyStr(buf))
1195 strcpy(buf, "sysop");
1197 strcpy(message.recipient, buf);
1200 if (!IsEmptyStr(reply_subject)) {
1201 if (!strncasecmp(reply_subject,
1203 strcpy(message.subject, reply_subject);
1206 snprintf(message.subject,
1207 sizeof message.subject,
1214 if (room_flags & QR_ANONOPT) {
1215 scr_printf("Anonymous (Y/N)? ");
1217 message.anonymous = 1;
1220 /* If it's mail, we've got to check the validity of the recipient... */
1221 if (!IsEmptyStr(message.recipient)) {
1222 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1224 scr_printf("%s\n", buf);
1229 /* Learn the number of the newest message in in the room, so we can
1230 * tell upon saving whether someone else has posted too.
1233 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1235 scr_printf("%s\n", buf);
1237 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1241 /* Now compose the message... */
1242 if (client_make_message(ipc, temp, message.recipient,
1243 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1244 if (msgarr) free(msgarr);
1248 /* Reopen the temp file that was created, so we can send it */
1249 fp = fopen(temp, "r");
1251 if (!fp || !(message.text = load_message_from_file(fp))) {
1252 err_printf("*** Internal error while trying to save message!\n"
1254 temp, strerror(errno));
1261 /* Break lines that are >1024 characters, otherwise the server
1262 * will truncate them.
1264 break_big_lines(message.text);
1266 /* Transmit message to the server */
1267 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1269 scr_printf("%s\n", buf);
1273 /* Yes, unlink it now, so it doesn't stick around if we crash */
1276 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1278 if (msgarr) free(msgarr);
1280 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1282 scr_printf("%s\n", buf);
1284 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1288 /* get new highest message number in room to set lrp for goto... */
1289 maxmsgnum = msgarr[num_msgs - 1];
1291 /* now see if anyone else has posted in here */
1293 for (a = 0; a < num_msgs; ++a) {
1294 if (msgarr[a] > highmsg) {
1298 if (msgarr) free(msgarr);
1301 /* In the Mail> room, this algorithm always counts one message
1302 * higher than in public rooms, so we decrement it by one.
1309 scr_printf("*** 1 additional message has been entered "
1310 "in this room by another user.\n");
1313 scr_printf("*** %d additional messages have been entered "
1314 "in this room by other users.\n", b);
1322 * Do editing on a quoted file
1324 void process_quote(void)
1326 FILE *qfile, *tfile;
1328 int line, qstart, qend;
1330 /* Unlink the second temp file as soon as it's opened, so it'll get
1331 * deleted even if the program dies
1333 qfile = fopen(temp2, "r");
1336 /* Display the quotable text with line numbers added */
1338 fgets(buf, 128, qfile);
1339 while (fgets(buf, 128, qfile) != NULL) {
1340 scr_printf("%2d %s", ++line, buf);
1342 scr_printf("Begin quoting at [ 1] : ");
1343 ctdl_getline(buf, 3);
1344 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1345 scr_printf(" End quoting at [%d] : ", line);
1346 ctdl_getline(buf, 3);
1347 qend = (buf[0] == 0) ? (line) : atoi(buf);
1350 fgets(buf, 128, qfile);
1351 tfile = fopen(temp, "w");
1352 while (fgets(buf, 128, qfile) != NULL) {
1353 if ((++line >= qstart) && (line <= qend))
1354 fprintf(tfile, " >%s", buf);
1356 fprintf(tfile, " \n");
1365 * List the URL's which were embedded in the previous message
1367 void list_urls(CtdlIPC *ipc)
1372 if (num_urls == 0) {
1373 scr_printf("There were no URL's in the previous message.\n\n");
1377 for (i = 0; i < num_urls; ++i) {
1378 scr_printf("%3d %s\n", i + 1, urls[i]);
1381 if ((i = num_urls) != 1)
1382 i = intprompt("Display which one", 1, 1, num_urls);
1384 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1391 * Run image viewer in background
1393 int do_image_view(const char *filename)
1398 snprintf(cmd, sizeof cmd, imagecmd, filename);
1405 if (childpid == 0) {
1407 pid_t grandchildpid;
1409 grandchildpid = fork();
1410 if (grandchildpid < 0) {
1411 return grandchildpid;
1414 if (grandchildpid == 0) {
1419 nullfd = open("/dev/null", O_WRONLY);
1426 retcode = system(cmd);
1436 if (grandchildpid > 0) {
1444 waitpid(childpid, &retcode, 0);
1453 * View an image attached to a message
1455 void image_view(CtdlIPC *ipc, unsigned long msg)
1457 struct parts *ptr = last_message_parts;
1461 /* Run through available parts */
1462 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1463 if ((!strcasecmp(ptr->disposition, "attachment")
1464 || !strcasecmp(ptr->disposition, "inline"))
1465 && !strncmp(ptr->mimetype, "image/", 6)) {
1468 strcpy(part, ptr->number);
1475 strprompt("View which part (0 when done)", part, SIZ-1);
1477 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1478 if ((!strcasecmp(ptr->disposition, "attachment")
1479 || !strcasecmp(ptr->disposition, "inline"))
1480 && !strncmp(ptr->mimetype, "image/", 6)
1481 && !strcasecmp(ptr->number, part)) {
1484 void *file = NULL; /* The downloaded file */
1489 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1491 scr_printf("%s\n", buf);
1495 len = (size_t)extract_long(buf, 0);
1496 progress(ipc, len, len);
1498 CtdlMakeTempFileName(tmp, sizeof tmp);
1499 strcat(tmp, ptr->filename);
1500 save_buffer(file, len, tmp);
1514 * Read the messages in the current room
1516 void readmsgs(CtdlIPC *ipc,
1517 enum MessageList c, /* see listing in citadel_ipc.h */
1518 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1519 int q /* Number of msgs to read (if c==3) */
1521 int a, b, e, f, g, start;
1527 char prtfile[PATH_MAX];
1530 char targ[ROOMNAMELEN];
1531 char filename[PATH_MAX];
1532 char save_to[PATH_MAX];
1533 void *attachment = NULL; /* Downloaded attachment */
1534 FILE *dest = NULL; /* Alternate destination other than screen */
1535 int r; /* IPC response code */
1542 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1548 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1550 scr_printf("%s\n", cmd);
1552 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1556 if (num_msgs == 0) { /* TODO look at this later */
1557 if (c == LastMessages) return;
1558 scr_printf("*** There are no ");
1559 if (c == NewMessages) scr_printf("new ");
1560 if (c == OldMessages) scr_printf("old ");
1561 scr_printf("messages in this room.\n");
1567 /* this loop cycles through each message... */
1568 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1569 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1570 while (msg_arr[a] == 0L) {
1572 if ((a == num_msgs) || (a == (-1)))
1576 RAGAIN: pagin = ((arcflag == 0)
1578 && (userflags & US_PAGINATOR)) ? 1 : 0;
1580 /* If we're doing a quote, set the screenwidth to 72 */
1582 hold_sw = screenwidth;
1586 /* If printing or archiving, set the screenwidth to 80 */
1588 hold_sw = screenwidth;
1592 /* clear parts list */
1593 free_parts(last_message_parts);
1594 last_message_parts = NULL;
1596 /* now read the message... */
1597 e = read_message(ipc, msg_arr[a], pagin, dest);
1599 /* ...and set the screenwidth back if we have to */
1600 if ((quotflag) || (arcflag)) {
1601 screenwidth = hold_sw;
1603 RMSGREAD: scr_flush();
1604 highest_msg_read = msg_arr[a];
1609 enable_color = hold_color;
1616 enable_color = hold_color;
1619 freopen(prtfile, "r", stdin);
1621 stty_ctdl(SB_RESTORE);
1622 ka_system(printcmd);
1623 stty_ctdl(SB_NO_INTR);
1631 } while ((g != f) && (g >= 0));
1632 scr_printf("Message printed.\n");
1634 if (rc_alt_semantics && c == 1) {
1637 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1641 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1642 && (((room_flags & QR_MAILBOX) == 0)
1643 || (rc_force_mail_prompts == 0))) {
1648 color(BRIGHT_WHITE);
1649 scr_printf("%d", num_msgs - a - 1);
1653 keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
1654 if (rc_url_cmd[0] && num_urls)
1655 keyopt("<U>RLview ");
1656 if (has_images > 0 && !IsEmptyStr(imagecmd))
1657 keyopt("<I>mages ");
1658 keyopt("<?>help -> ");
1662 e = (inkey() & 127);
1664 /* return key same as <N> */ if (e == 10)
1666 /* space key same as <N> */ if (e == 32)
1668 /* del/move for aides only */
1669 if ( (!is_room_aide)
1670 && ((room_flags & QR_MAILBOX) == 0)
1671 && ((room_flags2 & QR2_COLLABDEL) == 0)
1673 if ((e == 'd') || (e == 'm'))
1676 /* print only if available */
1677 if ((e == 'p') && (IsEmptyStr(printcmd)))
1679 /* can't file if not allowed */
1681 && (rc_allow_attachments == 0))
1683 /* link only if browser avail*/
1685 && (IsEmptyStr(rc_url_cmd)))
1688 && (IsEmptyStr(imagecmd) || !has_images))
1690 } while ((e != 'a') && (e != 'n') && (e != 's')
1691 && (e != 'd') && (e != 'm') && (e != 'p')
1692 && (e != 'q') && (e != 'b') && (e != 'h')
1693 && (e != 'r') && (e != 'f') && (e != '?')
1694 && (e != 'u') && (e != 'c') && (e != 'y')
1701 scr_printf("Again");
1704 scr_printf("Delete");
1716 scr_printf("Print");
1719 scr_printf("Quote");
1725 scr_printf("Header");
1728 scr_printf("Reply");
1734 scr_printf("URL's");
1737 scr_printf("mY next");
1742 scr_printf("? <help>");
1745 if (userflags & US_DISAPPEAR || e == 'i')
1746 scr_printf("\r%79s\r", "");
1753 scr_printf("Options available here:\n"
1754 " ? Help (prints this message)\n"
1755 " S Stop reading immediately\n"
1756 " A Again (repeats last message)\n"
1757 " N Next (continue with next message)\n"
1758 " Y My Next (continue with next message you authored)\n"
1759 " B Back (go back to previous message)\n");
1761 || (room_flags & QR_MAILBOX)
1762 || (room_flags2 & QR2_COLLABDEL)
1764 scr_printf(" D Delete this message\n"
1765 " M Move message to another room\n");
1767 scr_printf(" C Copy message to another room\n");
1768 if (!IsEmptyStr(printcmd))
1769 scr_printf(" P Print this message\n");
1771 " Q Quote portions of this message for your next post\n"
1772 " H Headers (display message headers only)\n");
1774 scr_printf(" R Reply to this message\n");
1775 if (rc_allow_attachments)
1777 (" F (save attachments to a file)\n");
1778 if (!IsEmptyStr(rc_url_cmd))
1779 scr_printf(" U (list URL's for display)\n");
1780 if (!IsEmptyStr(imagecmd) && has_images > 0)
1781 scr_printf(" I Image viewer\n");
1786 dest = fopen(prtfile, "w");
1788 hold_color = enable_color;
1793 dest = fopen(temp2, "w");
1795 hold_color = enable_color;
1807 newprompt("Enter target room: ",
1808 targ, ROOMNAMELEN - 1);
1809 if (!IsEmptyStr(targ)) {
1810 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1811 msg_arr[a], targ, cmd);
1812 scr_printf("%s\n", cmd);
1818 if (r / 100 != 2) /* r will be init'ed, FIXME */
1819 goto RMSGREAD; /* the logic here sucks */
1822 newprompt("Which section? ", filename,
1823 ((sizeof filename) - 1));
1824 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1825 filename, &attachment, progress, cmd);
1827 scr_printf("%s\n", cmd);
1829 extract_token(filename, cmd, 2, '|', sizeof filename);
1831 * Part 1 won't have a filename; use the
1832 * subject of the message instead. IO
1834 if (IsEmptyStr(filename))
1835 strcpy(filename, reply_subject);
1836 destination_directory(save_to, filename);
1837 save_buffer(attachment,
1838 extract_unsigned_long(cmd, 0),
1847 scr_printf("*** Delete this message? ");
1849 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1850 scr_printf("%s\n", cmd);
1858 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1861 savedpos = num_msgs;
1862 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1863 num_msgs = savedpos;
1869 image_view(ipc, msg_arr[a]);
1872 { /* hack hack hack */
1873 /* find the next message by me, stay here if we find nothing */
1876 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1878 /* This is repetitively dumb, but that's what computers are for.
1879 We have to load up messages until we find one by us */
1882 struct ctdlipcmessage *msg = NULL;
1884 /* read the header so we can get 'from=' */
1885 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1886 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1887 a = lasta; /* meesa current */
1895 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1899 } /* end for loop */
1900 } /* end read routine */
1906 * View and edit a system message
1908 void edit_system_message(CtdlIPC *ipc, char *which_message)
1912 char write_cmd[SIZ];
1914 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1915 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1916 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1917 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1924 * Verify the message base
1926 void check_message_base(CtdlIPC *ipc)
1929 char *transcript = NULL;
1930 int r; /* IPC response code */
1933 ("Please read the documentation before running this command.\n"
1934 "Having done so, do you still want to check the message base? ");
1938 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1940 scr_printf("%s\n", buf);
1944 while (transcript && !IsEmptyStr(transcript)) {
1946 extract_token(buf, transcript, 0, '\n', sizeof buf);
1947 remove_token(transcript, 0, '\n');
1948 pprintf("%s\n", buf);
1950 if (transcript) free(transcript);
1956 * Loads the contents of a file into memory. Caller must free the allocated
1959 char *load_message_from_file(FILE *src)
1965 fseek(src, 0, SEEK_END);
1969 dest = (char *)calloc(1, i + 1);
1976 g = fread(dest + got, 1, i - got, src);
1979 /* Interrupted system call, keep going */
1982 /* At this point we have either EOF or error */