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