sync with 0.9.10cvs11
[claws.git] / src / common / nntp.c
index d9b6f4d6df345d90936c8917ff0a74ecca993a9b..991feb6c769f041374ec5a6c1dd48fbd01065118 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2004 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
 #include "socket.h"
 #include "utils.h"
 #include "log.h"
-#if USE_SSL
+#if USE_OPENSSL
 #  include "ssl.h"
 #endif
 
 static gint verbose = 1;
 
-static void nntp_gen_send      (NNTPSockInfo   *sock,
+static void nntp_session_destroy(Session       *session);
+
+static gint nntp_ok            (SockInfo       *sock,
+                                gchar          *argbuf);
+
+static gint nntp_gen_send      (SockInfo       *sock,
                                 const gchar    *format,
                                 ...);
-static gint nntp_gen_recv      (NNTPSockInfo   *sock,
+static gint nntp_gen_recv      (SockInfo       *sock,
                                 gchar          *buf,
                                 gint            size);
-static gint nntp_gen_command   (NNTPSockInfo   *sock,
+static gint nntp_gen_command   (NNTPSession    *session,
                                 gchar          *argbuf,
                                 const gchar    *format,
                                 ...);
 
-#if USE_SSL
-NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf,
-                       SSLType ssl_type)
+#if USE_OPENSSL
+Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
+                         const gchar *userid, const gchar *passwd,
+                         SSLType ssl_type)
 #else
-NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
+Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
+                         const gchar *userid, const gchar *passwd)
 #endif
 {
+       NNTPSession *session;
        SockInfo *sock;
-       NNTPSockInfo *nntp_sock;
 
        if ((sock = sock_connect(server, port)) == NULL) {
                log_warning(_("Can't connect to NNTP server: %s:%d\n"),
@@ -63,73 +70,98 @@ NNTPSockInfo *nntp_open(const gchar *server, gushort port, gchar *buf)
                return NULL;
        }
 
-#if USE_SSL
+#if USE_OPENSSL
        if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
                sock_close(sock);
                return NULL;
        }
 #endif
 
-       nntp_sock = g_new0(NNTPSockInfo, 1);
-       nntp_sock->sock = sock;
-
-       if (nntp_ok(nntp_sock, buf) == NN_SUCCESS)
-               return nntp_sock;
-       else {
+       if (nntp_ok(sock, buf) != NN_SUCCESS) {
                sock_close(sock);
-               g_free(nntp_sock);
                return NULL;
        }
-}
 
-#if USE_SSL
-NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
-                            const gchar *userid, const gchar *passwd,
-                            SSLType ssl_type)
-#else
-NNTPSockInfo *nntp_open_auth(const gchar *server, gushort port, gchar *buf,
-                            const gchar *userid, const gchar *passwd)
-#endif
-{
-       NNTPSockInfo *sock;
+       session = g_new0(NNTPSession, 1);
 
-#if USE_SSL
-       sock = nntp_open(server, port, buf, ssl_type);
-#else
-       sock = nntp_open(server, port, buf);
-#endif
+       session_init(SESSION(session));
+
+       SESSION(session)->type                  = SESSION_NEWS;
+       SESSION(session)->server                = g_strdup(server);
+       SESSION(session)->sock                  = sock;
+       SESSION(session)->last_access_time      = time(NULL);
+       SESSION(session)->data                  = NULL;
+
+       SESSION(session)->destroy               = nntp_session_destroy;
+
+       session->group = NULL;
+
+       if (userid && passwd) {
+               gint ok;
 
-       if (!sock) return NULL;
+               session->userid = g_strdup(userid);
+               session->passwd = g_strdup(passwd);
 
-       sock->userid = g_strdup(userid);
-       sock->passwd = g_strdup(passwd);
+               ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
+               if (ok != NN_SUCCESS) {
+                       session_destroy(SESSION(session));
+                       return NULL;
+               }
+               ok = nntp_ok(sock, NULL);
+               if (ok == NN_AUTHCONT) {
+                       ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
+                                          session->passwd);
+                       if (ok != NN_SUCCESS) {
+                               session_destroy(SESSION(session));
+                               return NULL;
+                       }
+                       ok = nntp_ok(sock, NULL);
+                       if (ok != NN_SUCCESS)
+                               session->auth_failed = TRUE;
+               }
+               if (ok == NN_SOCKET) {
+                       session_destroy(SESSION(session));
+                       return NULL;
+               }
+       }
 
-       return sock;
+       return SESSION(session);
 }
 
-void nntp_close(NNTPSockInfo *sock)
+void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
+
 {
-       if (!sock) return;
+       if (!session) return;
+               
+       nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
+
 
-       sock_close(sock->sock);
-       g_free(sock->userid);
-       g_free(sock->passwd);
-       g_free(sock);
 }
 
-gint nntp_group(NNTPSockInfo *sock, const gchar *group,
+static void nntp_session_destroy(Session *session)
+{
+       NNTPSession *nntp_session = NNTP_SESSION(session);
+
+       g_return_if_fail(session != NULL);
+
+       g_free(nntp_session->group);
+       g_free(nntp_session->userid);
+       g_free(nntp_session->passwd);
+}
+
+gint nntp_group(NNTPSession *session, const gchar *group,
                gint *num, gint *first, gint *last)
 {
        gint ok;
        gint resp;
        gchar buf[NNTPBUFSIZE];
 
-       ok = nntp_gen_command(sock, buf, "GROUP %s", group);
+       ok = nntp_gen_command(session, buf, "GROUP %s", group);
 
-       if (ok != NN_SUCCESS) {
-               ok = nntp_mode(sock, FALSE);
+       if (ok != NN_SUCCESS && ok != NN_SOCKET && ok != NN_AUTHREQ) {
+               ok = nntp_mode(session, FALSE);
                if (ok == NN_SUCCESS)
-                       ok = nntp_gen_command(sock, buf, "GROUP %s", group);
+                       ok = nntp_gen_command(session, buf, "GROUP %s", group);
        }
 
        if (ok != NN_SUCCESS)
@@ -144,16 +176,16 @@ gint nntp_group(NNTPSockInfo *sock, const gchar *group,
        return NN_SUCCESS;
 }
 
-gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
+gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
                      gchar **msgid)
 {
        gint ok;
        gchar buf[NNTPBUFSIZE];
 
        if (num > 0)
-               ok = nntp_gen_command(sock, buf, "%s %d", cmd, num);
+               ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
        else
-               ok = nntp_gen_command(sock, buf, cmd);
+               ok = nntp_gen_command(session, buf, cmd);
 
        if (ok != NN_SUCCESS)
                return ok;
@@ -161,40 +193,40 @@ gint nntp_get_article(NNTPSockInfo *sock, const gchar *cmd, gint num,
        extract_parenthesis(buf, '<', '>');
        if (buf[0] == '\0') {
                log_warning(_("protocol error\n"));
-               return NN_PROTOCOL;
-       }
-       *msgid = g_strdup(buf);
+               *msgid = g_strdup("0");
+       } else
+               *msgid = g_strdup(buf);
 
        return NN_SUCCESS;
 }
 
-gint nntp_article(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
 {
-       return nntp_get_article(sock, "ARTICLE", num, msgid);
+       return nntp_get_article(session, "ARTICLE", num, msgid);
 }
 
-gint nntp_body(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
 {
-       return nntp_get_article(sock, "BODY", num, msgid);
+       return nntp_get_article(session, "BODY", num, msgid);
 }
 
-gint nntp_head(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
 {
-       return nntp_get_article(sock, "HEAD", num, msgid);
+       return nntp_get_article(session, "HEAD", num, msgid);
 }
 
-gint nntp_stat(NNTPSockInfo *sock, gint num, gchar **msgid)
+gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
 {
-       return nntp_get_article(sock, "STAT", num, msgid);
+       return nntp_get_article(session, "STAT", num, msgid);
 }
 
-gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
+gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
 {
        gint ok;
        gint resp;
        gchar buf[NNTPBUFSIZE];
 
-       ok = nntp_gen_command(sock, buf, "NEXT");
+       ok = nntp_gen_command(session, buf, "NEXT");
 
        if (ok != NN_SUCCESS)
                return ok;
@@ -214,90 +246,82 @@ gint nntp_next(NNTPSockInfo *sock, gint *num, gchar **msgid)
        return NN_SUCCESS;
 }
 
-gint nntp_xover(NNTPSockInfo *sock, gint first, gint last)
+gint nntp_xover(NNTPSession *session, gint first, gint last)
 {
        gint ok;
        gchar buf[NNTPBUFSIZE];
 
-       ok = nntp_gen_command(sock, buf, "XOVER %d-%d", first, last);
+       ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
        if (ok != NN_SUCCESS)
                return ok;
 
        return NN_SUCCESS;
 }
 
-gint nntp_xhdr(NNTPSockInfo *sock, const gchar *header, gint first, gint last)
+gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
 {
        gint ok;
        gchar buf[NNTPBUFSIZE];
 
-       ok = nntp_gen_command(sock, buf, "XHDR %s %d-%d", header, first, last);
+       ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
+                             header, first, last);
        if (ok != NN_SUCCESS)
                return ok;
 
        return NN_SUCCESS;
 }
 
-gint nntp_list(NNTPSockInfo *sock)
+gint nntp_list(NNTPSession *session)
 {
-       return nntp_gen_command(sock, NULL, "LIST");
+       return nntp_gen_command(session, NULL, "LIST");
 }
 
-gint nntp_post(NNTPSockInfo *sock, FILE *fp)
+gint nntp_post(NNTPSession *session, FILE *fp)
 {
        gint ok;
        gchar buf[NNTPBUFSIZE];
+       gchar *msg;
 
-       ok = nntp_gen_command(sock, buf, "POST");
+       ok = nntp_gen_command(session, buf, "POST");
        if (ok != NN_SUCCESS)
                return ok;
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               strretchomp(buf);
-               if (buf[0] == '.') {
-                       if (sock_write(sock->sock, ".", 1) < 0) {
-                               log_warning(_("Error occurred while posting\n"));
-                               return NN_SOCKET;
-                       }
-               }
-
-               if (sock_puts(sock->sock, buf) < 0) {
-                       log_warning(_("Error occurred while posting\n"));
-                       return NN_SOCKET;
-               }
+       msg = get_outgoing_rfc2822_str(fp);
+       if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
+               log_warning(_("Error occurred while posting\n"));
+               g_free(msg);
+               return NN_SOCKET;
        }
+       g_free(msg);
 
-       sock_write(sock->sock, ".\r\n", 3);
-       if ((ok = nntp_ok(sock, buf)) != NN_SUCCESS)
+       sock_write_all(SESSION(session)->sock, ".\r\n", 3);
+       if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
                return ok;
 
        return NN_SUCCESS;
 }
 
-gint nntp_newgroups(NNTPSockInfo *sock)
+gint nntp_newgroups(NNTPSession *session)
 {
        return NN_SUCCESS;
 }
 
-gint nntp_newnews(NNTPSockInfo *sock)
+gint nntp_newnews(NNTPSession *session)
 {
        return NN_SUCCESS;
 }
 
-gint nntp_mode(NNTPSockInfo *sock, gboolean stream)
+gint nntp_mode(NNTPSession *session, gboolean stream)
 {
        gint ok;
 
-       if (sock->auth_failed)
-               return NN_AUTHREQ;
-
-       ok = nntp_gen_command(sock, NULL, "MODE %s",
+       ok = nntp_gen_command(session, NULL, "MODE %s",
                              stream ? "STREAM" : "READER");
 
        return ok;
 }
 
-gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
+static gint nntp_ok(SockInfo *sock, gchar *argbuf)
 {
        gint ok;
        gchar buf[NNTPBUFSIZE];
@@ -324,7 +348,7 @@ gint nntp_ok(NNTPSockInfo *sock, gchar *argbuf)
        return ok;
 }
 
-static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
+static gint nntp_gen_send(SockInfo *sock, const gchar *format, ...)
 {
        gchar buf[NNTPBUFSIZE];
        va_list args;
@@ -341,12 +365,17 @@ static void nntp_gen_send(NNTPSockInfo *sock, const gchar *format, ...)
        }
 
        strcat(buf, "\r\n");
-       sock_write(sock->sock, buf, strlen(buf));
+       if (sock_write_all(sock, buf, strlen(buf)) < 0) {
+               log_warning(_("Error occurred while sending command\n"));
+               return NN_SOCKET;
+       }
+
+       return NN_SUCCESS;
 }
 
-static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
+static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
 {
-       if (sock_gets(sock->sock, buf, size) == -1)
+       if (sock_gets(sock, buf, size) == -1)
                return NN_SOCKET;
 
        strretchomp(buf);
@@ -357,39 +386,59 @@ static gint nntp_gen_recv(NNTPSockInfo *sock, gchar *buf, gint size)
        return NN_SUCCESS;
 }
 
-static gint nntp_gen_command(NNTPSockInfo *sock, gchar *argbuf,
+static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
                             const gchar *format, ...)
 {
        gchar buf[NNTPBUFSIZE];
        va_list args;
        gint ok;
+       SockInfo *sock;
 
        va_start(args, format);
        g_vsnprintf(buf, sizeof(buf), format, args);
        va_end(args);
 
-       nntp_gen_send(sock, "%s", buf);
+       sock = SESSION(session)->sock;
+       ok = nntp_gen_send(sock, "%s", buf);
+       if (ok != NN_SUCCESS)
+               return ok;
        ok = nntp_ok(sock, argbuf);
        if (ok == NN_AUTHREQ) {
-               if (!sock->userid || !sock->passwd) {
-                       sock->auth_failed = TRUE;
+               if (!session->userid || !session->passwd) {
+                       session->auth_failed = TRUE;
                        return ok;
                }
 
-               nntp_gen_send(sock, "AUTHINFO USER %s", sock->userid);
+               ok = nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
+               if (ok != NN_SUCCESS)
+                       return ok;
                ok = nntp_ok(sock, NULL);
                if (ok == NN_AUTHCONT) {
-                       nntp_gen_send(sock, "AUTHINFO PASS %s", sock->passwd);
+                       ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
+                                          session->passwd);
+                       if (ok != NN_SUCCESS)
+                               return ok;
                        ok = nntp_ok(sock, NULL);
                }
                if (ok != NN_SUCCESS) {
-                       sock->auth_failed = TRUE;
+                       session->auth_failed = TRUE;
                        return ok;
                }
 
-               nntp_gen_send(sock, "%s", buf);
+               ok = nntp_gen_send(sock, "%s", buf);
+               if (ok != NN_SUCCESS)
+                       return ok;
                ok = nntp_ok(sock, argbuf);
-       }
+
+       } else if (ok == NN_AUTHCONT) {
+                ok = nntp_gen_send(sock, "AUTHINFO PASS %s",
+                                  session->passwd);
+               if (ok != NN_SUCCESS)  {
+                       session->auth_failed = TRUE;
+                       return ok;
+               }
+                ok = nntp_ok(sock, NULL);
+       }
 
        return ok;
 }