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;
367 int final_line_is_blank = 0;
372 strcpy(reply_to, NO_REPLY_TO);
373 strcpy(reply_subject, "");
375 r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
376 (can_do_msg4 ? 4 : 0),
379 err_printf("*** msg #%ld: %d %s\n", num, r, buf);
382 checkpagin(lines_printed, pagin, screenheight);
388 fprintf(dest, "\n ");
392 lines_printed = checkpagin(lines_printed, pagin, screenheight);
396 if (pagin == 1 && !dest) {
400 /* View headers only */
402 pprintf("nhdr=%s\nfrom=%s\ntype=%d\nmsgn=%s\n",
403 message->nhdr ? "yes" : "no",
404 message->author, message->type,
406 if (strlen(message->subject)) {
407 pprintf("subj=%s\n", message->subject);
409 if (strlen(message->email)) {
410 pprintf("rfca=%s\n", message->email);
412 pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
413 message->hnod, message->room,
415 asctime(localtime(&message->time)));
416 if (strlen(message->recipient)) {
417 pprintf("rcpt=%s\n", message->recipient);
419 if (message->attachments) {
422 for (ptr = message->attachments; ptr; ptr = ptr->next) {
423 pprintf("part=%s|%s|%s|%s|%s|%ld\n",
424 ptr->name, ptr->filename, ptr->number,
425 ptr->disposition, ptr->mimetype,
434 if (rc_display_message_numbers) {
436 fprintf(dest, "[#%s] ", message->msgid);
441 scr_printf("#%s", message->msgid);
446 if (nhdr == 1 && !is_room_aide) {
448 fprintf(dest, " ****");
453 fmt_date(now, sizeof now, message->time, 0);
455 fprintf(dest, "%s from %s ", now, message->author);
456 if (strlen(message->email)) {
457 fprintf(dest, "<%s> ", message->email);
461 scr_printf("%s ", now);
465 scr_printf("%s ", message->author);
466 if (strlen(message->email)) {
470 scr_printf("%s", message->email);
475 if (strlen(message->node)) {
476 if ((room_flags & QR_NETWORK)
477 || ((strcasecmp(message->node, serv_info.serv_nodename)
478 && (strcasecmp(message->node, serv_info.serv_fqdn))))) {
479 if (strlen(message->email) == 0) {
481 fprintf(dest, "@%s ", message->node);
485 color(BRIGHT_YELLOW);
486 scr_printf("%s ", message->node);
491 if (strcasecmp(message->hnod, serv_info.serv_humannode)
492 && (strlen(message->hnod)) && (!strlen(message->email))) {
494 fprintf(dest, "(%s) ", message->hnod);
499 scr_printf("%s", message->hnod);
504 if (strcasecmp(message->room, room_name) && (strlen(message->email) == 0)) {
506 fprintf(dest, "in %s> ", message->room);
510 color(BRIGHT_MAGENTA);
511 scr_printf("%s> ", message->room);
514 if (strlen(message->recipient)) {
516 fprintf(dest, "to %s ", message->recipient);
521 scr_printf("%s ", message->recipient);
532 /* Set the reply-to address to an Internet e-mail address if possible
534 if (message->email != NULL) if (strlen(message->email) > 0) {
535 safestrncpy(reply_to, message->email, sizeof reply_to);
538 /* But if we can't do that, set it to a Citadel address.
540 if (!strcmp(reply_to, NO_REPLY_TO)) {
541 snprintf(reply_to, sizeof(reply_to), "%s @ %s",
542 message->author, message->node);
547 lines_printed = checkpagin(lines_printed, pagin, screenheight);
550 if (message->subject != NULL) {
551 safestrncpy(reply_subject, message->subject,
552 sizeof reply_subject);
553 if (strlen(message->subject) > 0) {
555 fprintf(dest, "Subject: %s\n",
559 scr_printf("Subject: ");
561 scr_printf("%s\n", message->subject);
563 lines_printed = checkpagin(lines_printed,
564 pagin, screenheight);
569 if (pagin == 1 && !dest) {
573 /******* end of header output, start of message text output *******/
576 * Convert HTML to plain text, formatting for the actual width
577 * of the client screen.
579 if (!strcasecmp(message->content_type, "text/html")) {
580 converted_text = html_to_ascii(message->text, screenwidth, 0);
581 if (converted_text != NULL) {
583 message->text = converted_text;
588 /* Text/plain is a different type */
589 if (!strcasecmp(message->content_type, "text/plain")) {
596 if (format_type == 0) {
597 fr = fmout(screenwidth, NULL, message->text, dest,
598 ((pagin == 1) ? 1 : 0), screenheight, (-1), 1);
600 /* renderer for text/plain */
602 lineptr = message->text;
605 nextline = strchr(lineptr, '\n');
606 if (nextline != NULL) {
609 if (*nextline == 0) nextline = NULL;
612 if (sigcaught == 0) {
613 linelen = strlen(lineptr);
614 if (lineptr[linelen-1] == '\r') {
615 lineptr[--linelen] = 0;
618 fprintf(dest, "%s\n", lineptr);
620 scr_printf("%s\n", lineptr);
621 lines_printed = lines_printed + 1 +
622 (linelen / screenwidth);
624 checkpagin(lines_printed, pagin,
628 if (lineptr[0] == 0) final_line_is_blank = 1;
629 else final_line_is_blank = 0;
634 if (!final_line_is_blank) {
641 lines_printed = checkpagin(lines_printed, pagin, screenheight);
645 /* Enumerate any attachments */
646 if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
649 for (ptr = message->attachments; ptr; ptr = ptr->next) {
650 if ( (!strcasecmp(ptr->disposition, "attachment"))
651 || (!strcasecmp(ptr->disposition, "inline"))) {
654 color(BRIGHT_MAGENTA);
655 scr_printf("%s", ptr->number);
659 scr_printf("%s", ptr->filename);
661 scr_printf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
670 if (pagin == 1 && !dest)
677 * replace string function for the built-in editor
679 void replace_string(char *filename, long int startpos)
688 int substitutions = 0;
691 scr_printf("Enter text to be replaced:\n: ");
692 getline(srch_str, (sizeof(srch_str)-1) );
693 if (strlen(srch_str) == 0)
696 scr_printf("Enter text to replace it with:\n: ");
697 getline(rplc_str, (sizeof(rplc_str)-1) );
699 fp = fopen(filename, "r+");
704 fseek(fp, startpos, 0);
706 while (a = getc(fp), a > 0) {
708 buf[strlen(buf) + 1] = 0;
709 buf[strlen(buf)] = a;
710 if (strlen(buf) >= strlen(srch_str)) {
711 ptr = (&buf[strlen(buf) - strlen(srch_str)]);
712 if (!strncmp(ptr, srch_str, strlen(srch_str))) {
713 strcpy(ptr, rplc_str);
717 if (strlen(buf) > 384) {
720 fwrite((char *) buf, 128, 1, fp);
721 strcpy(buf, &buf[128]);
728 fwrite((char *) buf, strlen(buf), 1, fp);
731 truncate(filename, wpos);
732 scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
736 * Function to begin composing a new message
738 int client_make_message(CtdlIPC *ipc,
739 char *filename, /* temporary file name */
740 char *recipient, /* NULL if it's not mail */
744 char *subject) /* buffer to store subject line */
751 char *editor_path = NULL;
756 if((mode-2) < MAX_EDITORS && strlen(editor_paths[mode-2]) > 0) {
757 editor_path = editor_paths[mode-2];
758 } else if (strlen(editor_paths[0]) > 0) {
759 editor_path = editor_paths[0];
762 ("*** No editor available, using built-in editor\n");
767 fmt_date(datestr, sizeof datestr, time(NULL), 0);
770 if (room_flags & QR_ANONONLY && !recipient) {
771 snprintf(header, sizeof header, " ****");
774 snprintf(header, sizeof header,
777 (is_anonymous ? "[anonymous]" : fullname)
779 if (strlen(recipient) > 0) {
780 size_t tmp = strlen(header);
781 snprintf(&header[tmp], sizeof header - tmp,
782 " to %s", recipient);
785 scr_printf("%s\n", header);
786 if (subject != NULL) if (strlen(subject) > 0) {
787 scr_printf("Subject: %s\n", subject);
793 scr_printf("(Press ctrl-d when finished)\n");
797 fp = fopen(filename, "r");
799 fmout(screenwidth, fp, NULL, NULL, 0, screenheight, 0, 0);
803 fp = fopen(filename, "w");
805 err_printf("*** Error opening temp file!\n"
807 filename, strerror(errno));
817 fp = fopen(filename, "r+");
819 err_printf("*** Error opening temp file!\n"
821 filename, strerror(errno));
829 fp = fopen(filename, "a");
831 err_printf("*** Error opening temp file!\n"
833 filename, strerror(errno));
853 default: /* allow 2+ modes */
854 e_ex_code = 1; /* start with a failed exit code */
856 cksum = file_checksum(filename);
859 if (editor_pid == 0) {
862 chmod(filename, 0600);
863 snprintf(tmp, sizeof tmp, "WINDOW_TITLE=%s", header);
865 execlp(editor_path, editor_path, filename, NULL);
871 b = ka_wait(&e_ex_code);
872 } while ((b != editor_pid) && (b >= 0));
879 MECR: if (mode >= 2) {
880 if (file_checksum(filename) == cksum) {
881 err_printf("*** Aborted message.\n");
889 b = keymenu("Entry command (? for options)",
890 "<A>bort|<C>ontinue|<S>ave message|<P>rint formatted|"
892 "<R>eplace string|<H>old message");
901 scr_printf(" %s from %s", datestr, fullname);
902 if (strlen(recipient) > 0)
903 scr_printf(" to %s", recipient);
905 if (subject != NULL) if (strlen(subject) > 0) {
906 scr_printf("Subject: %s\n", subject);
908 fp = fopen(filename, "r");
910 fmout(screenwidth, fp, NULL, NULL,
911 ((userflags & US_PAGINATOR) ? 1 : 0),
919 replace_string(filename, 0L);
926 if (subject != NULL) {
927 newprompt("Subject: ", subject, 70);
934 MEABT: scr_printf("Are you sure? ");
938 MEABT2: unlink(filename);
945 * Transmit message text to the server.
947 * This loop also implements a "tick" counter that displays the progress, if
948 * we're sending something that will take a long time to transmit.
950 void transmit_message(CtdlIPC *ipc, FILE *fp)
957 fseek(fp, 0L, SEEK_END);
960 lasttick = time(NULL);
962 while (ch = getc(fp), (ch >= 0)) {
964 if (!strcmp(buf, "000"))
966 CtdlIPC_putline(ipc, buf);
972 if ((ch == 32) && (strlen(buf) > 200)) {
974 if (!strcmp(buf, "000"))
976 CtdlIPC_putline(ipc, buf);
979 if (strlen(buf) > 250) {
980 if (!strcmp(buf, "000"))
982 CtdlIPC_putline(ipc, buf);
987 if ((time(NULL) - lasttick) > 2L) {
988 scr_printf(" %3ld%% completed\r",
989 ((ftell(fp) * 100L) / msglen));
991 lasttick = time(NULL);
995 CtdlIPC_putline(ipc, buf);
1003 * Make sure there's room in msg_arr[] for at least one more.
1005 void check_msg_arr_size(void) {
1006 if ((num_msgs + 1) > msg_arr_size) {
1007 msg_arr_size += 512;
1008 msg_arr = realloc(msg_arr,
1009 ((sizeof(long)) * msg_arr_size) );
1016 * entmsg() - edit and create a message
1017 * returns 0 if message was saved
1019 int entmsg(CtdlIPC *ipc,
1020 int is_reply, /* nonzero if this was a <R>eply command */
1030 struct ctdlipcmessage message;
1031 unsigned long *msgarr = NULL;
1032 int r; /* IPC response code */
1039 strcpy(subject, "");
1042 * First, check to see if we have permission to enter a message in
1043 * this room. The server will return an error code if we can't.
1045 strcpy(message.recipient, "");
1046 strcpy(message.author, "");
1047 strcpy(message.subject, "");
1048 message.text = message.author; /* point to "", changes later */
1049 message.anonymous = 0;
1050 message.type = mode;
1051 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1053 if (r / 100 != 2 && r / 10 != 57) {
1054 scr_printf("%s\n", buf);
1058 /* Error code 570 is special. It means that we CAN enter a message
1059 * in this room, but a recipient needs to be specified.
1065 /* If the user is a dumbass, tell them how to type. */
1066 if ((userflags & US_EXPERT) == 0) {
1067 formout(ipc, "entermsg");
1070 /* Handle the selection of a recipient, if necessary. */
1072 if (need_recp == 1) {
1075 strcpy(buf, reply_to);
1077 scr_printf("Enter recipient: ");
1078 getline(buf, (SIZ-100) );
1079 if (strlen(buf) == 0)
1083 strcpy(buf, "sysop");
1085 strcpy(message.recipient, buf);
1088 if (strlen(reply_subject) > 0) {
1089 if (!strncasecmp(reply_subject,
1091 strcpy(message.subject, reply_subject);
1094 snprintf(message.subject,
1095 sizeof message.subject,
1102 if (room_flags & QR_ANONOPT) {
1103 scr_printf("Anonymous (Y/N)? ");
1105 message.anonymous = 1;
1108 /* If it's mail, we've got to check the validity of the recipient... */
1109 if (strlen(message.recipient) > 0) {
1110 r = CtdlIPCPostMessage(ipc, 0, &message, buf);
1112 scr_printf("%s\n", buf);
1117 /* Learn the number of the newest message in in the room, so we can
1118 * tell upon saving whether someone else has posted too.
1121 r = CtdlIPCGetMessages(ipc, LastMessages, 1, NULL, &msgarr, buf);
1123 scr_printf("%s\n", buf);
1125 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1129 /* Now compose the message... */
1130 if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject) != 0) {
1134 /* Reopen the temp file that was created, so we can send it */
1135 fp = fopen(temp, "r");
1137 /* Yes, unlink it now, so it doesn't stick around if we crash */
1140 if (!fp || !(message.text = load_message_from_file(fp))) {
1141 err_printf("*** Internal error while trying to save message!\n"
1143 temp, strerror(errno));
1149 /* Transmit message to the server */
1150 r = CtdlIPCPostMessage(ipc, 1, &message, buf);
1152 scr_printf("%s\n", buf);
1156 if (num_msgs >= 1) highmsg = msgarr[num_msgs - 1];
1158 if (msgarr) free(msgarr);
1160 r = CtdlIPCGetMessages(ipc, NewMessages, 0, NULL, &msgarr, buf);
1162 scr_printf("%s\n", buf);
1164 for (num_msgs = 0; msgarr[num_msgs]; num_msgs++)
1168 /* get new highest message number in room to set lrp for goto... */
1169 maxmsgnum = msgarr[num_msgs - 1];
1171 /* now see if anyone else has posted in here */
1173 for (a = 0; a < num_msgs; ++a) {
1174 if (msgarr[a] > highmsg) {
1178 if (msgarr) free(msgarr);
1181 /* In the Mail> room, this algorithm always counts one message
1182 * higher than in public rooms, so we decrement it by one.
1189 scr_printf("*** 1 additional message has been entered "
1190 "in this room by another user.\n");
1193 scr_printf("*** %d additional messages have been entered "
1194 "in this room by other users.\n", b);
1201 * Do editing on a quoted file
1203 void process_quote(void)
1205 FILE *qfile, *tfile;
1207 int line, qstart, qend;
1209 /* Unlink the second temp file as soon as it's opened, so it'll get
1210 * deleted even if the program dies
1212 qfile = fopen(temp2, "r");
1215 /* Display the quotable text with line numbers added */
1217 fgets(buf, 128, qfile);
1218 while (fgets(buf, 128, qfile) != NULL) {
1219 scr_printf("%2d %s", ++line, buf);
1221 scr_printf("Begin quoting at [ 1] : ");
1223 qstart = (buf[0] == 0) ? (1) : atoi(buf);
1224 scr_printf(" End quoting at [%d] : ", line);
1226 qend = (buf[0] == 0) ? (line) : atoi(buf);
1229 fgets(buf, 128, qfile);
1230 tfile = fopen(temp, "w");
1231 while (fgets(buf, 128, qfile) != NULL) {
1232 if ((++line >= qstart) && (line <= qend))
1233 fprintf(tfile, " >%s", buf);
1235 fprintf(tfile, " \n");
1244 * List the URL's which were embedded in the previous message
1246 void list_urls(CtdlIPC *ipc)
1251 if (num_urls == 0) {
1252 scr_printf("There were no URL's in the previous message.\n\n");
1256 for (i = 0; i < num_urls; ++i) {
1257 scr_printf("%3d %s\n", i + 1, urls[i]);
1260 if ((i = num_urls) != 1)
1261 i = intprompt("Display which one", 1, 1, num_urls);
1263 snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
1269 * Read the messages in the current room
1271 void readmsgs(CtdlIPC *ipc,
1272 enum MessageList c, /* see listing in citadel_ipc.h */
1273 enum MessageDirection rdir, /* 1=Forward (-1)=Reverse */
1274 int q /* Number of msgs to read (if c==3) */
1276 int a, b, e, f, g, start;
1282 char prtfile[PATH_MAX];
1285 char targ[ROOMNAMELEN];
1286 char filename[PATH_MAX];
1287 char save_to[PATH_MAX];
1288 void *attachment = NULL; /* Downloaded attachment */
1289 FILE *dest = NULL; /* Alternate destination other than screen */
1290 int r; /* IPC response code */
1297 strcpy(prtfile, tmpnam(NULL));
1303 r = CtdlIPCGetMessages(ipc, c, q, NULL, &msg_arr, cmd);
1305 scr_printf("%s\n", cmd);
1307 for (num_msgs = 0; msg_arr[num_msgs]; num_msgs++)
1311 if (num_msgs == 0) { /* TODO look at this later */
1312 if (c == LastMessages) return;
1313 scr_printf("*** There are no ");
1314 if (c == NewMessages) scr_printf("new ");
1315 if (c == OldMessages) scr_printf("old ");
1316 scr_printf("messages in this room.\n");
1322 /* this loop cycles through each message... */
1323 start = ((rdir == 1) ? 0 : (num_msgs - 1));
1324 for (a = start; ((a < num_msgs) && (a >= 0)); a = a + rdir) {
1325 while (msg_arr[a] == 0L) {
1327 if ((a == num_msgs) || (a == (-1)))
1331 RAGAIN: pagin = ((arcflag == 0)
1333 && (userflags & US_PAGINATOR)) ? 1 : 0;
1335 /* If we're doing a quote, set the screenwidth to 72 */
1337 hold_sw = screenwidth;
1341 /* If printing or archiving, set the screenwidth to 80 */
1343 hold_sw = screenwidth;
1347 /* now read the message... */
1348 e = read_message(ipc, msg_arr[a], pagin, dest);
1350 /* ...and set the screenwidth back if we have to */
1351 if ((quotflag) || (arcflag)) {
1352 screenwidth = hold_sw;
1354 RMSGREAD: scr_flush();
1355 highest_msg_read = msg_arr[a];
1360 enable_color = hold_color;
1367 enable_color = hold_color;
1370 freopen(prtfile, "r", stdin);
1372 sttybbs(SB_RESTORE);
1373 ka_system(printcmd);
1374 sttybbs(SB_NO_INTR);
1382 } while ((g != f) && (g >= 0));
1383 scr_printf("Message printed.\n");
1385 if (rc_alt_semantics && c == 1) {
1388 r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
1392 if (((userflags & US_NOPROMPT) || (e == 2))
1393 && (((room_flags & QR_MAILBOX) == 0)
1394 || (rc_force_mail_prompts == 0))) {
1399 color(BRIGHT_WHITE);
1400 scr_printf("%d", num_msgs - a - 1);
1404 keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top m<Y> next ");
1405 if (rc_url_cmd[0] && num_urls)
1406 keyopt("<U>RLview ");
1407 keyopt("<?>help -> ");
1411 e = (inkey() & 127);
1413 /* return key same as <N> */ if (e == 10)
1415 /* space key same as <N> */ if (e == 32)
1417 /* del/move for aides only */
1419 && ((room_flags & QR_MAILBOX) ==
1421 if ((e == 'd') || (e == 'm'))
1424 /* print only if available */
1425 if ((e == 'p') && (strlen(printcmd) == 0))
1427 /* can't file if not allowed */
1429 && (rc_allow_attachments == 0))
1431 /* link only if browser avail*/
1433 && (strlen(rc_url_cmd) == 0))
1435 } while ((e != 'a') && (e != 'n') && (e != 's')
1436 && (e != 'd') && (e != 'm') && (e != 'p')
1437 && (e != 'q') && (e != 'b') && (e != 'h')
1438 && (e != 'r') && (e != 'f') && (e != '?')
1439 && (e != 'u') && (e != 'c') && (e != 'y'));
1445 scr_printf("Again");
1448 scr_printf("Delete");
1460 scr_printf("Print");
1463 scr_printf("Quote");
1469 scr_printf("Header");
1472 scr_printf("Reply");
1478 scr_printf("URL's");
1481 scr_printf("mY next");
1484 scr_printf("? <help>");
1487 if (userflags & US_DISAPPEAR)
1488 scr_printf("\r%79s\r", "");
1495 scr_printf("Options available here:\n"
1496 " ? Help (prints this message)\n"
1497 " S Stop reading immediately\n"
1498 " A Again (repeats last message)\n"
1499 " N Next (continue with next message)\n"
1500 " Y My Next (continue with next message you authored)\n"
1501 " B Back (go back to previous message)\n");
1503 || (room_flags & QR_MAILBOX)) {
1504 scr_printf(" D Delete this message\n"
1505 " M Move message to another room\n");
1507 scr_printf(" C Copy message to another room\n");
1508 if (strlen(printcmd) > 0)
1509 scr_printf(" P Print this message\n");
1511 " Q Quote portions of this message for your next post\n"
1512 " H Headers (display message headers only)\n");
1514 scr_printf(" R Reply to this message\n");
1515 if (rc_allow_attachments)
1517 (" F (save attachments to a file)\n");
1518 if (strlen(rc_url_cmd) > 0)
1519 scr_printf(" U (list URL's for display)\n");
1524 dest = fopen(prtfile, "w");
1526 hold_color = enable_color;
1531 dest = fopen(temp2, "w");
1533 hold_color = enable_color;
1545 newprompt("Enter target room: ",
1546 targ, ROOMNAMELEN - 1);
1547 if (strlen(targ) > 0) {
1548 r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
1549 msg_arr[a], targ, cmd);
1550 scr_printf("%s\n", cmd);
1556 if (r / 100 != 2) /* r will be init'ed, FIXME */
1557 goto RMSGREAD; /* the logic here sucks */
1560 newprompt("Which section? ", filename,
1561 ((sizeof filename) - 1));
1562 r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
1563 filename, &attachment, progress, cmd);
1565 scr_printf("%s\n", cmd);
1567 extract(filename, cmd, 2);
1568 destination_directory(save_to, filename);
1569 save_buffer(attachment,
1570 extract_unsigned_long(cmd, 0),
1573 if (attachment) free(attachment);
1576 scr_printf("*** Delete this message? ");
1578 r = CtdlIPCDeleteMessage(ipc, msg_arr[a], cmd);
1579 scr_printf("%s\n", cmd);
1587 read_message(ipc, msg_arr[a], READ_HEADER, NULL);
1590 savedpos = num_msgs;
1591 entmsg(ipc, 1, (DEFAULT_ENTRY == 46 ? 2 : 0));
1592 num_msgs = savedpos;
1598 { /* hack hack hack */
1599 /* find the next message by me, stay here if we find nothing */
1602 for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
1604 /* This is repetitively dumb, but that's what computers are for.
1605 We have to load up messages until we find one by us */
1609 snprintf(buf, sizeof buf, "MSG0 %ld|1", msg_arr[finda]); /* read the header so we can get 'from=' */
1610 CtdlIPC_putline(ipc, buf);
1611 CtdlIPC_getline(ipc, buf);
1612 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"))
1614 if ((!strncasecmp(buf, "from=", 5)) && (finda != a)) /* Skip current message. */
1616 if (strcasecmp(buf+5, fullname) == 0)
1618 a = lasta; /* meesa current */
1623 // we are now in synch with the server
1626 lasta = finda; /* keep one behind or we skip on the reentrance to the for */
1630 } /* end for loop */
1631 } /* end read routine */
1637 * View and edit a system message
1639 void edit_system_message(CtdlIPC *ipc, char *which_message)
1643 char write_cmd[SIZ];
1645 snprintf(desc, sizeof desc, "system message '%s'", which_message);
1646 snprintf(read_cmd, sizeof read_cmd, "MESG %s", which_message);
1647 snprintf(write_cmd, sizeof write_cmd, "EMSG %s", which_message);
1648 do_edit(ipc, desc, read_cmd, "NOOP", write_cmd);
1655 * Verify the message base
1657 void check_message_base(CtdlIPC *ipc)
1660 char *transcript = NULL;
1661 int r; /* IPC response code */
1664 ("Please read the documentation before running this command.\n"
1665 "Having done so, do you still want to check the message base? ");
1669 r = CtdlIPCMessageBaseCheck(ipc, &transcript, buf);
1671 scr_printf("%s\n", buf);
1675 while (transcript && strlen(transcript)) {
1677 extract_token(buf, transcript, 0, '\n');
1678 remove_token(transcript, 0, '\n');
1679 pprintf("%s\n", buf);
1681 if (transcript) free(transcript);
1687 * Loads the contents of a file into memory. Caller must free the allocated
1690 char *load_message_from_file(FILE *src)
1696 fseek(src, 0, SEEK_END);
1700 dest = (char *)calloc(1, i + 1);
1707 g = fread(dest + got, 1, i - got, src);
1710 /* Interrupted system call, keep going */
1713 /* At this point we have either EOF or error */