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 char urls[MAXURLS][SIZ];
93 void ka_sigcatch(int signum)
96 signal(SIGALRM, ka_sigcatch);
97 CtdlIPCNoop(ipc_for_signal_handlers);
102 * server keep-alive version of wait() (needed for external editor)
104 pid_t ka_wait(int *kstatus)
109 signal(SIGALRM, ka_sigcatch);
113 } while (errno == EINTR);
114 signal(SIGALRM, SIG_IGN);
121 * version of system() that uses ka_wait()
123 int ka_system(char *shc)
132 perror("Cannot fork");
134 return ((pid_t) childpid);
138 execlp("/bin/sh", "sh", "-c", shc, NULL);
144 waitpid = ka_wait(&retcode);
145 } while (waitpid != childpid);
155 * add a newline to the buffer...
157 void add_newline(struct cittext *textlist)
162 while (ptr->next != NULL)
165 while (ptr->text[strlen(ptr->text) - 1] == 32)
166 ptr->text[strlen(ptr->text) - 1] = 0;
167 /* strcat(ptr->text,"\n"); */
169 ptr->next = (struct cittext *)
170 malloc(sizeof(struct cittext));
173 strcpy(ptr->text, "");
178 * add a word to the buffer...
180 void add_word(struct cittext *textlist, char *wordbuf)
185 while (ptr->next != NULL)
188 if (3 + strlen(ptr->text) + strlen(wordbuf) > screenwidth) {
189 ptr->next = (struct cittext *)
190 malloc(sizeof(struct cittext));
193 strcpy(ptr->text, "");
196 strcat(ptr->text, wordbuf);
197 strcat(ptr->text, " ");
202 * begin editing of an opened file pointed to by fp
204 void citedit(CtdlIPC *ipc, FILE * fp)
206 int a, prev, finished, b, last_space;
208 struct cittext *textlist = NULL;
210 char wordbuf[MAXWORDBUF];
212 /* first, load the text into the buffer */
214 textlist = (struct cittext *) malloc(sizeof(struct cittext));
215 textlist->next = NULL;
216 strcpy(textlist->text, "");
220 while (a = getc(fp), a >= 0) {
222 if ((a == 32) || (a == 9) || (a == 13) || (a == 10)) {
223 add_word(textlist, wordbuf);
225 if ((prev == 13) || (prev == 10)) {
226 add_word(textlist, "\n");
227 add_newline(textlist);
228 add_word(textlist, "");
231 wordbuf[strlen(wordbuf) + 1] = 0;
232 wordbuf[strlen(wordbuf)] = a;
234 if (strlen(wordbuf) + 3 > screenwidth) {
235 add_word(textlist, wordbuf);
243 prev = (appending ? 13 : (-1));
255 /******* new ***********/
256 if ((a > 32) && (a < 127) && (prev == 13)) {
257 add_word(textlist, "\n");
260 /***********************/
262 if ((a == 32) && (prev == 13)) {
263 add_word(textlist, "\n");
264 add_newline(textlist);
268 if (strlen(wordbuf) > 0) {
269 wordbuf[strlen(wordbuf) - 1] = 0;
274 } else if (a == 23) {
276 wordbuf[strlen(wordbuf) - 1] = 0;
280 } while (strlen(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
281 } else if (a == 13) {
283 if (strlen(wordbuf) == 0)
286 for (b = 0; b < strlen(wordbuf); ++b)
287 if (wordbuf[b] == 32) {
295 add_word(textlist, wordbuf);
300 wordbuf[strlen(wordbuf) + 1] = 0;
301 wordbuf[strlen(wordbuf)] = a;
303 if ((strlen(wordbuf) + 3) > screenwidth) {
305 for (b = 0; b < strlen(wordbuf); ++b)
306 if (wordbuf[b] == 32)
308 if (last_space >= 0) {
309 for (b = 0; b < strlen(wordbuf); ++b)
310 if (wordbuf[b] == 32) {
318 for (b = 0; b < strlen(wordbuf); ++b) {
323 scr_printf("\n%s", wordbuf);
325 add_word(textlist, wordbuf);
331 } while (finished == 0);
334 /* write the buffer back to disk */
336 for (ptr = textlist; ptr != NULL; ptr = ptr->next) {
337 fprintf(fp, "%s", ptr->text);
341 ftruncate(fileno(fp), ftell(fp));
343 /* and deallocate the memory we used */
344 while (textlist != NULL) {
345 ptr = textlist->next;
351 /* Read a message from the server
353 int read_message(CtdlIPC *ipc,
354 long num, /* message number */
355 int pagin, /* 0 = normal read, 1 = read with pagination, 2 = header */
356 FILE *dest) /* Destination file, NULL for screen */
363 struct ctdlipcmessage *message = NULL;
364 int r; /* IPC response code */
365 char *converted_text = NULL;
372 int final_line_is_blank = 0;
377 strcpy(reply_to, NO_REPLY_TO);
378 strcpy(reply_subject, "");
380 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
381 (can_do_msg4 ? 4 : 0),
384 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
387 checkpagin(lines_printed, pagin, screenheight);
393 fprintf(dest, "\n ");
397 lines_printed = checkpagin(lines_printed, pagin, screenheight);
401 if (pagin == 1 && !dest) {
405 /* View headers only */
407 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
408 message->nhdr ? "yes" : "no",
409 message->author, message->type,
411 if (strlen(message->subject)) {
412 pprintf("subj=%s\n", message->subject);
414 if (strlen(message->email)) {
415 pprintf("rfca=%s\n", message->email);
417 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
418 message->hnod, message->room,
420 asctime(localtime(&message->time)));
421 if (strlen(message->recipient)) {
422 pprintf("rcpt=%s\n", message->recipient);
424 if (message->attachments) {
427 for (ptr = message->attachments; ptr; ptr = ptr->next) {
428 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
429 ptr->name, ptr->filename, ptr->number,
430 ptr->disposition, ptr->mimetype,
439 if (rc_display_message_numbers) {
441 fprintf(dest, "[#%s] ", message->msgid);
446 scr_printf("#%s", message->msgid);
451 if (nhdr == 1 && !is_room_aide) {
453 fprintf(dest, " ****");
458 fmt_date(now, sizeof now, message->time, 0);
460 fprintf(dest, "%s from %s ", now, message->author);
461 if (strlen(message->email)) {
462 fprintf(dest, "<%s> ", message->email);
466 scr_printf("%s ", now);
470 scr_printf("%s ", message->author);
471 if (strlen(message->email)) {
475 scr_printf("%s", message->email);
480 if (strlen(message->node)) {
481 if ((room_flags & QR_NETWORK)
482 || ((strcasecmp(message->node, serv_info.serv_nodename)
483 && (strcasecmp(message->node, serv_info.serv_fqdn))))) {
484 if (strlen(message->email) == 0) {
486 fprintf(dest, "@%s ", message->node);
490 color(BRIGHT_YELLOW);
491 scr_printf("%s ", message->node);
496 if (strcasecmp(message->hnod, serv_info.serv_humannode)
497 && (strlen(message->hnod)) && (!strlen(message->email))) {
499 fprintf(dest, "(%s) ", message->hnod);
504 scr_printf("%s", message->hnod);
509 if (strcasecmp(message->room, room_name) && (strlen(message->email) == 0)) {
511 fprintf(dest, "in %s> ", message->room);
515 color(BRIGHT_MAGENTA);
516 scr_printf("%s> ", message->room);
519 if (strlen(message->recipient)) {
521 fprintf(dest, "to %s ", message->recipient);
526 scr_printf("%s ", message->recipient);
537 /* Set the reply-to address to an Internet e-mail address if possible
539 if (message->email != NULL) if (strlen(message->email) > 0) {
540 safestrncpy(reply_to, message->email, sizeof reply_to);
543 /* But if we can't do that, set it to a Citadel address.
545 if (!strcmp(reply_to, NO_REPLY_TO)) {
546 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
547 message->author, message->node);
552 lines_printed = checkpagin(lines_printed, pagin, screenheight);
555 if (message->subject != NULL) {
556 safestrncpy(reply_subject, message->subject,
557 sizeof reply_subject);
558 if (strlen(message->subject) > 0) {
560 fprintf(dest, "Subject: %s\n",
564 scr_printf("Subject: ");
566 scr_printf("%s\n", message->subject);
568 lines_printed = checkpagin(lines_printed,
569 pagin, screenheight);
574 if (pagin == 1 && !dest) {
578 /******* end of header output, start of message text output *******/
581 * Convert HTML to plain text, formatting for the actual width
582 * of the client screen.
584 if (!strcasecmp(message->content_type, "text/html")) {
585 converted_text = html_to_ascii(message->text, screenwidth, 0);
586 if (converted_text != NULL) {
588 message->text = converted_text;
593 /* Text/plain is a different type */
594 if (!strcasecmp(message->content_type, "text/plain")) {
599 num_urls = 0; /* Start with a clean slate */
600 searchptr = message->text;
601 while (searchptr != NULL) {
602 searchptr = strstr(searchptr, "http://");
603 if (searchptr != NULL) {
604 safestrncpy(urls[num_urls], searchptr, sizeof(urls[num_urls]));
605 for (i = 0; i < strlen(urls[num_urls]); i++) {
606 ch = urls[num_urls][i];
607 if (ch == '>' || ch == '\"' || ch == ')' ||
608 ch == ' ' || ch == '\n') {
609 urls[num_urls][i] = 0;
621 if (format_type == 0) {
622 fr = fmout(screenwidth, NULL, message->text, dest,
623 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
625 /* renderer for text/plain */
627 lineptr = message->text;
630 nextline = strchr(lineptr, '\n');
631 if (nextline != NULL) {
634 if (*nextline == 0) nextline = NULL;
637 if (sigcaught == 0) {
638 linelen = strlen(lineptr);
639 if (lineptr[linelen-1] == '\r') {
640 lineptr[--linelen] = 0;
643 fprintf(dest, "%s\n", lineptr);
645 scr_printf("%s\n", lineptr);
646 lines_printed = lines_printed + 1 +
647 (linelen / screenwidth);
649 checkpagin(lines_printed, pagin,
653 if (lineptr[0] == 0) final_line_is_blank = 1;
654 else final_line_is_blank = 0;
659 if (!final_line_is_blank) {
666 lines_printed = checkpagin(lines_printed, pagin, screenheight);
670 /* Enumerate any attachments */
671 if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
674 for (ptr = message->attachments; ptr; ptr = ptr->next) {
675 if ( (!strcasecmp(ptr->disposition, "attachment"))
676 || (!strcasecmp(ptr->disposition, "inline"))) {
679 color(BRIGHT_MAGENTA);
680 scr_printf("%s", ptr->number);
684 scr_printf("%s", ptr->filename);
686 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
695 if (pagin == 1 && !dest)
702 * replace string function for the built-in editor
704 void replace_string(char *filename, long int startpos)
713 int substitutions = 0;
716 scr_printf("Enter text to be replaced:\n: ");
717 getline(srch_str, (sizeof(srch_str)-1) );
718 if (strlen(srch_str) == 0)
721 scr_printf("Enter text to replace it with:\n: ");
722 getline(rplc_str, (sizeof(rplc_str)-1) );
724 fp = fopen(filename, "r+");
729 fseek(fp, startpos, 0);
731 while (a = getc(fp), a > 0) {
733 buf[strlen(buf) + 1] = 0;
734 buf[strlen(buf)] = a;
735 if (strlen(buf) >= strlen(srch_str)) {
736 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
737 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
738 strcpy(ptr, rplc_str);
742 if (strlen(buf) > 384) {
745 fwrite((char *) buf, 128, 1, fp);
746 strcpy(buf, &buf[128]);
753 fwrite((char *) buf, strlen(buf), 1, fp);
756 truncate(filename, wpos);
757 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
761 * Function to begin composing a new message
763 int client_make_message(CtdlIPC *ipc,
764 char *filename, /* temporary file name */
765 char *recipient, /* NULL if it's not mail */
769 char *subject) /* buffer to store subject line */
776 char *editor_path = NULL;
781 if((mode-2) < MAX_EDITORS && strlen(editor_paths[mode-2]) > 0) {
782 editor_path = editor_paths[mode-2];
783 } else if (strlen(editor_paths[0]) > 0) {
784 editor_path = editor_paths[0];
787 ("*** No editor available, using built-in editor\n");
792 fmt_date(datestr, sizeof datestr, time(NULL), 0);
795 if (room_flags & QR_ANONONLY && !recipient) {
796 snprintf(header, sizeof header, " ****");
799 snprintf(header, sizeof header,
802 (is_anonymous ? "[anonymous]" : fullname)
804 if (strlen(recipient) > 0) {
805 size_t tmp = strlen(header);
806 snprintf(&header[tmp], sizeof header - tmp,
807 " to %s", recipient);
810 scr_printf("%s\n", header);
811 if (subject != NULL) if (strlen(subject) > 0) {
812 scr_printf("Subject: %s\n", subject);
818 scr_printf("(Press ctrl-d when finished)\n");
822 fp = fopen(filename, "r");
824 fmout(screenwidth, fp, NULL, NULL, 0, screenheight, 0, 0);
828 fp = fopen(filename, "w");
830 err_printf("*** Error opening temp file!\n"
832 filename, strerror(errno));
842 fp = fopen(filename, "r+");
844 err_printf("*** Error opening temp file!\n"
846 filename, strerror(errno));
854 fp = fopen(filename, "a");
856 err_printf("*** Error opening temp file!\n"
858 filename, strerror(errno));
878 default: /* allow 2+ modes */
879 e_ex_code = 1; /* start with a failed exit code */
881 cksum = file_checksum(filename);
884 if (editor_pid == 0) {
887 chmod(filename, 0600);
888 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
890 execlp(editor_path, editor_path, filename, NULL);
896 b = ka_wait(&e_ex_code);
897 } while ((b != editor_pid) && (b >= 0));
904 MECR: if (mode >= 2) {
905 if (file_checksum(filename) == cksum) {
906 err_printf("*** Aborted message.\n");
914 b = keymenu("Entry command (? for options)",
915 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
917 "<R>eplace string|<H>old message");
926 scr_printf(" %s from %s", datestr, fullname);
927 if (strlen(recipient) > 0)
928 scr_printf(" to %s", recipient);
930 if (subject != NULL) if (strlen(subject) > 0) {
931 scr_printf("Subject: %s\n", subject);
933 fp = fopen(filename, "r");
935 fmout(screenwidth, fp, NULL, NULL,
936 ((userflags & US_PAGINATOR) ? 1 : 0),
944 replace_string(filename, 0L);
951 if (subject != NULL) {
952 newprompt("Subject: ", subject, 70);
959 MEABT: scr_printf("Are you sure? ");
963 MEABT2: unlink(filename);
970 * Transmit message text to the server.
972 * This loop also implements a "tick" counter that displays the progress, if
973 * we're sending something that will take a long time to transmit.
975 void transmit_message(CtdlIPC *ipc, FILE *fp)
982 fseek(fp, 0L, SEEK_END);
985 lasttick = time(NULL);
987 while (ch = getc(fp), (ch >= 0)) {
989 if (!strcmp(buf, "000"))
991 CtdlIPC_putline(ipc, buf);
997 if ((ch == 32) && (strlen(buf) > 200)) {
999 if (!strcmp(buf, "000"))
1000 strcpy(buf, ">000");
1001 CtdlIPC_putline(ipc, buf);
1004 if (strlen(buf) > 250) {
1005 if (!strcmp(buf, "000"))
1006 strcpy(buf, ">000");
1007 CtdlIPC_putline(ipc, buf);
1012 if ((time(NULL) - lasttick) > 2L) {
1013 scr_printf(" %3ld%% completed\r",
1014 ((ftell(fp) * 100L) / msglen));
1016 lasttick = time(NULL);
1020 CtdlIPC_putline(ipc, buf);
1028 * Make sure there's room in msg_arr[] for at least one more.
1030 void check_msg_arr_size(void) {
1031 if ((num_msgs + 1) > msg_arr_size) {
1032 msg_arr_size += 512;
1033 msg_arr = realloc(msg_arr,
1034 ((sizeof(long)) * msg_arr_size) );
1041 * entmsg() - edit and create a message
1042 * returns 0 if message was saved
1044 int entmsg(CtdlIPC *ipc,
1045 int is_reply, /* nonzero if this was a <R>eply command */
1055 struct ctdlipcmessage message;
1056 unsigned long *msgarr = NULL;
1057 int r; /* IPC response code */
1064 strcpy(subject, "");
1067 * First, check to see if we have permission to enter a message in
1068 * this room. The server will return an error code if we can't.
1070 strcpy(message.recipient, "");
1071 strcpy(message.author, "");
1072 strcpy(message.subject, "");
1073 message.text = message.author; /* point to "", changes later */
1074 message.anonymous = 0;
1075 message.type = mode;
1076 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1078 if (r / 100 != 2 && r / 10 != 57) {
1079 scr_printf("%s\n", buf);
1083 /* Error code 570 is special. It means that we CAN enter a message
1084 * in this room, but a recipient needs to be specified.
1090 /* If the user is a dumbass, tell them how to type. */
1091 if ((userflags & US_EXPERT) == 0) {
1092 formout(ipc, "entermsg");
1095 /* Handle the selection of a recipient, if necessary. */
1097 if (need_recp == 1) {
1100 strcpy(buf, reply_to);
1102 scr_printf("Enter recipient: ");
1103 getline(buf, (SIZ-100) );
1104 if (strlen(buf) == 0)
1108 strcpy(buf, "sysop");
1110 strcpy(message.recipient, buf);
1113 if (strlen(reply_subject) > 0) {
1114 if (!strncasecmp(reply_subject,
1116 strcpy(message.subject, reply_subject);
1119 snprintf(message.subject,
1120 sizeof message.subject,
1127 if (room_flags & QR_ANONOPT) {
1128 scr_printf("Anonymous (Y/N)? ");
1130 message.anonymous = 1;
1133 /* If it's mail, we've got to check the validity of the recipient... */
1134 if (strlen(message.recipient) > 0) {
1135 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1137 scr_printf("%s\n", buf);
1142 /* Learn the number of the newest message in in the room, so we can
1143 * tell upon saving whether someone else has posted too.
1146 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1148 scr_printf("%s\n", buf);
1150 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1154 /* Now compose the message... */
1155 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject) != 0) {
1159 /* Reopen the temp file that was created, so we can send it */
1160 fp = fopen(temp, "r");
1162 /* Yes, unlink it now, so it doesn't stick around if we crash */
1165 if (!fp || !(message.text = load_message_from_file(fp))) {
1166 err_printf("*** Internal error while trying to save message!\n"
1168 temp, strerror(errno));
1174 /* Transmit message to the server */
1175 r = CtdlIPCPostMessage(ipc, 1, &message, buf);
1177 scr_printf("%s\n", buf);
1181 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1183 if (msgarr) free(msgarr);
1185 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1187 scr_printf("%s\n", buf);
1189 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1193 /* get new highest message number in room to set lrp for goto... */
1194 maxmsgnum = msgarr[num_msgs - 1];
1196 /* now see if anyone else has posted in here */
1198 for (a = 0; a < num_msgs; ++a) {
1199 if (msgarr[a] > highmsg) {
1203 if (msgarr) free(msgarr);
1206 /* In the Mail> room, this algorithm always counts one message
1207 * higher than in public rooms, so we decrement it by one.
1214 scr_printf("*** 1 additional message has been entered "
1215 "in this room by another user.\n");
1218 scr_printf("*** %d additional messages have been entered "
1219 "in this room by other users.\n", b);
1226 * Do editing on a quoted file
1228 void process_quote(void)
1230 FILE *qfile, *tfile;
1232 int line, qstart, qend;
1234 /* Unlink the second temp file as soon as it's opened, so it'll get
1235 * deleted even if the program dies
1237 qfile = fopen(temp2, "r");
1240 /* Display the quotable text with line numbers added */
1242 fgets(buf, 128, qfile);
1243 while (fgets(buf, 128, qfile) != NULL) {
1244 scr_printf("%2d %s", ++line, buf);
1246 scr_printf("Begin quoting at [ 1] : ");
1248 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1249 scr_printf(" End quoting at [%d] : ", line);
1251 qend = (buf[0] == 0) ? (line) : atoi(buf);
1254 fgets(buf, 128, qfile);
1255 tfile = fopen(temp, "w");
1256 while (fgets(buf, 128, qfile) != NULL) {
1257 if ((++line >= qstart) && (line <= qend))
1258 fprintf(tfile, " >%s", buf);
1260 fprintf(tfile, " \n");
1269 * List the URL's which were embedded in the previous message
1271 void list_urls(CtdlIPC *ipc)
1276 if (num_urls == 0) {
1277 scr_printf("There were no URL's in the previous message.\n\n");
1281 for (i = 0; i < num_urls; ++i) {
1282 scr_printf("%3d %s\n", i + 1, urls[i]);
1285 if ((i = num_urls) != 1)
1286 i = intprompt("Display which one", 1, 1, num_urls);
1288 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1294 * Read the messages in the current room
1296 void readmsgs(CtdlIPC *ipc,
1297 enum MessageList c, /* see listing in citadel_ipc.h */
1298 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1299 int q /* Number of msgs to read (if c==3) */
1301 int a, b, e, f, g, start;
1307 char prtfile[PATH_MAX];
1310 char targ[ROOMNAMELEN];
1311 char filename[PATH_MAX];
1312 char save_to[PATH_MAX];
1313 void *attachment = NULL; /* Downloaded attachment */
1314 FILE *dest = NULL; /* Alternate destination other than screen */
1315 int r; /* IPC response code */
1322 strcpy(prtfile, tmpnam(NULL));
1328 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1330 scr_printf("%s\n", cmd);
1332 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1336 if (num_msgs == 0) { /* TODO look at this later */
1337 if (c == LastMessages) return;
1338 scr_printf("*** There are no ");
1339 if (c == NewMessages) scr_printf("new ");
1340 if (c == OldMessages) scr_printf("old ");
1341 scr_printf("messages in this room.\n");
1347 /* this loop cycles through each message... */
1348 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1349 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1350 while (msg_arr[a] == 0L) {
1352 if ((a == num_msgs) || (a == (-1)))
1356 RAGAIN: pagin = ((arcflag == 0)
1358 && (userflags & US_PAGINATOR)) ? 1 : 0;
1360 /* If we're doing a quote, set the screenwidth to 72 */
1362 hold_sw = screenwidth;
1366 /* If printing or archiving, set the screenwidth to 80 */
1368 hold_sw = screenwidth;
1372 /* now read the message... */
1373 e = read_message(ipc, msg_arr[a], pagin, dest);
1375 /* ...and set the screenwidth back if we have to */
1376 if ((quotflag) || (arcflag)) {
1377 screenwidth = hold_sw;
1379 RMSGREAD: scr_flush();
1380 highest_msg_read = msg_arr[a];
1385 enable_color = hold_color;
1392 enable_color = hold_color;
1395 freopen(prtfile, "r", stdin);
1397 sttybbs(SB_RESTORE);
1398 ka_system(printcmd);
1399 sttybbs(SB_NO_INTR);
1407 } while ((g != f) && (g >= 0));
1408 scr_printf("Message printed.\n");
1410 if (rc_alt_semantics && c == 1) {
1413 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1417 if (((userflags & US_NOPROMPT) || (e == 2))
1418 && (((room_flags & QR_MAILBOX) == 0)
1419 || (rc_force_mail_prompts == 0))) {
1424 color(BRIGHT_WHITE);
1425 scr_printf("%d", num_msgs - a - 1);
1429 keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top m<Y> next ");
1430 if (rc_url_cmd[0] && num_urls)
1431 keyopt("<U>RLview ");
1432 keyopt("<?>help -> ");
1436 e = (inkey() & 127);
1438 /* return key same as <N> */ if (e == 10)
1440 /* space key same as <N> */ if (e == 32)
1442 /* del/move for aides only */
1444 && ((room_flags & QR_MAILBOX) ==
1446 if ((e == 'd') || (e == 'm'))
1449 /* print only if available */
1450 if ((e == 'p') && (strlen(printcmd) == 0))
1452 /* can't file if not allowed */
1454 && (rc_allow_attachments == 0))
1456 /* link only if browser avail*/
1458 && (strlen(rc_url_cmd) == 0))
1460 } while ((e != 'a') && (e != 'n') && (e != 's')
1461 && (e != 'd') && (e != 'm') && (e != 'p')
1462 && (e != 'q') && (e != 'b') && (e != 'h')
1463 && (e != 'r') && (e != 'f') && (e != '?')
1464 && (e != 'u') && (e != 'c') && (e != 'y'));
1470 scr_printf("Again");
1473 scr_printf("Delete");
1485 scr_printf("Print");
1488 scr_printf("Quote");
1494 scr_printf("Header");
1497 scr_printf("Reply");
1503 scr_printf("URL's");
1506 scr_printf("mY next");
1509 scr_printf("? <help>");
1512 if (userflags & US_DISAPPEAR)
1513 scr_printf("\r%79s\r", "");
1520 scr_printf("Options available here:\n"
1521 " ? Help (prints this message)\n"
1522 " S Stop reading immediately\n"
1523 " A Again (repeats last message)\n"
1524 " N Next (continue with next message)\n"
1525 " Y My Next (continue with next message you authored)\n"
1526 " B Back (go back to previous message)\n");
1528 || (room_flags & QR_MAILBOX)) {
1529 scr_printf(" D Delete this message\n"
1530 " M Move message to another room\n");
1532 scr_printf(" C Copy message to another room\n");
1533 if (strlen(printcmd) > 0)
1534 scr_printf(" P Print this message\n");
1536 " Q Quote portions of this message for your next post\n"
1537 " H Headers (display message headers only)\n");
1539 scr_printf(" R Reply to this message\n");
1540 if (rc_allow_attachments)
1542 (" F (save attachments to a file)\n");
1543 if (strlen(rc_url_cmd) > 0)
1544 scr_printf(" U (list URL's for display)\n");
1549 dest = fopen(prtfile, "w");
1551 hold_color = enable_color;
1556 dest = fopen(temp2, "w");
1558 hold_color = enable_color;
1570 newprompt("Enter target room: ",
1571 targ, ROOMNAMELEN - 1);
1572 if (strlen(targ) > 0) {
1573 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1574 msg_arr[a], targ, cmd);
1575 scr_printf("%s\n", cmd);
1581 if (r / 100 != 2) /* r will be init'ed, FIXME */
1582 goto RMSGREAD; /* the logic here sucks */
1585 newprompt("Which section? ", filename,
1586 ((sizeof filename) - 1));
1587 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1588 filename, &attachment, progress, cmd);
1590 scr_printf("%s\n", cmd);
1592 extract(filename, cmd, 2);
1594 * Part 1 won't have a filename; use the
1595 * subject of the message instead. IO
1597 if (!strlen(filename))
1598 strcpy(filename, reply_subject);
1599 destination_directory(save_to, filename);
1600 save_buffer(attachment,
1601 extract_unsigned_long(cmd, 0),
1604 if (attachment) free(attachment);
1607 scr_printf("*** Delete this message? ");
1609 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1610 scr_printf("%s\n", cmd);
1618 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1621 savedpos = num_msgs;
1622 entmsg(ipc, 1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1623 num_msgs = savedpos;
1629 { /* hack hack hack */
1630 /* find the next message by me, stay here if we find nothing */
1633 for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1635 /* This is repetitively dumb, but that's what computers are for.
1636 We have to load up messages until we find one by us */
1640 /* read the header so we can get 'from=' */
1641 snprintf(buf, sizeof buf, "MSG0 %ld|1", msg_arr[finda]);
1642 CtdlIPC_putline(ipc, buf);
1643 CtdlIPC_getline(ipc, buf);
1644 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"))
1646 if ((!strncasecmp(buf, "from=", 5)) && (finda != a)) /* Skip current message. */
1648 if (strcasecmp(buf+5, fullname) == 0)
1650 a = lasta; /* meesa current */
1655 // we are now in synch with the server
1658 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1662 } /* end for loop */
1663 } /* end read routine */
1669 * View and edit a system message
1671 void edit_system_message(CtdlIPC *ipc, char *which_message)
1675 char write_cmd[SIZ];
1677 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1678 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1679 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1680 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1687 * Verify the message base
1689 void check_message_base(CtdlIPC *ipc)
1692 char *transcript = NULL;
1693 int r; /* IPC response code */
1696 ("Please read the documentation before running this command.\n"
1697 "Having done so, do you still want to check the message base? ");
1701 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1703 scr_printf("%s\n", buf);
1707 while (transcript && strlen(transcript)) {
1709 extract_token(buf, transcript, 0, '\n');
1710 remove_token(transcript, 0, '\n');
1711 pprintf("%s\n", buf);
1713 if (transcript) free(transcript);
1719 * Loads the contents of a file into memory. Caller must free the allocated
1722 char *load_message_from_file(FILE *src)
1728 fseek(src, 0, SEEK_END);
1732 dest = (char *)calloc(1, i + 1);
1739 g = fread(dest + got, 1, i - got, src);
1742 /* Interrupted system call, keep going */
1745 /* At this point we have either EOF or error */