]> granicus.if.org Git - neomutt/commitdiff
Imap: add Account
authorRichard Russon <rich@flatcap.org>
Tue, 23 Oct 2018 22:06:27 +0000 (23:06 +0100)
committerRichard Russon <rich@flatcap.org>
Thu, 25 Oct 2018 20:38:43 +0000 (21:38 +0100)
imap/imap.c
imap/imap_private.h
imap/util.c

index 467581e480757163b691690ede5cef302a0f6fed..80781e9fe5d1f1498d665a7d8e810d7b62eb04fe 100644 (file)
@@ -6,6 +6,7 @@
  * Copyright (C) 1996-1998,2012 Michael R. Elkins <me@mutt.org>
  * Copyright (C) 1996-1999 Brandon Long <blong@fiction.net>
  * Copyright (C) 1999-2009,2012,2017 Brendan Cully <brendan@kublai.com>
+ * Copyright (C) 2018 Richard Russon <rich@flatcap.org>
  *
  * @copyright
  * This program is free software: you can redistribute it and/or modify it under
@@ -42,6 +43,7 @@
 #include "conn/conn.h"
 #include "mutt.h"
 #include "imap.h"
+#include "account.h"
 #include "auth.h"
 #include "bcache.h"
 #include "commands.h"
@@ -1142,7 +1144,7 @@ void imap_logout(struct ImapAccountData **adata)
   }
 
   mutt_socket_close((*adata)->conn);
-  imap_adata_free(adata);
+  imap_adata_free((void **) adata);
 }
 
 /**
@@ -2204,6 +2206,75 @@ out:
   return rc;
 }
 
+/**
+ * imap_ac_find - Find a Account that matches a Mailbox path
+ */
+struct Account *imap_ac_find(struct Account *a, const char *path)
+{
+  if (!a || (a->type != MUTT_IMAP) || !path)
+    return NULL;
+
+  struct Url url;
+  char tmp[PATH_MAX];
+  mutt_str_strfcpy(tmp, path, sizeof(tmp));
+  url_parse(&url, tmp);
+
+  struct ImapAccountData *adata = a->adata;
+  struct ConnAccount *ac = &adata->conn_account;
+
+  if (mutt_str_strcasecmp(url.host, ac->host) != 0)
+    return NULL;
+
+  if (mutt_str_strcasecmp(url.user, ac->user) != 0)
+    return NULL;
+
+  // if (mutt_str_strcmp(path, a->mailbox->realpath) == 0)
+  //   return a;
+
+  return a;
+}
+
+/**
+ * imap_ac_add - Add a Mailbox to a Account
+ */
+int imap_ac_add(struct Account *a, struct Mailbox *m)
+{
+  if (!a || !m)
+    return -1;
+
+  if (m->magic != MUTT_IMAP)
+    return -1;
+
+  // if (a->type == MUTT_UNKNOWN)
+  if (!a->adata)
+  {
+    struct ImapAccountData *adata = imap_adata_new();
+    a->type = 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;
+  }
+
+  m->account = a;
+
+  struct MailboxNode *np = mutt_mem_calloc(1, sizeof(*np));
+  np->m = m;
+  STAILQ_INSERT_TAIL(&a->mailboxes, np, entries);
+  return 0;
+}
+
 /**
  * imap_mbox_open - Implements MxOps::mbox_open()
  */
@@ -2835,6 +2906,8 @@ int imap_path_parent(char *buf, size_t buflen)
 struct MxOps mx_imap_ops = {
   .magic            = MUTT_IMAP,
   .name             = "imap",
+  .ac_find          = imap_ac_find,
+  .ac_add           = imap_ac_add,
   .mbox_open        = imap_mbox_open,
   .mbox_open_append = imap_mbox_open_append,
   .mbox_check       = imap_mbox_check,
index ffab848d8b34395c3d0d956253c2a5a8089cf0f9..ae9a16d42430b16cdc9fd7137e697105b13db905 100644 (file)
@@ -5,6 +5,7 @@
  * @authors
  * Copyright (C) 1996-1999 Brandon Long <blong@fiction.net>
  * Copyright (C) 1999-2009 Brendan Cully <brendan@kublai.com>
+ * Copyright (C) 2018 Richard Russon <rich@flatcap.org>
  *
  * @copyright
  * This program is free software: you can redistribute it and/or modify it under
 #include <stdio.h>
 #include <time.h>
 #include "mutt/mutt.h"
+#include "conn/conn.h"
 #ifdef USE_HCACHE
 #include "hcache/hcache.h"
 #endif
 
-struct ConnAccount;
 struct Context;
 struct Email;
 struct ImapEmailData;
@@ -210,6 +211,8 @@ enum ImapCommandType
 struct ImapAccountData
 {
   struct Connection *conn;
+  struct ConnAccount conn_account;
+  struct Account *account;
   bool recovering;
   unsigned char state;  ///< ImapState, e.g. #IMAP_AUTHENTICATED
   unsigned char status; ///< ImapFlags, e.g. #IMAP_FATAL
@@ -249,6 +252,7 @@ struct ImapAccountData
   /* The following data is all specific to the currently SELECTED mbox */
   char delim;
   struct Context *ctx;
+  struct Mailbox *mailbox;
   char *mbox_name;
   unsigned short check_status; /**< Flags, e.g. #IMAP_NEWMAIL_PENDING */
   unsigned char reopen;        /**< Flags, e.g. #IMAP_REOPEN_ALLOW */
@@ -343,7 +347,7 @@ char *imap_hcache_get_uid_seqset(struct ImapAccountData *adata);
 int imap_continue(const char *msg, const char *resp);
 void imap_error(const char *where, const char *msg);
 struct ImapAccountData *imap_adata_new(void);
-void imap_adata_free(struct ImapAccountData **adata);
+void imap_adata_free(void **ptr);
 char *imap_fix_path(struct ImapAccountData *adata, const char *mailbox, char *path, size_t plen);
 void imap_cachepath(struct ImapAccountData *adata, const char *mailbox, char *dest, size_t dlen);
 int imap_get_literal_count(const char *buf, unsigned int *bytes);
index 046e67987aef002c9fdc91c5fb113c55f281f7aa..607e8bb73e44b54738cab4f45854d7035f6a3a74 100644 (file)
@@ -730,21 +730,23 @@ struct ImapAccountData *imap_adata_new(void)
 
 /**
  * imap_adata_free - Release and clear storage in an ImapAccountData structure
- * @param adata Imap Account data
+ * @param ptr Imap Account data
  */
-void imap_adata_free(struct ImapAccountData **adata)
+void imap_adata_free(void **ptr)
 {
-  if (!adata)
+  if (!ptr || !*ptr)
     return;
 
-  FREE(&(*adata)->capstr);
-  mutt_list_free(&(*adata)->flags);
-  imap_mboxcache_free(*adata);
-  mutt_buffer_free(&(*adata)->cmdbuf);
-  FREE(&(*adata)->buf);
-  mutt_bcache_close(&(*adata)->bcache);
-  FREE(&(*adata)->cmds);
-  FREE(adata);
+  struct ImapAccountData *adata = *ptr;
+
+  FREE(&adata->capstr);
+  mutt_list_free(&adata->flags);
+  imap_mboxcache_free(adata);
+  mutt_buffer_free(&adata->cmdbuf);
+  FREE(&adata->buf);
+  mutt_bcache_close(&adata->bcache);
+  FREE(&adata->cmds);
+  FREE(ptr);
 }
 
 /**