Create some directories to hold the source files for the utility
[citadel.git] / citadel / messages.c
index 108638879d493010a9f05e6a5709a3f75ca39d69..fdd8d46b4c2649ad23967994f283bbddf4d04138 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * $Id$
  *
- * Citadel/UX message support routines
- * see copyright.txt for copyright information
+ * Citadel message support routines
+ * see COPYING for copyright information
  */
 
 #include "sysdep.h"
 #endif
 
 #include <stdarg.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "citadel_ipc.h"
 #include "citadel_decls.h"
 #include "messages.h"
 #include "commands.h"
 #include "rooms.h"
-#include "tools.h"
-#include "html.h"
 #ifndef HAVE_SNPRINTF
 #include "snprintf.h"
 #endif
 
 char reply_to[SIZ];
 char reply_subject[SIZ];
+char reply_references[SIZ];
+char reply_inreplyto[SIZ];
 
 struct cittext {
        struct cittext *next;
        char text[MAXWORDBUF];
 };
 
-void sttybbs(int cmd);
+void stty_ctdl(int cmd);
 int haschar(const char *st, int ch);
-void getline(char *string, int lim);
+void ctdl_getline(char *string, int lim);
 int file_checksum(char *filename);
 void progress(CtdlIPC* ipc, unsigned long curr, unsigned long cmax);
 
@@ -69,9 +70,10 @@ unsigned long *msg_arr = NULL;
 int msg_arr_size = 0;
 int num_msgs;
 char rc_alt_semantics;
-char rc_reply_extedit;
 extern char room_name[];
+extern char tempdir[];
 extern unsigned room_flags;
+extern unsigned room_flags2;
 extern long highest_msg_read;
 extern char temp[];
 extern char temp2[];
@@ -217,6 +219,7 @@ void citedit(CtdlIPC *ipc, FILE * fp)
        struct cittext *textlist = NULL;
        struct cittext *ptr;
        char wordbuf[MAXWORDBUF];
+       int rv = 0;
 
        /* first, load the text into the buffer */
        fseek(fp, 0L, 0);
@@ -261,12 +264,10 @@ void citedit(CtdlIPC *ipc, FILE * fp)
                if (a == 127)
                        a = 8;
 
-       /******* new ***********/
-               if ((a > 32) && (a < 127) && (prev == 13)) {
+               if ((a != 32) && (prev == 13)) {
                        add_word(textlist, "\n");
                        scr_printf(" ");
                }
-       /***********************/
 
                if ((a == 32) && (prev == 13)) {
                        add_word(textlist, "\n");
@@ -274,7 +275,7 @@ void citedit(CtdlIPC *ipc, FILE * fp)
                }
 
                if (a == 8) {
-                       if (strlen(wordbuf) > 0) {
+                       if (!IsEmptyStr(wordbuf)) {
                                wordbuf[strlen(wordbuf) - 1] = 0;
                                scr_putc(8);
                                scr_putc(32);
@@ -286,10 +287,10 @@ void citedit(CtdlIPC *ipc, FILE * fp)
                                scr_putc(8);
                                scr_putc(32);
                                scr_putc(8);
-                       } while (strlen(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
+                       } while (!IsEmptyStr(wordbuf) && wordbuf[strlen(wordbuf) - 1] != ' ');
                } else if (a == 13) {
                        scr_printf("\n");
-                       if (strlen(wordbuf) == 0)
+                       if (IsEmptyStr(wordbuf))
                                finished = 1;
                        else {
                                for (b = 0; b < strlen(wordbuf); ++b)
@@ -347,7 +348,7 @@ void citedit(CtdlIPC *ipc, FILE * fp)
        }
        putc(10, fp);
        fflush(fp);
-       ftruncate(fileno(fp), ftell(fp));
+       rv = ftruncate(fileno(fp), ftell(fp));
 
        /* and deallocate the memory we used */
        while (textlist != NULL) {
@@ -402,20 +403,22 @@ int read_message(CtdlIPC *ipc,
        has_images = 0;
 
        sigcaught = 0;
-       sttybbs(1);
+       stty_ctdl(1);
 
        strcpy(reply_to, NO_REPLY_TO);
        strcpy(reply_subject, "");
+       strcpy(reply_references, "");
+       strcpy(reply_inreplyto, "");
 
-       r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0),
-                               (can_do_msg4 ? 4 : 0),
-                               &message, buf);
+       r = CtdlIPCGetSingleMessage(ipc, num, (pagin == READ_HEADER ? 1 : 0), 4, &message, buf);
        if (r / 100 != 1) {
                err_printf("*** msg #%ld: %d %s\n", num, r, buf);
                ++lines_printed;
-               lines_printed =
-                   checkpagin(lines_printed, pagin, screenheight);
-               sttybbs(0);
+               lines_printed = checkpagin(lines_printed, pagin, screenheight);
+               stty_ctdl(0);
+               free(message->text);
+               free_parts(message->attachments);
+               free(message);
                return (0);
        }
 
@@ -438,17 +441,17 @@ int read_message(CtdlIPC *ipc,
                                message->nhdr ? "yes" : "no",
                                message->author, message->type,
                                message->msgid);
-               if (strlen(message->subject)) {
+               if (!IsEmptyStr(message->subject)) {
                        pprintf("subj=%s\n", message->subject);
                }
-               if (strlen(message->email)) {
+               if (!IsEmptyStr(message->email)) {
                        pprintf("rfca=%s\n", message->email);
                }
                pprintf("hnod=%s\nroom=%s\nnode=%s\ntime=%s",
                                message->hnod, message->room,
                                message->node, 
                                asctime(localtime(&message->time)));
-               if (strlen(message->recipient)) {
+               if (!IsEmptyStr(message->recipient)) {
                        pprintf("rcpt=%s\n", message->recipient);
                }
                if (message->attachments) {
@@ -462,7 +465,10 @@ int read_message(CtdlIPC *ipc,
                        }
                }
                pprintf("\n");
-               sttybbs(0);
+               stty_ctdl(0);
+               free(message->text);
+               free_parts(message->attachments);
+               free(message);
                return (0);
        }
 
@@ -488,7 +494,7 @@ int read_message(CtdlIPC *ipc,
                fmt_date(now, sizeof now, message->time, 0);
                if (dest) {
                        fprintf(dest, "%s from %s ", now, message->author);
-                       if (strlen(message->email)) {
+                       if (!IsEmptyStr(message->email)) {
                                fprintf(dest, "<%s> ", message->email);
                        }
                } else {
@@ -498,7 +504,7 @@ int read_message(CtdlIPC *ipc,
                        scr_printf("from ");
                        color(BRIGHT_CYAN);
                        scr_printf("%s ", message->author);
-                       if (strlen(message->email)) {
+                       if (!IsEmptyStr(message->email)) {
                                color(DIM_WHITE);
                                scr_printf("<");
                                color(BRIGHT_BLUE);
@@ -507,11 +513,11 @@ int read_message(CtdlIPC *ipc,
                                scr_printf("> ");
                        }
                }
-               if (strlen(message->node)) {
+               if (!IsEmptyStr(message->node)) {
                        if ((room_flags & QR_NETWORK)
                            || ((strcasecmp(message->node, ipc->ServInfo.nodename)
                             && (strcasecmp(message->node, ipc->ServInfo.fqdn))))) {
-                               if (strlen(message->email) == 0) {
+                               if (IsEmptyStr(message->email)) {
                                        if (dest) {
                                                fprintf(dest, "@%s ", message->node);
                                        } else {
@@ -524,7 +530,7 @@ int read_message(CtdlIPC *ipc,
                        }
                }
                if (strcasecmp(message->hnod, ipc->ServInfo.humannode)
-                   && (strlen(message->hnod)) && (!strlen(message->email))) {
+                   && (!IsEmptyStr(message->hnod)) && (IsEmptyStr(message->email))) {
                        if (dest) {
                                fprintf(dest, "(%s) ", message->hnod);
                        } else {
@@ -536,7 +542,7 @@ int read_message(CtdlIPC *ipc,
                                scr_printf(") ");
                        }
                }
-               if (strcasecmp(message->room, room_name) && (strlen(message->email) == 0)) {
+               if (strcasecmp(message->room, room_name) && (IsEmptyStr(message->email))) {
                        if (dest) {
                                fprintf(dest, "in %s> ", message->room);
                        } else {
@@ -546,7 +552,7 @@ int read_message(CtdlIPC *ipc,
                                scr_printf("%s> ", message->room);
                        }
                }
-               if (strlen(message->recipient)) {
+               if (!IsEmptyStr(message->recipient)) {
                        if (dest) {
                                fprintf(dest, "to %s ", message->recipient);
                        } else {
@@ -566,8 +572,13 @@ int read_message(CtdlIPC *ipc,
 
        /* Set the reply-to address to an Internet e-mail address if possible
         */
-       if (message->email != NULL) if (strlen(message->email) > 0) {
-               safestrncpy(reply_to, message->email, sizeof reply_to);
+       if ((message->email != NULL) && (!IsEmptyStr(message->email))) {
+               if (!IsEmptyStr(message->author)) {
+                       snprintf(reply_to, sizeof reply_to, "%s <%s>", message->author, message->email);
+               }
+               else {
+                       safestrncpy(reply_to, message->email, sizeof reply_to);
+               }
        }
 
        /* But if we can't do that, set it to a Citadel address.
@@ -582,10 +593,18 @@ int read_message(CtdlIPC *ipc,
                lines_printed = checkpagin(lines_printed, pagin, screenheight);
        }
 
+
+       if (message->msgid != NULL) {
+               safestrncpy(reply_inreplyto, message->msgid, sizeof reply_inreplyto);
+       }
+
+       if (message->references != NULL) if (!IsEmptyStr(message->references)) {
+               safestrncpy(reply_references, message->references, sizeof reply_references);
+       }
+
        if (message->subject != NULL) {
-               safestrncpy(reply_subject, message->subject,
-                                               sizeof reply_subject);
-               if (strlen(message->subject) > 0) {
+               safestrncpy(reply_subject, message->subject, sizeof reply_subject);
+               if (!IsEmptyStr(message->subject)) {
                        if (dest) {
                                fprintf(dest, "Subject: %s\n",
                                                        message->subject);
@@ -612,7 +631,7 @@ int read_message(CtdlIPC *ipc,
         * of the client screen.
         */
        if (!strcasecmp(message->content_type, "text/html")) {
-               converted_text = html_to_ascii(message->text, screenwidth, 0);
+               converted_text = html_to_ascii(message->text, 0, screenwidth, 0);
                if (converted_text != NULL) {
                        free(message->text);
                        message->text = converted_text;
@@ -666,7 +685,7 @@ int read_message(CtdlIPC *ipc,
 
                        if (sigcaught == 0) {
                                linelen = strlen(lineptr);
-                               if (lineptr[linelen-1] == '\r') {
+                               if (linelen && (lineptr[linelen-1] == '\r')) {
                                        lineptr[--linelen] = 0;
                                }
                                if (dest) {
@@ -694,28 +713,36 @@ int read_message(CtdlIPC *ipc,
                        scr_printf("\n");
                        ++lines_printed;
                        lines_printed = checkpagin(lines_printed, pagin, screenheight);
+                       fr = sigcaught;         
                }
        }
 
        /* Enumerate any attachments */
-       if ( (pagin == 1) && (can_do_msg4) && (message->attachments) ) {
+       if ( (pagin == 1) && (message->attachments) ) {
                struct parts *ptr;
 
                for (ptr = message->attachments; ptr; ptr = ptr->next) {
                        if ( (!strcasecmp(ptr->disposition, "attachment"))
-                          || (!strcasecmp(ptr->disposition, "inline"))) {
-                               color(DIM_WHITE);
-                               pprintf("Part ");
-                               color(BRIGHT_MAGENTA);
-                               pprintf("%s", ptr->number);
-                               color(DIM_WHITE);
-                               pprintf(": ");
-                               color(BRIGHT_CYAN);
-                               pprintf("%s", ptr->filename);
-                               color(DIM_WHITE);
-                               pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
-                               if (!strncmp(ptr->mimetype, "image/", 6))
-                                       has_images++;
+                          || (!strcasecmp(ptr->disposition, "inline"))
+                          || (!strcasecmp(ptr->disposition, ""))
+                       ) {
+                               if ( (strcasecmp(ptr->number, message->mime_chosen))
+                                  && (!IsEmptyStr(ptr->mimetype))
+                               ) {
+                                       color(DIM_WHITE);
+                                       pprintf("Part ");
+                                       color(BRIGHT_MAGENTA);
+                                       pprintf("%s", ptr->number);
+                                       color(DIM_WHITE);
+                                       pprintf(": ");
+                                       color(BRIGHT_CYAN);
+                                       pprintf("%s", ptr->filename);
+                                       color(DIM_WHITE);
+                                       pprintf(" (%s, %ld bytes)\n", ptr->mimetype, ptr->length);
+                                       if (!strncmp(ptr->mimetype, "image/", 6)) {
+                                               has_images++;
+                                       }
+                               }
                        }
                }
        }
@@ -729,7 +756,7 @@ int read_message(CtdlIPC *ipc,
 
        if (pagin == 1 && !dest)
                color(DIM_WHITE);
-       sttybbs(0);
+       stty_ctdl(0);
        return (fr);
 }
 
@@ -747,18 +774,21 @@ void replace_string(char *filename, long int startpos)
        char *ptr;
        int substitutions = 0;
        long msglen = 0L;
+       int rv;
 
        scr_printf("Enter text to be replaced:\n: ");
-       getline(srch_str, (sizeof(srch_str)-1) );
-       if (strlen(srch_str) == 0)
+       ctdl_getline(srch_str, (sizeof(srch_str)-1) );
+       if (IsEmptyStr(srch_str)) {
                return;
+       }
 
        scr_printf("Enter text to replace it with:\n: ");
-       getline(rplc_str, (sizeof(rplc_str)-1) );
+       ctdl_getline(rplc_str, (sizeof(rplc_str)-1) );
 
        fp = fopen(filename, "r+");
-       if (fp == NULL)
+       if (fp == NULL) {
                return;
+       }
 
        wpos = startpos;
        fseek(fp, startpos, 0);
@@ -777,18 +807,19 @@ void replace_string(char *filename, long int startpos)
                if (strlen(buf) > 384) {
                        rpos = ftell(fp);
                        fseek(fp, wpos, 0);
-                       fwrite((char *) buf, 128, 1, fp);
+                       rv = fwrite((char *) buf, 128, 1, fp);
                        strcpy(buf, &buf[128]);
                        wpos = ftell(fp);
                        fseek(fp, rpos, 0);
                }
        }
        fseek(fp, wpos, 0);
-       if (strlen(buf) > 0)
-               fwrite((char *) buf, strlen(buf), 1, fp);
+       if (!IsEmptyStr(buf)) {
+               rv = fwrite((char *) buf, strlen(buf), 1, fp);
+       }
        wpos = ftell(fp);
        fclose(fp);
-       truncate(filename, wpos);
+       rv = truncate(filename, wpos);
        scr_printf("<R>eplace made %d substitution(s).\n\n", substitutions);
 }
 
@@ -796,12 +827,13 @@ void replace_string(char *filename, long int startpos)
  * Function to begin composing a new message
  */
 int client_make_message(CtdlIPC *ipc,
-               char *filename,         /* temporary file name */
-               char *recipient,        /* NULL if it's not mail */
-               int is_anonymous,
-               int format_type,
-               int mode,
-               char *subject)          /* buffer to store subject line */
+                                               char *filename,         /* temporary file name */
+                                               char *recipient,        /* NULL if it's not mail */
+                                               int is_anonymous,
+                                               int format_type,
+                                               int mode,
+                                               char *subject,          /* buffer to store subject line */
+                                               int subject_required)
 {
        FILE *fp;
        int a, b, e_ex_code;
@@ -813,13 +845,13 @@ int client_make_message(CtdlIPC *ipc,
 
        if (mode >= 2)
        {
-               if((mode-2) < MAX_EDITORS && strlen(editor_paths[mode-2]) > 0) {
+               if((mode-2) < MAX_EDITORS && !IsEmptyStr(editor_paths[mode-2])) {
                        editor_path = editor_paths[mode-2];
-               } else if (strlen(editor_paths[0]) > 0) {
+               } else if (!IsEmptyStr(editor_paths[0])) {
                        editor_path = editor_paths[0];
                } else {
-                       err_printf
-                           ("*** No editor available, using built-in editor\n");
+                       err_printf("*** No editor available, "
+                               "using built-in editor\n");
                        mode = 0;
                }
        }
@@ -836,16 +868,20 @@ int client_make_message(CtdlIPC *ipc,
                        datestr,
                        (is_anonymous ? "[anonymous]" : fullname)
                        );
-               if (strlen(recipient) > 0) {
+               if (!IsEmptyStr(recipient)) {
                        size_t tmp = strlen(header);
                        snprintf(&header[tmp], sizeof header - tmp,
                                " to %s", recipient);
                }
        }
        scr_printf("%s\n", header);
-       if (subject != NULL) if (strlen(subject) > 0) {
+       if (subject != NULL) if (!IsEmptyStr(subject)) {
                scr_printf("Subject: %s\n", subject);
        }
+       
+       if ( (subject_required) && (IsEmptyStr(subject)) ) {
+               newprompt("Subject: ", subject, 70);
+       }
 
        beg = 0L;
 
@@ -856,7 +892,8 @@ int client_make_message(CtdlIPC *ipc,
        if (mode == 0) {
                fp = fopen(filename, "r");
                if (fp != NULL) {
-                       fmout(screenwidth, fp, NULL, NULL, 0, screenheight, 0, 0);
+                       fmout(screenwidth, fp, NULL, NULL, 0,
+                               screenheight, 0, 0);
                        beg = ftell(fp);
                        fclose(fp);
                } else {
@@ -913,7 +950,7 @@ ME1:        switch (mode) {
        default:        /* allow 2+ modes */
                e_ex_code = 1;  /* start with a failed exit code */
                screen_reset();
-               sttybbs(SB_RESTORE);
+               stty_ctdl(SB_RESTORE);
                editor_pid = fork();
                cksum = file_checksum(filename);
                if (editor_pid == 0) {
@@ -931,7 +968,7 @@ ME1:        switch (mode) {
                                b = ka_wait(&e_ex_code);
                        } while ((b != editor_pid) && (b >= 0));
                editor_pid = (-1);
-               sttybbs(0);
+               stty_ctdl(0);
                screen_set();
                break;
        }
@@ -941,8 +978,9 @@ MECR:       if (mode >= 2) {
                        err_printf("*** Aborted message.\n");
                        e_ex_code = 1;
                }
-               if (e_ex_code == 0)
+               if (e_ex_code == 0) {
                        goto MEFIN;
+               }
                goto MEABT2;
        }
 
@@ -951,18 +989,16 @@ MECR:     if (mode >= 2) {
                    "add s<U>bject|"
                    "<R>eplace string|<H>old message");
 
-       if (b == 'a')
-               goto MEABT;
-       if (b == 'c')
-               goto ME1;
-       if (b == 's')
-               goto MEFIN;
+       if (b == 'a') goto MEABT;
+       if (b == 'c') goto ME1;
+       if (b == 's') goto MEFIN;
        if (b == 'p') {
                scr_printf(" %s from %s", datestr, fullname);
-               if (strlen(recipient) > 0)
+               if (!IsEmptyStr(recipient)) {
                        scr_printf(" to %s", recipient);
+               }
                scr_printf("\n");
-               if (subject != NULL) if (strlen(subject) > 0) {
+               if (subject != NULL) if (!IsEmptyStr(subject)) {
                        scr_printf("Subject: %s\n", subject);
                }
                fp = fopen(filename, "r");
@@ -1000,65 +1036,6 @@ MEABT2:  unlink(filename);
 }
 
 
-#if 0
-/*
- * Transmit message text to the server.
- * 
- * This loop also implements a "tick" counter that displays the progress, if
- * we're sending something that will take a long time to transmit.
- */
-void transmit_message(CtdlIPC *ipc, FILE *fp)
-{
-       char buf[SIZ];
-       int ch, a;
-       long msglen;
-       time_t lasttick;
-
-       fseek(fp, 0L, SEEK_END);
-       msglen = ftell(fp);
-       rewind(fp);
-       lasttick = time(NULL);
-       strcpy(buf, "");
-       while (ch = getc(fp), (ch >= 0)) {
-               if (ch == 10) {
-                       if (!strcmp(buf, "000"))
-                               strcpy(buf, ">000");
-                       CtdlIPC_putline(ipc, buf);
-                       strcpy(buf, "");
-               } else {
-                       a = strlen(buf);
-                       buf[a + 1] = 0;
-                       buf[a] = ch;
-                       if ((ch == 32) && (strlen(buf) > 200)) {
-                               buf[a] = 0;
-                               if (!strcmp(buf, "000"))
-                                       strcpy(buf, ">000");
-                               CtdlIPC_putline(ipc, buf);
-                               strcpy(buf, "");
-                       }
-                       if (strlen(buf) > 250) {
-                               if (!strcmp(buf, "000"))
-                                       strcpy(buf, ">000");
-                               CtdlIPC_putline(ipc, buf);
-                               strcpy(buf, "");
-                       }
-               }
-
-               if ((time(NULL) - lasttick) > 2L) {
-                       scr_printf(" %3ld%% completed\r",
-                              ((ftell(fp) * 100L) / msglen));
-                       scr_flush();
-                       lasttick = time(NULL);
-               }
-
-       }
-       CtdlIPC_putline(ipc, buf);
-       scr_printf("                \r");
-       scr_flush();
-}
-#endif
-
-
 /*
  * Make sure there's room in msg_arr[] for at least one more.
  */
@@ -1071,6 +1048,29 @@ void check_msg_arr_size(void) {
 }
 
 
+/*
+ * break_big_lines()  -  break up lines that are >1024 characters
+ *                       otherwise the server will truncate
+ */
+void break_big_lines(char *msg) {
+       char *ptr;
+       char *break_here;
+
+       if (msg == NULL) {
+               return;
+       }
+
+       ptr = msg;
+       while (strlen(ptr) > 1000) {
+               break_here = strchr(&ptr[900], ' ');
+               if ((break_here == NULL) || (break_here > &ptr[999])) {
+                       break_here = &ptr[999];
+               }
+               *break_here = '\n';
+               ptr = break_here++;
+       }
+}
+
 
 /*
  * entmsg()  -  edit and create a message
@@ -1078,8 +1078,9 @@ void check_msg_arr_size(void) {
  */
 int entmsg(CtdlIPC *ipc,
                int is_reply,   /* nonzero if this was a <R>eply command */
-               int c)          /* mode */
-{
+               int c,          /* mode */
+               int masquerade  /* prompt for a non-default display name? */
+) {
        char buf[SIZ];
        int a, b;
        int need_recp = 0;
@@ -1090,6 +1091,7 @@ int entmsg(CtdlIPC *ipc,
        struct ctdlipcmessage message;
        unsigned long *msgarr = NULL;
        int r;                  /* IPC response code */
+       int subject_required = 0;
 
        if (c > 0)
                mode = 1;
@@ -1105,10 +1107,16 @@ int entmsg(CtdlIPC *ipc,
        strcpy(message.recipient, "");
        strcpy(message.author, "");
        strcpy(message.subject, "");
-       message.text = message.author;  /* point to "", changes later */
+       strcpy(message.references, "");
+       message.text = "";              /* point to "", changes later */
        message.anonymous = 0;
        message.type = mode;
-       r = CtdlIPCPostMessage(ipc, 0, &message, buf);
+
+       if (masquerade) {
+               newprompt("Display name for this message: ", message.author, 40);
+       }
+
+       r = CtdlIPCPostMessage(ipc, 0, &subject_required, &message, buf);
 
        if (r / 100 != 2 && r / 10 != 57) {
                scr_printf("%s\n", buf);
@@ -1119,8 +1127,9 @@ int entmsg(CtdlIPC *ipc,
         * in this room, but a recipient needs to be specified.
         */
        need_recp = 0;
-       if (r / 10 == 57)
+       if (r / 10 == 57) {
                need_recp = 1;
+       }
 
        /* If the user is a dumbass, tell them how to type. */
        if ((userflags & US_EXPERT) == 0) {
@@ -1135,8 +1144,8 @@ int entmsg(CtdlIPC *ipc,
                                strcpy(buf, reply_to);
                        } else {
                                scr_printf("Enter recipient: ");
-                               getline(buf, (SIZ-100) );
-                               if (strlen(buf) == 0)
+                               ctdl_getline(buf, (SIZ-100) );
+                               if (IsEmptyStr(buf))
                                        return (1);
                        }
                } else
@@ -1145,7 +1154,8 @@ int entmsg(CtdlIPC *ipc,
        strcpy(message.recipient, buf);
 
        if (is_reply) {
-               if (strlen(reply_subject) > 0) {
+
+               if (!IsEmptyStr(reply_subject)) {
                        if (!strncasecmp(reply_subject,
                           "Re: ", 3)) {
                                strcpy(message.subject, reply_subject);
@@ -1157,6 +1167,21 @@ int entmsg(CtdlIPC *ipc,
                                        reply_subject);
                        }
                }
+
+               /* Trim down excessively long lists of thread references.  We eliminate the
+                * second one in the list so that the thread root remains intact.
+                */
+               int rrtok = num_tokens(reply_references, '|');
+               int rrlen = strlen(reply_references);
+               if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
+                       remove_token(reply_references, 1, '|');
+               }
+
+               snprintf(message.references, sizeof message.references, "%s%s%s",
+                       reply_references,
+                       (IsEmptyStr(reply_references) ? "" : "|"),
+                       reply_inreplyto
+               );
        }
 
        if (room_flags & QR_ANONOPT) {
@@ -1166,8 +1191,8 @@ int entmsg(CtdlIPC *ipc,
        }
 
        /* If it's mail, we've got to check the validity of the recipient... */
-       if (strlen(message.recipient) > 0) {
-               r = CtdlIPCPostMessage(ipc, 0, &message, buf);
+       if (!IsEmptyStr(message.recipient)) {
+               r = CtdlIPCPostMessage(ipc, 0, &subject_required,  &message, buf);
                if (r / 100 != 2) {
                        scr_printf("%s\n", buf);
                        return (1);
@@ -1187,7 +1212,9 @@ int entmsg(CtdlIPC *ipc,
        }
 
        /* Now compose the message... */
-       if (client_make_message(ipc, temp, message.recipient, message.anonymous, 0, c, message.subject) != 0) {
+       if (client_make_message(ipc, temp, message.recipient,
+          message.anonymous, 0, c, message.subject, subject_required) != 0) {
+           if (msgarr) free(msgarr);   
                return (2);
        }
 
@@ -1204,8 +1231,13 @@ int entmsg(CtdlIPC *ipc,
 
        if (fp) fclose(fp);
 
+       /* Break lines that are >1024 characters, otherwise the server
+        * will truncate them.
+        */
+       break_big_lines(message.text);
+
        /* Transmit message to the server */
-       r = CtdlIPCPostMessage(ipc, 1, &message, buf);
+       r = CtdlIPCPostMessage(ipc, 1, NULL, &message, buf);
        if (r / 100 != 4) {
                scr_printf("%s\n", buf);
                return (1);
@@ -1254,6 +1286,7 @@ int entmsg(CtdlIPC *ipc,
                scr_printf("*** %d additional messages have been entered "
                        "in this room by other users.\n", b);
        }
+    free(message.text);
 
        return(0);
 }
@@ -1275,19 +1308,23 @@ void process_quote(void)
 
        /* Display the quotable text with line numbers added */
        line = 0;
-       fgets(buf, 128, qfile);
+       if (fgets(buf, 128, qfile) == NULL) {
+               /* we're skipping a line here */
+       }
        while (fgets(buf, 128, qfile) != NULL) {
-               scr_printf("%2d %s", ++line, buf);
+               scr_printf("%3d %s", ++line, buf);
        }
-       scr_printf("Begin quoting at [ 1] : ");
-       getline(buf, 3);
+       scr_printf("Begin quoting at [1] : ");
+       ctdl_getline(buf, 4);
        qstart = (buf[0] == 0) ? (1) : atoi(buf);
        scr_printf("  End quoting at [%d] : ", line);
-       getline(buf, 3);
+       ctdl_getline(buf, 4);
        qend = (buf[0] == 0) ? (line) : atoi(buf);
        rewind(qfile);
        line = 0;
-       fgets(buf, 128, qfile);
+       if (fgets(buf, 128, qfile) == NULL) {
+               /* we're skipping a line here */
+       }
        tfile = fopen(temp, "w");
        while (fgets(buf, 128, qfile) != NULL) {
                if ((++line >= qstart) && (line <= qend))
@@ -1308,6 +1345,7 @@ void list_urls(CtdlIPC *ipc)
 {
        int i;
        char cmd[SIZ];
+       int rv;
 
        if (num_urls == 0) {
                scr_printf("There were no URL's in the previous message.\n\n");
@@ -1322,7 +1360,7 @@ void list_urls(CtdlIPC *ipc)
                i = intprompt("Display which one", 1, 1, num_urls);
 
        snprintf(cmd, sizeof cmd, rc_url_cmd, urls[i - 1]);
-       system(cmd);
+       rv = system(cmd);
        scr_printf("\n");
 }
 
@@ -1424,7 +1462,7 @@ void image_view(CtdlIPC *ipc, unsigned long msg)
                                void *file = NULL; /* The downloaded file */
                                int r;
        
-                               // view image
+                               /* view image */
                                found = -found;
                                r = CtdlIPCAttachmentDownload(ipc, msg, ptr->number, &file, progress, buf);
                                if (r / 100 != 2) {
@@ -1435,9 +1473,8 @@ void image_view(CtdlIPC *ipc, unsigned long msg)
                                        len = (size_t)extract_long(buf, 0);
                                        progress(ipc, len, len);
                                        scr_flush();
-                                       snprintf(tmp, sizeof tmp, "%s.%s",
-                                               tmpnam(NULL),
-                                               ptr->filename);
+                                       CtdlMakeTempFileName(tmp, sizeof tmp);
+                                       strcat(tmp, ptr->filename);
                                        save_buffer(file, len, tmp);
                                        free(file);
                                        do_image_view(tmp);
@@ -1472,15 +1509,17 @@ void readmsgs(CtdlIPC *ipc,
        char filename[PATH_MAX];
        char save_to[PATH_MAX];
        void *attachment = NULL;        /* Downloaded attachment */
-       FILE *dest = NULL;      /* Alternate destination other than screen */
+       FILE *dest = NULL;              /* Alternate destination other than screen */
        int r;                          /* IPC response code */
+       static int att_seq = 0;         /* Attachment download sequence number */
+       int rv = 0;                     /* silence the stupid warn_unused_result warnings */
 
        if (c < 0)
                b = (num_msgs - 1);
        else
                b = 0;
 
-       strcpy(prtfile, tmpnam(NULL));
+       CtdlMakeTempFileName(prtfile, sizeof prtfile);
 
        if (msg_arr) {
                free(msg_arr);
@@ -1549,6 +1588,8 @@ RMSGREAD: scr_flush();
                        quotflag = 0;
                        enable_color = hold_color;
                        process_quote();
+                       e = 'r';
+                       goto DONE_QUOTING;
                }
                if (arcflag) {
                        fclose(dest);
@@ -1557,11 +1598,13 @@ RMSGREAD:       scr_flush();
                        enable_color = hold_color;
                        f = fork();
                        if (f == 0) {
-                               freopen(prtfile, "r", stdin);
+                               if (freopen(prtfile, "r", stdin) == NULL) {
+                                       /* we probably should handle the error condition here */
+                               }
                                screen_reset();
-                               sttybbs(SB_RESTORE);
+                               stty_ctdl(SB_RESTORE);
                                ka_system(printcmd);
-                               sttybbs(SB_NO_INTR);
+                               stty_ctdl(SB_NO_INTR);
                                screen_set();
                                unlink(prtfile);
                                exit(0);
@@ -1577,10 +1620,10 @@ RMSGREAD:       scr_flush();
 
                        r = CtdlIPCSetMessageSeen(ipc, msg_arr[a], 1, buf);
                }
-               if (e == 3)
+               if (e == SIGQUIT)
                        return;
-               if (((userflags & US_NOPROMPT) || (e == 2))
-                   && (((room_flags & QR_MAILBOX) == 0)
+               if (((userflags & US_NOPROMPT) || (e == SIGINT))
+                       && (((room_flags & QR_MAILBOX) == 0)
                        || (rc_force_mail_prompts == 0))) {
                        e = 'n';
                } else {
@@ -1591,10 +1634,10 @@ RMSGREAD:       scr_flush();
                        color(DIM_WHITE);
                        scr_printf(") ");
 
-                       keyopt("<B>ack <A>gain <Q>uote <R>eply <N>ext <S>top ");
+                       keyopt("<B>ack <A>gain <R>eply reply<Q>uoted <N>ext <S>top ");
                        if (rc_url_cmd[0] && num_urls)
                                keyopt("<U>RLview ");
-                       if (has_images > 0 && strlen(imagecmd) > 0)
+                       if (has_images > 0 && !IsEmptyStr(imagecmd))
                                keyopt("<I>mages ");
                        keyopt("<?>help -> ");
 
@@ -1607,14 +1650,15 @@ RMSGREAD:       scr_flush();
 /* space key same as <N> */ if (e == 32)
                                        e = 'n';
 /* del/move for aides only */
-                                   if ((!is_room_aide)
-                                       && ((room_flags & QR_MAILBOX) ==
-                                           0)) {
+                                   if (  (!is_room_aide)
+                                      && ((room_flags & QR_MAILBOX) == 0)
+                                      && ((room_flags2 & QR2_COLLABDEL) == 0)
+                                      ) {
                                        if ((e == 'd') || (e == 'm'))
                                                e = 0;
                                }
 /* print only if available */
-                               if ((e == 'p') && (strlen(printcmd) == 0))
+                               if ((e == 'p') && (IsEmptyStr(printcmd)))
                                        e = 0;
 /* can't file if not allowed */
                                    if ((e == 'f')
@@ -1622,17 +1666,17 @@ RMSGREAD:       scr_flush();
                                        e = 0;
 /* link only if browser avail*/
                                    if ((e == 'u')
-                                       && (strlen(rc_url_cmd) == 0))
+                                       && (IsEmptyStr(rc_url_cmd)))
                                        e = 0;
                                if ((e == 'i')
-                                       && (!strlen(imagecmd) || !has_images))
+                                       && (IsEmptyStr(imagecmd) || !has_images))
                                        e = 0;
                        } while ((e != 'a') && (e != 'n') && (e != 's')
                                 && (e != 'd') && (e != 'm') && (e != 'p')
                                 && (e != 'q') && (e != 'b') && (e != 'h')
                                 && (e != 'r') && (e != 'f') && (e != '?')
                                 && (e != 'u') && (e != 'c') && (e != 'y')
-                                && (e != 'i'));
+                                && (e != 'i') && (e != 'o') );
                        switch (e) {
                        case 's':
                                scr_printf("Stop");
@@ -1656,7 +1700,7 @@ RMSGREAD: scr_flush();
                                scr_printf("Print");
                                break;
                        case 'q':
-                               scr_printf("Quote");
+                               scr_printf("reply Quoted");
                                break;
                        case 'b':
                                scr_printf("Back");
@@ -1667,6 +1711,9 @@ RMSGREAD: scr_flush();
                        case 'r':
                                scr_printf("Reply");
                                break;
+                       case 'o':
+                               scr_printf("Open attachments");
+                               break;
                        case 'f':
                                scr_printf("File");
                                break;
@@ -1688,7 +1735,7 @@ RMSGREAD: scr_flush();
                                scr_printf("\n");
                        scr_flush();
                }
-               switch (e) {
+DONE_QUOTING:  switch (e) {
                case '?':
                        scr_printf("Options available here:\n"
                                " ?  Help (prints this message)\n"
@@ -1697,25 +1744,28 @@ RMSGREAD:       scr_flush();
                                " N  Next (continue with next message)\n"
                                " Y  My Next (continue with next message you authored)\n"
                                " B  Back (go back to previous message)\n");
-                       if ((is_room_aide)
-                           || (room_flags & QR_MAILBOX)) {
+                       if (  (is_room_aide)
+                          || (room_flags & QR_MAILBOX)
+                          || (room_flags2 & QR2_COLLABDEL)
+                       ) {
                                scr_printf(" D  Delete this message\n"
                                        " M  Move message to another room\n");
                        }
                        scr_printf(" C  Copy message to another room\n");
-                       if (strlen(printcmd) > 0)
+                       if (!IsEmptyStr(printcmd))
                                scr_printf(" P  Print this message\n");
                        scr_printf(
-                               " Q  Quote portions of this message for your next post\n"
+                               " Q  Reply to this message, quoting portions of it\n"
                                " H  Headers (display message headers only)\n");
                        if (is_mail)
                                scr_printf(" R  Reply to this message\n");
-                       if (rc_allow_attachments)
-                               scr_printf
-                                   (" F  (save attachments to a file)\n");
-                       if (strlen(rc_url_cmd) > 0)
+                       if (rc_allow_attachments) {
+                               scr_printf(" O  (Open attachments)\n");
+                               scr_printf(" F  (save attachments to a File)\n");
+                       }
+                       if (!IsEmptyStr(rc_url_cmd))
                                scr_printf(" U  (list URL's for display)\n");
-                       if (strlen(imagecmd) > 0 && has_images > 0)
+                       if (!IsEmptyStr(imagecmd) && has_images > 0)
                                scr_printf(" I  Image viewer\n");
                        scr_printf("\n");
                        goto RMSGREAD;
@@ -1744,7 +1794,7 @@ RMSGREAD: scr_flush();
                case 'c':
                        newprompt("Enter target room: ",
                                  targ, ROOMNAMELEN - 1);
-                       if (strlen(targ) > 0) {
+                       if (!IsEmptyStr(targ)) {
                                r = CtdlIPCMoveMessage(ipc, (e == 'c' ? 1 : 0),
                                                       msg_arr[a], targ, cmd);
                                scr_printf("%s\n", cmd);
@@ -1756,25 +1806,36 @@ RMSGREAD:       scr_flush();
                        if (r / 100 != 2)       /* r will be init'ed, FIXME */
                                goto RMSGREAD;  /* the logic here sucks */
                        break;
+               case 'o':
                case 'f':
-                       newprompt("Which section? ", filename,
-                                 ((sizeof filename) - 1));
+                       newprompt("Which section? ", filename, ((sizeof filename) - 1));
                        r = CtdlIPCAttachmentDownload(ipc, msg_arr[a],
                                        filename, &attachment, progress, cmd);
                        if (r / 100 != 2) {
                                scr_printf("%s\n", cmd);
                        } else {
-                               extract(filename, cmd, 2);
+                               extract_token(filename, cmd, 2, '|', sizeof filename);
                                /*
                                 * Part 1 won't have a filename; use the
                                 * subject of the message instead. IO
                                 */
-                               if (!strlen(filename))
+                               if (IsEmptyStr(filename)) {
                                        strcpy(filename, reply_subject);
-                               destination_directory(save_to, filename);
-                               save_buffer(attachment,
-                                               extract_unsigned_long(cmd, 0),
-                                               save_to);
+                               }
+                               if (e == 'o') {         /* open attachment */
+                                       mkdir(tempdir, 0700);
+                                       snprintf(save_to, sizeof save_to, "%s/%04x.%s",
+                                               tempdir,
+                                               ++att_seq,
+                                               filename);
+                                       save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
+                                       snprintf(cmd, sizeof cmd, rc_open_cmd, save_to);
+                                       rv = system(cmd);
+                               }
+                               else {                  /* save attachment to disk */
+                                       destination_directory(save_to, filename);
+                                       save_buffer(attachment, extract_unsigned_long(cmd, 0), save_to);
+                               }
                        }
                        if (attachment) {
                                free(attachment);
@@ -1797,7 +1858,7 @@ RMSGREAD: scr_flush();
                        goto RMSGREAD;
                case 'r':
                        savedpos = num_msgs;
-                       entmsg(ipc, 1, (rc_reply_extedit ? 2 : 0));
+                       entmsg(ipc, 1, ((userflags & US_EXTEDIT) ? 2 : 0), 0);
                        num_msgs = savedpos;
                        goto RMSGREAD;
                case 'u':
@@ -1811,13 +1872,13 @@ RMSGREAD:       scr_flush();
             /* find the next message by me, stay here if we find nothing */
             int finda;
             int lasta = a;
-            for (finda = a; ((finda < num_msgs) && (finda >= 0)); finda += rdir)
+            for (finda = (a + rdir); ((finda < num_msgs) && (finda >= 0)); finda += rdir)
               {
                /* This is repetitively dumb, but that's what computers are for.
                   We have to load up messages until we find one by us */
                char buf[SIZ];
                int founda = 0;
-               struct ctdlipcmessage *msg;
+               struct ctdlipcmessage *msg = NULL;
                 
                /* read the header so we can get 'from=' */
                r = CtdlIPCGetSingleMessage(ipc, msg_arr[finda], 1, 0, &msg, buf);
@@ -1879,9 +1940,9 @@ void check_message_base(CtdlIPC *ipc)
                return;
        }
 
-       while (transcript && strlen(transcript)) {
+       while (transcript && !IsEmptyStr(transcript)) {
                lines_printed = 1;
-               extract_token(buf, transcript, 0, '\n');
+               extract_token(buf, transcript, 0, '\n', sizeof buf);
                remove_token(transcript, 0, '\n');
                pprintf("%s\n", buf);
        }