From: Richard Russon Date: Tue, 23 Oct 2018 22:06:27 +0000 (+0100) Subject: Imap: add Account X-Git-Tag: 2019-10-25~593^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=17c233d791f69d80c12d39f63068e5a434f74886;p=neomutt Imap: add Account --- diff --git a/imap/imap.c b/imap/imap.c index 467581e48..80781e9fe 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -6,6 +6,7 @@ * Copyright (C) 1996-1998,2012 Michael R. Elkins * Copyright (C) 1996-1999 Brandon Long * Copyright (C) 1999-2009,2012,2017 Brendan Cully + * Copyright (C) 2018 Richard Russon * * @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, diff --git a/imap/imap_private.h b/imap/imap_private.h index ffab848d8..ae9a16d42 100644 --- a/imap/imap_private.h +++ b/imap/imap_private.h @@ -5,6 +5,7 @@ * @authors * Copyright (C) 1996-1999 Brandon Long * Copyright (C) 1999-2009 Brendan Cully + * Copyright (C) 2018 Richard Russon * * @copyright * This program is free software: you can redistribute it and/or modify it under @@ -28,11 +29,11 @@ #include #include #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); diff --git a/imap/util.c b/imap/util.c index 046e67987..607e8bb73 100644 --- a/imap/util.c +++ b/imap/util.c @@ -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); } /**