2 * Text client functions for reading and writing of messages
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <sys/types.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
45 #include <libcitadel.h>
47 #include "citadel_ipc.h"
48 #include "citadel_decls.h"
57 #define MAXWORDBUF SIZ
58 #define NO_REPLY_TO "nobody ... xxxxxx"
61 char reply_subject[SIZ];
62 char reply_references[SIZ];
63 char reply_inreplyto[SIZ];
67 char text[MAXWORDBUF];
70 void stty_ctdl(int cmd);
71 int haschar(const char *st, int ch);
72 void ctdl_getline(char *string, int lim);
73 int file_checksum(char *filename);
74 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
76 unsigned long *msg_arr = NULL;
79 char rc_alt_semantics;
80 extern char room_name[];
81 extern char tempdir[];
82 extern unsigned room_flags;
83 extern unsigned room_flags2;
85 extern long highest_msg_read;
88 extern int screenwidth;
89 extern int screenheight;
90 extern long maxmsgnum;
93 extern char is_room_aide;
94 extern char fullname[];
96 extern unsigned userflags;
97 extern char sigcaught;
98 extern char printcmd[];
99 extern int rc_allow_attachments;
100 extern int rc_display_message_numbers;
101 extern int rc_force_mail_prompts;
102 extern int editor_pid;
103 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
105 char urls[MAXURLS][SIZ];
107 int has_images = 0; /* Current msg has images */
108 struct parts *last_message_parts = NULL; /* Parts from last msg */
112 void ka_sigcatch(int signum)
115 signal(SIGALRM, ka_sigcatch);
116 CtdlIPCNoop(ipc_for_signal_handlers);
121 * server keep-alive version of wait() (needed for external editor)
123 pid_t ka_wait(int *kstatus)
128 signal(SIGALRM, ka_sigcatch);
132 } while (errno == EINTR);
133 signal(SIGALRM, SIG_IGN);
140 * version of system() that uses ka_wait()
142 int ka_system(char *shc)
151 perror("Cannot fork");
153 return ((pid_t) childpid);
157 execlp("/bin/sh", "sh", "-c", shc, NULL);
163 waitpid = ka_wait(&retcode);
164 } while (waitpid != childpid);
174 * add a newline to the buffer...
176 void add_newline(struct cittext *textlist)
181 while (ptr->next != NULL)
184 while (ptr->text[strlen(ptr->text) - 1] == 32)
185 ptr->text[strlen(ptr->text) - 1] = 0;
186 /* strcat(ptr->text,"\n"); */
188 ptr->next = (struct cittext *)
189 malloc(sizeof(struct cittext));
192 strcpy(ptr->text, "");
197 * add a word to the buffer...
199 void add_word(struct cittext *textlist, char *wordbuf)
204 while (ptr->next != NULL)
207 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
208 ptr->next = (struct cittext *)
209 malloc(sizeof(struct cittext));
212 strcpy(ptr->text, "");
215 strcat(ptr->text, wordbuf);
216 strcat(ptr->text, " ");
221 * begin editing of an opened file pointed to by fp
223 void citedit(FILE *fp)
225 int a, prev, finished, b, last_space;
227 struct cittext *textlist = NULL;
229 char wordbuf[MAXWORDBUF];
232 /* first, load the text into the buffer */
234 textlist = (struct cittext *) malloc(sizeof(struct cittext));
235 textlist->next = NULL;
236 strcpy(textlist->text, "");
240 while (a = getc(fp), a >= 0) {
242 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
243 add_word(textlist, wordbuf);
245 if ((prev == 13) || (prev == 10)) {
246 add_word(textlist, "\n");
247 add_newline(textlist);
248 add_word(textlist, "");
251 wordbuf[strlen(wordbuf) + 1] = 0;
252 wordbuf[strlen(wordbuf)] = a;
254 if (strlen(wordbuf) + 3 > screenwidth) {
255 add_word(textlist, wordbuf);
263 prev = (appending ? 13 : (-1));
275 if ((a != 32) && (prev == 13)) {
276 add_word(textlist, "\n");
280 if ((a == 32) && (prev == 13)) {
281 add_word(textlist, "\n");
282 add_newline(textlist);
286 if (!IsEmptyStr(wordbuf)) {
287 wordbuf[strlen(wordbuf) - 1] = 0;
292 } else if (a == 23) {
294 wordbuf[strlen(wordbuf) - 1] = 0;
298 } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
299 } else if (a == 13) {
301 if (IsEmptyStr(wordbuf))
304 for (b = 0; b < strlen(wordbuf); ++b)
305 if (wordbuf[b] == 32) {
313 add_word(textlist, wordbuf);
318 wordbuf[strlen(wordbuf) + 1] = 0;
319 wordbuf[strlen(wordbuf)] = a;
321 if ((strlen(wordbuf) + 3) > screenwidth) {
323 for (b = 0; b < strlen(wordbuf); ++b)
324 if (wordbuf[b] == 32)
326 if (last_space >= 0) {
327 for (b = 0; b < strlen(wordbuf); ++b)
328 if (wordbuf[b] == 32) {
336 for (b = 0; b < strlen(wordbuf); ++b) {
341 scr_printf("\n%s", wordbuf);
343 add_word(textlist, wordbuf);
349 } while (finished == 0);
352 /* write the buffer back to disk */
354 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
355 fprintf(fp, "%s", ptr->text);
359 rv = ftruncate(fileno(fp), ftell(fp));
361 scr_printf("failed to set message buffer: %s\n", strerror(errno));
364 /* and deallocate the memory we used */
365 while (textlist != NULL) {
366 ptr = textlist->next;
374 * Free the struct parts
376 void free_parts(struct parts *p)
378 struct parts *a_part = p;
384 a_part = a_part->next;
391 * This is a mini RFC2047 decoder.
392 * It only handles strings encoded from UTF-8 as Quoted-printable.
394 void mini_2047_decode(char *s) {
397 char *qstart = strstr(s, "=?UTF-8?Q?");
400 char *qend = strstr(s, "?=");
403 if (qend <= qstart) return;
405 strcpy(qstart, &qstart[10]);
418 sscanf(ch, "%02x", &c);
420 strcpy(&p[1], &p[3]);
431 strcpy(qend, &qend[2]);
435 * Read a message from the server
437 int read_message(CtdlIPC *ipc,
438 long num, /* message number */
439 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
440 FILE *dest) /* Destination file, NULL for screen */
447 struct ctdlipcmessage *message = NULL;
448 int r; /* IPC response code */
449 char *converted_text = NULL;
456 int final_line_is_blank = 0;
463 strcpy(reply_to, NO_REPLY_TO);
464 strcpy(reply_subject, "");
465 strcpy(reply_references, "");
466 strcpy(reply_inreplyto, "");
468 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
470 scr_printf("*** msg #%ld: %d %s\n", num, r, buf);
473 free_parts(message->attachments);
479 fprintf(dest, "\n ");
485 if (pagin == 1 && !dest) {
489 /* View headers only */
491 scr_printf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
492 message->nhdr ? "yes" : "no",
493 message->author, message->type,
495 if (!IsEmptyStr(message->subject)) {
496 scr_printf("subj=%s\n", message->subject);
498 if (!IsEmptyStr(message->email)) {
499 scr_printf("rfca=%s\n", message->email);
501 scr_printf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
502 message->hnod, message->room,
504 asctime(localtime(&message->time)));
505 if (!IsEmptyStr(message->recipient)) {
506 scr_printf("rcpt=%s\n", message->recipient);
508 if (message->attachments) {
511 for (ptr = message->attachments; ptr; ptr = ptr->next) {
512 scr_printf("part=%s|%s|%s|%s|%s|%ld\n",
513 ptr->name, ptr->filename, ptr->number,
514 ptr->disposition, ptr->mimetype,
522 free_parts(message->attachments);
527 if (rc_display_message_numbers) {
529 fprintf(dest, "[#%s] ", message->msgid);
534 scr_printf("#%s", message->msgid);
539 if (nhdr == 1 && !is_room_aide) {
541 fprintf(dest, " ****");
546 fmt_date(now, sizeof now, message->time, 0);
548 fprintf(dest, "%s from %s ", now, message->author);
549 if (!IsEmptyStr(message->email)) {
550 fprintf(dest, "<%s> ", message->email);
554 scr_printf("%s ", now);
558 scr_printf("%s ", message->author);
559 if (!IsEmptyStr(message->email)) {
563 scr_printf("%s", message->email);
568 if (!IsEmptyStr(message->node)) {
569 if ((room_flags & QR_NETWORK)
570 || ((strcasecmp(message->node, ipc->ServInfo.nodename)
571 && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
572 if (IsEmptyStr(message->email)) {
574 fprintf(dest, "@%s ", message->node);
578 color(BRIGHT_YELLOW);
579 scr_printf("%s ", message->node);
584 if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
585 && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
587 fprintf(dest, "(%s) ", message->hnod);
592 scr_printf("%s", message->hnod);
597 if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
599 fprintf(dest, "in %s> ", message->room);
603 color(BRIGHT_MAGENTA);
604 scr_printf("%s> ", message->room);
607 if (!IsEmptyStr(message->recipient)) {
609 fprintf(dest, "to %s ", message->recipient);
614 scr_printf("%s ", message->recipient);
625 /* Set the reply-to address to an Internet e-mail address if possible
627 if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
628 if (!IsEmptyStr(message->author)) {
629 snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
632 safestrncpy(reply_to, message->email, sizeof reply_to);
636 /* But if we can't do that, set it to a Citadel address.
638 if (!strcmp(reply_to, NO_REPLY_TO)) {
639 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
640 message->author, message->node);
643 if (message->msgid != NULL) {
644 safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
647 if (message->references != NULL) if (!IsEmptyStr(message->references)) {
648 safestrncpy(reply_references, message->references, sizeof reply_references);
651 if (message->subject != NULL) {
652 safestrncpy(reply_subject, message->subject, sizeof reply_subject);
653 if (!IsEmptyStr(message->subject)) {
655 fprintf(dest, "Subject: %s\n", message->subject);
658 scr_printf("Subject: ");
660 mini_2047_decode(message->subject);
661 scr_printf("%s\n", message->subject);
666 if (pagin == 1 && !dest) {
670 /******* end of header output, start of message text output *******/
673 * Convert HTML to plain text, formatting for the actual width
674 * of the client screen.
676 if (!strcasecmp(message->content_type, "text/html")) {
677 converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
678 if (converted_text != NULL) {
680 message->text = converted_text;
685 /* Text/plain is a different type */
686 if (!strcasecmp(message->content_type, "text/plain")) {
691 num_urls = 0; /* Start with a clean slate */
692 searchptr = message->text;
693 while ( (searchptr != NULL) && (num_urls < MAXURLS) ) {
694 searchptr = strstr(searchptr, "http://");
695 if (searchptr != NULL) {
696 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
697 for (i = 0; i < strlen(urls[num_urls]); i++) {
698 ch = urls[num_urls][i];
699 if (ch == '>' || ch == '\"' || ch == ')' ||
700 ch == ' ' || ch == '\n') {
701 urls[num_urls][i] = 0;
713 if (format_type == 0) {
714 fr = fmout(screenwidth, NULL, message->text, dest, 1);
716 /* renderer for text/plain */
718 lineptr = message->text;
721 nextline = strchr(lineptr, '\n');
722 if (nextline != NULL) {
725 if (*nextline == 0) nextline = NULL;
728 if (sigcaught == 0) {
729 linelen = strlen(lineptr);
730 if (linelen && (lineptr[linelen-1] == '\r')) {
731 lineptr[--linelen] = 0;
734 fprintf(dest, "%s\n", lineptr);
736 scr_printf("%s\n", lineptr);
739 if (lineptr[0] == 0) final_line_is_blank = 1;
740 else final_line_is_blank = 0;
745 if (!final_line_is_blank) {
755 /* Enumerate any attachments */
756 if ( (pagin == 1) && (message->attachments) ) {
759 for (ptr = message->attachments; ptr; ptr = ptr->next) {
760 if ( (!strcasecmp(ptr->disposition, "attachment"))
761 || (!strcasecmp(ptr->disposition, "inline"))
762 || (!strcasecmp(ptr->disposition, ""))
764 if ( (strcasecmp(ptr->number, message->mime_chosen))
765 && (!IsEmptyStr(ptr->mimetype))
769 color(BRIGHT_MAGENTA);
770 scr_printf("%s", ptr->number);
774 scr_printf("%s", ptr->filename);
776 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
777 if (!strncmp(ptr->mimetype, "image/", 6)) {
785 /* Save the attachments info for later */
786 last_message_parts = message->attachments;
792 if (pagin == 1 && !dest)
799 * replace string function for the built-in editor
801 void replace_string(char *filename, long int startpos)
810 int substitutions = 0;
814 scr_printf("Enter text to be replaced:\n: ");
815 ctdl_getline(srch_str, (sizeof(srch_str)-1) );
816 if (IsEmptyStr(srch_str)) {
820 scr_printf("Enter text to replace it with:\n: ");
821 ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
823 fp = fopen(filename, "r+");
829 fseek(fp, startpos, 0);
831 while (a = getc(fp), a > 0) {
833 buf[strlen(buf) + 1] = 0;
834 buf[strlen(buf)] = a;
835 if (strlen(buf) >= strlen(srch_str)) {
836 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
837 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
838 strcpy(ptr, rplc_str);
842 if (strlen(buf) > 384) {
845 rv = fwrite((char *) buf, 128, 1, fp);
847 scr_printf("failed to replace string: %s\n", strerror(errno));
850 strcpy(buf, &buf[128]);
856 if (!IsEmptyStr(buf)) {
857 rv = fwrite((char *) buf, strlen(buf), 1, fp);
861 rv = truncate(filename, wpos);
862 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
866 * Function to begin composing a new message
868 int client_make_message(CtdlIPC *ipc,
869 char *filename, /* temporary file name */
870 char *recipient, /* NULL if it's not mail */
874 char *subject, /* buffer to store subject line */
882 char *editor_path = NULL;
887 if ((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2]))
889 editor_path = editor_paths[mode-2];
890 } else if (!IsEmptyStr(editor_paths[0]))
892 editor_path = editor_paths[0];
894 scr_printf("*** No editor available; using built-in editor.\n");
899 fmt_date(datestr, sizeof datestr, time(NULL), 0);
902 if (room_flags & QR_ANONONLY && !recipient) {
903 snprintf(header, sizeof header, " ****");
906 snprintf(header, sizeof header,
909 (is_anonymous ? "[anonymous]" : fullname)
911 if (!IsEmptyStr(recipient)) {
912 size_t tmp = strlen(header);
913 snprintf(&header[tmp], sizeof header - tmp,
914 " to %s", recipient);
917 scr_printf("%s\n", header);
918 if (subject != NULL) if (!IsEmptyStr(subject)) {
919 scr_printf("Subject: %s\n", subject);
922 if ( (subject_required) && (IsEmptyStr(subject)) ) {
923 newprompt("Subject: ", subject, 70);
927 scr_printf("(Press ctrl-d when finished)\n");
931 fp = fopen(filename, "r");
933 fmout(screenwidth, fp, NULL, NULL, 0);
936 scr_printf("failed to get stream position %s\n",
940 fp = fopen(filename, "w");
942 scr_printf("*** Error opening temp file!\n %s: %s\n",
943 filename, strerror(errno)
954 fp = fopen(filename, "r+");
956 scr_printf("*** Error opening temp file!\n %s: %s\n",
957 filename, strerror(errno)
966 fp = fopen(filename, "a");
968 scr_printf("*** Error opening temp file!\n"
970 filename, strerror(errno));
990 default: /* allow 2+ modes */
991 e_ex_code = 1; /* start with a failed exit code */
992 stty_ctdl(SB_RESTORE);
994 cksum = file_checksum(filename);
995 if (editor_pid == 0) {
998 chmod(filename, 0600);
999 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
1001 execlp(editor_path, editor_path, filename, NULL);
1007 b = ka_wait(&e_ex_code);
1008 } while ((b != editor_pid) && (b >= 0));
1014 MECR: if (mode >= 2) {
1015 if (file_checksum(filename) == cksum) {
1016 scr_printf("*** Aborted message.\n");
1019 if (e_ex_code == 0) {
1025 b = keymenu("Entry command (? for options)",
1029 "<P>rint formatted|"
1035 if (b == 'a') goto MEABT;
1036 if (b == 'c') goto ME1;
1037 if (b == 's') goto MEFIN;
1039 scr_printf(" %s from %s", datestr, fullname);
1040 if (!IsEmptyStr(recipient)) {
1041 scr_printf(" to %s", recipient);
1044 if (subject != NULL) if (!IsEmptyStr(subject)) {
1045 scr_printf("Subject: %s\n", subject);
1047 fp = fopen(filename, "r");
1049 fmout(screenwidth, fp, NULL, NULL, 0);
1052 scr_printf("failed to get stream position %s\n",
1059 replace_string(filename, 0L);
1066 if (subject != NULL) {
1067 newprompt("Subject: ", subject, 70);
1074 MEABT: scr_printf("Are you sure? ");
1078 MEABT2: unlink(filename);
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;
1141 scr_printf("You may not enter messages in this type of room.\n");
1152 strcpy(subject, "");
1155 * First, check to see if we have permission to enter a message in
1156 * this room. The server will return an error code if we can't.
1158 strcpy(message.recipient, "");
1159 strcpy(message.author, "");
1160 strcpy(message.subject, "");
1161 strcpy(message.references, "");
1162 message.text = ""; /* point to "", changes later */
1163 message.anonymous = 0;
1164 message.type = mode;
1167 newprompt("Display name for this message: ", message.author, 40);
1172 if (!IsEmptyStr(reply_subject)) {
1173 if (!strncasecmp(reply_subject,
1175 strcpy(message.subject, reply_subject);
1178 snprintf(message.subject,
1179 sizeof message.subject,
1185 /* Trim down excessively long lists of thread references. We eliminate the
1186 * second one in the list so that the thread root remains intact.
1188 int rrtok = num_tokens(reply_references, '|');
1189 int rrlen = strlen(reply_references);
1190 if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
1191 remove_token(reply_references, 1, '|');
1194 snprintf(message.references, sizeof message.references, "%s%s%s",
1196 (IsEmptyStr(reply_references) ? "" : "|"),
1201 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1203 if (r / 100 != 2 && r / 10 != 57) {
1204 scr_printf("%s\n", buf);
1208 /* Error code 570 is special. It means that we CAN enter a message
1209 * in this room, but a recipient needs to be specified.
1216 /* If the user is a dumbass, tell them how to type. */
1217 if ((userflags & US_EXPERT) == 0) {
1218 formout(ipc, "entermsg");
1221 /* Handle the selection of a recipient, if necessary. */
1223 if (need_recp == 1) {
1224 if (axlevel >= AxProbU) {
1226 strcpy(buf, reply_to);
1228 scr_printf("Enter recipient: ");
1229 ctdl_getline(buf, (SIZ-100) );
1230 if (IsEmptyStr(buf))
1234 strcpy(buf, "sysop");
1236 strcpy(message.recipient, buf);
1238 if (room_flags & QR_ANONOPT) {
1239 scr_printf("Anonymous (Y/N)? ");
1241 message.anonymous = 1;
1244 /* If it's mail, we've got to check the validity of the recipient... */
1245 if (!IsEmptyStr(message.recipient)) {
1246 r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
1248 scr_printf("%s\n", buf);
1253 /* Learn the number of the newest message in in the room, so we can
1254 * tell upon saving whether someone else has posted too.
1257 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1259 scr_printf("%s\n", buf);
1261 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1265 /* Now compose the message... */
1266 if (client_make_message(ipc, temp, message.recipient,
1267 message.anonymous, 0, c, message.subject, subject_required) != 0) {
1268 if (msgarr) free(msgarr);
1272 /* Reopen the temp file that was created, so we can send it */
1273 fp = fopen(temp, "r");
1275 if (!fp || !(message.text = load_message_from_file(fp))) {
1276 scr_printf("*** Internal error while trying to save message!\n"
1278 temp, strerror(errno));
1285 /* Break lines that are >1024 characters, otherwise the server
1286 * will truncate them.
1288 break_big_lines(message.text);
1290 /* Transmit message to the server */
1291 r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
1293 scr_printf("%s\n", buf);
1297 /* Yes, unlink it now, so it doesn't stick around if we crash */
1300 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1302 if (msgarr) free(msgarr);
1304 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1306 scr_printf("%s\n", buf);
1308 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1312 /* get new highest message number in room to set lrp for goto... */
1313 maxmsgnum = msgarr[num_msgs - 1];
1315 /* now see if anyone else has posted in here */
1317 for (a = 0; a < num_msgs; ++a) {
1318 if (msgarr[a] > highmsg) {
1322 if (msgarr) free(msgarr);
1325 /* In the Mail> room, this algorithm always counts one message
1326 * higher than in public rooms, so we decrement it by one.
1333 scr_printf("*** 1 additional message has been entered "
1334 "in this room by another user.\n");
1337 scr_printf("*** %d additional messages have been entered "
1338 "in this room by other users.\n", b);
1346 * Do editing on a quoted file
1348 void process_quote(void)
1350 FILE *qfile, *tfile;
1352 int line, qstart, qend;
1354 /* Unlink the second temp file as soon as it's opened, so it'll get
1355 * deleted even if the program dies
1357 qfile = fopen(temp2, "r");
1360 /* Display the quotable text with line numbers added */
1362 if (fgets(buf, 128, qfile) == NULL) {
1363 /* we're skipping a line here */
1365 while (fgets(buf, 128, qfile) != NULL) {
1366 scr_printf("%3d %s", ++line, buf);
1368 scr_printf("Begin quoting at [1] : ");
1369 ctdl_getline(buf, 4);
1370 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1371 scr_printf(" End quoting at [%d] : ", line);
1372 ctdl_getline(buf, 4);
1373 qend = (buf[0] == 0) ? (line) : atoi(buf);
1376 if (fgets(buf, 128, qfile) == NULL) {
1377 /* we're skipping a line here */
1379 tfile = fopen(temp, "w");
1380 while (fgets(buf, 128, qfile) != NULL) {
1381 if ((++line >= qstart) && (line <= qend))
1382 fprintf(tfile, " >%s", buf);
1384 fprintf(tfile, " \n");
1393 * List the URL's which were embedded in the previous message
1395 void list_urls(CtdlIPC *ipc)
1401 if (num_urls == 0) {
1402 scr_printf("There were no URL's in the previous message.\n\n");
1406 for (i = 0; i < num_urls; ++i) {
1407 scr_printf("%3d %s\n", i + 1, urls[i]);
1410 if ((i = num_urls) != 1)
1411 i = intprompt("Display which one", 1, 1, num_urls);
1413 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1416 scr_printf("failed to '%s' by %d\n", cmd, rv);
1422 * Run image viewer in background
1424 int do_image_view(const char *filename)
1429 snprintf(cmd, sizeof cmd, imagecmd, filename);
1436 if (childpid == 0) {
1438 pid_t grandchildpid;
1440 grandchildpid = fork();
1441 if (grandchildpid < 0) {
1442 return grandchildpid;
1445 if (grandchildpid == 0) {
1450 nullfd = open("/dev/null", O_WRONLY);
1457 retcode = system(cmd);
1467 if (grandchildpid > 0) {
1475 waitpid(childpid, &retcode, 0);
1484 * View an image attached to a message
1486 void image_view(CtdlIPC *ipc, unsigned long msg)
1488 struct parts *ptr = last_message_parts;
1492 /* Run through available parts */
1493 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1494 if ((!strcasecmp(ptr->disposition, "attachment")
1495 || !strcasecmp(ptr->disposition, "inline"))
1496 && !strncmp(ptr->mimetype, "image/", 6)) {
1499 strcpy(part, ptr->number);
1506 strprompt("View which part (0 when done)", part, SIZ-1);
1508 for (ptr = last_message_parts; ptr; ptr = ptr->next) {
1509 if ((!strcasecmp(ptr->disposition, "attachment")
1510 || !strcasecmp(ptr->disposition, "inline"))
1511 && !strncmp(ptr->mimetype, "image/", 6)
1512 && !strcasecmp(ptr->number, part)) {
1515 void *file = NULL; /* The downloaded file */
1520 r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
1522 scr_printf("%s\n", buf);
1526 len = (size_t)extract_long(buf, 0);
1527 progress(ipc, len, len);
1529 CtdlMakeTempFileName(tmp, sizeof tmp);
1530 strcat(tmp, ptr->filename);
1531 save_buffer(file, len, tmp);
1545 * Read the messages in the current room
1547 void readmsgs(CtdlIPC *ipc,
1548 enum MessageList c, /* see listing in citadel_ipc.h */
1549 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1550 int q /* Number of msgs to read (if c==3) */
1552 int a, e, f, g, start;
1558 char prtfile[PATH_MAX];
1561 char targ[ROOMNAMELEN];
1562 char filename[PATH_MAX];
1563 char save_to[PATH_MAX];
1564 void *attachment = NULL; /* Downloaded attachment */
1565 FILE *dest = NULL; /* Alternate destination other than screen */
1566 int r; /* IPC response code */
1567 static int att_seq = 0; /* Attachment download sequence number */
1568 int rv = 0; /* silence the stupid warn_unused_result warnings */
1570 CtdlMakeTempFileName(prtfile, sizeof prtfile);
1576 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1578 scr_printf("%s\n", cmd);
1580 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1584 if (num_msgs == 0) { /* TODO look at this later */
1585 if (c == LastMessages) return;
1586 scr_printf("*** There are no ");
1587 if (c == NewMessages) scr_printf("new ");
1588 if (c == OldMessages) scr_printf("old ");
1589 scr_printf("messages in this room.\n");
1593 /* this loop cycles through each message... */
1594 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1595 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1596 while (msg_arr[a] == 0L) {
1598 if ((a == num_msgs) || (a == (-1)))
1602 RAGAIN: pagin = ((arcflag == 0)
1604 && (userflags & US_PAGINATOR)) ? 1 : 0;
1606 /* If we're doing a quote, set the screenwidth to 72 */
1608 hold_sw = screenwidth;
1612 /* If printing or archiving, set the screenwidth to 80 */
1614 hold_sw = screenwidth;
1618 /* clear parts list */
1619 free_parts(last_message_parts);
1620 last_message_parts = NULL;
1622 /* now read the message... */
1623 e = read_message(ipc, msg_arr[a], pagin, dest);
1625 /* ...and set the screenwidth back if we have to */
1626 if ((quotflag) || (arcflag)) {
1627 screenwidth = hold_sw;
1630 highest_msg_read = msg_arr[a];
1635 enable_color = hold_color;
1644 enable_color = hold_color;
1647 if (freopen(prtfile, "r", stdin) == NULL) {
1648 /* we probably should handle the error condition here */
1650 stty_ctdl(SB_RESTORE);
1651 ka_system(printcmd);
1652 stty_ctdl(SB_NO_INTR);
1659 } while ((g != f) && (g >= 0));
1660 scr_printf("Message printed.\n");
1662 if (rc_alt_semantics && c == 1) {
1665 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1669 if (((userflags & US_NOPROMPT) || (e == SIGINT))
1670 && (((room_flags & QR_MAILBOX) == 0)
1671 || (rc_force_mail_prompts == 0))) {
1676 color(BRIGHT_WHITE);
1677 scr_printf("%d", num_msgs - a - 1);
1681 keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
1682 if (rc_url_cmd[0] && num_urls)
1683 keyopt("<U>RLview ");
1684 if (has_images > 0 && !IsEmptyStr(imagecmd))
1685 keyopt("<I>mages ");
1686 keyopt("<?>help -> ");
1689 e = (inkey() & 127);
1691 /* return key same as <N> */ if (e == 10)
1693 /* space key same as <N> */ if (e == 32)
1695 /* del/move for aides only */
1696 if ( (!is_room_aide)
1697 && ((room_flags & QR_MAILBOX) == 0)
1698 && ((room_flags2 & QR2_COLLABDEL) == 0)
1700 if ((e == 'd') || (e == 'm'))
1703 /* print only if available */
1704 if ((e == 'p') && (IsEmptyStr(printcmd)))
1706 /* can't file if not allowed */
1708 && (rc_allow_attachments == 0))
1710 /* link only if browser avail*/
1712 && (IsEmptyStr(rc_url_cmd)))
1715 && (IsEmptyStr(imagecmd) || !has_images))
1717 } while ((e != 'a') && (e != 'n') && (e != 's')
1718 && (e != 'd') && (e != 'm') && (e != 'p')
1719 && (e != 'q') && (e != 'b') && (e != 'h')
1720 && (e != 'r') && (e != 'f') && (e != '?')
1721 && (e != 'u') && (e != 'c') && (e != 'y')
1722 && (e != 'i') && (e != 'o') );
1728 scr_printf("Again");
1731 scr_printf("Delete");
1743 scr_printf("Print");
1746 scr_printf("reply Quoted");
1752 scr_printf("Header");
1755 scr_printf("Reply");
1758 scr_printf("Open attachments");
1764 scr_printf("URL's");
1767 scr_printf("mY next");
1772 scr_printf("? <help>");
1775 if (userflags & US_DISAPPEAR || e == 'i')
1776 scr_printf("\r%79s\r", "");
1780 DONE_QUOTING: switch (e) {
1782 scr_printf("Options available here:\n"
1783 " ? Help (prints this message)\n"
1784 " S Stop reading immediately\n"
1785 " A Again (repeats last message)\n"
1786 " N Next (continue with next message)\n"
1787 " Y My Next (continue with next message you authored)\n"
1788 " B Back (go back to previous message)\n");
1790 || (room_flags & QR_MAILBOX)
1791 || (room_flags2 & QR2_COLLABDEL)
1793 scr_printf(" D Delete this message\n"
1794 " M Move message to another room\n");
1796 scr_printf(" C Copy message to another room\n");
1797 if (!IsEmptyStr(printcmd))
1798 scr_printf(" P Print this message\n");
1800 " Q Reply to this message, quoting portions of it\n"
1801 " H Headers (display message headers only)\n");
1803 scr_printf(" R Reply to this message\n");
1804 if (rc_allow_attachments) {
1805 scr_printf(" O (Open attachments)\n");
1806 scr_printf(" F (save attachments to a File)\n");
1808 if (!IsEmptyStr(rc_url_cmd))
1809 scr_printf(" U (list URL's for display)\n");
1810 if (!IsEmptyStr(imagecmd) && has_images > 0)
1811 scr_printf(" I Image viewer\n");
1816 dest = fopen(prtfile, "w");
1818 hold_color = enable_color;
1823 dest = fopen(temp2, "w");
1825 hold_color = enable_color;
1837 newprompt("Enter target room: ",
1838 targ, ROOMNAMELEN - 1);
1839 if (!IsEmptyStr(targ)) {
1840 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1841 msg_arr[a], targ, cmd);
1842 scr_printf("%s\n", cmd);
1848 if (r / 100 != 2) /* r will be init'ed, FIXME */
1849 goto RMSGREAD; /* the logic here sucks */
1853 newprompt("Which section? ", filename, ((sizeof filename) - 1));
1854 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1855 filename, &attachment, progress, cmd);
1857 scr_printf("%s\n", cmd);
1859 extract_token(filename, cmd, 2, '|', sizeof filename);
1861 * Part 1 won't have a filename; use the
1862 * subject of the message instead. IO
1864 if (IsEmptyStr(filename)) {
1865 strcpy(filename, reply_subject);
1867 if (e == 'o') { /* open attachment */
1868 mkdir(tempdir, 0700);
1869 snprintf(save_to, sizeof save_to, "%s/%04x.%s",
1873 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1874 snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
1877 scr_printf("failed to save %s Reason %d\n", cmd, rv);
1879 else { /* save attachment to disk */
1880 destination_directory(save_to, filename);
1881 save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
1890 scr_printf("*** Delete this message? ");
1892 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1893 scr_printf("%s\n", cmd);
1901 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1904 savedpos = num_msgs;
1905 entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
1906 num_msgs = savedpos;
1912 image_view(ipc, msg_arr[a]);
1915 { /* hack hack hack */
1916 /* find the next message by me, stay here if we find nothing */
1919 for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1921 /* This is repetitively dumb, but that's what computers are for.
1922 We have to load up messages until we find one by us */
1925 struct ctdlipcmessage *msg = NULL;
1927 /* read the header so we can get 'from=' */
1928 r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
1929 if (!strncasecmp(msg->author, fullname, sizeof(fullname))) {
1930 a = lasta; /* meesa current */
1938 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1942 } /* end for loop */
1943 } /* end read routine */
1949 * View and edit a system message
1951 void edit_system_message(CtdlIPC *ipc, char *which_message)
1955 char write_cmd[SIZ];
1957 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1958 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1959 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1960 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1967 * Verify the message base
1969 void check_message_base(CtdlIPC *ipc)
1972 char *transcript = NULL;
1973 int r; /* IPC response code */
1976 ("Please read the documentation before running this command.\n"
1977 "Having done so, do you still want to check the message base? ");
1981 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1983 scr_printf("%s\n", buf);
1987 while (transcript && !IsEmptyStr(transcript)) {
1988 extract_token(buf, transcript, 0, '\n', sizeof buf);
1989 remove_token(transcript, 0, '\n');
1990 scr_printf("%s\n", buf);
1992 if (transcript) free(transcript);
1998 * Loads the contents of a file into memory. Caller must free the allocated
2001 char *load_message_from_file(FILE *src)
2007 fseek(src, 0, SEEK_END);
2011 dest = (char *)calloc(1, i + 1);
2018 g = fread(dest + got, 1, i - got, src);
2021 /* Interrupted system call, keep going */
2024 /* At this point we have either EOF or error */