Nico Golde:
[apps/madmutt.git] / mutt_socket.c
index c79d927..f0ca158 100644 (file)
@@ -1,21 +1,12 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 1998 Michael R. Elkins <me@mutt.org>
  * Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>
  * Copyright (C) 1999-2000 Tommi Komulainen <Tommi.Komulainen@iki.fi>
- * 
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- * 
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
  */
 
 #if HAVE_CONFIG_H
 #include "globals.h"
 #include "mutt_socket.h"
 #include "mutt_tunnel.h"
-#if defined(USE_SSL) || defined(USE_GNUTLS) || defined(USE_NSS)
+#if defined(USE_SSL) || defined(USE_GNUTLS)
 # include "mutt_ssl.h"
 #endif
 
 #include "mutt_idna.h"
 
+#include "lib/mem.h"
+#include "lib/intl.h"
+#include "lib/str.h"
+#include "lib/debug.h"
+
 #include <unistd.h>
 #include <netinet/in.h>
 #include <netdb.h>
@@ -48,7 +44,7 @@ static CONNECTION *Connections = NULL;
 /* forward declarations */
 static int socket_preconnect (void);
 static int socket_connect (int fd, struct sockaddr *sa);
-static CONNECTION *socket_new_conn ();
+static CONNECTION *socket_new_conn (void);
 
 /* Wrappers */
 int mutt_socket_open (CONNECTION * conn)
@@ -64,9 +60,7 @@ int mutt_socket_close (CONNECTION * conn)
   int rc = -1;
 
   if (conn->fd < 0)
-    dprint (1,
-            (debugfile,
-             "mutt_socket_close: Attempt to close closed connection.\n"));
+    debug_print (1, ("Attempt to close closed connection.\n"));
   else
     rc = conn->conn_close (conn);
 
@@ -81,9 +75,7 @@ int mutt_socket_read (CONNECTION * conn, char *buf, size_t len)
   int rc;
 
   if (conn->fd < 0) {
-    dprint (1,
-            (debugfile,
-             "mutt_socket_read: attempt to read from closed connection\n"));
+    debug_print (1, ("attempt to read from closed connection\n"));
     return -1;
   }
 
@@ -104,28 +96,23 @@ int mutt_socket_write_d (CONNECTION * conn, const char *buf, int dbg)
   int rc;
   int len;
 
-  dprint (dbg, (debugfile, "> %s", buf));
+  debug_print (dbg, ("> %s", buf));
 
   if (conn->fd < 0) {
-    dprint (1,
-            (debugfile,
-             "mutt_socket_write: attempt to write to closed connection\n"));
+    debug_print (1, ("attempt to write to closed connection\n"));
     return -1;
   }
 
-  len = mutt_strlen (buf);
+  len = str_len (buf);
   if ((rc = conn->conn_write (conn, buf, len)) < 0) {
-    dprint (1, (debugfile,
-                "mutt_socket_write: error writing, closing socket\n"));
+    debug_print (1, ("error writing, closing socket\n"));
     mutt_socket_close (conn);
 
     return -1;
   }
 
   if (rc < len) {
-    dprint (1, (debugfile,
-                "mutt_socket_write: ERROR: wrote %d of %d bytes!\n", rc,
-                len));
+    debug_print (1, ("ERROR: wrote %d of %d bytes!\n", rc, len));
   }
 
   return rc;
@@ -139,9 +126,7 @@ int mutt_socket_readchar (CONNECTION * conn, char *c)
       conn->available =
         conn->conn_read (conn, conn->inbuf, sizeof (conn->inbuf));
     else {
-      dprint (1,
-              (debugfile,
-               "mutt_socket_readchar: attempt to read from closed connection.\n"));
+      debug_print (1, ("attempt to read from closed connection.\n"));
       return -1;
     }
     conn->bufpos = 0;
@@ -182,9 +167,9 @@ int mutt_socket_readln_d (char *buf, size_t buflen, CONNECTION * conn,
   else
     buf[i] = '\0';
 
-  dprint (dbg, (debugfile, "< %s\n", buf));
+  debug_print (dbg, ("< %s\n", buf));
 
-  /* number of bytes read, not strlen */
+  /* number of bytes read, not str_len */
   return i + 1;
 }
 
@@ -204,7 +189,7 @@ void mutt_socket_free (CONNECTION * conn)
   /* head is special case, doesn't need prev updated */
   if (iter == conn) {
     Connections = iter->next;
-    FREE (&iter);
+    mem_free (&iter);
     return;
   }
 
@@ -212,7 +197,7 @@ void mutt_socket_free (CONNECTION * conn)
     if (iter->next == conn) {
       tmp = iter->next;
       iter->next = tmp->next;
-      FREE (&tmp);
+      mem_free (&tmp);
       return;
     }
     iter = iter->next;
@@ -252,12 +237,8 @@ CONNECTION *mutt_conn_find (const CONNECTION * start, const ACCOUNT * account)
   if (Tunnel && *Tunnel)
     mutt_tunnel_socket_setup (conn);
   else if (account->flags & M_ACCT_SSL) {
-#ifdef USE_SSL
-    ssl_socket_setup (conn);
-#elif USE_NSS
-    mutt_nss_socket_setup (conn);
-#elif USE_GNUTLS
-    if (mutt_gnutls_socket_setup (conn) < 0) {
+#if defined (USE_SSL) || defined (USE_GNUTLS)
+    if (mutt_ssl_socket_setup (conn) < 0) {
       mutt_socket_free (conn);
       return NULL;
     }
@@ -285,10 +266,10 @@ static int socket_preconnect (void)
   int rc;
   int save_errno;
 
-  if (mutt_strlen (Preconnect)) {
-    dprint (2, (debugfile, "Executing preconnect: %s\n", Preconnect));
+  if (str_len (Preconnect)) {
+    debug_print (2, ("Executing preconnect: %s\n", Preconnect));
     rc = mutt_system (Preconnect);
-    dprint (2, (debugfile, "Preconnect result: %d\n", rc));
+    debug_print (2, ("Preconnect result: %d\n", rc));
     if (rc) {
       save_errno = errno;
       mutt_perror (_("Preconnect command failed."));
@@ -314,7 +295,7 @@ static int socket_connect (int fd, struct sockaddr *sa)
     sa_size = sizeof (struct sockaddr_in6);
 #endif
   else {
-    dprint (1, (debugfile, "Unknown address family!\n"));
+    debug_print (1, ("Unknown address family!\n"));
     return -1;
   }
 
@@ -327,7 +308,7 @@ static int socket_connect (int fd, struct sockaddr *sa)
 
   if (connect (fd, sa, sa_size) < 0) {
     save_errno = errno;
-    dprint (2, (debugfile, "Connection failed. errno: %d...\n", errno));
+    debug_print (2, ("Connection failed. errno: %d...\n", errno));
     SigInt = 0;                 /* reset in case we caught SIGINTR while in connect() */
   }
 
@@ -339,11 +320,11 @@ static int socket_connect (int fd, struct sockaddr *sa)
 }
 
 /* socket_new_conn: allocate and initialise a new connection. */
-static CONNECTION *socket_new_conn ()
+static CONNECTION *socket_new_conn (void)
 {
   CONNECTION *conn;
 
-  conn = (CONNECTION *) safe_calloc (1, sizeof (CONNECTION));
+  conn = (CONNECTION *) mem_calloc (1, sizeof (CONNECTION));
   conn->fd = -1;
 
   return conn;
@@ -423,11 +404,12 @@ int raw_socket_open (CONNECTION * conn)
   rc = getaddrinfo (host_idna, port, &hints, &res);
 
 # ifdef HAVE_LIBIDN
-  FREE (&host_idna);
+  mem_free (&host_idna);
 # endif
 
   if (rc) {
     mutt_error (_("Could not find the host \"%s\""), conn->account.host);
+    mutt_sleep (2);
     return -1;
   }
 
@@ -473,7 +455,7 @@ int raw_socket_open (CONNECTION * conn)
 
   if ((he = gethostbyname (host_idna)) == NULL) {
 # ifdef HAVE_LIBIDN
-    FREE (&host_idna);
+    mem_free (&host_idna);
 # endif
     mutt_error (_("Could not find the host \"%s\""), conn->account.host);
 
@@ -481,7 +463,7 @@ int raw_socket_open (CONNECTION * conn)
   }
 
 # ifdef HAVE_LIBIDN
-  FREE (&host_idna);
+  mem_free (&host_idna);
 # endif
 
   mutt_message (_("Connecting to %s..."), conn->account.host);