2008-07-05 [colin] 3.5.0cvs11
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #ifdef HAVE_LIBETPAN
25
26 #include "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <dirent.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <libetpan/libetpan.h>
37
38 #include "nntp-thread.h"
39 #include "news.h"
40 #include "news_gtk.h"
41 #include "socket.h"
42 #include "recv.h"
43 #include "procmsg.h"
44 #include "procheader.h"
45 #include "folder.h"
46 #include "session.h"
47 #include "statusbar.h"
48 #include "codeconv.h"
49 #include "utils.h"
50 #include "prefs_common.h"
51 #include "prefs_account.h"
52 #include "inputdialog.h"
53 #include "log.h"
54 #include "progressindicator.h"
55 #include "remotefolder.h"
56 #include "alertpanel.h"
57 #include "inc.h"
58 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
59 #  include "ssl.h"
60 #endif
61
62 #define NNTP_PORT       119
63 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
64 #define NNTPS_PORT      563
65 #endif
66
67 typedef struct _NewsFolder      NewsFolder;
68 typedef struct _NewsSession     NewsSession;
69
70 #define NEWS_FOLDER(obj)        ((NewsFolder *)obj)
71 #define NEWS_SESSION(obj)       ((NewsSession *)obj)
72
73 struct _NewsFolder
74 {
75         RemoteFolder rfolder;
76
77         gboolean use_auth;
78         gboolean lock_count;
79         guint refcnt;
80 };
81
82 struct _NewsSession
83 {
84         Session session;
85
86         gchar *group;
87 };
88
89 static void news_folder_init(Folder *folder, const gchar *name,
90                              const gchar *path);
91
92 static Folder   *news_folder_new        (const gchar    *name,
93                                          const gchar    *folder);
94 static void      news_folder_destroy    (Folder         *folder);
95
96 static gchar *news_fetch_msg            (Folder         *folder,
97                                          FolderItem     *item,
98                                          gint            num);
99 static void news_remove_cached_msg      (Folder         *folder, 
100                                          FolderItem     *item, 
101                                          MsgInfo        *msginfo);
102 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
103 static Session *news_session_new         (Folder        *folder,
104                                           const gchar   *server,
105                                           gushort        port,
106                                           const gchar   *userid,
107                                           const gchar   *passwd,
108                                           SSLType        ssl_type);
109 #else
110 static Session *news_session_new         (Folder        *folder,
111                                           const gchar   *server,
112                                           gushort        port,
113                                           const gchar   *userid,
114                                           const gchar   *passwd);
115 #endif
116
117 static gint news_get_article             (Folder        *folder,
118                                           gint           num,
119                                           gchar         *filename);
120
121 static gint news_select_group            (Folder        *folder,
122                                           const gchar   *group,
123                                           gint          *num,
124                                           gint          *first,
125                                           gint          *last);
126 static MsgInfo *news_parse_xover         (struct newsnntp_xover_resp_item *item);
127 static gchar *news_parse_xhdr            (clist         *list,
128                                           MsgInfo       *msginfo);
129 static gint news_get_num_list                    (Folder        *folder, 
130                                           FolderItem    *item,
131                                           GSList       **list,
132                                           gboolean      *old_uids_valid);
133 static MsgInfo *news_get_msginfo                 (Folder        *folder, 
134                                           FolderItem    *item,
135                                           gint           num);
136 static GSList *news_get_msginfos                 (Folder        *folder,
137                                           FolderItem    *item,
138                                           GSList        *msgnum_list);
139 static gboolean news_scan_required               (Folder        *folder,
140                                           FolderItem    *item);
141
142 static gchar *news_folder_get_path       (Folder        *folder);
143 static gchar *news_item_get_path                 (Folder        *folder,
144                                           FolderItem    *item);
145 static void news_synchronise             (FolderItem    *item, gint days);
146 static int news_remove_msg               (Folder        *folder, 
147                                           FolderItem    *item, 
148                                           gint           msgnum);
149 static gint news_rename_folder           (Folder *folder,
150                                           FolderItem *item,
151                                           const gchar *name);
152 static gint news_remove_folder           (Folder        *folder,
153                                           FolderItem    *item);
154 static FolderClass news_class;
155
156 FolderClass *news_get_class(void)
157 {
158         if (news_class.idstr == NULL) {
159                 news_class.type = F_NEWS;
160                 news_class.idstr = "news";
161                 news_class.uistr = "News";
162
163                 /* Folder functions */
164                 news_class.new_folder = news_folder_new;
165                 news_class.destroy_folder = news_folder_destroy;
166
167                 /* FolderItem functions */
168                 news_class.item_get_path = news_item_get_path;
169                 news_class.get_num_list = news_get_num_list;
170                 news_class.scan_required = news_scan_required;
171                 news_class.rename_folder = news_rename_folder;
172                 news_class.remove_folder = news_remove_folder;
173
174                 /* Message functions */
175                 news_class.get_msginfo = news_get_msginfo;
176                 news_class.get_msginfos = news_get_msginfos;
177                 news_class.fetch_msg = news_fetch_msg;
178                 news_class.synchronise = news_synchronise;
179                 news_class.remove_msg = news_remove_msg;
180                 news_class.remove_cached_msg = news_remove_cached_msg;
181         };
182
183         return &news_class;
184 }
185
186 guint nntp_folder_get_refcnt(Folder *folder)
187 {
188         return ((NewsFolder *)folder)->refcnt;
189 }
190
191 void nntp_folder_ref(Folder *folder)
192 {
193         ((NewsFolder *)folder)->refcnt++;
194 }
195
196 void nntp_folder_unref(Folder *folder)
197 {
198         if (((NewsFolder *)folder)->refcnt > 0)
199                 ((NewsFolder *)folder)->refcnt--;
200 }
201
202 static int news_remove_msg               (Folder        *folder, 
203                                           FolderItem    *item, 
204                                           gint           msgnum)
205 {
206         gchar *path, *filename;
207
208         g_return_val_if_fail(folder != NULL, -1);
209         g_return_val_if_fail(item != NULL, -1);
210
211         path = folder_item_get_path(item);
212         if (!is_dir_exist(path))
213                 make_dir_hier(path);
214         
215         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
216         g_free(path);
217         claws_unlink(filename);
218         g_free(filename);
219         return 0;
220 }
221
222 static void news_folder_lock(NewsFolder *folder)
223 {
224         folder->lock_count++;
225 }
226
227 static void news_folder_unlock(NewsFolder *folder)
228 {
229         if (folder->lock_count > 0)
230                 folder->lock_count--;
231 }
232
233 int news_folder_locked(Folder *folder)
234 {
235         if (folder == NULL)
236                 return 0;
237
238         return NEWS_FOLDER(folder)->lock_count;
239 }
240
241 static Folder *news_folder_new(const gchar *name, const gchar *path)
242 {
243         Folder *folder;
244
245         folder = (Folder *)g_new0(NewsFolder, 1);
246         folder->klass = &news_class;
247         news_folder_init(folder, name, path);
248
249         return folder;
250 }
251
252 static void news_folder_destroy(Folder *folder)
253 {
254         gchar *dir;
255
256         while (nntp_folder_get_refcnt(folder) > 0)
257                 gtk_main_iteration();
258
259         dir = news_folder_get_path(folder);
260         if (is_dir_exist(dir))
261                 remove_dir_recursive(dir);
262         g_free(dir);
263
264         nntp_done(folder);
265         folder_remote_folder_destroy(REMOTE_FOLDER(folder));
266 }
267
268 static void news_folder_init(Folder *folder, const gchar *name,
269                              const gchar *path)
270 {
271         folder_remote_folder_init(folder, name, path);
272 }
273
274 static void news_session_destroy(Session *session)
275 {
276         NewsSession *news_session = NEWS_SESSION(session);
277
278         g_return_if_fail(session != NULL);
279
280         if (news_session->group)
281                 g_free(news_session->group);
282 }
283
284 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
285 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
286                                  const gchar *userid, const gchar *passwd,
287                                  SSLType ssl_type)
288 #else
289 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
290                                  const gchar *userid, const gchar *passwd)
291 #endif
292 {
293         NewsSession *session;
294         int r = 0;
295         g_return_val_if_fail(server != NULL, NULL);
296
297         log_message(LOG_PROTOCOL, _("creating NNTP connection to %s:%d ...\n"), server, port);
298
299         session = g_new0(NewsSession, 1);
300         session_init(SESSION(session), folder->account, FALSE);
301         SESSION(session)->type             = SESSION_NEWS;
302         SESSION(session)->server           = g_strdup(server);
303         SESSION(session)->sock             = NULL;
304         SESSION(session)->destroy          = news_session_destroy;
305         
306         nntp_init(folder);
307
308 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
309         if (ssl_type != SSL_NONE)
310                 r = nntp_threaded_connect_ssl(folder, server, port);
311         else
312 #endif
313                 r = nntp_threaded_connect(folder, server, port);
314         
315         if (r != NEWSNNTP_NO_ERROR) {
316                 log_error(LOG_PROTOCOL, _("Error logging in to %s:%d ...\n"), server, port);
317                 session_destroy(SESSION(session));
318                 return NULL;
319         }
320         
321         return SESSION(session);
322 }
323
324 static Session *news_session_new_for_folder(Folder *folder)
325 {
326         Session *session;
327         PrefsAccount *ac;
328         const gchar *userid = NULL;
329         gchar *passwd = NULL;
330         gushort port;
331
332         g_return_val_if_fail(folder != NULL, NULL);
333         g_return_val_if_fail(folder->account != NULL, NULL);
334
335         ac = folder->account;
336         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
337                 userid = ac->userid;
338                 if (ac->passwd && ac->passwd[0])
339                         passwd = g_strdup(ac->passwd);
340                 else
341                         passwd = input_dialog_query_password(ac->nntp_server,
342                                                              userid);
343         }
344
345 #if (defined(USE_OPENSSL) || defined (USE_GNUTLS))
346         port = ac->set_nntpport ? ac->nntpport
347                 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
348         session = news_session_new(folder, ac->nntp_server, port, userid, passwd,
349                                    ac->ssl_nntp);
350 #else
351         if (ac->ssl_nntp != SSL_NONE) {
352                 if (alertpanel_full(_("Insecure connection"),
353                         _("This connection is configured to be secured "
354                           "using SSL, but SSL is not available in this "
355                           "build of Claws Mail. \n\n"
356                           "Do you want to continue connecting to this "
357                           "server? The communication would not be "
358                           "secure."),
359                           GTK_STOCK_CANCEL, _("Con_tinue connecting"), 
360                           NULL, FALSE, NULL, ALERT_WARNING,
361                           G_ALERTDEFAULT) != G_ALERTALTERNATE)
362                         return NULL;
363         }
364         port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
365         session = news_session_new(folder, ac->nntp_server, port, userid, passwd);
366 #endif
367
368         if ((session != NULL) && ac->use_nntp_auth) { /* FIXME:  && ac->use_nntp_auth_onconnect */
369                 if (nntp_threaded_login(folder, userid, passwd) !=
370                         NEWSNNTP_NO_ERROR) {
371                         log_error(LOG_PROTOCOL, _("Error authenticating to %s:%d ...\n"), ac->nntp_server, port);
372                         session_destroy(SESSION(session));
373                         g_free(passwd);
374                         return NULL;
375                 }
376         }
377         g_free(passwd);
378
379         return session;
380 }
381
382 static NewsSession *news_session_get(Folder *folder)
383 {
384         RemoteFolder *rfolder = REMOTE_FOLDER(folder);
385         struct tm lt;
386         int r;
387         
388         g_return_val_if_fail(folder != NULL, NULL);
389         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
390         g_return_val_if_fail(folder->account != NULL, NULL);
391
392         if (prefs_common.work_offline && 
393             !inc_offline_should_override(FALSE,
394                 _("Claws Mail needs network access in order "
395                   "to access the News server."))) {
396                 return NULL;
397         }
398
399         if (!rfolder->session) {
400                 rfolder->session = news_session_new_for_folder(folder);
401                 return NEWS_SESSION(rfolder->session);
402         }
403
404         if (time(NULL) - rfolder->session->last_access_time <
405                 SESSION_TIMEOUT_INTERVAL) {
406                 return NEWS_SESSION(rfolder->session);
407         }
408
409         if ((r = nntp_threaded_date(folder, &lt)) != NEWSNNTP_NO_ERROR) {
410                 if (r != NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED &&
411                     r != NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD) {
412                         log_warning(LOG_PROTOCOL, _("NNTP connection to %s:%d has been"
413                               " disconnected. Reconnecting...\n"),
414                             folder->account->nntp_server,
415                             folder->account->set_nntpport ?
416                             folder->account->nntpport : NNTP_PORT);
417                         session_destroy(rfolder->session);
418                         rfolder->session = news_session_new_for_folder(folder);
419                 }
420         }
421         
422         if (rfolder->session)
423                 session_set_access_time(rfolder->session);
424
425         return NEWS_SESSION(rfolder->session);
426 }
427
428 static void news_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
429 {
430         gchar *path, *filename;
431
432         path = folder_item_get_path(item);
433
434         if (!is_dir_exist(path)) {
435                 g_free(path);
436                 return;
437         }
438
439         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
440         g_free(path);
441
442         if (is_file_exist(filename)) {
443                 claws_unlink(filename);
444         }
445         g_free(filename);
446 }
447
448 static gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
449 {
450         gchar *path, *filename;
451         NewsSession *session;
452         gint ok;
453
454         g_return_val_if_fail(folder != NULL, NULL);
455         g_return_val_if_fail(item != NULL, NULL);
456
457         path = folder_item_get_path(item);
458         if (!is_dir_exist(path))
459                 make_dir_hier(path);
460         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
461         g_free(path);
462
463         if (is_file_exist(filename)) {
464                 debug_print("article %d has been already cached.\n", num);
465                 return filename;
466         }
467
468         session = news_session_get(folder);
469         if (!session) {
470                 g_free(filename);
471                 return NULL;
472         }
473
474         ok = news_select_group(folder, item->path, NULL, NULL, NULL);
475         if (ok != NEWSNNTP_NO_ERROR) {
476                 if (ok == NEWSNNTP_ERROR_STREAM) {
477                         session_destroy(SESSION(session));
478                         REMOTE_FOLDER(folder)->session = NULL;
479                 }
480                 g_free(filename);
481                 return NULL;
482         }
483
484         debug_print("getting article %d...\n", num);
485         ok = news_get_article(folder,
486                               num, filename);
487         if (ok != NEWSNNTP_NO_ERROR) {
488                 g_warning("can't read article %d\n", num);
489                 if (ok == NEWSNNTP_ERROR_STREAM) {
490                         session_destroy(SESSION(session));
491                         REMOTE_FOLDER(folder)->session = NULL;
492                 }
493                 g_free(filename);
494                 return NULL;
495         }
496         GTK_EVENTS_FLUSH();
497         return filename;
498 }
499
500 static NewsGroupInfo *news_group_info_new(const gchar *name,
501                                           gint first, gint last, gchar type)
502 {
503         NewsGroupInfo *ginfo;
504
505         ginfo = g_new(NewsGroupInfo, 1);
506         ginfo->name = g_strdup(name);
507         ginfo->first = first;
508         ginfo->last = last;
509         ginfo->type = type;
510
511         return ginfo;
512 }
513
514 static void news_group_info_free(NewsGroupInfo *ginfo)
515 {
516         g_free(ginfo->name);
517         g_free(ginfo);
518 }
519
520 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
521                                     NewsGroupInfo *ginfo2)
522 {
523         return g_ascii_strcasecmp(ginfo1->name, ginfo2->name);
524 }
525
526 GSList *news_get_group_list(Folder *folder)
527 {
528         gchar *path, *filename;
529         FILE *fp;
530         GSList *list = NULL;
531         GSList *last = NULL;
532         gchar buf[BUFFSIZE];
533
534         g_return_val_if_fail(folder != NULL, NULL);
535         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
536
537         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
538         if (!is_dir_exist(path))
539                 make_dir_hier(path);
540         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
541         g_free(path);
542
543         if ((fp = g_fopen(filename, "rb")) == NULL) {
544                 NewsSession *session;
545                 gint ok;
546                 clist *grouplist = NULL;
547                 clistiter *cur;
548                 fp = fopen(filename, "wb");
549                 
550                 if (!fp) {
551                         g_free(filename);
552                         return NULL;
553                 }
554                 session = news_session_get(folder);
555                 if (!session) {
556                         fclose(fp);
557                         g_free(filename);
558                         return NULL;
559                 }
560
561                 ok = nntp_threaded_list(folder, &grouplist);
562                 
563                 if (ok != NEWSNNTP_NO_ERROR) {
564                         if (ok == NEWSNNTP_ERROR_STREAM) {
565                                 session_destroy(SESSION(session));
566                                 REMOTE_FOLDER(folder)->session = NULL;
567                         }
568                         fclose(fp);
569                         g_free(filename);
570                         return NULL;
571                 }
572                 
573                 for (cur = clist_begin(grouplist); cur; cur = clist_next(cur)) {
574                         struct newsnntp_group_info *info = (struct newsnntp_group_info *)
575                                                                 clist_content(cur);
576                         if (fprintf(fp, "%s %d %d %c\n",
577                                 info->grp_name,
578                                 info->grp_last,
579                                 info->grp_first,
580                                 info->grp_type) < 0) {
581                                 log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
582                                 session_destroy(SESSION(session));
583                                 REMOTE_FOLDER(folder)->session = NULL;
584                                 fclose(fp);
585                                 g_free(filename);
586                                 newsnntp_list_free(grouplist);
587                                 return NULL;
588                         }
589                 }
590                 newsnntp_list_free(grouplist);
591
592                 if (fclose(fp) == EOF) {
593                         log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
594                         session_destroy(SESSION(session));
595                         REMOTE_FOLDER(folder)->session = NULL;
596                         g_free(filename);
597                         return NULL;
598                 }
599
600                 if ((fp = g_fopen(filename, "rb")) == NULL) {
601                         FILE_OP_ERROR(filename, "fopen");
602                         g_free(filename);
603                         return NULL;
604                 }
605         }
606
607         while (fgets(buf, sizeof(buf), fp) != NULL) {
608                 gchar *p = buf;
609                 gchar *name;
610                 gint last_num;
611                 gint first_num;
612                 gchar type;
613                 NewsGroupInfo *ginfo;
614
615                 p = strchr(p, ' ');
616                 if (!p) continue;
617                 *p = '\0';
618                 p++;
619                 name = buf;
620
621                 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
622                         continue;
623
624                 ginfo = news_group_info_new(name, first_num, last_num, type);
625
626                 if (!last)
627                         last = list = g_slist_append(NULL, ginfo);
628                 else {
629                         last = g_slist_append(last, ginfo);
630                         last = last->next;
631                 }
632         }
633
634         fclose(fp);
635         g_free(filename);
636
637         list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
638
639         return list;
640 }
641
642 void news_group_list_free(GSList *group_list)
643 {
644         GSList *cur;
645
646         if (!group_list) return;
647
648         for (cur = group_list; cur != NULL; cur = cur->next)
649                 news_group_info_free((NewsGroupInfo *)cur->data);
650         g_slist_free(group_list);
651 }
652
653 void news_remove_group_list_cache(Folder *folder)
654 {
655         gchar *path, *filename;
656
657         g_return_if_fail(folder != NULL);
658         g_return_if_fail(FOLDER_CLASS(folder) == &news_class);
659
660         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
661         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
662         g_free(path);
663
664         if (is_file_exist(filename)) {
665                 if (remove(filename) < 0)
666                         FILE_OP_ERROR(filename, "remove");
667         }
668         g_free(filename);
669 }
670
671 gint news_post(Folder *folder, const gchar *file)
672 {
673         gint ok;
674         char *contents = file_read_to_str_no_recode(file);
675         NewsSession *session;
676
677         g_return_val_if_fail(folder != NULL, -1);
678         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
679         g_return_val_if_fail(contents != NULL, -1);
680         
681         session = news_session_get(folder);
682         if (!session)  {
683                 g_free(contents);
684                 return -1;
685         }
686         
687         ok = nntp_threaded_post(folder, contents, strlen(contents));
688
689         if (ok != NEWSNNTP_NO_ERROR && ok != NEWSNNTP_ERROR_STREAM) {
690                 ok = nntp_threaded_mode_reader(folder);
691                 if (ok == NEWSNNTP_NO_ERROR)
692                         ok = nntp_threaded_post(folder, contents, strlen(contents));
693         }
694         g_free(contents);
695
696         if (ok == NEWSNNTP_ERROR_STREAM) {
697                 session_destroy(SESSION(session));
698                 REMOTE_FOLDER(folder)->session = NULL;
699         }
700
701         return (ok == NEWSNNTP_NO_ERROR ? 0 : -1);
702 }
703
704 static gint news_get_article(Folder *folder, gint num, gchar *filename)
705 {
706         size_t len;
707         char *result = NULL;
708         int r;
709         
710         r = nntp_threaded_article(folder, num, &result, &len);
711         
712         if (r == NEWSNNTP_NO_ERROR) {
713                 if (str_write_to_file(result, filename) < 0)
714                         return -1;
715         }
716         
717         g_free(result);
718         return r;
719 }
720
721 /**
722  * news_select_group:
723  * @session: Active NNTP session.
724  * @group: Newsgroup name.
725  * @num: Estimated number of articles.
726  * @first: First article number.
727  * @last: Last article number.
728  *
729  * Select newsgroup @group with the GROUP command if it is not already
730  * selected in @session, or article numbers need to be returned.
731  *
732  * Return value: NNTP result code.
733  **/
734 static gint news_select_group(Folder *folder, const gchar *group,
735                               gint *num, gint *first, gint *last)
736 {
737         gint ok;
738         gint num_, first_, last_;
739         struct newsnntp_group_info *info = NULL;
740         NewsSession *session = NEWS_SESSION(news_session_get(folder));
741
742         g_return_val_if_fail(session != NULL, -1);
743         
744         if (!num || !first || !last) {
745                 if (session->group && g_ascii_strcasecmp(session->group, group) == 0)
746                         return NEWSNNTP_NO_ERROR;
747                 num = &num_;
748                 first = &first_;
749                 last = &last_;
750         }
751
752         g_free(session->group);
753         session->group = NULL;
754
755         ok = nntp_threaded_group(folder, group, &info);
756         
757         if (ok != NEWSNNTP_NO_ERROR && 
758             ok != NEWSNNTP_ERROR_STREAM && 
759             ok != NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME) {
760                 ok = nntp_threaded_mode_reader(folder);
761                 if (ok == NEWSNNTP_NO_ERROR)
762                         ok = nntp_threaded_group(folder, group, &info);
763         }
764
765         if (ok == NEWSNNTP_NO_ERROR && info) {
766                 session->group = g_strdup(group);
767                 *num = info->grp_first;
768                 *first = info->grp_first;
769                 *last = info->grp_last;
770                 newsnntp_group_free(info);
771         } else {
772                 log_warning(LOG_PROTOCOL, _("couldn't select group: %s\n"), group);
773         }
774         return ok;
775 }
776
777 static MsgInfo *news_parse_xover(struct newsnntp_xover_resp_item *item)
778 {
779         MsgInfo *msginfo;
780
781         /* set MsgInfo */
782         msginfo = procmsg_msginfo_new();
783         msginfo->msgnum = item->ovr_article;
784         msginfo->size = item->ovr_size;
785
786         msginfo->date = g_strdup(item->ovr_date);
787         msginfo->date_t = procheader_date_parse(NULL, item->ovr_date, 0);
788
789         msginfo->from = conv_unmime_header(item->ovr_author, NULL);
790         msginfo->fromname = procheader_get_fromname(msginfo->from);
791
792         msginfo->subject = conv_unmime_header(item->ovr_subject, NULL);
793
794         remove_return(msginfo->from);
795         remove_return(msginfo->fromname);
796         remove_return(msginfo->subject);
797
798         if (item->ovr_message_id) {
799                 gchar *tmp = g_strdup(item->ovr_message_id);
800                 extract_parenthesis(tmp, '<', '>');
801                 remove_space(tmp);
802                 if (*tmp != '\0')
803                         msginfo->msgid = g_strdup(tmp);
804                 g_free(tmp);
805         }                        
806
807         /* FIXME: this is a quick fix; references' meaning was changed
808          * into having the actual list of references in the References: header.
809          * We need a GSList here, so msginfo_free() and msginfo_copy() can do 
810          * their things properly. */ 
811         if (item->ovr_references && *(item->ovr_references)) {   
812                 gchar **ref_tokens = g_strsplit(item->ovr_references, " ", -1);
813                 guint i = 0;
814                 char *tmp;
815                 char *p;
816                 while (ref_tokens[i]) {
817                         gchar *cur_ref = ref_tokens[i];
818                         msginfo->references = references_list_append(msginfo->references, 
819                                         cur_ref);
820                         i++;
821                 }
822                 g_strfreev(ref_tokens);
823                 
824                 tmp = g_strdup(item->ovr_references);
825                 eliminate_parenthesis(tmp, '(', ')');
826                 if ((p = strrchr(tmp, '<')) != NULL) {
827                         extract_parenthesis(p, '<', '>');
828                         remove_space(p);
829                         if (*p != '\0')
830                                 msginfo->inreplyto = g_strdup(p);
831                 }
832                 g_free(tmp);
833         } 
834
835         return msginfo;
836 }
837
838 static gchar *news_parse_xhdr(clist *hdrlist, MsgInfo *msginfo)
839 {
840         struct newsnntp_xhdr_resp_item *hdr;
841         
842         hdr = clist_content(clist_begin(hdrlist));
843         if (hdr->hdr_article != msginfo->msgnum)
844                 return NULL;
845         return g_strdup(hdr->hdr_value);
846 }
847
848 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
849 {
850         gchar * tmp;
851         FILE * tmpfp;
852         gchar buf[BUFFSIZE];
853
854         tmp = g_strdup_printf("%s%ccancel%p", get_tmp_dir(),
855                               G_DIR_SEPARATOR, msginfo);
856         if (tmp == NULL)
857                 return -1;
858
859         if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
860                 FILE_OP_ERROR(tmp, "fopen");
861                 return -1;
862         }
863         if (change_file_mode_rw(tmpfp, tmp) < 0) {
864                 FILE_OP_ERROR(tmp, "chmod");
865                 g_warning("can't change file mode\n");
866         }
867         
868         get_rfc822_date(buf, sizeof(buf));
869         if (fprintf(tmpfp, "From: %s\r\n"
870                        "Newsgroups: %s\r\n"
871                        "Subject: cmsg cancel <%s>\r\n"
872                        "Control: cancel <%s>\r\n"
873                        "Approved: %s\r\n"
874                        "X-Cancelled-by: %s\r\n"
875                        "Date: %s\r\n"
876                        "\r\n"
877                        "removed with Claws Mail\r\n",
878                        msginfo->from,
879                        msginfo->newsgroups,
880                        msginfo->msgid,
881                        msginfo->msgid,
882                        msginfo->from,
883                        msginfo->from,
884                        buf) < 0) {
885                 FILE_OP_ERROR(tmp, "fprintf");
886                 fclose(tmpfp);
887                 claws_unlink(tmp);
888                 g_free(tmp);
889                 return -1;
890         }
891
892         if (fclose(tmpfp) == EOF) {
893                 FILE_OP_ERROR(tmp, "fclose");
894                 claws_unlink(tmp);
895                 g_free(tmp);
896                 return -1;
897         }
898
899         news_post(folder, tmp);
900         remove(tmp);
901
902         g_free(tmp);
903
904         return 0;
905 }
906
907 static gchar *news_folder_get_path(Folder *folder)
908 {
909         gchar *folder_path;
910
911         g_return_val_if_fail(folder->account != NULL, NULL);
912
913         folder_path = g_strconcat(get_news_cache_dir(),
914                                   G_DIR_SEPARATOR_S,
915                                   folder->account->nntp_server,
916                                   NULL);
917         return folder_path;
918 }
919
920 static gchar *news_item_get_path(Folder *folder, FolderItem *item)
921 {
922         gchar *folder_path, *path;
923
924         g_return_val_if_fail(folder != NULL, NULL);
925         g_return_val_if_fail(item != NULL, NULL);
926         folder_path = news_folder_get_path(folder);
927
928         g_return_val_if_fail(folder_path != NULL, NULL);
929         if (folder_path[0] == G_DIR_SEPARATOR) {
930                 if (item->path)
931                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
932                                            item->path, NULL);
933                 else
934                         path = g_strdup(folder_path);
935         } else {
936                 if (item->path)
937                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
938                                            folder_path, G_DIR_SEPARATOR_S,
939                                            item->path, NULL);
940                 else
941                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
942                                            folder_path, NULL);
943         }
944         g_free(folder_path);
945
946         return path;
947 }
948
949 static gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
950 {
951         NewsSession *session;
952         gint i, ok, num, first, last, nummsgs = 0;
953         gchar *dir;
954
955         g_return_val_if_fail(item != NULL, -1);
956         g_return_val_if_fail(item->folder != NULL, -1);
957         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
958
959         session = news_session_get(folder);
960         g_return_val_if_fail(session != NULL, -1);
961
962         *old_uids_valid = TRUE;
963         
964         news_folder_lock(NEWS_FOLDER(item->folder));
965
966         ok = news_select_group(folder, item->path, &num, &first, &last);
967         if (ok != NEWSNNTP_NO_ERROR) {
968                 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
969                 news_folder_unlock(NEWS_FOLDER(item->folder));
970                 return -1;
971         }
972
973         dir = news_folder_get_path(folder);
974         if (num <= 0)
975                 remove_all_numbered_files(dir);
976         else if (last < first)
977                 log_warning(LOG_PROTOCOL, _("invalid article range: %d - %d\n"),
978                             first, last);
979         else {
980                 for (i = first; i <= last; i++) {
981                         *msgnum_list = g_slist_prepend(*msgnum_list, 
982                                                        GINT_TO_POINTER(i));
983                         nummsgs++;
984                 }
985                 debug_print("removing old messages from %d to %d in %s\n",
986                             first, last, dir);
987                 remove_numbered_files(dir, 1, first - 1);
988         }
989         g_free(dir);
990         news_folder_unlock(NEWS_FOLDER(item->folder));
991         return nummsgs;
992 }
993
994 static void news_set_msg_flags(FolderItem *item, MsgInfo *msginfo)
995 {
996         msginfo->flags.tmp_flags = 0;
997         if (item->folder->account->mark_crosspost_read && msginfo->msgid) {
998                 if (item->folder->newsart &&
999                     g_hash_table_lookup(item->folder->newsart, msginfo->msgid) != NULL) {
1000                         msginfo->flags.perm_flags = MSG_COLORLABEL_TO_FLAGS(item->folder->account->crosspost_col);
1001                                 
1002                 } else {
1003                         if (!item->folder->newsart) 
1004                                 item->folder->newsart = g_hash_table_new(g_str_hash, g_str_equal);
1005                         g_hash_table_insert(item->folder->newsart,
1006                                         g_strdup(msginfo->msgid), GINT_TO_POINTER(1));
1007                         msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1008                 }
1009         } else {
1010                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1011         }
1012 }
1013
1014 static MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
1015 {
1016         NewsSession *session;
1017         MsgInfo *msginfo = NULL;
1018         gint ok;
1019         struct newsnntp_xover_resp_item *result = NULL;
1020         clist *hdrlist = NULL;
1021
1022         session = news_session_get(folder);
1023         g_return_val_if_fail(session != NULL, NULL);
1024         g_return_val_if_fail(item != NULL, NULL);
1025         g_return_val_if_fail(item->folder != NULL, NULL);
1026         g_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
1027
1028         log_message(LOG_PROTOCOL, _("getting xover %d in %s...\n"),
1029                     num, item->path);
1030         news_folder_lock(NEWS_FOLDER(item->folder));
1031
1032         ok = nntp_threaded_xover(folder, num, num, &result, NULL);
1033         if (ok != NEWSNNTP_NO_ERROR) {
1034                 log_warning(LOG_PROTOCOL, _("couldn't get xover\n"));
1035                 if (ok == NEWSNNTP_ERROR_STREAM) {
1036                         session_destroy(SESSION(session));
1037                         REMOTE_FOLDER(item->folder)->session = NULL;
1038                 }
1039                 news_folder_unlock(NEWS_FOLDER(item->folder));
1040                 return NULL;
1041         }
1042         
1043         msginfo = news_parse_xover(result);
1044         xover_resp_item_free(result);
1045         if (!msginfo) {
1046                 log_warning(LOG_PROTOCOL, _("invalid xover line\n"));
1047         }
1048
1049         if(!msginfo) {
1050                 news_folder_unlock(NEWS_FOLDER(item->folder));
1051                 return NULL;
1052         }
1053
1054         msginfo->folder = item;
1055         
1056         news_set_msg_flags(item, msginfo);
1057         msginfo->flags.tmp_flags |= MSG_NEWS;
1058         msginfo->newsgroups = g_strdup(item->path);
1059
1060         ok = nntp_threaded_xhdr(folder, "to", num, num, &hdrlist);
1061         
1062         if (ok != NEWSNNTP_NO_ERROR) {
1063                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1064                 if (ok == NEWSNNTP_ERROR_STREAM) {
1065                         session_destroy(SESSION(session));
1066                         REMOTE_FOLDER(item->folder)->session = NULL;
1067                 }
1068                 news_folder_unlock(NEWS_FOLDER(item->folder));
1069                 return msginfo;
1070         }
1071
1072         msginfo->to = news_parse_xhdr(hdrlist, msginfo);
1073         newsnntp_xhdr_free(hdrlist);
1074
1075         ok = nntp_threaded_xhdr(folder, "cc", num, num, &hdrlist);
1076         
1077         if (ok != NEWSNNTP_NO_ERROR) {
1078                 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1079                 if (ok == NEWSNNTP_ERROR_STREAM) {
1080                         session_destroy(SESSION(session));
1081                         REMOTE_FOLDER(item->folder)->session = NULL;
1082                 }
1083                 news_folder_unlock(NEWS_FOLDER(item->folder));
1084                 return msginfo;
1085         }
1086
1087         msginfo->cc = news_parse_xhdr(hdrlist, msginfo);
1088         newsnntp_xhdr_free(hdrlist);
1089
1090         news_folder_unlock(NEWS_FOLDER(item->folder));
1091         return msginfo;
1092 }
1093
1094 static GSList *news_get_msginfos_for_range(NewsSession *session, FolderItem *item, guint begin, guint end)
1095 {
1096         GSList *newlist = NULL;
1097         GSList *llast = NULL;
1098         MsgInfo *msginfo;
1099         gint ok;
1100         clist *msglist = NULL;
1101         clistiter *cur;
1102         g_return_val_if_fail(session != NULL, NULL);
1103         g_return_val_if_fail(item != NULL, NULL);
1104
1105         log_message(LOG_PROTOCOL, _("getting xover %d - %d in %s...\n"),
1106                     begin, end, item->path);
1107
1108         news_folder_lock(NEWS_FOLDER(item->folder));
1109         
1110         ok = nntp_threaded_xover(item->folder, begin, end, NULL, &msglist);
1111         
1112         if (ok != NEWSNNTP_NO_ERROR) {
1113                 log_warning(LOG_PROTOCOL, _("couldn't get xover\n"));
1114                 if (ok == NEWSNNTP_ERROR_STREAM) {
1115                         session_destroy(SESSION(session));
1116                         REMOTE_FOLDER(item->folder)->session = NULL;
1117                 }
1118                 news_folder_unlock(NEWS_FOLDER(item->folder));
1119                 return NULL;
1120         }
1121
1122         for (cur = clist_begin(msglist); cur; cur = clist_next(cur)) {
1123                 struct newsnntp_xover_resp_item *ritem = (struct newsnntp_xover_resp_item *)clist_content(cur);
1124                 msginfo = news_parse_xover(ritem);
1125                 
1126                 if (!msginfo) {
1127                         log_warning(LOG_PROTOCOL, _("invalid xover line\n"));
1128                         continue;
1129                 }
1130
1131                 msginfo->folder = item;
1132                 news_set_msg_flags(item, msginfo);
1133                 msginfo->flags.tmp_flags |= MSG_NEWS;
1134                 msginfo->newsgroups = g_strdup(item->path);
1135
1136                 if (!newlist)
1137                         llast = newlist = g_slist_append(newlist, msginfo);
1138                 else {
1139                         llast = g_slist_append(llast, msginfo);
1140                         llast = llast->next;
1141                 }
1142         }
1143         newsnntp_xover_resp_list_free(msglist);
1144
1145         news_folder_unlock(NEWS_FOLDER(item->folder));
1146
1147         session_set_access_time(SESSION(session));
1148
1149         return newlist;
1150 }
1151
1152 static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1153 {
1154         NewsSession *session;
1155         GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1156         guint first, last, next;
1157         guint tofetch, fetched;
1158         
1159         g_return_val_if_fail(folder != NULL, NULL);
1160         g_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1161         g_return_val_if_fail(msgnum_list != NULL, NULL);
1162         g_return_val_if_fail(item != NULL, NULL);
1163         
1164         session = news_session_get(folder);
1165         g_return_val_if_fail(session != NULL, NULL);
1166
1167         tmp_msgnum_list = g_slist_copy(msgnum_list);
1168         tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1169
1170         progressindicator_start(PROGRESS_TYPE_NETWORK);
1171         tofetch = g_slist_length(tmp_msgnum_list);
1172         fetched = 0;
1173
1174         first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1175         last = first;
1176         
1177         news_folder_lock(NEWS_FOLDER(item->folder));
1178         
1179         for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1180                 next = GPOINTER_TO_INT(elem->data);
1181                 if(next != (last + 1)) {
1182 /*                      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1183                         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1184 */
1185                         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1186                         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1187                         fetched = last - first + 1;
1188                         first = next;
1189                 }
1190                 last = next;
1191         }
1192         
1193         news_folder_unlock(NEWS_FOLDER(item->folder));
1194         
1195 /*      session->fetch_base_percentage = ((gfloat) fetched) / ((gfloat) tofetch);
1196         session->fetch_total_percentage = ((gfloat) (last - first + 1)) / ((gfloat) tofetch);
1197 */
1198         tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1199         msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1200
1201         g_slist_free(tmp_msgnum_list);
1202         
1203         progressindicator_stop(PROGRESS_TYPE_NETWORK);
1204
1205         return msginfo_list;
1206 }
1207
1208 static gboolean news_scan_required(Folder *folder, FolderItem *item)
1209 {
1210         return TRUE;
1211 }
1212
1213 void news_synchronise(FolderItem *item, gint days) 
1214 {
1215         news_gtk_synchronise(item, days);
1216 }
1217
1218 static gint news_rename_folder(Folder *folder, FolderItem *item,
1219                                 const gchar *name)
1220 {
1221         gchar *path;
1222          
1223         g_return_val_if_fail(folder != NULL, -1);
1224         g_return_val_if_fail(item != NULL, -1);
1225         g_return_val_if_fail(item->path != NULL, -1);
1226         g_return_val_if_fail(name != NULL, -1);
1227
1228         path = folder_item_get_path(item);
1229         if (!is_dir_exist(path))
1230                 make_dir_hier(path);
1231
1232         g_free(item->name);
1233         item->name = g_strdup(name);
1234
1235         return 0;
1236 }
1237
1238 static gint news_remove_folder(Folder *folder, FolderItem *item)
1239 {
1240         gchar *path;
1241
1242         g_return_val_if_fail(folder != NULL, -1);
1243         g_return_val_if_fail(item != NULL, -1);
1244         g_return_val_if_fail(item->path != NULL, -1);
1245
1246         path = folder_item_get_path(item);
1247         if (remove_dir_recursive(path) < 0) {
1248                 g_warning("can't remove directory `%s'\n", path);
1249                 g_free(path);
1250                 return -1;
1251         }
1252
1253         g_free(path);
1254         folder_item_remove(item);
1255         return 0;
1256 }
1257
1258 #else
1259 #include <glib.h>
1260 #include <glib/gi18n.h>
1261 #include <gtk/gtk.h>
1262 #include "folder.h"
1263 #include "alertpanel.h"
1264
1265 static FolderClass news_class;
1266
1267 static void warn_etpan(void)
1268 {
1269         static gboolean missing_news_warning = TRUE;
1270         if (missing_news_warning) {
1271                 missing_news_warning = FALSE;
1272                 alertpanel_error(
1273                         _("You have one or more News accounts "
1274                           "defined. However this version of "
1275                           "Claws Mail has been built without "
1276                           "News support; your News account(s) are "
1277                           "disabled.\n\n"
1278                           "You probably need to "
1279                           "install libetpan and recompile "
1280                           "Claws Mail."));
1281         }
1282 }
1283 static Folder *news_folder_new(const gchar *name, const gchar *path)
1284 {
1285         warn_etpan();
1286         return NULL;
1287 }
1288 void news_group_list_free(GSList *group_list)
1289 {
1290         warn_etpan();
1291 }
1292 void news_remove_group_list_cache(Folder *folder)
1293 {
1294         warn_etpan();
1295 }
1296 int news_folder_locked(Folder *folder)
1297 {
1298         warn_etpan();
1299         return 0;
1300 }
1301 gint news_post(Folder *folder, const gchar *file)
1302 {
1303         warn_etpan();
1304         return -1;
1305 }
1306
1307 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
1308 {
1309         warn_etpan();
1310         return -1;
1311 }
1312
1313 GSList *news_get_group_list(Folder *folder)
1314 {
1315         warn_etpan();
1316         return NULL;
1317 }
1318
1319
1320 FolderClass *news_get_class(void)
1321 {
1322         if (news_class.idstr == NULL) {
1323                 news_class.type = F_NEWS;
1324                 news_class.idstr = "news";
1325                 news_class.uistr = "News";
1326
1327                 /* Folder functions */
1328                 news_class.new_folder = news_folder_new;
1329         };
1330
1331         return &news_class;
1332 }
1333
1334
1335 #endif