]> granicus.if.org Git - neomutt/commitdiff
add $record support
authorKarel Zak <kzak@redhat.com>
Sun, 23 Sep 2012 07:31:54 +0000 (09:31 +0200)
committerRichard Russon <rich@flatcap.org>
Mon, 4 Apr 2016 15:30:06 +0000 (16:30 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
README.notmuch
compose.c
globals.h
init.h
mutt.h
mutt_notmuch.c
mutt_notmuch.h
protos.h
send.c
sendlib.c

index f2e30f552b039e366d21363e07324876e74ca674..701fbb912020e4d32628cc1c34ebc0b8a9dc7bd0 100644 (file)
@@ -140,6 +140,22 @@ notmuch support for mutt
       the sidebar. It's possible to toggle between virtual and normal folders by
       sidebar-toggle command.
 
+   nm_record = <boolean>
+
+      Add messages stored to the mutt record (see $record in the mutt docs)
+      also to notmuch DB. If you reply to an email then the new email inherits
+      tags from the original email.
+
+   nm_record_tags = <comma delimited list>
+
+      Tags that should be removed or added to the to the messages stored in the mutt record.
+
+      example:
+
+      set record = "~/sent-mails"
+      set nm_record = yes
+      set nm_record_tags = "-inbox,archive,me"
+
    nm_open_timeout = <seconds>
 
       This option specifies timeout for Notmuch database. Default is 5 seconds.
@@ -214,6 +230,10 @@ notmuch support for mutt
 
 * .muttrc example:
 
+       set record="~/Mail/Maildir/sent-mail"
+       set nm_record = yes
+       set nm_record_tags ="-inbox me archive"
+
        set nm_default_uri="notmuch:///home/kzak/Mail/Maildir"
        set virtual_spoolfile   = yes
 
index 37dc9a54f8aa658b797e41c73afa1db0fd147e4c..cfcef8d5068729d76a48aed25786cb5a56a65d05 100644 (file)
--- a/compose.c
+++ b/compose.c
@@ -1238,7 +1238,7 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
          if (msg->content->next)
            msg->content = mutt_make_multipart (msg->content);
 
-         if (mutt_write_fcc (fname, msg, NULL, 0, NULL) < 0)
+         if (mutt_write_fcc (fname, msg, NULL, 0, NULL, NULL) < 0)
            msg->content = mutt_remove_multipart (msg->content);
          else
            mutt_message _("Message written.");
index e0bf286e42125f06479c76ffae640aa5a99a5caa..0f6ca1eaa614d307915758dad0fb7c94bef7410e 100644 (file)
--- a/globals.h
+++ b/globals.h
@@ -289,6 +289,7 @@ WHERE char *NotmuchHiddenTags;
 WHERE char *VirtFolderFormat;
 WHERE int NotmuchDBLimit;
 WHERE char *NotmuchQueryType;
+WHERE char *NotmuchRecordTags;
 #endif
 
 
diff --git a/init.h b/init.h
index a2edaf3a696efb30e3a107251993d9ba88f97de4..ec70191658431cd46c5ffef69039b7da6b809705 100644 (file)
--- a/init.h
+++ b/init.h
@@ -1706,6 +1706,16 @@ struct option_t MuttVars[] = {
    ** .pp
    ** This variable specifies the default query type (threads or messages) used in notmuch queries.
    */
+  { "nm_record", DT_BOOL, R_NONE, OPTNOTMUCHRECORD, 0 },
+  /*
+   ** .pp
+   ** This variable specifies if the mutt record should indexed by notmuch.
+   */
+  { "nm_record_tags", DT_STR, R_NONE, UL &NotmuchRecordTags, 0 },
+  /*
+   ** .pp
+   ** This variable specifies the default tags applied to messages stored to the mutt record.
+   */
 #endif
   { "pager",           DT_PATH, R_NONE, UL &Pager, UL "builtin" },
   /*
diff --git a/mutt.h b/mutt.h
index 35c59a8aa08f329d49c3bcb6d1da2951c36d8be5..9916fe81d0488bd1c7d1d672672e1da5afd502c9 100644 (file)
--- a/mutt.h
+++ b/mutt.h
@@ -553,6 +553,7 @@ enum
 
 #ifdef USE_NOTMUCH
   OPTVIRTSPOOLFILE,
+  OPTNOTMUCHRECORD,
 #endif
 
   OPTMAX
index 6eb14c7b045431e48e7098851f00c3046a72975a..2c3e72bcc37b7063fa4741c469dd01d9dc067832 100644 (file)
@@ -1541,6 +1541,52 @@ done:
               new_flags ? M_FLAGS : 0;
 }
 
+int nm_record_message(CONTEXT *ctx, char *path, HEADER *h)
+{
+       notmuch_database_t *db;
+       notmuch_status_t st;
+       notmuch_message_t *msg;
+       int rc = -1;
+       struct nm_ctxdata *data = get_ctxdata(ctx);
+
+       if (!path || !data || access(path, F_OK) != 0)
+               return 0;
+       db = get_db(data, TRUE);
+       if (!db)
+               return -1;
+
+       dprint(1, (debugfile, "nm: record message: %s\n", path));
+       st = notmuch_database_begin_atomic(db);
+       if (st)
+               return -1;
+
+       st = notmuch_database_add_message(db, path, &msg);
+
+       if (st != NOTMUCH_STATUS_SUCCESS &&
+           st != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
+               dprint(1, (debugfile, "nm: failed to add '%s' [st=%d]\n", path, (int) st));
+               goto done;
+       }
+
+       if (st == NOTMUCH_STATUS_SUCCESS && msg) {
+               notmuch_message_maildir_flags_to_tags(msg);
+               if (h)
+                       update_tags(msg, nm_header_get_tags(h));
+               if (NotmuchRecordTags)
+                       update_tags(msg, NotmuchRecordTags);
+       }
+
+       rc = 0;
+done:
+       if (msg)
+               notmuch_message_destroy(msg);
+       notmuch_database_end_atomic(db);
+
+       if (!is_longrun(data))
+               release_db(data);
+       return rc;
+}
+
 /*
  * Fill a list with all notmuch tags.
  *
@@ -1556,7 +1602,7 @@ int nm_get_all_tags(CONTEXT *ctx, char **tag_list, int *tag_count)
        if (!data)
                return -1;
 
-       if (!(db = get_db(data, TRUE)) ||
+       if (!(db = get_db(data, FALSE)) ||
                        !(tags = notmuch_database_get_all_tags(db)))
                goto done;
 
index bc769eca0243677fcb3e3a4e9f51a3803d75fc4a..7ea77f3941531389f8b02b5dd46ddafe2fea4c72 100644 (file)
@@ -20,6 +20,8 @@ void nm_longrun_done(CONTEXT *cxt);
 
 char *nm_get_description(CONTEXT *ctx);
 
+int nm_record_message(CONTEXT *ctx, char *path, HEADER *h);
+
 void nm_debug_check(CONTEXT *ctx);
 int nm_get_all_tags(CONTEXT *ctx, char **tag_list, int *tag_count);
 
index c00de1be0bc97e0daa5b7bfe98bf6253fa7470e2..40b0fe32336bf479c94c9d80e143ad17c578f4ba 100644 (file)
--- a/protos.h
+++ b/protos.h
@@ -376,7 +376,7 @@ int mutt_user_is_recipient (HEADER *);
 void mutt_update_num_postponed (void);
 int mutt_wait_filter (pid_t);
 int mutt_which_case (const char *);
-int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int, char *);
+int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int, char *, char **);
 int mutt_write_mime_body (BODY *, FILE *);
 int mutt_write_mime_header (BODY *, FILE *);
 int mutt_write_one_header (FILE *fp, const char *tag, const char *value, const char *pfx, int wraplen, int flags);
diff --git a/send.c b/send.c
index d4ac0377c7ee666bfbecaca4199cf460311fc34e..82b10a1eca7d735edfbb8cb989f97279b2b9c590 100644 (file)
--- a/send.c
+++ b/send.c
@@ -48,6 +48,9 @@
 #include "remailer.h"
 #endif
 
+#ifdef USE_NOTMUCH
+#include "mutt_notmuch.h"
+#endif
 
 static void append_signature (FILE *f)
 {
@@ -1164,6 +1167,7 @@ ci_send_message (int flags,               /* send mode */
   char *smime_default_key = NULL;
   char *tag = NULL, *err = NULL;
   char *ctype;
+  char *finalpath = NULL;
 
   int rv = -1;
   
@@ -1646,7 +1650,9 @@ main_loop:
       mutt_prepare_envelope (msg->env, 0);
       mutt_env_to_intl (msg->env, NULL, NULL); /* Handle bad IDNAs the next time. */
 
-      if (!Postponed || mutt_write_fcc (NONULL (Postponed), msg, (cur && (flags & SENDREPLY)) ? cur->env->message_id : NULL, 1, fcc) < 0)
+      if (!Postponed || mutt_write_fcc (NONULL (Postponed), msg,
+                           (cur && (flags & SENDREPLY)) ?
+                                    cur->env->message_id : NULL, 1, fcc, NULL) < 0)
       {
        msg->content = mutt_remove_multipart (msg->content);
        decode_descriptions (msg->content);
@@ -1829,7 +1835,7 @@ full_fcc:
        * message was first postponed.
        */
       msg->received = time (NULL);
-      if (mutt_write_fcc (fcc, msg, NULL, 0, NULL) == -1)
+      if (mutt_write_fcc (fcc, msg, NULL, 0, NULL, &finalpath) == -1)
       {
        /*
         * Error writing FCC, we should abort sending.
@@ -1890,6 +1896,7 @@ full_fcc:
       msg->content = mutt_remove_multipart (msg->content);
       decode_descriptions (msg->content);
       mutt_unprepare_envelope (msg->env);
+      FREE(&finalpath);
       goto main_loop;
     }
     else
@@ -1898,8 +1905,13 @@ full_fcc:
       goto cleanup;
     }
   }
-  else if (!option (OPTNOCURSES) && ! (flags & SENDMAILX))
+  else if (!option (OPTNOCURSES) && ! (flags & SENDMAILX)) {
     mutt_message (i == 0 ? _("Mail sent.") : _("Sending in background."));
+#ifdef USE_NOTMUCH
+    if (option(OPTNOTMUCHRECORD))
+      nm_record_message(ctx, finalpath, cur);
+#endif
+  }
 
   if (WithCrypto && (msg->security & ENCRYPT))
     FREE (&pgpkeylist);
@@ -1944,7 +1956,8 @@ cleanup:
   safe_fclose (&tempfp);
   if (! (flags & SENDNOFREEHEADER))
     mutt_free_header (&msg);
-  
+
+  FREE(&finalpath);
   return rv;
 }
 
index 23acb81361ddebbf7313af0fc173b1891d7de121..54d42962eac90912262f462d3dca3506614703b3 100644 (file)
--- a/sendlib.c
+++ b/sendlib.c
@@ -2684,7 +2684,8 @@ static void set_noconv_flags (BODY *b, short flag)
   }
 }
 
-int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post, char *fcc)
+int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid,
+                   int post, char *fcc, char **finalpath)
 {
   CONTEXT f;
   MESSAGE *msg;
@@ -2873,6 +2874,8 @@ int mutt_write_fcc (const char *path, HEADER *hdr, const char *msgid, int post,
 
   if (mx_commit_message (msg, &f) != 0)
     r = -1;
+  else if (finalpath)
+    *finalpath = safe_strdup(msg->commited_path);
   mx_close_message (&msg);
   mx_close_mailbox (&f, NULL);