Added NNTP authentication support
authorSergey Vlasov <vsu@users.sourceforge.net>
Thu, 26 Apr 2001 16:41:29 +0000 (16:41 +0000)
committerSergey Vlasov <vsu@users.sourceforge.net>
Thu, 26 Apr 2001 16:41:29 +0000 (16:41 +0000)
ChangeLog.claws
src/news.c
src/nntp.c
src/nntp.h
src/prefs_account.c

index f40778edd2e6caf1446755484f19cec61ce59ad0..eb4be31669c3d18b12ce431603dee0c1b196f73c 100644 (file)
@@ -1,3 +1,14 @@
+2001-04-26 [sergey]
+
+       * src/news.c: news_query_password(), news_authenticate(): new
+         functions.
+         news_get_uncached_articles(): added NNTP authentication
+         handling.
+       * src/nntp.[ch]: nntp_authinfo_user(), nntp_authinfo_pass():
+         new functions.
+       * src/prefs_account.c: prefs_account_protocol_activated():
+         enable userid and password fields for NNTP.
+
 2001-04-26
 
        * configure.in: check for availability of strftime()
index de69fb915ef48e26ae2802bc70ed9dc1e9ff44f6..8f843388f9f8f738f51ecd94c015c548dc468be6 100644 (file)
@@ -42,6 +42,9 @@
 #include "codeconv.h"
 #include "utils.h"
 #include "prefs_common.h"
+#include "prefs_account.h"
+#include "inputdialog.h"
+#include "alertpanel.h"
 
 static gint news_get_article_cmd        (NNTPSession   *session,
                                          const gchar   *cmd,
@@ -267,6 +270,58 @@ static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
        return news_get_article_cmd(session, "HEAD", num, filename);
 }
 
+static gchar *news_query_password(NNTPSession *session,
+                                 const gchar *user)
+{
+       gchar *message;
+       gchar *pass;
+
+       message = g_strdup_printf
+               (_("Input password for %s on %s:"),
+                user,
+                SESSION(session)->server);
+
+       pass = input_dialog_with_invisible(_("Input password"),
+                                          message, NULL);
+       g_free(message);
+/*     manage_window_focus_in(inc_dialog->mainwin->window, */
+/*                            NULL, NULL); */
+       return pass;
+}
+
+static gint news_authenticate(NNTPSession *session,
+                             FolderItem *item)
+{
+       gint ok;
+       const gchar *user;
+       gchar *pass;
+       gboolean need_free_pass = FALSE;
+
+       debug_print(_("news server requested authentication\n"));
+       if (!item || !item->folder || !item->folder->account)
+               return NN_ERROR;
+       user = item->folder->account->userid;
+       if (!user)
+               return NN_ERROR;
+       ok = nntp_authinfo_user(SESSION(session)->sock, user);
+       if (ok == NN_AUTHCONT) {
+               pass = item->folder->account->passwd;
+               if (!pass || !pass[0]) {
+                       pass = news_query_password(session, user);
+                       need_free_pass = TRUE;
+               }
+               ok = nntp_authinfo_pass(SESSION(session)->sock, pass);
+               if (need_free_pass)
+                       g_free(pass);
+       }
+       if (ok != NN_SUCCESS) {
+               log_warning(_("NNTP authentication failed\n"));
+               alertpanel_error(_("Authentication for %s on %s failed."),
+                                user, SESSION(session)->server);
+       }
+       return ok;
+}
+
 static GSList *news_get_uncached_articles(NNTPSession *session,
                                          FolderItem *item, gint cache_last,
                                          gint *rfirst, gint *rlast)
@@ -288,6 +343,13 @@ static GSList *news_get_uncached_articles(NNTPSession *session,
 
        ok = nntp_group(SESSION(session)->sock, item->path,
                        &num, &first, &last);
+       if (ok == NN_AUTHREQ) {
+               ok = news_authenticate(session, item);
+               if (ok != NN_SUCCESS)
+                       return NULL;
+               ok = nntp_group(SESSION(session)->sock, item->path,
+                               &num, &first, &last);
+       }
        if (ok != NN_SUCCESS) {
                log_warning(_("can't set group: %s\n"), item->path);
                return NULL;
index 708207830498cab3d566ca5151e646a0d83d7e56..59a30d0a93f400b40268730a69933522ccc2df61 100644 (file)
@@ -209,6 +209,28 @@ gint nntp_mode(gint sock, gboolean stream)
        return ok;
 }
 
+gint nntp_authinfo_user(gint sock, const gchar *user)
+{
+       gint ok;
+       gchar buf[NNTPBUFSIZE];
+
+       nntp_gen_send(sock, "AUTHINFO USER %s", user);
+       ok = nntp_ok(sock, buf);
+
+       return ok;
+}
+
+gint nntp_authinfo_pass(gint sock, const gchar *pass)
+{
+       gint ok;
+       gchar buf[NNTPBUFSIZE];
+
+       nntp_gen_send(sock, "AUTHINFO PASS %s", pass);
+       ok = nntp_ok(sock, buf);
+
+       return ok;
+}
+
 gint nntp_ok(gint sock, gchar *argbuf)
 {
        gint ok;
@@ -223,8 +245,12 @@ gint nntp_ok(gint sock, gchar *argbuf)
                        if (argbuf)
                                strcpy(argbuf, buf);
 
+                       if (!strncmp(buf, "381 ", 4))
+                               return NN_AUTHCONT;
                        return NN_SUCCESS;
-               } else
+               } else if (!strncmp(buf, "480 ", 4))
+                       return NN_AUTHREQ;
+               else
                        return NN_ERROR;
        }
 
@@ -240,8 +266,12 @@ static void nntp_gen_send(gint sock, const gchar *format, ...)
        g_vsnprintf(buf, sizeof(buf), format, args);
        va_end(args);
 
-       if (verbose)
-               log_print("NNTP> %s\n", buf);
+       if (verbose) {
+               if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
+                       log_print("NNTP> AUTHINFO PASS ***\n");
+               else
+                       log_print("NNTP> %s\n", buf);
+       }
 
        strcat(buf, "\r\n");
        sock_write(sock, buf, strlen(buf));
index 8eb9742a32055d7c0877ebe847fc06973d27f993..54bbd090c53e9c6594f000dd6434d6c7182e1fa6 100644 (file)
@@ -27,6 +27,8 @@
 #define NN_SYNTAX      5
 #define NN_IOERR       6
 #define NN_ERROR       7
+#define NN_AUTHREQ     8
+#define NN_AUTHCONT    9
 
 #define NNTPBUFSIZE    8192
 
@@ -40,6 +42,8 @@ gint nntp_head(gint sock, gint num, gchar **msgid);
 gint nntp_stat(gint sock, gint num, gchar **msgid);
 gint nntp_next(gint sock, gint *num, gchar **msgid);
 gint nntp_xover(gint sock, gint first, gint last);
+gint nntp_authinfo_user(gint sock, const gchar *user);
+gint nntp_authinfo_pass(gint sock, const gchar *pass);
 gint nntp_post(gint sock, FILE *fp);
 gint nntp_newgroups(gint sock);
 gint nntp_newnews(gint sock);
index c336dc99cbaa6c56245cc1736882773e0fe74b4a..e4241028e67367e32486e279373cdcbdc5e9b386 100644 (file)
@@ -1259,10 +1259,10 @@ static void prefs_account_protocol_activated(GtkMenuItem *menuitem)
                gtk_widget_hide(basic.smtpserv_label);
                gtk_widget_hide(basic.smtpserv_entry);
                gtk_table_set_row_spacing (GTK_TABLE (basic.serv_table), 3, 0);
-               gtk_widget_set_sensitive(basic.uid_label,  FALSE);
-               gtk_widget_set_sensitive(basic.pass_label, FALSE);
-               gtk_widget_set_sensitive(basic.uid_entry,  FALSE);
-               gtk_widget_set_sensitive(basic.pass_entry, FALSE);
+               gtk_widget_set_sensitive(basic.uid_label,  TRUE);
+               gtk_widget_set_sensitive(basic.pass_label, TRUE);
+               gtk_widget_set_sensitive(basic.uid_entry,  TRUE);
+               gtk_widget_set_sensitive(basic.pass_entry, TRUE);
                gtk_widget_set_sensitive(receive.pop3_frame, FALSE);
                break;
        case A_LOCAL: