From d915b258fa0b8996331499e60068f04596a843e4 Mon Sep 17 00:00:00 2001 From: Thomas Roessler Date: Fri, 25 Aug 2000 06:28:22 +0000 Subject: [PATCH] patch-bac.imapurl-2, with small modifications. --- account.c | 31 +++++++++++++- account.h | 3 ++ browser.c | 16 ++++++- imap/imap.c | 12 +++--- imap/imap_ssl.c | 7 ---- imap/imap_ssl.h | 1 - imap/util.c | 109 ++++++++++++++++++++++++++++++------------------ mutt_socket.c | 12 +++++- mx.c | 16 ++++++- url.c | 30 ++++++------- url.h | 14 +------ 11 files changed, 164 insertions(+), 87 deletions(-) diff --git a/account.c b/account.c index 2853d254..22a60a73 100644 --- a/account.c +++ b/account.c @@ -18,8 +18,9 @@ /* remote host account manipulation (POP/IMAP) */ -#include "account.h" #include "mutt.h" +#include "account.h" +#include "url.h" /* mutt_account_match: compare account info (host/port/user) */ int mutt_account_match (const ACCOUNT* a1, const ACCOUNT* a2) @@ -53,6 +54,34 @@ int mutt_account_match (const ACCOUNT* a1, const ACCOUNT* a2) return 1; } +/* mutt_account_fromurl: fill account with information from url. */ +int mutt_account_fromurl (ACCOUNT* account, ciss_url_t* url) +{ + /* must be present */ + if (url->host) + strfcpy (account->host, url->host, sizeof (account->host)); + else + return -1; + + if (url->user) + { + strfcpy (account->user, url->user, sizeof (account->user)); + account->flags |= M_ACCT_USER; + } + if (url->pass) + { + strfcpy (account->pass, url->pass, sizeof (account->pass)); + account->flags |= M_ACCT_PASS; + } + if (url->port) + { + account->port = url->port; + account->flags |= M_ACCT_PORT; + } + + return 0; +} + /* mutt_account_getuser: retrieve username into ACCOUNT, if neccessary */ int mutt_account_getuser (ACCOUNT* account) { diff --git a/account.h b/account.h index 9e21c269..bae65297 100644 --- a/account.h +++ b/account.h @@ -21,6 +21,8 @@ #ifndef _MUTT_ACCOUNT_H_ #define _MUTT_ACCOUNT_H_ 1 +#include "url.h" + /* account types */ enum { @@ -46,6 +48,7 @@ typedef struct } ACCOUNT; int mutt_account_match (const ACCOUNT* a1, const ACCOUNT* m2); +int mutt_account_fromurl (ACCOUNT* account, ciss_url_t* url); int mutt_account_getuser (ACCOUNT* account); int mutt_account_getpass (ACCOUNT* account); diff --git a/browser.c b/browser.c index 4b8d5631..2f5d527a 100644 --- a/browser.c +++ b/browser.c @@ -144,6 +144,7 @@ folder_format_str (char *dest, size_t destlen, char op, const char *src, FOLDER *folder = (FOLDER *) data; struct passwd *pw; struct group *gr; + int optional = (flags & M_FORMAT_OPTIONAL); switch (op) { @@ -257,8 +258,13 @@ folder_format_str (char *dest, size_t destlen, char op, const char *src, #ifdef USE_IMAP if (mx_is_imap (folder->ff->desc)) { - snprintf (tmp, sizeof (tmp), "%%%sd", fmt); - snprintf (dest, destlen, tmp, folder->ff->new); + if (!optional) + { + snprintf (tmp, sizeof (tmp), "%%%sd", fmt); + snprintf (dest, destlen, tmp, folder->ff->new); + } + else if (!folder->ff->new) + optional = 0; break; } #endif @@ -310,6 +316,12 @@ folder_format_str (char *dest, size_t destlen, char op, const char *src, snprintf (dest, destlen, tmp, op); break; } + + if (optional) + mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0); + else if (flags & M_FORMAT_OPTIONAL) + mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0); + return (src); } diff --git a/imap/imap.c b/imap/imap.c index 2fbdad7e..558833df 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -310,7 +310,9 @@ IMAP_DATA* imap_conn_find (const ACCOUNT* account, int flags) IMAP_DATA* idata; ACCOUNT* creds; - conn = mutt_conn_find (NULL, account); + if (!(conn = mutt_conn_find (NULL, account))) + return NULL; + /* if opening a new UNSELECTED connection, preserve existing creds */ creds = &(conn->account); @@ -318,11 +320,11 @@ IMAP_DATA* imap_conn_find (const ACCOUNT* account, int flags) if (flags & M_IMAP_CONN_NOSELECT) while (conn->data && ((IMAP_DATA*) conn->data)->state == IMAP_SELECTED) { - conn = mutt_conn_find (conn, account); + if (!(conn = mutt_conn_find (conn, account))) + return NULL; memcpy (&(conn->account), creds, sizeof (ACCOUNT)); } - idata = (IMAP_DATA*) conn->data; /* don't open a new connection if one isn't wanted */ @@ -1272,7 +1274,7 @@ int imap_complete(char* dest, size_t dlen, char* path) { /* reformat path for IMAP list, and append wildcard */ /* don't use INBOX in place of "" */ - if (mx.mbox[0]) + if (mx.mbox && mx.mbox[0]) imap_fix_path (idata, mx.mbox, list, sizeof(list)); else list[0] = '\0'; @@ -1284,7 +1286,7 @@ int imap_complete(char* dest, size_t dlen, char* path) { imap_cmd_start (idata, buf); /* and see what the results are */ - strfcpy (completion, mx.mbox, sizeof(completion)); + strfcpy (completion, NONULL(mx.mbox), sizeof(completion)); do { if (imap_parse_list_response(idata, &list_word, &noselect, &noinferiors, diff --git a/imap/imap_ssl.c b/imap/imap_ssl.c index 42915e68..6d362ff6 100644 --- a/imap/imap_ssl.c +++ b/imap/imap_ssl.c @@ -144,13 +144,6 @@ static int add_entropy (const char *file) return n; } -void imap_set_ssl (ACCOUNT* account) -{ - if (! (account->flags & M_ACCT_PORT)) - account->port = IMAP_SSL_PORT; - account->flags |= M_ACCT_SSL; -} - static int ssl_socket_open_err (CONNECTION *conn) { mutt_error (_("SSL disabled due the lack of entropy")); diff --git a/imap/imap_ssl.h b/imap/imap_ssl.h index 8c8ec747..575580a3 100644 --- a/imap/imap_ssl.h +++ b/imap/imap_ssl.h @@ -25,6 +25,5 @@ extern char *SslCertFile; extern char *SslEntropyFile; extern int ssl_socket_setup (CONNECTION *conn); -extern void imap_set_ssl (ACCOUNT* account); #endif /* _MUTT_SSL_H_ */ diff --git a/imap/util.c b/imap/util.c index 51a63112..ecd346dd 100644 --- a/imap/util.c +++ b/imap/util.c @@ -22,6 +22,7 @@ #include "mutt.h" #include "mx.h" /* for M_IMAP */ +#include "url.h" #include "imap_private.h" #include "imap_ssl.h" @@ -132,62 +133,90 @@ char *imap_next_word (char *s) int imap_parse_path (const char* path, IMAP_MBOX* mx) { char tmp[128]; + url_scheme_t scheme; + ciss_url_t url; char *c; int n; - - if (sscanf (path, "{%128[^}]}", tmp) != 1) - return -1; - - mx->account.type = M_ACCT_TYPE_IMAP; - c = strchr (path, '}'); - if (!c) - return -1; - else - /* walk past closing '}' */ - mx->mbox = safe_strdup (c+1); - /* Defaults */ mx->account.flags = 0; mx->account.port = IMAP_PORT; + mx->account.type = M_ACCT_TYPE_IMAP; + +#ifdef USE_SSL + if (option (OPTIMAPFORCESSL)) + mx->account.flags |= M_ACCT_SSL; +#endif - if ((c = strrchr (tmp, '@'))) + scheme = url_check_scheme (NONULL (path)); + if (scheme == U_IMAP || scheme == U_IMAPS) { - *c = '\0'; - strfcpy (mx->account.user, tmp, sizeof (mx->account.user)); - strfcpy (tmp, c+1, sizeof (tmp)); - mx->account.flags |= M_ACCT_USER; + if (url_parse_ciss (&url, path) < 0) + return -1; + + n = mutt_account_fromurl (&mx->account, &url); + FREE (&url.user); + FREE (&url.pass); + FREE (&url.host); + + if (n < 0) + { + FREE (&url.path); + return -1; + } + + mx->mbox = url.path; + + if (scheme == U_IMAPS) + mx->account.flags |= M_ACCT_SSL; } - - if ((n = sscanf (tmp, "%128[^:/]%128s", mx->account.host, tmp)) < 1) + /* old PINE-compatibility code */ + else { - dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path)); - FREE (&mx->mbox); - return -1; - } + if (sscanf (path, "{%128[^}]}", tmp) != 1) + return -1; + + c = strchr (path, '}'); + if (!c) + return -1; + else + /* walk past closing '}' */ + mx->mbox = safe_strdup (c+1); - if (n > 1) { - if (sscanf (tmp, ":%hd%128s", &(mx->account.port), tmp) >= 1) - mx->account.flags |= M_ACCT_PORT; - if (sscanf (tmp, "/%s", tmp) == 1) + if ((c = strrchr (tmp, '@'))) { -#ifdef USE_SSL - if (!strncmp (tmp, "ssl", 3)) - imap_set_ssl (&(mx->account)); - else -#endif + *c = '\0'; + strfcpy (mx->account.user, tmp, sizeof (mx->account.user)); + strfcpy (tmp, c+1, sizeof (tmp)); + mx->account.flags |= M_ACCT_USER; + } + + if ((n = sscanf (tmp, "%128[^:/]%128s", mx->account.host, tmp)) < 1) + { + dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path)); + FREE (&mx->mbox); + return -1; + } + + if (n > 1) { + if (sscanf (tmp, ":%hd%128s", &(mx->account.port), tmp) >= 1) + mx->account.flags |= M_ACCT_PORT; + if (sscanf (tmp, "/%s", tmp) == 1) { - dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path)); - FREE (&mx->mbox); - return -1; + if (!strncmp (tmp, "ssl", 3)) + mx->account.flags |= M_ACCT_SSL; + else + { + dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path)); + FREE (&mx->mbox); + return -1; + } } } } - -#ifdef USE_SSL - if (option (OPTIMAPFORCESSL)) - imap_set_ssl (&(mx->account)); -#endif + + if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT)) + mx->account.port = IMAP_SSL_PORT; return 0; } diff --git a/mutt_socket.c b/mutt_socket.c index fd3e2eb6..97d06bb9 100644 --- a/mutt_socket.c +++ b/mutt_socket.c @@ -191,11 +191,19 @@ CONNECTION* mutt_conn_find (const CONNECTION* start, const ACCOUNT* account) conn->next = Connections; Connections = conn; -#ifdef USE_SSL if (account->flags & M_ACCT_SSL) + { +#ifdef USE_SSL ssl_socket_setup (conn); - else +#else + mutt_error _("SSL is unavailable."); + sleep (2); + FREE (&conn); + + return NULL; #endif + } + else { conn->read = raw_socket_read; conn->write = raw_socket_write; diff --git a/mx.c b/mx.c index 45f3986d..74e4d286 100644 --- a/mx.c +++ b/mx.c @@ -24,7 +24,7 @@ #include "mailbox.h" #include "copy.h" #include "keymap.h" - +#include "url.h" #ifdef HAVE_PGP #include "pgp.h" @@ -313,7 +313,19 @@ void mx_unlink_empty (const char *path) int mx_is_imap(const char *p) { - return p && (*p == '{'); + url_scheme_t scheme; + + if (!p) + return 0; + + if (*p == '{') + return 1; + + scheme = url_check_scheme (p); + if (scheme == U_IMAP || scheme == U_IMAPS) + return 1; + + return 0; } #endif diff --git a/url.c b/url.c index fa210325..6c7ae30d 100644 --- a/url.c +++ b/url.c @@ -31,20 +31,10 @@ static struct mapping_t UrlMap[] = { { "file", U_FILE }, -#ifdef USE_IMAP { "imap", U_IMAP }, -#endif -#ifdef USE_POP - { "pop", U_POP }, -#endif -#ifdef USE_SSL -# ifdef USE_IMAP { "imaps", U_IMAPS }, -# endif -# ifdef USE_POP + { "pop", U_POP }, { "pops", U_POPS }, -# endif -#endif { "mailto", U_MAILTO }, { NULL, U_UNKNOWN} }; @@ -53,6 +43,10 @@ static struct mapping_t UrlMap[] = static void url_pct_decode (char *s) { char *d; + + if (!s) + return; + for (d = s; *s; s++) { if (*s == '%' && s[1] && s[2] && @@ -106,12 +100,17 @@ static char *ciss_parse_userhost (ciss_url_t *ciss, char *src) char *t; char *p; char *path; - + + ciss->user = NULL; + ciss->pass = NULL; + ciss->host = NULL; + ciss->port = 0; + if (strncmp (src, "//", 2)) return src; src += 2; - + if ((path = strchr (src, '/'))) *path++ = '\0'; @@ -122,8 +121,10 @@ static char *ciss_parse_userhost (ciss_url_t *ciss, char *src) { *p = '\0'; ciss->pass = safe_strdup (p + 1); + url_pct_decode (ciss->pass); } ciss->user = safe_strdup (src); + url_pct_decode (ciss->user); t++; } else @@ -138,12 +139,13 @@ static char *ciss_parse_userhost (ciss_url_t *ciss, char *src) ciss->port = 0; ciss->host = safe_strdup (t); + url_pct_decode (ciss->host); return path; } static void ciss_parse_path (ciss_url_t *ciss, char *src) { - ciss->path = src; + ciss->path = safe_strdup (src); url_pct_decode (ciss->path); } diff --git a/url.h b/url.h index 5648c342..63024e04 100644 --- a/url.h +++ b/url.h @@ -1,25 +1,13 @@ #ifndef _URL_H # define _URL_H -# include "config.h" - typedef enum url_scheme { U_FILE, -# ifdef USE_POP U_POP, -# endif -# ifdef USE_IMAP + U_POPS, U_IMAP, -# endif -# ifdef USE_SSL -# ifdef USE_IMAP U_IMAPS, -# endif -# ifdef USE_POP - U_POPS, -# endif -# endif U_MAILTO, U_UNKNOWN } -- 2.50.1