]> granicus.if.org Git - neomutt/commitdiff
conn: add parameter to mutt_socket_new to specify socket type
authorDamien Riegel <damien.riegel@gmail.com>
Wed, 8 Nov 2017 21:08:23 +0000 (16:08 -0500)
committerRichard Russon <rich@flatcap.org>
Thu, 12 Apr 2018 08:51:56 +0000 (09:51 +0100)
It's currently the responsibility of mutt_conn_find to setup the
Connection, but this should really be done by mutt_socket_new, as it's
part of the socket initialization. To let it know which kind of
Connection it should create, add a parameter to allow callers to
specify the type.

conn/socket.c
conn/socket.h
mutt_socket.c

index 0e214adf04da2e64b16336e4a8b0d72cab44b1f9..01ec0f9c8bbea7c4e0d5c1cba4fbe3844c2ac6be 100644 (file)
 #include "globals.h"
 #include "options.h"
 #include "protos.h"
-#ifdef USE_SSL
-#include "ssl.h"
-#endif
 
 #include "socket.h"
+#include "ssl.h"
+#include "tunnel.h"
 
 /* support for multiple socket connections */
 static struct ConnectionList Connections = TAILQ_HEAD_INITIALIZER(Connections);
@@ -343,14 +342,36 @@ int mutt_socket_readln_d(char *buf, size_t buflen, struct Connection *conn, int
 
 /**
  * mutt_socket_new - allocate and initialise a new connection
+ * @param type Type of the new Connection
  * @retval ptr New Connection
  */
-struct Connection *mutt_socket_new(void)
+struct Connection *mutt_socket_new(enum ConnectionType type)
 {
   struct Connection *conn = mutt_mem_calloc(1, sizeof(struct Connection));
   conn->fd = -1;
 
-  TAILQ_INSERT_HEAD(&Connections, conn, entries);
+  if (type == MUTT_CONNECTION_TUNNEL)
+  {
+    mutt_tunnel_socket_setup(conn);
+  }
+  else if (type == MUTT_CONNECTION_SSL)
+  {
+    int ret = mutt_ssl_socket_setup(conn);
+
+    if (ret < 0)
+      FREE(&conn);
+  }
+  else
+  {
+    conn->conn_read = raw_socket_read;
+    conn->conn_write = raw_socket_write;
+    conn->conn_open = raw_socket_open;
+    conn->conn_close = raw_socket_close;
+    conn->conn_poll = raw_socket_poll;
+  }
+
+  if (conn)
+    TAILQ_INSERT_HEAD(&Connections, conn, entries);
 
   return conn;
 }
index 2d05777ca96a3a27ac25276576bfdb8fabd1a97b..d7b07c2c91987dc3ca026358e6b221065af2d33a 100644 (file)
 
 #include "mutt/queue.h"
 
+enum ConnectionType
+{
+  MUTT_CONNECTION_SIMPLE,
+  MUTT_CONNECTION_TUNNEL,
+  MUTT_CONNECTION_SSL,
+};
+
 struct Connection;
 
 /**
@@ -39,7 +46,7 @@ TAILQ_HEAD(ConnectionList, Connection);
 /* stupid hack for imap_logout_all */
 struct ConnectionList *mutt_socket_head(void);
 
-struct Connection *mutt_socket_new(void);
+struct Connection *mutt_socket_new(enum ConnectionType type);
 void mutt_socket_free(struct Connection *conn);
 
 int mutt_socket_open(struct Connection *conn);
index 0a8c05ffa9ba51a6f2b66fb6bb1fd855306c764a..4bb3a6fb9acadad74e9f837d94c9670be3f6034a 100644 (file)
@@ -41,6 +41,7 @@
  */
 struct Connection *mutt_conn_find(const struct Connection *start, const struct Account *account)
 {
+  enum ConnectionType conn_type;
   struct Url url;
   char hook[LONG_STRING];
 
@@ -58,32 +59,28 @@ struct Connection *mutt_conn_find(const struct Connection *start, const struct A
     conn = TAILQ_NEXT(conn, entries);
   }
 
-  conn = mutt_socket_new();
-  memcpy(&conn->account, account, sizeof(struct Account));
-
   if (Tunnel && *Tunnel)
-    mutt_tunnel_socket_setup(conn);
+    conn_type = MUTT_CONNECTION_TUNNEL;
   else if (account->flags & MUTT_ACCT_SSL)
+    conn_type = MUTT_CONNECTION_SSL;
+  else
+    conn_type = MUTT_CONNECTION_SIMPLE;
+
+  conn = mutt_socket_new(conn_type);
+  if (conn)
+  {
+    memcpy(&conn->account, account, sizeof(struct Account));
+  }
+  else
   {
-    if (mutt_ssl_socket_setup(conn) < 0)
+    if (conn_type == MUTT_CONNECTION_SSL)
     {
 #ifndef USE_SSL
       /* that's probably why it failed */
       mutt_error(_("SSL is unavailable, cannot connect to %s"), account->host);
 #endif
-      mutt_socket_free(conn);
-
-      return NULL;
     }
   }
-  else
-  {
-    conn->conn_read = raw_socket_read;
-    conn->conn_write = raw_socket_write;
-    conn->conn_open = raw_socket_open;
-    conn->conn_close = raw_socket_close;
-    conn->conn_poll = raw_socket_poll;
-  }
 
   return conn;
 }