]> granicus.if.org Git - neomutt/commitdiff
Unsubscribe after deleting an imap folder (#730)
authorseishinryohosha <seishinryohosha@users.noreply.github.com>
Fri, 22 Sep 2017 14:51:19 +0000 (16:51 +0200)
committerPietro Cerutti <gahr@gahr.ch>
Fri, 22 Sep 2017 14:51:19 +0000 (15:51 +0100)
* Unsubscribe after deleting an imap folder

Only added two line to `browser.c` in `_mutt_select_file` case `OP_DELETE_MAILBOX`.

* Fixed typo: Unsubsribe -> Unsubscribe

* Revert previous change

* Added unsubscribe-from command

* Added `unsubscribe-from` command (init.h)
* Added `parse_unsubscribe_from` prototype (init.h)
* Implemented `parse_unsubscribe_from` uses imap/imap.h Header (init.c)

Tokenize buffer and expand each token (path) and finally unsubscribe from it.
No pattern matching is done.

* Unsubscribe after deleting an imap folder

Only added two line to `browser.c` in `_mutt_select_file` case `OP_DELETE_MAILBOX`.

* Fixed typo: Unsubsribe -> Unsubscribe

* Revert previous change

* Added unsubscribe-from command

* Added `unsubscribe-from` command (init.h)
* Added `parse_unsubscribe_from` prototype (init.h)
* Implemented `parse_unsubscribe_from` uses imap/imap.h Header (init.c)

Tokenize buffer and expand each token (path) and finally unsubscribe from it.
No pattern matching is done.

* Unsubscribe after deleting an imap folder

Only added two line to `browser.c` in `_mutt_select_file` case `OP_DELETE_MAILBOX`.

* Fixed typo: Unsubsribe -> Unsubscribe

* Revert previous change

* Unsubscribe after deleting an imap folder

Only added two line to `browser.c` in `_mutt_select_file` case `OP_DELETE_MAILBOX`.

* Fixed typo: Unsubsribe -> Unsubscribe

* Revert previous change

* Added unsubscribe-from command

* Added `unsubscribe-from` command (init.h)
* Added `parse_unsubscribe_from` prototype (init.h)
* Implemented `parse_unsubscribe_from` uses imap/imap.h Header (init.c)

Tokenize buffer and expand each token (path) and finally unsubscribe from it.
No pattern matching is done.

* Added subscribe-to command and updated example

    * Added `subscribe-to` command (init.h)
    * Added `parse_subscribe_to` prototype (init.h)
    * Implemented `parse_subscribe_to` uses imap/imap.h Header (init.c)
    * Example now shows folder and subfolders

    Tokenize buffer and expand each token (path) and finally subscribe from it.
    No pattern matching is done.

* Added return check
It should be noted, that imap_subscribe(FOO, 0) /* unsubcribe */
won't return -1, if mailbox doesn't exists.

* Added return -1, and do not silently ignore

* Added braces to satisfy styleguides

* Added better user output

* Typo Successfull -> Successful

* Reverted back to one parameter only and errors into err.

* Reverted back to one parameter only.
* Better error messages
* Error messages are now stored inside struct Buffer *err

* Changed from strfcpy -> mutt_buffer_*

* Copy&Paste Error correction

* Cleanup

* set expandtab
* set tabstop=2
* merged mutt_buffer_addstr -> mutt_buffer_printf
* corrected "Corrupted buffer" -> mutt_debug(5, ...)
* Compared with clang-format to get sure

* Reverted back bool -> int, to change this in a later PR

inside `parse_unsubscribe_from(...)` and `parse_subscribe_to(...)`
for the `imap_subscribe(...)`-call.

init.c
init.h

diff --git a/init.c b/init.c
index 2aff16170ca197db82450d3a97532bf9b050c637..bee9390beb9723bc06156d0188087f03181af3c2 100644 (file)
--- a/init.c
+++ b/init.c
@@ -70,6 +70,9 @@
 #ifdef USE_NOTMUCH
 #include "mutt_notmuch.h"
 #endif
+#ifdef USE_IMAP
+#include "imap/imap.h" /* for imap_subscribe() */
+#endif
 
 #define CHECK_PAGER                                                                  \
   if ((CurrentMenu == MENU_PAGER) && (idx >= 0) && (MuttVars[idx].flags & R_RESORT)) \
@@ -4563,6 +4566,118 @@ static int parse_tag_formats(struct Buffer *b, struct Buffer *s,
 }
 #endif
 
+#ifdef USE_IMAP
+/**
+ * parse_subscribe_to - 'subscribe-to' command: Add an IMAP subscription.
+ * @param b    Buffer space shared by all command handlers
+ * @param s    Current line of the config file
+ * @param data Data field from init.h:struct Command
+ * @param err  Buffer for any error message
+ * @retval  0 Success
+ * @retval -1 Failed
+ *
+ * The 'subscribe-to' command allows to subscribe to an IMAP-Mailbox.
+ * Patterns are not supported.
+ * Use it as follows: subscribe-to =folder
+ */
+static int parse_subscribe_to(struct Buffer *b, struct Buffer *s,
+                              unsigned long data, struct Buffer *err)
+{
+  if (!b || !s || !err)
+    return -1;
+
+  mutt_buffer_reset(err);
+
+  if (MoreArgs(s))
+  {
+    mutt_extract_token(b, s, 0);
+
+    if (MoreArgs(s))
+    {
+      mutt_buffer_addstr(err, _("Too many arguments"));
+      return -1;
+    }
+
+    if (b->data && *b->data)
+    {
+      /* Expand and subscribe */
+      if (imap_subscribe(mutt_expand_path(b->data, b->dsize), 1) != 0)
+      {
+        mutt_buffer_printf(err, _("Could not subscribe to %s"), b->data);
+        return -1;
+      }
+      else
+      {
+        mutt_message(_("Subscribed to %s"), b->data);
+        return 0;
+      }
+    }
+    else
+    {
+      mutt_debug(5, "Corrupted buffer");
+      return -1;
+    }
+  }
+
+  mutt_buffer_addstr(err, _("No folder specified"));
+  return -1;
+}
+
+/**
+ * parse_unsubscribe_from - 'unsubscribe-from' command: Cancel an IMAP subscription.
+ * @param b    Buffer space shared by all command handlers
+ * @param s    Current line of the config file
+ * @param data Data field from init.h:struct Command
+ * @param err  Buffer for any error message
+ * @retval  0 Success
+ * @retval -1 Failed
+ *
+ * The 'unsubscribe-from' command allows to unsubscribe from an IMAP-Mailbox.
+ * Patterns are not supported.
+ * Use it as follows: unsubscribe-from =folder
+ */
+static int parse_unsubscribe_from(struct Buffer *b, struct Buffer *s,
+                                  unsigned long data, struct Buffer *err)
+{
+  if (!b || !s || !err)
+    return -1;
+
+  if (MoreArgs(s))
+  {
+    mutt_extract_token(b, s, 0);
+
+    if (MoreArgs(s))
+    {
+      mutt_buffer_addstr(err, _("Too many arguments"));
+      return -1;
+    }
+
+    if (b->data && *b->data)
+    {
+      /* Expand and subscribe */
+      if (imap_subscribe(mutt_expand_path(b->data, b->dsize), 0) != 0)
+      {
+        mutt_buffer_printf(err, _("Could not unsubscribe from %s"), b->data);
+        return -1;
+      }
+      else
+      {
+        mutt_message(_("Unsubscribed from %s"), b->data);
+        return 0;
+      }
+    }
+    else
+    {
+      mutt_debug(5, "Corrupted buffer");
+      return -1;
+    }
+  }
+
+  mutt_buffer_addstr(err, _("No folder specified"));
+  return -1;
+}
+#endif
+
 const char *myvar_get(const char *var)
 {
   struct MyVar *cur = NULL;
diff --git a/init.h b/init.h
index 583a7b37af218227576ae73c798a7f6d0e25ca1b..1deb8d41169f6c4d52705455e43d0890711bde38 100644 (file)
--- a/init.h
+++ b/init.h
@@ -4590,6 +4590,13 @@ static int parse_tag_formats(struct Buffer *buf, struct Buffer *s,
                              unsigned long data, struct Buffer *err);
 #endif
 
+#ifdef USE_IMAP
+static int parse_subscribe_to(struct Buffer *buf, struct Buffer *s,
+                                unsigned long data, struct Buffer *err);
+static int parse_unsubscribe_from(struct Buffer *buf, struct Buffer *s,
+                                unsigned long data, struct Buffer *err);
+#endif
+
 const struct Command Commands[] = {
 #ifdef USE_SOCKET
   { "account-hook",        mutt_parse_hook,        MUTT_ACCOUNTHOOK },
@@ -4658,6 +4665,9 @@ const struct Command Commands[] = {
   { "startup-hook",        mutt_parse_hook,        MUTT_STARTUPHOOK | MUTT_GLOBALHOOK },
   { "subjectrx",           parse_subjectrx_list,   UL &SubjectRegexList },
   { "subscribe",           parse_subscribe,        0 },
+#ifdef USE_IMAP
+  { "subscribe-to",        parse_subscribe_to,     0 },
+#endif
 #ifdef USE_NOTMUCH
   { "tag-formats",         parse_tag_formats,      0 },
   { "tag-transforms",      parse_tag_transforms,   0 },
@@ -4690,6 +4700,9 @@ const struct Command Commands[] = {
 #endif
   { "unsubjectrx",         parse_unsubjectrx_list, UL &SubjectRegexList },
   { "unsubscribe",         parse_unsubscribe,      0 },
+#ifdef USE_IMAP
+  { "unsubscribe-from",    parse_unsubscribe_from, 0 },
+#endif
 #ifdef USE_NOTMUCH
   { "unvirtual-mailboxes", mutt_parse_unmailboxes, MUTT_VIRTUAL },
   { "virtual-mailboxes",   mutt_parse_mailboxes,   MUTT_VIRTUAL | MUTT_NAMED },