added SSL support for POP using OpenSSL
[claws.git] / src / socket.c
index 7ee73bfaab1a550b5ff07b13429ddc4088a5f993..4a8f73f6e0a5efb171869dadfcad736f8be4525e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 Hiroyuki Yamamoto
  *
  * 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
  *
  * 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
@@ -397,6 +397,11 @@ gint sock_read(SockInfo *sock, gchar *buf, gint len)
 {
        g_return_val_if_fail(sock != NULL, -1);
 
 {
        g_return_val_if_fail(sock != NULL, -1);
 
+#if USE_SSL
+       if(sock->ssl) {
+               return ssl_read(sock->ssl, buf, len);
+       }
+#endif
        return fd_read(sock->sock, buf, len);
 }
 
        return fd_read(sock->sock, buf, len);
 }
 
@@ -405,10 +410,22 @@ gint fd_read(gint fd, gchar *buf, gint len)
        return read(fd, buf, len);
 }
 
        return read(fd, buf, len);
 }
 
+#ifdef USE_SSL
+gint ssl_read(SSL *ssl, gchar *buf, gint len)
+{
+       return SSL_read(ssl, buf, len);
+}
+#endif
+
 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
 {
        g_return_val_if_fail(sock != NULL, -1);
 
 gint sock_write(SockInfo *sock, const gchar *buf, gint len)
 {
        g_return_val_if_fail(sock != NULL, -1);
 
+#if USE_SSL
+       if(sock->ssl) {
+               return ssl_write(sock->ssl, buf, len);
+       }
+#endif
        return fd_write(sock->sock, buf, len);
 }
 
        return fd_write(sock->sock, buf, len);
 }
 
@@ -428,6 +445,24 @@ gint fd_write(gint fd, const gchar *buf, gint len)
        return wrlen;
 }
 
        return wrlen;
 }
 
+#ifdef USE_SSL
+gint ssl_write(SSL *ssl, const gchar *buf, gint len)
+{
+       gint n, wrlen = 0;
+
+       while (len) {
+               n = SSL_write(ssl, buf, len);
+               if (n <= 0)
+                       return -1;
+               len -= n;
+               wrlen += n;
+               buf += n;
+       }
+
+       return wrlen;
+}
+#endif
+
 gint fd_gets(gint fd, gchar *buf, gint len)
 {
        gchar *newline, *bp = buf;
 gint fd_gets(gint fd, gchar *buf, gint len)
 {
        gchar *newline, *bp = buf;
@@ -450,13 +485,100 @@ gint fd_gets(gint fd, gchar *buf, gint len)
        return bp - buf;
 }
 
        return bp - buf;
 }
 
+#if USE_SSL
+gint ssl_gets(SSL *ssl, gchar *buf, gint len)
+{
+       gchar *buf2 = buf;
+       gboolean newline = FALSE;
+       gint n, count = 0;
+
+       if (--len < 1)
+               return -1;
+       while(len > 0 && !newline) {
+               *buf2 = '\0';
+               if((n = SSL_read(ssl, buf2, 1)) < 0)
+                       return -1;
+               if(*buf2 == '\n')
+                       newline = TRUE;
+               buf2 += n;
+               count += n;
+       }
+
+       *buf2 = '\0';
+       return n;
+}
+#endif
+
 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
 {
        g_return_val_if_fail(sock != NULL, -1);
 
 gint sock_gets(SockInfo *sock, gchar *buf, gint len)
 {
        g_return_val_if_fail(sock != NULL, -1);
 
+#if USE_SSL
+       if(sock->ssl) {
+               return ssl_gets(sock->ssl, buf, len);
+       }
+#endif
        return fd_gets(sock->sock, buf, len);
 }
 
        return fd_gets(sock->sock, buf, len);
 }
 
+gchar *fd_getline(gint fd)
+{
+       gchar buf[BUFFSIZE];
+       gchar *str = NULL;
+       gint len;
+       gulong size = 1;
+
+       while ((len = fd_gets(fd, buf, sizeof(buf))) > 0) {
+               size += len;
+               if (!str)
+                       str = g_strdup(buf);
+               else {
+                       str = g_realloc(str, size);
+                       strcat(str, buf);
+               }
+               if (buf[len - 1] == '\n')
+                       break;
+       }
+
+       return str;
+}
+
+#if USE_SSL
+gchar *ssl_getline(SSL *ssl)
+{
+       gchar buf[BUFFSIZE];
+       gchar *str = NULL;
+       gint len;
+       gulong size = 1;
+
+       while ((len = ssl_gets(ssl, buf, sizeof(buf))) > 0) {
+               size += len;
+               if (!str)
+                       str = g_strdup(buf);
+               else {
+                       str = g_realloc(str, size);
+                       strcat(str, buf);
+               }
+               if (buf[len - 1] == '\n')
+                       break;
+       }
+
+       return str;
+}
+#endif
+
+gchar *sock_getline(SockInfo *sock)
+{
+       g_return_val_if_fail(sock != NULL, NULL);
+
+#if USE_SSL
+       if(sock->ssl) {
+               return ssl_getline(sock->ssl);
+       }
+#endif
+       return fd_getline(sock->sock);
+}
+
 gint sock_puts(SockInfo *sock, const gchar *buf)
 {
        gint ret;
 gint sock_puts(SockInfo *sock, const gchar *buf)
 {
        gint ret;
@@ -500,9 +622,9 @@ gint fd_close(gint fd)
 }
 
 gint sock_gdk_input_add(SockInfo *sock,
 }
 
 gint sock_gdk_input_add(SockInfo *sock,
-                        GdkInputCondition condition,
-                        GdkInputFunction function,
-                        gpointer data)
+                       GdkInputCondition condition,
+                       GdkInputFunction function,
+                       gpointer data)
 {
        g_return_val_if_fail(sock != NULL, -1);
 
 {
        g_return_val_if_fail(sock != NULL, -1);