]> granicus.if.org Git - neomutt/commitdiff
imap: simplify getter around ImapAccountData
authorMehdi Abaakouk <sileht@sileht.net>
Mon, 12 Nov 2018 20:47:03 +0000 (21:47 +0100)
committerRichard Russon <rich@flatcap.org>
Tue, 13 Nov 2018 13:04:41 +0000 (13:04 +0000)
Remove now useless attributes of imap_prepare_mailbox()
Rename imap_ac_data_find() to imap_adata_find() and move it in util.c
aside other adata related function.
Parse path within ImapMbox in imap_adata_find() to remove some common
code.
Create ImapAccountData resource during imap_ac_add()

imap/browse.c
imap/imap.c
imap/imap_private.h
imap/util.c

index bf0ab366be1dca6febe7f60601dce42b41dcf15e..c9463c9fa66a1777c87346a269a2b9c93a73a812 100644 (file)
@@ -225,13 +225,9 @@ int imap_browse(char *path, struct BrowserState *state)
     // Pick first mailbox connected on the same server
     if (imap_account_match(&mx.account, &mx_tmp.account))
     {
-      /* ensure we are connected */
-      int rc = imap_prepare_mailbox(np->m, &mx, path, buf, sizeof(buf), false, true);
-      if (rc < 0)
-        continue;
-
-      adata = np->m->account->adata;
-      break;
+      adata = imap_adata_get(np->m);
+      if (adata)
+        break;
     }
   }
   FREE(&mx_tmp.mbox);
@@ -389,13 +385,7 @@ int imap_mailbox_create(const char *folder)
   char buf[PATH_MAX];
   short n;
 
-  if (imap_parse_path(folder, &mx) < 0)
-  {
-    mutt_debug(1, "Bad starting path %s\n", folder);
-    return -1;
-  }
-
-  adata = imap_ac_data_find(&mx);
+  adata = imap_adata_find(folder, &mx);
   if (!adata)
   {
     mutt_debug(1, "Couldn't find open connection to %s\n", folder);
@@ -450,13 +440,7 @@ int imap_mailbox_rename(const char *mailbox)
   char buf[PATH_MAX];
   char newname[PATH_MAX];
 
-  if (imap_parse_path(mailbox, &mx) < 0)
-  {
-    mutt_debug(1, "Bad source mailbox %s\n", mailbox);
-    return -1;
-  }
-
-  adata = imap_ac_data_find(&mx);
+  adata = imap_adata_find(mailbox, &mx);
   if (!adata)
   {
     mutt_debug(1, "Couldn't find open connection to %s\n", mailbox);
index 10eb4aa81903269a8fff041ac2326c3ef7e3847a..cfd64fd89b7197b7419e7eb6dc2fc418140e3152 100644 (file)
@@ -326,59 +326,33 @@ static int sync_helper(struct ImapAccountData *adata, int right, int flag, const
 }
 
 /**
- * imap_prepare_mailbox - this method ensure we have a valid Mailbox
+ * imap_prepare_mailbox - Ensure we have a valid Mailbox
  * @param m                     Mailbox
  * @param mx                    Imap Mailbox
- * @param path                  Mailbox path
  * @param mailbox               Buffer for tidied mailbox path
  * @param mailboxlen            Length of buffer
- * @param run_hook              should we run account hook
- * @param create_new_connection should we create a new connection
  * @retval  0 Success
  * @retval -1 Failure
  *
  * This method ensure we have a valid Mailbox object with the ImapAccountData
  * structure setuped and ready to use.
  */
-int imap_prepare_mailbox(struct Mailbox *m, struct ImapMbox *mx,
-                         const char *path, char *mailbox, size_t mailboxlen,
-                         bool run_hook, bool create_new_connection)
+int imap_prepare_mailbox(struct Mailbox *m, struct ImapMbox *mx, char *mailbox, size_t mailboxlen)
 {
   if (!m || !m->account)
     return -1;
 
-  if (!path)
-    path = m->path;
-
-  if ((imap_path_probe(path, NULL) != MUTT_IMAP) || imap_parse_path(path, mx))
+  if ((imap_path_probe(m->path, NULL) != MUTT_IMAP) || imap_parse_path(m->path, mx))
   {
-    mutt_debug(1, "Error parsing %s\n", path);
+    mutt_debug(1, "Error parsing %s\n", m->path);
     mutt_error(_("Bad mailbox name"));
     return -1;
   }
 
   struct ImapAccountData *adata = m->account->adata;
-  if (!adata)
-  {
-    if (!create_new_connection)
-      return -1;
-    adata = imap_adata_new();
-    m->account->adata = adata;
-    m->account->free_adata = imap_adata_free;
-  }
-
-  struct Connection *conn = adata->conn;
-  if (!conn)
-  {
-    adata->conn = mutt_conn_new(&mx->account);
-    if (!adata->conn)
-      return -1;
-  }
 
-  if (run_hook)
-    mutt_account_hook(m->realpath);
+  mutt_account_hook(m->realpath);
 
-  /* we require a connection which isn't currently in IMAP_SELECTED state */
   if (imap_login(adata) < 0)
     return -1;
 
@@ -406,10 +380,7 @@ int get_mailbox(const char *path, struct ImapAccountData **adata, char *buf, siz
 {
   struct ImapMbox mx;
 
-  if (imap_parse_path(path, &mx) < 0)
-    return -1;
-
-  *adata = imap_ac_data_find(&mx);
+  *adata = imap_adata_find(path, &mx);
   if (!*adata)
   {
     FREE(&mx.mbox);
@@ -785,16 +756,13 @@ int imap_delete_mailbox(struct Mailbox *m, char *path)
 {
   struct ImapMbox mx;
   char buf[PATH_MAX], mbox[PATH_MAX];
-  int rc;
 
-  rc = imap_prepare_mailbox(m, &mx, path, buf, sizeof(buf), true, !ImapPassive);
-  if (rc < 0)
-  {
-    FREE(&mx.mbox);
+  if (imap_parse_path(path, &mx) < 0)
     return -1;
-  }
 
-  imap_munge_mbox_name(m->account->adata, mbox, sizeof(mbox), mx.mbox);
+  struct ImapAccountData *adata = imap_adata_get(m);
+
+  imap_munge_mbox_name(adata, mbox, sizeof(mbox), mx.mbox);
   snprintf(buf, sizeof(buf), "DELETE %s", mbox);
   FREE(&mx.mbox);
 
@@ -1436,9 +1404,6 @@ int imap_mailbox_check(bool check_stats)
     if (np->m->magic != MUTT_IMAP)
       continue;
 
-    // TODO(sileht): get_mailbox also browse AllMailboxes we could do an
-    // optimization here.
-    mutt_account_hook(np->m->realpath);
     if (get_mailbox(np->m->path, &adata, name, sizeof(name)) < 0)
     {
       np->m->has_new = false;
@@ -1742,22 +1707,6 @@ int imap_subscribe(char *path, bool subscribe)
   return 0;
 }
 
-struct ImapAccountData *imap_ac_data_find(struct ImapMbox *mx)
-{
-  struct Account *np = NULL;
-  struct ImapAccountData *adata = NULL;
-  TAILQ_FOREACH(np, &AllAccounts, entries)
-  {
-    if (np->magic != MUTT_IMAP)
-      continue;
-
-    adata = np->adata;
-    if (imap_account_match(&adata->conn_account, &mx->account))
-      return adata;
-  }
-  mutt_debug(3, "no ImapAccountData found\n");
-  return NULL;
-}
 
 /**
  * imap_complete - Try to complete an IMAP folder path
@@ -1783,13 +1732,7 @@ int imap_complete(char *buf, size_t buflen, char *path)
   struct ImapMbox mx;
   int rc;
 
-  if (imap_parse_path(path, &mx))
-  {
-    mutt_str_strfcpy(buf, path, buflen);
-    return complete_hosts(buf, buflen);
-  }
-
-  adata = imap_ac_data_find(&mx);
+  adata = imap_adata_find(path, &mx);
   if (!adata)
   {
     FREE(&mx.mbox);
@@ -2221,28 +2164,18 @@ int imap_ac_add(struct Account *a, struct Mailbox *m)
   if (!a->adata)
   {
     struct ImapAccountData *adata = imap_adata_new();
+    struct ImapMbox mx;
     a->magic = MUTT_IMAP;
     a->adata = adata;
     a->free_adata = imap_adata_free;
 
-    struct Url url;
-    char tmp[PATH_MAX];
-    mutt_str_strfcpy(tmp, m->path, sizeof(tmp));
-    url_parse(&url, tmp);
-
-    mutt_str_strfcpy(adata->conn_account.user, url.user,
-                     sizeof(adata->conn_account.user));
-    mutt_str_strfcpy(adata->conn_account.pass, url.pass,
-                     sizeof(adata->conn_account.pass));
-    mutt_str_strfcpy(adata->conn_account.host, url.host,
-                     sizeof(adata->conn_account.host));
-    adata->conn_account.port = url.port;
-    adata->conn_account.type = MUTT_ACCT_TYPE_IMAP;
-
-    if (adata->conn_account.user[0] != '\0')
-      adata->conn_account.flags |= MUTT_ACCT_USER;
-    if (adata->conn_account.pass[0] != '\0')
-      adata->conn_account.flags |= MUTT_ACCT_PASS;
+    if (imap_parse_path(m->path, &mx) < 0)
+      return -1;
+    FREE(&mx.mbox);
+    adata->conn_account = mx.account;
+    adata->conn = mutt_conn_new(&adata->conn_account);
+    if (!adata->conn)
+      return -1;
   }
 
   m->account = a;
@@ -2333,7 +2266,7 @@ static int imap_mbox_open(struct Context *ctx)
   int rc;
   const char *condstore = NULL;
 
-  rc = imap_prepare_mailbox(m, &mx, NULL, buf, sizeof(buf), true, true);
+  rc = imap_prepare_mailbox(m, &mx, buf, sizeof(buf));
   if (rc < 0)
   {
     FREE(&mx.mbox);
@@ -2581,7 +2514,7 @@ static int imap_mbox_open_append(struct Context *ctx, int flags)
 
   /* in APPEND mode, we appear to hijack an existing IMAP connection -
    * ctx is brand new and mostly empty */
-  rc = imap_prepare_mailbox(m, &mx, NULL, mailbox, sizeof(mailbox), false, !ImapPassive);
+  rc = imap_prepare_mailbox(m, &mx, mailbox, sizeof(mailbox));
   FREE(&mx.mbox);
   if (rc < 0)
     return -1;
index 465f164697ecc259ce925b00111bbdad8da52ce4..7c1e9fa8780e75095794b740f2af461021a39c43 100644 (file)
@@ -306,8 +306,7 @@ int imap_login(struct ImapAccountData *adata);
 void imap_logout(struct ImapAccountData **adata);
 int imap_sync_message_for_copy(struct ImapAccountData *adata, struct Email *e, struct Buffer *cmd, int *err_continue);
 bool imap_has_flag(struct ListHead *flag_list, const char *flag);
-int imap_prepare_mailbox(struct Mailbox *m, struct ImapMbox *mx, const char *path, char *mailbox, size_t mailboxlen, bool run_hook, bool create_new_connection);
-struct ImapAccountData *imap_ac_data_find(struct ImapMbox *mx);
+struct ImapAccountData *imap_adata_find(const char *path, struct ImapMbox *mx);
 
 /* auth.c */
 int imap_authenticate(struct ImapAccountData *adata);
index 32acb550b0510ea73697a0a2a49a7775e60eaed3..c64c39023054d92b1deed808d4cda13de6dfd6e1 100644 (file)
@@ -126,6 +126,31 @@ struct ImapAccountData *imap_adata_get(struct Mailbox *m)
   return a->adata;
 }
 
+/**
+ * imap_adata_find - Find the Account data for this path
+ */
+struct ImapAccountData *imap_adata_find(const char *path, struct ImapMbox *mx)
+{
+
+  if (imap_parse_path(path, mx) < 0)
+    return NULL;
+
+  struct Account *np = NULL;
+  struct ImapAccountData *adata = NULL;
+  TAILQ_FOREACH(np, &AllAccounts, entries)
+  {
+    if (np->magic != MUTT_IMAP)
+      continue;
+
+    adata = np->adata;
+    if (imap_account_match(&adata->conn_account, &mx->account))
+      return adata;
+  }
+  mutt_debug(3, "no ImapAccountData found\n");
+  return NULL;
+}
+
+
 /**
  * imap_get_parent - Get an IMAP folder's parent
  * @param mbox   Mailbox whose parent is to be determined
@@ -185,13 +210,7 @@ void imap_get_parent_path(const char *path, char *buf, size_t buflen)
   struct ImapAccountData *adata = NULL;
   char mbox[LONG_STRING] = "";
 
-  if (imap_parse_path(path, &mx) < 0)
-  {
-    mutt_str_strfcpy(buf, path, buflen);
-    return;
-  }
-
-  adata = imap_ac_data_find(&mx);
+  adata = imap_adata_find(path, &mx);
   if (!adata)
   {
     mutt_str_strfcpy(buf, path, buflen);
@@ -223,10 +242,7 @@ void imap_clean_path(char *path, size_t plen)
   struct ImapAccountData *adata = NULL;
   char mbox[LONG_STRING] = "";
 
-  if (imap_parse_path(path, &mx) < 0)
-    return;
-
-  adata = imap_ac_data_find(&mx);
+  adata = imap_adata_find(path, &mx);
   if (!adata)
     return;