Fixed and improved NNTP authentication support.
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <dirent.h>
29 #include <unistd.h>
30
31 #include "defs.h"
32 #include "intl.h"
33 #include "news.h"
34 #include "nntp.h"
35 #include "socket.h"
36 #include "recv.h"
37 #include "procmsg.h"
38 #include "procheader.h"
39 #include "folder.h"
40 #include "session.h"
41 #include "statusbar.h"
42 #include "codeconv.h"
43 #include "utils.h"
44 #include "prefs_common.h"
45 #include "prefs_account.h"
46 #include "inputdialog.h"
47 #include "alertpanel.h"
48
49 static gint news_get_article_cmd         (Folder        *folder,
50                                           NNTPSession   *session,
51                                           const gchar   *cmd,
52                                           gint           num,
53                                           gchar         *filename);
54 static gint news_get_article             (Folder        *folder,
55                                           NNTPSession   *session,
56                                           gint           num,
57                                           gchar         *filename);
58 static gint news_get_header              (Folder        *folder,
59                                           NNTPSession   *session,
60                                           gint           num,
61                                           gchar         *filename);
62
63 static GSList *news_get_uncached_articles(NNTPSession   *session,
64                                           FolderItem    *item,
65                                           gint           cache_last,
66                                           gint          *rfirst,
67                                           gint          *rlast);
68 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
69 static GSList *news_delete_old_article   (GSList        *alist,
70                                           gint           first);
71 static void news_delete_all_article      (FolderItem    *item);
72
73 static gchar *news_query_password        (NNTPSession   *session,
74                                           const gchar   *user);
75 static gint news_authenticate            (Folder        *folder);
76 static gint news_nntp_group              (Folder        *folder,
77                                           SockInfo      *sock,
78                                           const gchar   *group,
79                                           gint          *num,
80                                           gint          *first,
81                                           gint          *last);
82 static gint news_nntp_list               (Folder        *folder,
83                                           SockInfo      *sock);
84 static gint news_nntp_xover              (Folder        *folder,
85                                           SockInfo      *sock,
86                                           gint           first,
87                                           gint           last);
88 static gint news_nntp_post               (Folder        *folder,
89                                           SockInfo      *sock,
90                                           FILE          *fp);
91 static gint news_nntp_mode               (Folder        *folder,
92                                           SockInfo      *sock,
93                                           gboolean       stream);
94
95 Session *news_session_new(const gchar *server, gushort port)
96 {
97         gchar buf[NNTPBUFSIZE];
98         NNTPSession *session;
99         SockInfo *nntp_sock;
100
101         g_return_val_if_fail(server != NULL, NULL);
102
103         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
104
105         if ((nntp_sock = nntp_open(server, port, buf)) < 0)
106                 return NULL;
107
108         session = g_new(NNTPSession, 1);
109         SESSION(session)->type      = SESSION_NEWS;
110         SESSION(session)->server    = g_strdup(server);
111         SESSION(session)->sock      = nntp_sock;
112         SESSION(session)->connected = TRUE;
113         SESSION(session)->phase     = SESSION_READY;
114         SESSION(session)->data      = NULL;
115         session->group = NULL;
116
117         return SESSION(session);
118 }
119
120 void news_session_destroy(NNTPSession *session)
121 {
122         sock_close(SESSION(session)->sock);
123         SESSION(session)->sock = NULL;
124
125         g_free(session->group);
126 }
127
128 NNTPSession *news_session_get(Folder *folder)
129 {
130         g_return_val_if_fail(folder != NULL, NULL);
131         g_return_val_if_fail(folder->type == F_NEWS, NULL);
132         g_return_val_if_fail(folder->account != NULL, NULL);
133
134         if (!REMOTE_FOLDER(folder)->session) {
135                 REMOTE_FOLDER(folder)->session =
136                         news_session_new(folder->account->nntp_server, 119);
137         } else {
138                 if (news_nntp_mode(folder,
139                                    REMOTE_FOLDER(folder)->session->sock,
140                                    FALSE)
141                     != NN_SUCCESS) {
142                         log_warning(_("NNTP connection to %s:%d has been"
143                                       " disconnected. Reconnecting...\n"),
144                                     folder->account->nntp_server, 119);
145                         session_destroy(REMOTE_FOLDER(folder)->session);
146                         REMOTE_FOLDER(folder)->session =
147                                 news_session_new(folder->account->nntp_server,
148                                                  119);
149                 }
150         }
151
152         return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
153 }
154
155 GSList *news_get_article_list(Folder *folder, FolderItem *item,
156                               gboolean use_cache)
157 {
158         GSList *alist;
159         NNTPSession *session;
160
161         g_return_val_if_fail(folder != NULL, NULL);
162         g_return_val_if_fail(item != NULL, NULL);
163         g_return_val_if_fail(folder->type == F_NEWS, NULL);
164
165         session = news_session_get(folder);
166
167         if (!session) {
168                 alist = procmsg_read_cache(item, FALSE);
169                 item->last_num = procmsg_get_last_num_in_cache(alist);
170         } else if (use_cache) {
171                 GSList *newlist;
172                 gint cache_last;
173                 gint first, last;
174
175                 alist = procmsg_read_cache(item, FALSE);
176
177                 cache_last = procmsg_get_last_num_in_cache(alist);
178                 newlist = news_get_uncached_articles
179                         (session, item, cache_last, &first, &last);
180                 alist = news_delete_old_article(alist, first);
181
182                 alist = g_slist_concat(alist, newlist);
183                 item->last_num = last;
184         } else {
185                 gint last;
186
187                 alist = news_get_uncached_articles
188                         (session, item, 0, NULL, &last);
189                 news_delete_all_article(item);
190                 item->last_num = last;
191         }
192
193         procmsg_set_flags(alist, item);
194
195         statusbar_pop_all();
196
197         return alist;
198 }
199
200 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
201 {
202         gchar *path, *filename;
203         gint ok;
204
205         g_return_val_if_fail(folder != NULL, NULL);
206         g_return_val_if_fail(item != NULL, NULL);
207
208         path = folder_item_get_path(item);
209         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
210         g_free(path);
211
212         if (is_file_exist(filename)) {
213                 debug_print(_("article %d has been already cached.\n"), num);
214                 return filename;
215         }
216
217         if (!REMOTE_FOLDER(folder)->session) {
218                 g_free(filename);
219                 return NULL;
220         }
221
222         debug_print(_("getting article %d...\n"), num);
223         ok = news_get_article(folder,
224                               NNTP_SESSION(REMOTE_FOLDER(folder)->session),
225                               num, filename);
226         statusbar_pop_all();
227         if (ok < 0) {
228                 g_warning(_("can't read article %d\n"), num);
229                 g_free(filename);
230                 return NULL;
231         }
232
233         return filename;
234 }
235
236 void news_scan_group(Folder *folder, FolderItem *item)
237 {
238 }
239
240 gint news_post(Folder *folder, const gchar *file)
241 {
242         NNTPSession *session;
243         FILE *fp;
244         gint ok;
245
246         g_return_val_if_fail(folder != NULL, -1);
247         g_return_val_if_fail(folder->type == F_NEWS, -1);
248         g_return_val_if_fail(file != NULL, -1);
249
250         session = news_session_get(folder);
251         if (!session) return -1;
252
253         if ((fp = fopen(file, "r")) == NULL) {
254                 FILE_OP_ERROR(file, "fopen");
255                 return -1;
256         }
257
258         ok = news_nntp_post(folder, SESSION(session)->sock, fp);
259         if (ok != NN_SUCCESS) {
260                 log_warning(_("can't post article.\n"));
261                 return -1;
262         }
263
264         fclose(fp);
265
266         statusbar_pop_all();
267
268         return 0;
269 }
270
271 static gint news_get_article_cmd(Folder *folder, NNTPSession *session,
272                                  const gchar *cmd,
273                                  gint num, gchar *filename)
274 {
275         gint ok;
276         gchar *msgid;
277
278         ok = nntp_get_article(SESSION(session)->sock, cmd, num, &msgid);
279         if (ok == NN_AUTHREQ) {
280                 ok = news_authenticate(folder);
281                 if (ok != NN_SUCCESS)
282                         return -1;
283                 ok = nntp_get_article(SESSION(session)->sock, cmd, num,
284                                       &msgid);
285         }
286         if (ok != NN_SUCCESS)
287                 return -1;
288
289         debug_print("Message-Id = %s, num = %d\n", msgid, num);
290         g_free(msgid);
291
292         if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
293                 log_warning(_("can't retrieve article %d\n"), num);
294                 return -1;
295         }
296
297         return 0;
298 }
299
300 static gint news_get_article(Folder *folder, NNTPSession *session,
301                              gint num, gchar *filename)
302 {
303         return news_get_article_cmd(folder, session, "ARTICLE", num, filename);
304 }
305
306 static gint news_get_header(Folder *folder, NNTPSession *session, 
307                             gint num, gchar *filename)
308 {
309         return news_get_article_cmd(folder, session, "HEAD", num, filename);
310 }
311
312 static GSList *news_get_uncached_articles(NNTPSession *session,
313                                           FolderItem *item, gint cache_last,
314                                           gint *rfirst, gint *rlast)
315 {
316         gint ok;
317         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
318         gchar buf[NNTPBUFSIZE];
319         GSList *newlist = NULL;
320         GSList *llast = NULL;
321         MsgInfo *msginfo;
322
323         if (rfirst) *rfirst = 0;
324         if (rlast)  *rlast  = 0;
325
326         g_return_val_if_fail(session != NULL, NULL);
327         g_return_val_if_fail(item != NULL, NULL);
328         g_return_val_if_fail(item->folder != NULL, NULL);
329         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
330
331         ok = news_nntp_group(item->folder, SESSION(session)->sock, item->path,
332                              &num, &first, &last);
333         if (ok != NN_SUCCESS) {
334                 log_warning(_("can't set group: %s\n"), item->path);
335                 return NULL;
336         }
337
338         /* calculate getting overview range */
339         if (first > last) {
340                 log_warning(_("invalid article range: %d - %d\n"),
341                             first, last);
342                 return NULL;
343         }
344         if (cache_last < first)
345                 begin = first;
346         else if (last < cache_last)
347                 begin = first;
348         else if (last == cache_last) {
349                 debug_print(_("no new articles.\n"));
350                 return NULL;
351         } else
352                 begin = cache_last + 1;
353         end = last;
354
355         if (prefs_common.max_articles > 0 &&
356             end - begin + 1 > prefs_common.max_articles)
357                 begin = end - prefs_common.max_articles + 1;
358
359         log_message(_("getting xover %d - %d in %s...\n"),
360                     begin, end, item->path);
361         if (news_nntp_xover(item->folder, SESSION(session)->sock,
362                             begin, end) != NN_SUCCESS) {
363                 log_warning(_("can't get xover\n"));
364                 return NULL;
365         }
366
367         for (;;) {
368                 if (sock_read(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
369                         log_warning(_("error occurred while getting xover.\n"));
370                         return newlist;
371                 }
372
373                 if (buf[0] == '.' && buf[1] == '\r') break;
374
375                 msginfo = news_parse_xover(buf);
376                 if (!msginfo) {
377                         log_warning(_("invalid xover line: %s\n"), buf);
378                         continue;
379                 }
380
381                 msginfo->folder = item;
382                 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
383
384                 if (!newlist)
385                         llast = newlist = g_slist_append(newlist, msginfo);
386                 else {
387                         llast = g_slist_append(llast, msginfo);
388                         llast = llast->next;
389                 }
390         }
391
392         if (rfirst) *rfirst = first;
393         if (rlast)  *rlast  = last;
394         return newlist;
395 }
396
397 #define PARSE_ONE_PARAM(p, srcp) \
398 { \
399         p = strchr(srcp, '\t'); \
400         if (!p) return NULL; \
401         else \
402                 *p++ = '\0'; \
403 }
404
405 static MsgInfo *news_parse_xover(const gchar *xover_str)
406 {
407         MsgInfo *msginfo;
408         gchar buf[NNTPBUFSIZE];
409         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
410         gchar *p;
411         gint num, size_int, line_int;
412         gchar *xover_buf;
413
414         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
415         strcpy(xover_buf, xover_str);
416
417         PARSE_ONE_PARAM(subject, xover_buf);
418         PARSE_ONE_PARAM(sender, subject);
419         PARSE_ONE_PARAM(date, sender);
420         PARSE_ONE_PARAM(msgid, date);
421         PARSE_ONE_PARAM(ref, msgid);
422         PARSE_ONE_PARAM(size, ref);
423         PARSE_ONE_PARAM(line, size);
424
425         tmp = strchr(line, '\t');
426         if (!tmp) tmp = strchr(line, '\r');
427         if (!tmp) tmp = strchr(line, '\n');
428         if (tmp) *tmp = '\0';
429
430         num = atoi(xover_str);
431         size_int = atoi(size);
432         line_int = atoi(line);
433
434         /* set MsgInfo */
435         msginfo = g_new0(MsgInfo, 1);
436         msginfo->msgnum = num;
437         msginfo->size = size_int;
438
439         msginfo->date = g_strdup(date);
440         msginfo->date_t = procheader_date_parse(NULL, date, 0);
441
442         conv_unmime_header(buf, sizeof(buf), sender, NULL);
443         msginfo->from = g_strdup(buf);
444         msginfo->fromname = procheader_get_fromname(buf);
445
446         conv_unmime_header(buf, sizeof(buf), subject, NULL);
447         msginfo->subject = g_strdup(buf);
448
449         extract_parenthesis(msgid, '<', '>');
450         remove_space(msgid);
451         if (*msgid != '\0')
452                 msginfo->msgid = g_strdup(msgid);
453
454         eliminate_parenthesis(ref, '(', ')');
455         if ((p = strrchr(ref, '<')) != NULL) {
456                 extract_parenthesis(p, '<', '>');
457                 remove_space(p);
458                 if (*p != '\0')
459                         msginfo->inreplyto = g_strdup(p);
460         }
461
462         return msginfo;
463 }
464
465 static GSList *news_delete_old_article(GSList *alist, gint first)
466 {
467         GSList *cur, *next;
468         MsgInfo *msginfo;
469         gchar *cache_file;
470
471         if (first < 2) return alist;
472
473         for (cur = alist; cur != NULL; ) {
474                 next = cur->next;
475
476                 msginfo = (MsgInfo *)cur->data;
477                 if (msginfo && msginfo->msgnum < first) {
478                         debug_print(_("deleting article %d...\n"),
479                                     msginfo->msgnum);
480
481                         cache_file = procmsg_get_message_file_path(msginfo);
482                         if (is_file_exist(cache_file)) unlink(cache_file);
483                         g_free(cache_file);
484
485                         procmsg_msginfo_free(msginfo);
486                         alist = g_slist_remove(alist, msginfo);
487                 }
488
489                 cur = next;
490         }
491
492         return alist;
493 }
494
495 static void news_delete_all_article(FolderItem *item)
496 {
497         DIR *dp;
498         struct dirent *d;
499         gchar *dir;
500         gchar *file;
501
502         dir = folder_item_get_path(item);
503         if ((dp = opendir(dir)) == NULL) {
504                 FILE_OP_ERROR(dir, "opendir");
505                 g_free(dir);
506                 return;
507         }
508
509         debug_print(_("\tDeleting all cached articles... "));
510
511         while ((d = readdir(dp)) != NULL) {
512                 if (to_number(d->d_name) < 0) continue;
513
514                 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
515
516                 if (is_file_exist(file)) {
517                         if (unlink(file) < 0)
518                                 FILE_OP_ERROR(file, "unlink");
519                 }
520
521                 g_free(file);
522         }
523
524         closedir(dp);
525         g_free(dir);
526
527         debug_print(_("done.\n"));
528 }
529
530 /*
531   news_get_group_list returns a strings list.
532   These strings are the names of the newsgroups of a server.
533   item is the FolderItem of the news server.
534   The names of the newsgroups are cached into a file so that
535   when the function is called again, there is no need to make
536   a request to the server.
537  */
538
539 GSList * news_get_group_list(FolderItem *item)
540 {
541         gchar *path, *filename;
542         gint ok;
543         NNTPSession *session;
544         GSList * group_list = NULL;
545         FILE * f;
546         gchar buf[NNTPBUFSIZE];
547         int len;
548
549         if (item == NULL)
550           return NULL;
551
552         path = folder_item_get_path(item);
553
554         if (!is_dir_exist(path))
555                 make_dir_hier(path);
556
557         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
558         g_free(path);
559
560         session = news_session_get(item->folder);
561
562         if (session == NULL)
563           return NULL;
564
565         if (is_file_exist(filename)) {
566                 debug_print(_("group list has been already cached.\n"));
567         }
568         else {
569             ok = news_nntp_list(item->folder, SESSION(session)->sock);
570             if (ok != NN_SUCCESS)
571               return NULL;
572             
573             if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
574               log_warning(_("can't retrieve group list\n"));
575               return NULL;
576             }
577         }
578
579         f = fopen(filename, "r");
580         while (fgets(buf, NNTPBUFSIZE, f)) {
581             char * s;
582
583             len = 0;
584             while ((buf[len] != 0) && (buf[len] != ' '))
585               len++;
586             buf[len] = 0;
587             s = g_strdup(buf);
588
589             group_list = g_slist_append(group_list, s);
590         }
591         fclose(f);
592         g_free(filename);
593
594         group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
595
596         return group_list;
597 }
598
599 /*
600   remove the cache file of the names of the newsgroups.
601  */
602
603 void news_reset_group_list(FolderItem *item)
604 {
605         gchar *path, *filename;
606
607         debug_print(_("\tDeleting cached group list... "));
608         path = folder_item_get_path(item);
609         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
610         g_free(path);
611         if (remove(filename) != 0)
612           log_warning(_("can't delete cached group list %s\n"), filename);
613         g_free(filename);
614 }
615
616 \f
617 /* NNTP Authentication support (RFC2980). */
618
619
620 /**
621  * news_query_password:
622  * @session: Active NNTP session.
623  * @user: User name.
624  * 
625  * Ask user for the NNTP authentication password.
626  * 
627  * Return value: Password string; needs to be freed with g_free()
628  * after use.
629  **/
630 static gchar *news_query_password(NNTPSession *session,
631                                   const gchar *user)
632 {
633         gchar *message;
634         gchar *pass;
635
636         message = g_strdup_printf
637                 (_("Input password for %s on %s:"),
638                  user,
639                  SESSION(session)->server);
640
641         pass = input_dialog_with_invisible(_("Input password"),
642                                            message, NULL);
643         g_free(message);
644 /*      manage_window_focus_in(inc_dialog->mainwin->window, */
645 /*                             NULL, NULL); */
646         return pass;
647 }
648
649 /**
650  * news_authenticate:
651  * @folder: NNTP folder.
652  * 
653  * Send NNTP authentication information (AUTHINFO USER, AUTHINFO PASS)
654  * as described in RFC2980.  May ask user for the password if it is
655  * not stored in account settings.
656  * 
657  * Return value: NNTP result code (NN_*).
658  **/
659 static gint news_authenticate(Folder *folder)
660 {
661         NNTPSession *session;
662         gint ok;
663         const gchar *user;
664         gchar *pass;
665         gboolean need_free_pass = FALSE;
666
667         debug_print(_("news server requested authentication\n"));
668         if (!folder || !folder->account)
669                 return NN_ERROR;
670         user = folder->account->userid;
671         if (!user)
672                 return NN_ERROR;
673         session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
674         if (!session)
675                 return NN_ERROR;
676         ok = nntp_authinfo_user(SESSION(session)->sock, user);
677         if (ok == NN_AUTHCONT) {
678                 pass = folder->account->passwd;
679                 if (!pass || !pass[0]) {
680                         pass = news_query_password(session, user);
681                         need_free_pass = TRUE;
682                 }
683                 ok = nntp_authinfo_pass(SESSION(session)->sock, pass);
684                 if (need_free_pass)
685                         g_free(pass);
686         }
687         if (ok != NN_SUCCESS) {
688                 log_warning(_("NNTP authentication failed\n"));
689                 alertpanel_error(_("Authentication for %s on %s failed."),
690                                  user, SESSION(session)->server);
691         }
692         return ok;
693 }
694
695
696 /**
697  * news_nntp_group:
698  * @folder: NNTP folder.
699  * @sock: NNTP socket.
700  * @group: Group name to select.
701  * @num: Pointer to returned number of messages in the group.
702  * @first: Pointer to returned number of first message.
703  * @last: Pointer to returned number of last message.
704  * 
705  * Wrapper for nntp_group() to do authentication if requested.
706  * Selects newsgroup @group and returns information from the server
707  * response in @num, @first, @last (which cannot be NULL).
708  * 
709  * Return value: NNTP result code (NN_*).
710  **/
711 static gint news_nntp_group(Folder *folder, SockInfo *sock,
712                             const gchar *group,
713                             gint *num, gint *first, gint *last)
714 {
715         gint ok;
716
717         if ((ok = nntp_group(sock, group, num, first, last)) == NN_AUTHREQ) {
718                 ok = news_authenticate(folder);
719                 if (ok != NN_SUCCESS)
720                         return ok;
721                 ok = nntp_group(sock, group, num, first, last);
722         }
723         return ok;
724 }
725
726 /**
727  * news_nntp_list:
728  * @folder: NNTP folder.
729  * @sock: NNTP socket.
730  * 
731  * Wrapper for nntp_list() to do authentication if requested.  Sends
732  * the LIST command to @sock and receives the status code line.
733  * 
734  * Return value: NNTP result code (NN_*).
735  **/
736 static gint news_nntp_list(Folder *folder, SockInfo *sock)
737 {
738         gint ok;
739
740         if ((ok = nntp_list(sock)) == NN_AUTHREQ) {
741                 ok = news_authenticate(folder);
742                 if (ok != NN_SUCCESS)
743                         return ok;
744                 ok = nntp_list(sock);
745         }
746         return ok;
747 }
748
749 /**
750  * news_nntp_xover:
751  * @folder: NNTP folder.
752  * @sock: NNTP socket.
753  * @first: First message number.
754  * @last: Last message number.
755  * 
756  * Wrapper for nntp_xover() to do authentication if requested.  Sends
757  * the XOVER command with parameters @first and @last to @sock and
758  * receives the status code line.
759  * 
760  * Return value: NNTP result code (NN_*).
761  **/
762 static gint news_nntp_xover(Folder *folder, SockInfo *sock,
763                             gint first, gint last)
764 {
765         gint ok;
766
767         if ((ok = nntp_xover(sock, first, last)) == NN_AUTHREQ) {
768                 ok = news_authenticate(folder);
769                 if (ok != NN_SUCCESS)
770                         return ok;
771                 ok = nntp_xover(sock, first, last);
772         }
773         return ok;
774 }
775
776 /**
777  * news_nntp_post:
778  * @folder: NNTP folder.
779  * @sock: NNTP socket.
780  * @fp: File with the message (header and body), opened for reading.
781  * 
782  * Wrapper for nntp_post() to do authentication if requested.  Sends
783  * the POST command to @sock, and if the server accepts it, sends the
784  * message from @fp.
785  * 
786  * Return value: NNTP result code (NN_*).
787  **/
788 static gint news_nntp_post(Folder *folder, SockInfo *sock, FILE *fp)
789 {
790         gint ok;
791
792         if ((ok = nntp_post(sock, fp)) == NN_AUTHREQ) {
793                 ok = news_authenticate(folder);
794                 if (ok != NN_SUCCESS)
795                         return ok;
796                 ok = nntp_post(sock, fp);
797         }
798         return ok;
799 }
800
801 /**
802  * news_nntp_mode:
803  * @folder: NNTP folder.
804  * @sock: NNTP socket.
805  * @stream: TRUE for stream mode, FALSE for reader mode.
806  * 
807  * Wrapper for nntp_mode() to do authentication if requested.  Sends
808  * the "MODE READER" or "MODE STREAM" command to @sock, depending on
809  * @stream, and receives the status responce.
810  * 
811  * Return value: NNTP result code (NN_*).
812  **/
813 static gint news_nntp_mode(Folder *folder, SockInfo *sock, gboolean stream)
814 {
815         gint ok;
816
817         if ((ok = nntp_mode(sock, stream)) == NN_AUTHREQ) {
818                 ok = news_authenticate(folder);
819                 if (ok != NN_SUCCESS)
820                         return ok;
821                 ok = nntp_mode(sock, stream);
822         }
823         return ok;
824 }