1 /**
2  * @file
3  * NeoMutt connections
4  *
5  * @authors
6  * Copyright (C) 2000-2007 Brendan Cully <brendan@kublai.com>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /**
24  * @page neo_mutt_socket NeoMutt connections
25  *
26  * NeoMutt connections
27  */
28 
29 #include "config.h"
30 #include <stdio.h>
31 #include <string.h>
32 #include "config/lib.h"
33 #include "email/lib.h"
34 #include "core/lib.h"
35 #include "conn/lib.h"
36 #include "mutt_socket.h"
37 #include "hook.h"
38 #include "mutt_account.h"
39 #ifndef USE_SSL
40 #include "mutt/lib.h"
41 #endif
42 
43 /**
44  * mutt_conn_new - Create a new Connection
45  * @param cac Credentials to use
46  * @retval ptr New Connection
47  */
mutt_conn_new(const struct ConnAccount * cac)48 struct Connection *mutt_conn_new(const struct ConnAccount *cac)
49 {
50   enum ConnectionType conn_type;
51 
52   const char *const c_tunnel = cs_subset_string(NeoMutt->sub, "tunnel");
53   if (c_tunnel)
54     conn_type = MUTT_CONNECTION_TUNNEL;
55   else if (cac->flags & MUTT_ACCT_SSL)
56     conn_type = MUTT_CONNECTION_SSL;
57   else
58     conn_type = MUTT_CONNECTION_SIMPLE;
59 
60   struct Connection *conn = mutt_socket_new(conn_type);
61   if (conn)
62   {
63     memcpy(&conn->account, cac, sizeof(struct ConnAccount));
64   }
65   else
66   {
67     if (conn_type == MUTT_CONNECTION_SSL)
68     {
69 #ifndef USE_SSL
70       /* that's probably why it failed */
71       mutt_error(_("SSL is unavailable, can't connect to %s"), cac->host);
72 #endif
73     }
74   }
75   return conn;
76 }
77 
78 /**
79  * mutt_conn_find - Find a connection from a list
80  * @param cac   ConnAccount to match
81  * @retval ptr Matching Connection
82  *
83  * find a connection off the list of connections whose account matches cac.
84  * If start is not null, only search for connections after the given connection
85  * (allows higher level socket code to make more fine-grained searches than
86  * account info. Eg in IMAP we may wish to find a connection which is not in
87  * IMAP_SELECTED state)
88  */
mutt_conn_find(const struct ConnAccount * cac)89 struct Connection *mutt_conn_find(const struct ConnAccount *cac)
90 {
91   struct Url url = { 0 };
92   char hook[1024];
93 
94   /* cac isn't actually modified, since url isn't either */
95   mutt_account_tourl((struct ConnAccount *) cac, &url);
96   url.path = NULL;
97   url_tostring(&url, hook, sizeof(hook), U_NO_FLAGS);
98   mutt_account_hook(hook);
99 
100   return mutt_conn_new(cac);
101 }
102