4 * Citadel/UX message support routines
5 * see copyright.txt for copyright information
11 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
35 #include "citadel_ipc.h"
36 #include "citadel_decls.h"
47 #define MAXWORDBUF SIZ
48 #define NO_REPLY_TO "nobody ... xxxxxx"
51 char reply_subject[SIZ];
55 char text[MAXWORDBUF];
58 void sttybbs(int cmd);
59 int haschar(const char *st, int ch);
60 void getline(char *string, int lim);
61 int file_checksum(char *filename);
62 void progress(unsigned long curr, unsigned long cmax);
64 unsigned long *msg_arr = NULL;
67 char rc_alt_semantics;
68 extern char room_name[];
69 extern unsigned room_flags;
70 extern long highest_msg_read;
71 extern struct CtdlServInfo serv_info;
74 extern int screenwidth;
75 extern int screenheight;
76 extern long maxmsgnum;
79 extern char is_room_aide;
80 extern char fullname[];
82 extern unsigned userflags;
83 extern char sigcaught;
84 extern char printcmd[];
85 extern int rc_allow_attachments;
86 extern int rc_display_message_numbers;
87 extern int rc_force_mail_prompts;
88 extern int editor_pid;
89 extern CtdlIPC *ipc_for_signal_handlers; /* KLUDGE cover your eyes */
91 void ka_sigcatch(int signum)
94 signal(SIGALRM, ka_sigcatch);
95 CtdlIPCNoop(ipc_for_signal_handlers);
100 * server keep-alive version of wait() (needed for external editor)
102 pid_t ka_wait(int *kstatus)
107 signal(SIGALRM, ka_sigcatch);
111 } while (errno == EINTR);
112 signal(SIGALRM, SIG_IGN);
119 * version of system() that uses ka_wait()
121 int ka_system(char *shc)
130 perror("Cannot fork");
132 return ((pid_t) childpid);
136 execlp("/bin/sh", "sh", "-c", shc, NULL);
142 waitpid = ka_wait(&retcode);
143 } while (waitpid != childpid);
153 * add a newline to the buffer...
155 void add_newline(struct cittext *textlist)
160 while (ptr->next != NULL)
163 while (ptr->text[strlen(ptr->text) - 1] == 32)
164 ptr->text[strlen(ptr->text) - 1] = 0;
165 /* strcat(ptr->text,"\n"); */
167 ptr->next = (struct cittext *)
168 malloc(sizeof(struct cittext));
171 strcpy(ptr->text, "");
176 * add a word to the buffer...
178 void add_word(struct cittext *textlist, char *wordbuf)
183 while (ptr->next != NULL)
186 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
187 ptr->next = (struct cittext *)
188 malloc(sizeof(struct cittext));
191 strcpy(ptr->text, "");
194 strcat(ptr->text, wordbuf);
195 strcat(ptr->text, " ");
200 * begin editing of an opened file pointed to by fp
202 void citedit(CtdlIPC *ipc, FILE * fp)
204 int a, prev, finished, b, last_space;
206 struct cittext *textlist = NULL;
208 char wordbuf[MAXWORDBUF];
210 /* first, load the text into the buffer */
212 textlist = (struct cittext *) malloc(sizeof(struct cittext));
213 textlist->next = NULL;
214 strcpy(textlist->text, "");
218 while (a = getc(fp), a >= 0) {
220 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
221 add_word(textlist, wordbuf);
223 if ((prev == 13) || (prev == 10)) {
224 add_word(textlist, "\n");
225 add_newline(textlist);
226 add_word(textlist, "");
229 wordbuf[strlen(wordbuf) + 1] = 0;
230 wordbuf[strlen(wordbuf)] = a;
232 if (strlen(wordbuf) + 3 > screenwidth) {
233 add_word(textlist, wordbuf);
241 prev = (appending ? 13 : (-1));
253 /******* new ***********/
254 if ((a > 32) && (a < 127) && (prev == 13)) {
255 add_word(textlist, "\n");
258 /***********************/
260 if ((a == 32) && (prev == 13)) {
261 add_word(textlist, "\n");
262 add_newline(textlist);
266 if (strlen(wordbuf) > 0) {
267 wordbuf[strlen(wordbuf) - 1] = 0;
272 } else if (a == 23) {
274 wordbuf[strlen(wordbuf) - 1] = 0;
278 } while (strlen(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
279 } else if (a == 13) {
281 if (strlen(wordbuf) == 0)
284 for (b = 0; b < strlen(wordbuf); ++b)
285 if (wordbuf[b] == 32) {
293 add_word(textlist, wordbuf);
298 wordbuf[strlen(wordbuf) + 1] = 0;
299 wordbuf[strlen(wordbuf)] = a;
301 if ((strlen(wordbuf) + 3) > screenwidth) {
303 for (b = 0; b < strlen(wordbuf); ++b)
304 if (wordbuf[b] == 32)
306 if (last_space >= 0) {
307 for (b = 0; b < strlen(wordbuf); ++b)
308 if (wordbuf[b] == 32) {
316 for (b = 0; b < strlen(wordbuf); ++b) {
321 scr_printf("\n%s", wordbuf);
323 add_word(textlist, wordbuf);
329 } while (finished == 0);
332 /* write the buffer back to disk */
334 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
335 fprintf(fp, "%s", ptr->text);
339 ftruncate(fileno(fp), ftell(fp));
341 /* and deallocate the memory we used */
342 while (textlist != NULL) {
343 ptr = textlist->next;
349 /* Read a message from the server
351 int read_message(CtdlIPC *ipc,
352 long num, /* message number */
353 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
354 FILE *dest) /* Destination file, NULL for screen */
361 struct ctdlipcmessage *message = NULL;
362 int r; /* IPC response code */
363 char *converted_text = NULL;
368 strcpy(reply_to, NO_REPLY_TO);
369 strcpy(reply_subject, "");
371 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
372 (can_do_msg4 ? 4 : 0),
375 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
378 checkpagin(lines_printed, pagin, screenheight);
384 fprintf(dest, "\n ");
388 lines_printed = checkpagin(lines_printed, pagin, screenheight);
392 if (pagin == 1 && !dest) {
396 /* View headers only */
398 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
399 message->nhdr ? "yes" : "no",
400 message->author, message->type,
402 if (strlen(message->subject)) {
403 pprintf("subj=%s\n", message->subject);
405 if (strlen(message->email)) {
406 pprintf("rfca=%s\n", message->email);
408 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
409 message->hnod, message->room,
411 asctime(localtime(&message->time)));
412 if (strlen(message->recipient)) {
413 pprintf("rcpt=%s\n", message->recipient);
415 if (message->attachments) {
418 for (ptr = message->attachments; ptr; ptr = ptr->next) {
419 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
420 ptr->name, ptr->filename, ptr->number,
421 ptr->disposition, ptr->mimetype,
430 if (rc_display_message_numbers) {
432 fprintf(dest, "[#%s] ", message->msgid);
437 scr_printf("#%s", message->msgid);
442 if (nhdr == 1 && !is_room_aide) {
444 fprintf(dest, " ****");
449 fmt_date(now, sizeof now, message->time, 0);
451 fprintf(dest, "%s from %s ", now, message->author);
452 if (strlen(message->email)) {
453 fprintf(dest, "<%s> ", message->email);
457 scr_printf("%s ", now);
461 scr_printf("%s ", message->author);
462 if (strlen(message->email)) {
466 scr_printf("%s", message->email);
471 if (strlen(message->node)) {
472 if ((room_flags & QR_NETWORK)
473 || ((strcasecmp(message->node, serv_info.serv_nodename)
474 && (strcasecmp(message->node, serv_info.serv_fqdn))))) {
475 if (strlen(message->email) == 0) {
477 fprintf(dest, "@%s ", message->node);
481 color(BRIGHT_YELLOW);
482 scr_printf("%s ", message->node);
487 if (strcasecmp(message->hnod, serv_info.serv_humannode)
488 && (strlen(message->hnod)) && (!strlen(message->email))) {
490 fprintf(dest, "(%s) ", message->hnod);
495 scr_printf("%s", message->hnod);
500 if (strcasecmp(message->room, room_name) && (strlen(message->email) == 0)) {
502 fprintf(dest, "in %s> ", message->room);
506 color(BRIGHT_MAGENTA);
507 scr_printf("%s> ", message->room);
510 if (strlen(message->recipient)) {
512 fprintf(dest, "to %s ", message->recipient);
517 scr_printf("%s ", message->recipient);
528 /* Set the reply-to address to an Internet e-mail address if possible
530 if (message->email != NULL) if (strlen(message->email) > 0) {
531 safestrncpy(reply_to, message->email, sizeof reply_to);
534 /* But if we can't do that, set it to a Citadel address.
536 if (!strcmp(reply_to, NO_REPLY_TO)) {
537 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
538 message->author, message->node);
543 lines_printed = checkpagin(lines_printed, pagin, screenheight);
546 if (message->subject != NULL) {
547 safestrncpy(reply_subject, message->subject,
548 sizeof reply_subject);
549 if (strlen(message->subject) > 0) {
551 fprintf(dest, "Subject: %s\n",
555 scr_printf("Subject: ");
557 scr_printf("%s\n", message->subject);
559 lines_printed = checkpagin(lines_printed,
560 pagin, screenheight);
565 if (pagin == 1 && !dest) {
569 /******* end of header output, start of message text output *******/
572 * Convert HTML to plain text, formatting for the actual width
573 * of the client screen.
575 if (!strcasecmp(message->content_type, "text/html")) {
576 converted_text = html_to_ascii(message->text, screenwidth, 0);
577 if (converted_text != NULL) {
579 message->text = converted_text;
584 /* Text/plain is a different type */
585 if (!strcasecmp(message->content_type, "text/plain")) {
592 if (format_type == 0) {
593 fr = fmout(screenwidth, NULL, message->text, dest,
594 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
596 /* FIXME: renderer for text/plain */
600 msgtext = message->text;
602 while (lineptr = strtok(msgtext, "\n"), lineptr != NULL) {
605 if (sigcaught == 0) {
607 fprintf(dest, "%s\n", lineptr);
609 scr_printf("%s\n", lineptr);
610 lines_printed = lines_printed + 1 +
611 (strlen(lineptr) / screenwidth);
613 checkpagin(lines_printed, pagin,
626 lines_printed = checkpagin(lines_printed, pagin, screenheight);
629 /* Enumerate any attachments */
630 if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
633 for (ptr = message->attachments; ptr; ptr = ptr->next) {
634 if ( (!strcasecmp(ptr->disposition, "attachment"))
635 || (!strcasecmp(ptr->disposition, "inline"))) {
638 color(BRIGHT_MAGENTA);
639 scr_printf("%s", ptr->number);
643 scr_printf("%s", ptr->filename);
645 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
654 if (pagin == 1 && !dest)
661 * replace string function for the built-in editor
663 void replace_string(char *filename, long int startpos)
672 int substitutions = 0;
675 scr_printf("Enter text to be replaced:\n: ");
676 getline(srch_str, (sizeof(srch_str)-1) );
677 if (strlen(srch_str) == 0)
680 scr_printf("Enter text to replace it with:\n: ");
681 getline(rplc_str, (sizeof(rplc_str)-1) );
683 fp = fopen(filename, "r+");
688 fseek(fp, startpos, 0);
690 while (a = getc(fp), a > 0) {
692 buf[strlen(buf) + 1] = 0;
693 buf[strlen(buf)] = a;
694 if (strlen(buf) >= strlen(srch_str)) {
695 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
696 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
697 strcpy(ptr, rplc_str);
701 if (strlen(buf) > 384) {
704 fwrite((char *) buf, 128, 1, fp);
705 strcpy(buf, &buf[128]);
712 fwrite((char *) buf, strlen(buf), 1, fp);
715 truncate(filename, wpos);
716 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
720 * Function to begin composing a new message
722 int client_make_message(CtdlIPC *ipc,
723 char *filename, /* temporary file name */
724 char *recipient, /* NULL if it's not mail */
725 int anon_type, /* see MES_ types in header file */
728 char *subject) /* buffer to store subject line */
735 char *editor_path = NULL;
740 if((mode-2) < MAX_EDITORS && strlen(editor_paths[mode-2]) > 0) {
741 editor_path = editor_paths[mode-2];
742 } else if (strlen(editor_paths[0]) > 0) {
743 editor_path = editor_paths[0];
746 ("*** No editor available, using built-in editor\n");
751 fmt_date(datestr, sizeof datestr, time(NULL), 0);
754 if (room_flags & QR_ANONONLY && !recipient) {
755 snprintf(header, sizeof header, " ****");
758 snprintf(header, sizeof header,
759 " %s from %s", datestr, fullname);
760 if (strlen(recipient) > 0) {
761 size_t tmp = strlen(header);
762 snprintf(&header[tmp], sizeof header - tmp,
763 " to %s", recipient);
766 scr_printf("%s\n", header);
767 if (subject != NULL) if (strlen(subject) > 0) {
768 scr_printf("Subject: %s\n", subject);
774 scr_printf("(Press ctrl-d when finished)\n");
778 fp = fopen(filename, "r");
780 fmout(screenwidth, fp, NULL, NULL, 0, screenheight, 0, 0);
784 fp = fopen(filename, "w");
786 err_printf("*** Error opening temp file!\n"
788 filename, strerror(errno));
798 fp = fopen(filename, "r+");
800 err_printf("*** Error opening temp file!\n"
802 filename, strerror(errno));
810 fp = fopen(filename, "a");
812 err_printf("*** Error opening temp file!\n"
814 filename, strerror(errno));
834 default: /* allow 2+ modes */
835 e_ex_code = 1; /* start with a failed exit code */
837 cksum = file_checksum(filename);
840 if (editor_pid == 0) {
843 chmod(filename, 0600);
844 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
846 execlp(editor_path, editor_path, filename, NULL);
852 b = ka_wait(&e_ex_code);
853 } while ((b != editor_pid) && (b >= 0));
860 MECR: if (mode >= 2) {
861 if (file_checksum(filename) == cksum) {
862 err_printf("*** Aborted message.\n");
870 b = keymenu("Entry command (? for options)",
871 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
873 "<R>eplace string|<H>old message");
882 scr_printf(" %s from %s", datestr, fullname);
883 if (strlen(recipient) > 0)
884 scr_printf(" to %s", recipient);
886 if (subject != NULL) if (strlen(subject) > 0) {
887 scr_printf("Subject: %s\n", subject);
889 fp = fopen(filename, "r");
891 fmout(screenwidth, fp, NULL, NULL,
892 ((userflags & US_PAGINATOR) ? 1 : 0),
900 replace_string(filename, 0L);
907 if (subject != NULL) {
908 newprompt("Subject: ", subject, 70);
915 MEABT: scr_printf("Are you sure? ");
919 MEABT2: unlink(filename);
926 * Transmit message text to the server.
928 * This loop also implements a "tick" counter that displays the progress, if
929 * we're sending something that will take a long time to transmit.
931 void transmit_message(CtdlIPC *ipc, FILE *fp)
938 fseek(fp, 0L, SEEK_END);
941 lasttick = time(NULL);
943 while (ch = getc(fp), (ch >= 0)) {
945 if (!strcmp(buf, "000"))
947 CtdlIPC_putline(ipc, buf);
953 if ((ch == 32) && (strlen(buf) > 200)) {
955 if (!strcmp(buf, "000"))
957 CtdlIPC_putline(ipc, buf);
960 if (strlen(buf) > 250) {
961 if (!strcmp(buf, "000"))
963 CtdlIPC_putline(ipc, buf);
968 if ((time(NULL) - lasttick) > 2L) {
969 scr_printf(" %3ld%% completed\r",
970 ((ftell(fp) * 100L) / msglen));
972 lasttick = time(NULL);
976 CtdlIPC_putline(ipc, buf);
984 * Make sure there's room in msg_arr[] for at least one more.
986 void check_msg_arr_size(void) {
987 if ((num_msgs + 1) > msg_arr_size) {
989 msg_arr = realloc(msg_arr,
990 ((sizeof(long)) * msg_arr_size) );
997 * entmsg() - edit and create a message
998 * returns 0 if message was saved
1000 int entmsg(CtdlIPC *ipc,
1001 int is_reply, /* nonzero if this was a <R>eply command */
1011 struct ctdlipcmessage message;
1012 int r; /* IPC response code */
1019 strcpy(subject, "");
1022 * First, check to see if we have permission to enter a message in
1023 * this room. The server will return an error code if we can't.
1025 strcpy(message.recipient, "");
1026 strcpy(message.author, "");
1027 strcpy(message.subject, "");
1028 message.text = message.author; /* point to "", changes later */
1029 message.anonymous = 0;
1030 message.type = mode;
1031 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1033 if (r / 100 != 2 && r / 10 != 57) {
1034 scr_printf("%s\n", buf);
1038 /* Error code 570 is special. It means that we CAN enter a message
1039 * in this room, but a recipient needs to be specified.
1045 /* If the user is a dumbass, tell them how to type. */
1046 if ((userflags & US_EXPERT) == 0) {
1047 formout(ipc, "entermsg");
1050 /* Handle the selection of a recipient, if necessary. */
1052 if (need_recp == 1) {
1055 strcpy(buf, reply_to);
1057 scr_printf("Enter recipient: ");
1058 getline(buf, (SIZ-100) );
1059 if (strlen(buf) == 0)
1063 strcpy(buf, "sysop");
1065 strcpy(message.recipient, buf);
1068 if (strlen(reply_subject) > 0) {
1069 if (!strncasecmp(reply_subject,
1071 strcpy(message.subject, reply_subject);
1074 snprintf(message.subject,
1075 sizeof message.subject,
1082 if (room_flags & QR_ANONOPT) {
1083 scr_printf("Anonymous (Y/N)? ");
1085 message.anonymous = 1;
1088 /* If it's mail, we've got to check the validity of the recipient... */
1089 if (strlen(message.recipient) > 0) {
1090 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1092 scr_printf("%s\n", buf);
1097 /* Learn the number of the newest message in in the room, so we can
1098 * tell upon saving whether someone else has posted too.
1101 free(msg_arr); msg_arr = NULL;
1102 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msg_arr, buf);
1104 scr_printf("%s\n", buf);
1106 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1110 /* Now compose the message... */
1111 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject) != 0) {
1115 /* Reopen the temp file that was created, so we can send it */
1116 fp = fopen(temp, "r");
1118 /* Yes, unlink it now, so it doesn't stick around if we crash */
1121 if (!fp || !(message.text = load_message_from_file(fp))) {
1122 err_printf("*** Internal error while trying to save message!\n"
1124 temp, strerror(errno));
1130 /* Transmit message to the server */
1131 r = CtdlIPCPostMessage(ipc, 1, &message, buf);
1133 scr_printf("%s\n", buf);
1137 if (num_msgs >= 1) highmsg = msg_arr[num_msgs - 1];
1139 free(msg_arr); msg_arr = NULL;
1140 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msg_arr, buf);
1142 scr_printf("%s\n", buf);
1144 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1148 /* get new highest message number in room to set lrp for goto... */
1149 maxmsgnum = msg_arr[num_msgs - 1];
1151 /* now see if anyone else has posted in here */
1153 for (a = 0; a < num_msgs; ++a) {
1154 if (msg_arr[a] > highmsg) {
1159 /* In the Mail> room, this algorithm always counts one message
1160 * higher than in public rooms, so we decrement it by one.
1167 scr_printf("*** 1 additional message has been entered "
1168 "in this room by another user.\n");
1171 scr_printf("*** %d additional messages have been entered "
1172 "in this room by other users.\n", b);
1179 * Do editing on a quoted file
1181 void process_quote(void)
1183 FILE *qfile, *tfile;
1185 int line, qstart, qend;
1187 /* Unlink the second temp file as soon as it's opened, so it'll get
1188 * deleted even if the program dies
1190 qfile = fopen(temp2, "r");
1193 /* Display the quotable text with line numbers added */
1195 fgets(buf, 128, qfile);
1196 while (fgets(buf, 128, qfile) != NULL) {
1197 scr_printf("%2d %s", ++line, buf);
1199 scr_printf("Begin quoting at [ 1] : ");
1201 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1202 scr_printf(" End quoting at [%d] : ", line);
1204 qend = (buf[0] == 0) ? (line) : atoi(buf);
1207 fgets(buf, 128, qfile);
1208 tfile = fopen(temp, "w");
1209 while (fgets(buf, 128, qfile) != NULL) {
1210 if ((++line >= qstart) && (line <= qend))
1211 fprintf(tfile, " >%s", buf);
1213 fprintf(tfile, " \n");
1222 * List the URL's which were embedded in the previous message
1224 void list_urls(CtdlIPC *ipc)
1229 if (num_urls == 0) {
1230 scr_printf("There were no URL's in the previous message.\n\n");
1234 for (i = 0; i < num_urls; ++i) {
1235 scr_printf("%3d %s\n", i + 1, urls[i]);
1238 if ((i = num_urls) != 1)
1239 i = intprompt("Display which one", 1, 1, num_urls);
1241 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1247 * Read the messages in the current room
1249 void readmsgs(CtdlIPC *ipc,
1250 enum MessageList c, /* see listing in citadel_ipc.h */
1251 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1252 int q /* Number of msgs to read (if c==3) */
1254 int a, b, e, f, g, start;
1260 char prtfile[PATH_MAX];
1263 char targ[ROOMNAMELEN];
1264 char filename[PATH_MAX];
1265 char save_to[PATH_MAX];
1266 void *attachment = NULL; /* Downloaded attachment */
1267 FILE *dest = NULL; /* Alternate destination other than screen */
1268 int r; /* IPC response code */
1275 strcpy(prtfile, tmpnam(NULL));
1281 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1283 scr_printf("%s\n", cmd);
1285 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1289 if (num_msgs == 0) { /* TODO look at this later */
1290 if (c == LastMessages) return;
1291 scr_printf("*** There are no ");
1292 if (c == NewMessages) scr_printf("new ");
1293 if (c == OldMessages) scr_printf("old ");
1294 scr_printf("messages in this room.\n");
1300 /* this loop cycles through each message... */
1301 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1302 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1303 while (msg_arr[a] == 0L) {
1305 if ((a == num_msgs) || (a == (-1)))
1309 RAGAIN: pagin = ((arcflag == 0)
1311 && (userflags & US_PAGINATOR)) ? 1 : 0;
1313 /* If we're doing a quote, set the screenwidth to 72 */
1315 hold_sw = screenwidth;
1319 /* If printing or archiving, set the screenwidth to 80 */
1321 hold_sw = screenwidth;
1325 /* now read the message... */
1326 e = read_message(ipc, msg_arr[a], pagin, dest);
1328 /* ...and set the screenwidth back if we have to */
1329 if ((quotflag) || (arcflag)) {
1330 screenwidth = hold_sw;
1332 RMSGREAD: scr_flush();
1333 highest_msg_read = msg_arr[a];
1338 enable_color = hold_color;
1345 enable_color = hold_color;
1348 freopen(prtfile, "r", stdin);
1350 sttybbs(SB_RESTORE);
1351 ka_system(printcmd);
1352 sttybbs(SB_NO_INTR);
1360 } while ((g != f) && (g >= 0));
1361 scr_printf("Message printed.\n");
1363 if (rc_alt_semantics && c == 1) {
1366 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1370 if (((userflags & US_NOPROMPT) || (e == 2))
1371 && (((room_flags & QR_MAILBOX) == 0)
1372 || (rc_force_mail_prompts == 0))) {
1377 color(BRIGHT_WHITE);
1378 scr_printf("%d", num_msgs - a - 1);
1382 keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top m<Y> next ");
1383 if (rc_url_cmd[0] && num_urls)
1384 keyopt("<U>RLview ");
1385 keyopt("<?>help -> ");
1389 e = (inkey() & 127);
1391 /* return key same as <N> */ if (e == 10)
1393 /* space key same as <N> */ if (e == 32)
1395 /* del/move for aides only */
1397 && ((room_flags & QR_MAILBOX) ==
1399 if ((e == 'd') || (e == 'm'))
1402 /* print only if available */
1403 if ((e == 'p') && (strlen(printcmd) == 0))
1405 /* can't file if not allowed */
1407 && (rc_allow_attachments == 0))
1409 /* link only if browser avail*/
1411 && (strlen(rc_url_cmd) == 0))
1413 } while ((e != 'a') && (e != 'n') && (e != 's')
1414 && (e != 'd') && (e != 'm') && (e != 'p')
1415 && (e != 'q') && (e != 'b') && (e != 'h')
1416 && (e != 'r') && (e != 'f') && (e != '?')
1417 && (e != 'u') && (e != 'c') && (e != 'y'));
1423 scr_printf("Again");
1426 scr_printf("Delete");
1438 scr_printf("Print");
1441 scr_printf("Quote");
1447 scr_printf("Header");
1450 scr_printf("Reply");
1456 scr_printf("URL's");
1459 scr_printf("mY next");
1462 scr_printf("? <help>");
1465 if (userflags & US_DISAPPEAR)
1466 scr_printf("\r%79s\r", "");
1473 scr_printf("Options available here:\n"
1474 " ? Help (prints this message)\n"
1475 " S Stop reading immediately\n"
1476 " A Again (repeats last message)\n"
1477 " N Next (continue with next message)\n"
1478 " Y My Next (continue with next message you authored)\n"
1479 " B Back (go back to previous message)\n");
1481 || (room_flags & QR_MAILBOX)) {
1482 scr_printf(" D Delete this message\n"
1483 " M Move message to another room\n");
1485 scr_printf(" C Copy message to another room\n");
1486 if (strlen(printcmd) > 0)
1487 scr_printf(" P Print this message\n");
1489 " Q Quote portions of this message for your next post\n"
1490 " H Headers (display message headers only)\n");
1492 scr_printf(" R Reply to this message\n");
1493 if (rc_allow_attachments)
1495 (" F (save attachments to a file)\n");
1496 if (strlen(rc_url_cmd) > 0)
1497 scr_printf(" U (list URL's for display)\n");
1502 dest = fopen(prtfile, "w");
1504 hold_color = enable_color;
1509 dest = fopen(temp2, "w");
1511 hold_color = enable_color;
1523 newprompt("Enter target room: ",
1524 targ, ROOMNAMELEN - 1);
1525 if (strlen(targ) > 0) {
1526 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1527 msg_arr[a], targ, cmd);
1528 scr_printf("%s\n", cmd);
1534 if (r / 100 != 2) /* r will be init'ed, FIXME */
1535 goto RMSGREAD; /* the logic here sucks */
1538 newprompt("Which section? ", filename,
1539 ((sizeof filename) - 1));
1540 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1541 filename, &attachment, progress, cmd);
1542 extract(filename, cmd, 2);
1543 destination_directory(save_to, filename);
1544 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1545 filename, &attachment, progress, cmd);
1547 scr_printf("%s\n", cmd);
1549 save_buffer(attachment,
1550 extract_unsigned_long(cmd, 0),
1553 if (attachment) free(attachment);
1556 scr_printf("*** Delete this message? ");
1558 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1559 scr_printf("%s\n", cmd);
1567 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1570 savedpos = num_msgs;
1571 entmsg(ipc, 1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1572 num_msgs = savedpos;
1578 { /* hack hack hack */
1579 /* find the next message by me, stay here if we find nothing */
1582 for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1584 /* This is repetitively dumb, but that's what computers are for.
1585 We have to load up messages until we find one by us */
1589 snprintf(buf, sizeof buf, "MSG0 %ld|1", msg_arr[finda]); /* read the header so we can get 'from=' */
1590 CtdlIPC_putline(ipc, buf);
1591 CtdlIPC_getline(ipc, buf);
1592 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"))
1594 if ((!strncasecmp(buf, "from=", 5)) && (finda != a)) /* Skip current message. */
1596 if (strcasecmp(buf+5, fullname) == 0)
1598 a = lasta; /* meesa current */
1603 // we are now in synch with the server
1606 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1610 } /* end for loop */
1611 } /* end read routine */
1617 * View and edit a system message
1619 void edit_system_message(CtdlIPC *ipc, char *which_message)
1623 char write_cmd[SIZ];
1625 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1626 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1627 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1628 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1635 * Verify the message base
1637 void check_message_base(CtdlIPC *ipc)
1640 char *transcript = NULL;
1641 int r; /* IPC response code */
1644 ("Please read the documentation before running this command.\n"
1645 "Having done so, do you still want to check the message base? ");
1649 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1651 scr_printf("%s\n", buf);
1655 while (transcript && strlen(transcript)) {
1657 extract_token(buf, transcript, 0, '\n');
1658 remove_token(transcript, 0, '\n');
1659 pprintf("%s\n", buf);
1661 if (transcript) free(transcript);
1667 * Loads the contents of a file into memory. Caller must free the allocated
1670 char *load_message_from_file(FILE *src)
1676 fseek(src, 0, SEEK_END);
1680 dest = (char *)calloc(1, i + 1);
1687 g = fread(dest + got, 1, i - got, src);
1690 /* Interrupted system call, keep going */
1693 /* At this point we have either EOF or error */