Keep track of thread references, in the server and in the text client. This required...
[citadel.git] / citadel / messages.c
index 79e03b1b4d32e931a62b89e52e988ee24fe0f787..bc5a6e5cfd4bd0c9ef44baced9fa9e74a8b21c62 100644 (file)
 #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
@@ -53,6 +52,7 @@
 
 char reply_to[SIZ];
 char reply_subject[SIZ];
+char reply_references[SIZ];
 
 struct cittext {
        struct cittext *next;
@@ -70,6 +70,7 @@ int msg_arr_size = 0;
 int num_msgs;
 char rc_alt_semantics;
 extern char room_name[];
+extern char tempdir[];
 extern unsigned room_flags;
 extern unsigned room_flags2;
 extern long highest_msg_read;
@@ -404,16 +405,17 @@ int read_message(CtdlIPC *ipc,
 
        strcpy(reply_to, NO_REPLY_TO);
        strcpy(reply_subject, "");
+       strcpy(reply_references, "");
 
-       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);
+               lines_printed = checkpagin(lines_printed, pagin, screenheight);
                stty_ctdl(0);
+               free(message->text);
+               free_parts(message->attachments);
+               free(message);
                return (0);
        }
 
@@ -461,6 +463,9 @@ int read_message(CtdlIPC *ipc,
                }
                pprintf("\n");
                stty_ctdl(0);
+               free(message->text);
+               free_parts(message->attachments);
+               free(message);
                return (0);
        }
 
@@ -585,9 +590,20 @@ int read_message(CtdlIPC *ipc,
                lines_printed = checkpagin(lines_printed, pagin, screenheight);
        }
 
+
+       /* Always do msgid before references ... the latter is a concatenation! */
+       if (message->msgid != NULL) {
+               safestrncpy(reply_references, message->msgid, sizeof reply_references);
+       }
+
+       if (message->references != NULL) if (!IsEmptyStr(message->references)) {
+               int l = strlen(reply_references);
+               strcpy(&reply_references[l++], "|");
+               safestrncpy(&reply_references[l], message->references, (sizeof(reply_references) - l));
+       }
+
        if (message->subject != NULL) {
-               safestrncpy(reply_subject, message->subject,
-                                               sizeof reply_subject);
+               safestrncpy(reply_subject, message->subject, sizeof reply_subject);
                if (!IsEmptyStr(message->subject)) {
                        if (dest) {
                                fprintf(dest, "Subject: %s\n",
@@ -669,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) {
@@ -697,11 +713,12 @@ 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) {
@@ -1015,65 +1032,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.
  */
@@ -1145,6 +1103,7 @@ int entmsg(CtdlIPC *ipc,
        strcpy(message.recipient, "");
        strcpy(message.author, "");
        strcpy(message.subject, "");
+       strcpy(message.references, "");
        message.text = "";              /* point to "", changes later */
        message.anonymous = 0;
        message.type = mode;
@@ -1191,6 +1150,7 @@ int entmsg(CtdlIPC *ipc,
        strcpy(message.recipient, buf);
 
        if (is_reply) {
+
                if (!IsEmptyStr(reply_subject)) {
                        if (!strncasecmp(reply_subject,
                           "Re: ", 3)) {
@@ -1203,6 +1163,8 @@ int entmsg(CtdlIPC *ipc,
                                        reply_subject);
                        }
                }
+
+               safestrncpy(message.references, reply_references, sizeof message.references);
        }
 
        if (room_flags & QR_ANONOPT) {
@@ -1331,13 +1293,13 @@ void process_quote(void)
        line = 0;
        fgets(buf, 128, qfile);
        while (fgets(buf, 128, qfile) != NULL) {
-               scr_printf("%2d %s", ++line, buf);
+               scr_printf("%3d %s", ++line, buf);
        }
-       scr_printf("Begin quoting at [ 1] : ");
-       ctdl_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);
-       ctdl_getline(buf, 3);
+       ctdl_getline(buf, 4);
        qend = (buf[0] == 0) ? (line) : atoi(buf);
        rewind(qfile);
        line = 0;
@@ -1525,8 +1487,9 @@ 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 */
 
        if (c < 0)
                b = (num_msgs - 1);
@@ -1630,10 +1593,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 {
@@ -1686,7 +1649,7 @@ RMSGREAD: scr_flush();
                                 && (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");
@@ -1721,6 +1684,9 @@ RMSGREAD: scr_flush();
                        case 'r':
                                scr_printf("Reply");
                                break;
+                       case 'o':
+                               scr_printf("Open attachments");
+                               break;
                        case 'f':
                                scr_printf("File");
                                break;
@@ -1766,9 +1732,10 @@ RMSGREAD:        scr_flush();
                                " 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 (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 (!IsEmptyStr(imagecmd) && has_images > 0)
@@ -1812,9 +1779,9 @@ 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) {
@@ -1825,12 +1792,23 @@ RMSGREAD:       scr_flush();
                                 * Part 1 won't have a filename; use the
                                 * subject of the message instead. IO
                                 */
-                               if (IsEmptyStr(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);
+                                       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);