Fix bug #3574: Template addressing
[claws.git] / src / news.c
blobd510da93b1425a0d80cbdf21893c0e667d783f07
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #ifdef HAVE_LIBETPAN
27 #include "defs.h"
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <dirent.h>
35 #include <unistd.h>
36 #include <time.h>
37 #include <libetpan/libetpan.h>
39 #include "nntp-thread.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 "passwordstore.h"
52 #include "prefs_common.h"
53 #include "prefs_account.h"
54 #include "inputdialog.h"
55 #include "log.h"
56 #include "progressindicator.h"
57 #include "remotefolder.h"
58 #include "alertpanel.h"
59 #include "inc.h"
60 #include "account.h"
61 #ifdef USE_GNUTLS
62 # include "ssl.h"
63 #endif
64 #include "main.h"
66 #define NNTP_PORT 119
67 #ifdef USE_GNUTLS
68 #define NNTPS_PORT 563
69 #endif
71 typedef struct _NewsFolder NewsFolder;
72 typedef struct _NewsSession NewsSession;
74 #define NEWS_FOLDER(obj) ((NewsFolder *)obj)
75 #define NEWS_SESSION(obj) ((NewsSession *)obj)
77 struct _NewsFolder
79 RemoteFolder rfolder;
81 gboolean use_auth;
82 gboolean lock_count;
83 guint refcnt;
86 struct _NewsSession
88 Session session;
89 Folder *folder;
90 gchar *group;
93 static void news_folder_init(Folder *folder, const gchar *name,
94 const gchar *path);
96 static Folder *news_folder_new (const gchar *name,
97 const gchar *folder);
98 static void news_folder_destroy (Folder *folder);
100 static gchar *news_fetch_msg (Folder *folder,
101 FolderItem *item,
102 gint num);
103 static void news_remove_cached_msg (Folder *folder,
104 FolderItem *item,
105 MsgInfo *msginfo);
106 #ifdef USE_GNUTLS
107 static Session *news_session_new (Folder *folder,
108 const gchar *server,
109 gushort port,
110 const gchar *userid,
111 const gchar *passwd,
112 SSLType ssl_type);
113 #else
114 static Session *news_session_new (Folder *folder,
115 const gchar *server,
116 gushort port,
117 const gchar *userid,
118 const gchar *passwd);
119 #endif
121 static gint news_get_article (Folder *folder,
122 gint num,
123 gchar *filename);
125 static gint news_select_group (Folder *folder,
126 const gchar *group,
127 gint *num,
128 gint *first,
129 gint *last);
130 static MsgInfo *news_parse_xover (struct newsnntp_xover_resp_item *item);
131 static gint news_get_num_list (Folder *folder,
132 FolderItem *item,
133 GSList **list,
134 gboolean *old_uids_valid);
135 static MsgInfo *news_get_msginfo (Folder *folder,
136 FolderItem *item,
137 gint num);
138 static GSList *news_get_msginfos (Folder *folder,
139 FolderItem *item,
140 GSList *msgnum_list);
141 static gboolean news_scan_required (Folder *folder,
142 FolderItem *item);
144 static gchar *news_folder_get_path (Folder *folder);
145 static gchar *news_item_get_path (Folder *folder,
146 FolderItem *item);
147 static void news_synchronise (FolderItem *item, gint days);
148 static int news_remove_msg (Folder *folder,
149 FolderItem *item,
150 gint msgnum);
151 static gint news_rename_folder (Folder *folder,
152 FolderItem *item,
153 const gchar *name);
154 static gint news_remove_folder (Folder *folder,
155 FolderItem *item);
156 static FolderClass news_class;
158 FolderClass *news_get_class(void)
160 if (news_class.idstr == NULL) {
161 news_class.type = F_NEWS;
162 news_class.idstr = "news";
163 news_class.uistr = "News";
164 news_class.supports_server_search = FALSE;
166 /* Folder functions */
167 news_class.new_folder = news_folder_new;
168 news_class.destroy_folder = news_folder_destroy;
170 /* FolderItem functions */
171 news_class.item_get_path = news_item_get_path;
172 news_class.get_num_list = news_get_num_list;
173 news_class.scan_required = news_scan_required;
174 news_class.rename_folder = news_rename_folder;
175 news_class.remove_folder = news_remove_folder;
177 /* Message functions */
178 news_class.get_msginfo = news_get_msginfo;
179 news_class.get_msginfos = news_get_msginfos;
180 news_class.fetch_msg = news_fetch_msg;
181 news_class.synchronise = news_synchronise;
182 news_class.search_msgs = folder_item_search_msgs_local;
183 news_class.remove_msg = news_remove_msg;
184 news_class.remove_cached_msg = news_remove_cached_msg;
187 return &news_class;
190 guint nntp_folder_get_refcnt(Folder *folder)
192 return ((NewsFolder *)folder)->refcnt;
195 void nntp_folder_ref(Folder *folder)
197 ((NewsFolder *)folder)->refcnt++;
200 void nntp_folder_unref(Folder *folder)
202 if (((NewsFolder *)folder)->refcnt > 0)
203 ((NewsFolder *)folder)->refcnt--;
206 static int news_remove_msg (Folder *folder,
207 FolderItem *item,
208 gint msgnum)
210 gchar *path, *filename;
212 cm_return_val_if_fail(folder != NULL, -1);
213 cm_return_val_if_fail(item != NULL, -1);
215 path = folder_item_get_path(item);
216 if (!is_dir_exist(path))
217 make_dir_hier(path);
219 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msgnum), NULL);
220 g_free(path);
221 claws_unlink(filename);
222 g_free(filename);
223 return 0;
226 static void news_folder_lock(NewsFolder *folder)
228 folder->lock_count++;
231 static void news_folder_unlock(NewsFolder *folder)
233 if (folder->lock_count > 0)
234 folder->lock_count--;
237 int news_folder_locked(Folder *folder)
239 if (folder == NULL)
240 return 0;
242 return NEWS_FOLDER(folder)->lock_count;
245 static Folder *news_folder_new(const gchar *name, const gchar *path)
247 Folder *folder;
249 folder = (Folder *)g_new0(NewsFolder, 1);
250 folder->klass = &news_class;
251 news_folder_init(folder, name, path);
253 return folder;
256 static void news_folder_destroy(Folder *folder)
258 gchar *dir;
260 while (nntp_folder_get_refcnt(folder) > 0)
261 gtk_main_iteration();
263 dir = news_folder_get_path(folder);
264 if (is_dir_exist(dir))
265 remove_dir_recursive(dir);
266 g_free(dir);
268 nntp_done(folder);
269 folder_remote_folder_destroy(REMOTE_FOLDER(folder));
272 static void news_folder_init(Folder *folder, const gchar *name,
273 const gchar *path)
275 folder_remote_folder_init(folder, name, path);
278 static void news_session_destroy(Session *session)
280 NewsSession *news_session = NEWS_SESSION(session);
282 cm_return_if_fail(session != NULL);
284 if (news_session->group)
285 g_free(news_session->group);
288 static gboolean nntp_ping(gpointer data)
290 Session *session = (Session *)data;
291 NewsSession *news_session = NEWS_SESSION(session);
292 int r;
293 struct tm lt;
295 if (session->state != SESSION_READY || news_folder_locked(news_session->folder))
296 return FALSE;
298 news_folder_lock(NEWS_FOLDER(news_session->folder));
300 if ((r = nntp_threaded_date(news_session->folder, &lt)) != NEWSNNTP_NO_ERROR) {
301 if (r != NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED &&
302 r != NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD) {
303 log_warning(LOG_PROTOCOL, _("NNTP connection to %s:%d has been"
304 " disconnected.\n"),
305 news_session->folder->account->nntp_server,
306 news_session->folder->account->set_nntpport ?
307 news_session->folder->account->nntpport : NNTP_PORT);
308 REMOTE_FOLDER(news_session->folder)->session = NULL;
309 news_folder_unlock(NEWS_FOLDER(news_session->folder));
310 session->state = SESSION_DISCONNECTED;
311 session->sock = NULL;
312 session_destroy(session);
313 return FALSE;
317 news_folder_unlock(NEWS_FOLDER(news_session->folder));
318 session_set_access_time(session);
319 return TRUE;
323 #ifdef USE_GNUTLS
324 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
325 const gchar *userid, const gchar *passwd,
326 SSLType ssl_type)
327 #else
328 static Session *news_session_new(Folder *folder, const gchar *server, gushort port,
329 const gchar *userid, const gchar *passwd)
330 #endif
332 NewsSession *session;
333 int r = 0;
334 cm_return_val_if_fail(server != NULL, NULL);
336 log_message(LOG_PROTOCOL, _("Account '%s': Connecting to NNTP server: %s:%d...\n"),
337 folder->account->account_name, server, port);
339 session = g_new0(NewsSession, 1);
340 session_init(SESSION(session), folder->account, FALSE);
341 SESSION(session)->type = SESSION_NEWS;
342 SESSION(session)->server = g_strdup(server);
343 SESSION(session)->port = port;
344 SESSION(session)->sock = NULL;
345 SESSION(session)->destroy = news_session_destroy;
347 nntp_init(folder);
349 #ifdef USE_GNUTLS
350 if (ssl_type != SSL_NONE)
351 r = nntp_threaded_connect_ssl(folder, server, port);
352 else
353 #endif
354 r = nntp_threaded_connect(folder, server, port);
356 if (r != NEWSNNTP_NO_ERROR) {
357 log_error(LOG_PROTOCOL, _("Error logging in to %s:%d...\n"), server, port);
358 session_destroy(SESSION(session));
359 return NULL;
362 session->folder = folder;
364 return SESSION(session);
367 static Session *news_session_new_for_folder(Folder *folder)
369 Session *session;
370 PrefsAccount *ac;
371 const gchar *userid = NULL;
372 gchar *passwd = NULL;
373 gushort port;
374 int r;
376 cm_return_val_if_fail(folder != NULL, NULL);
377 cm_return_val_if_fail(folder->account != NULL, NULL);
379 ac = folder->account;
381 #ifdef USE_GNUTLS
382 port = ac->set_nntpport ? ac->nntpport
383 : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
384 session = news_session_new(folder, ac->nntp_server, port, userid, passwd,
385 ac->ssl_nntp);
386 #else
387 if (ac->ssl_nntp != SSL_NONE) {
388 if (alertpanel_full(_("Insecure connection"),
389 _("This connection is configured to be secured "
390 "using SSL/TLS, but SSL/TLS is not available "
391 "in this build of Claws Mail. \n\n"
392 "Do you want to continue connecting to this "
393 "server? The communication would not be "
394 "secure."),
395 GTK_STOCK_CANCEL, _("Con_tinue connecting"),
396 NULL, FALSE, NULL, ALERT_WARNING,
397 G_ALERTDEFAULT) != G_ALERTALTERNATE)
398 return NULL;
400 port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
401 session = news_session_new(folder, ac->nntp_server, port, userid, passwd);
402 #endif
404 if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
405 userid = ac->userid;
406 if (password_get(userid, ac->nntp_server, "nntp", port, &passwd)) {
407 /* NOP */;
408 } else if ((passwd = passwd_store_get_account(ac->account_id,
409 PWS_ACCOUNT_RECV)) == NULL) {
410 passwd = input_dialog_query_password_keep(ac->nntp_server,
411 userid,
412 &(ac->session_passwd));
416 if (session != NULL)
417 r = nntp_threaded_mode_reader(folder);
418 else
419 r = NEWSNNTP_ERROR_CONNECTION_REFUSED;
421 if (r != NEWSNNTP_NO_ERROR) {
422 if (r == NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME) {
424 FIX ME when libetpan implements 480 to indicate authorization
425 is required to use this capability. Libetpan treats a 480 as a
426 381 which is clearly wrong.
427 RFC 4643 section 2.
428 Response code 480
429 Generic response
430 Meaning: command unavailable until the client
431 has authenticated itself.
433 /* if the server does not advertise the capability MODE-READER,
434 we normally should not send MODE READER. However this can't
435 hurt: a transit-only server returns 502 and closes the cnx.
436 Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
438 log_error(LOG_PROTOCOL, _("Libetpan does not support return code 480 "
439 "so for now we choose to continue\n"));
441 else if (r == NEWSNNTP_ERROR_UNEXPECTED_RESPONSE) {
442 /* if the server does not advertise the capability MODE-READER,
443 we normally should not send MODE READER. However this can't
444 hurt: a transit-only server returns 502 and closes the cnx.
445 Ref.: http://tools.ietf.org/html/rfc3977#section-5.3
447 log_error(LOG_PROTOCOL, _("Mode reader failed, continuing nevertheless\n"));
449 else {
450 /* An error state bail out */
451 log_error(LOG_PROTOCOL, _("Error creating session with %s:%d\n"), ac->nntp_server, port);
452 if (session != NULL)
453 session_destroy(SESSION(session));
454 g_free(passwd);
455 if (ac->session_passwd) {
456 g_free(ac->session_passwd);
457 ac->session_passwd = NULL;
459 return NULL;
463 if ((session != NULL) && ac->use_nntp_auth) { /* FIXME: && ac->use_nntp_auth_onconnect */
464 if (nntp_threaded_login(folder, userid, passwd) !=
465 NEWSNNTP_NO_ERROR) {
466 log_error(LOG_PROTOCOL, _("Error authenticating to %s:%d...\n"), ac->nntp_server, port);
467 session_destroy(SESSION(session));
468 g_free(passwd);
469 if (ac->session_passwd) {
470 g_free(ac->session_passwd);
471 ac->session_passwd = NULL;
473 return NULL;
476 g_free(passwd);
478 return session;
481 static NewsSession *news_session_get(Folder *folder)
483 RemoteFolder *rfolder = REMOTE_FOLDER(folder);
485 cm_return_val_if_fail(folder != NULL, NULL);
486 cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
487 cm_return_val_if_fail(folder->account != NULL, NULL);
489 if (prefs_common.work_offline &&
490 !inc_offline_should_override(FALSE,
491 _("Claws Mail needs network access in order "
492 "to access the News server."))) {
493 return NULL;
496 if (!rfolder->session) {
497 rfolder->session = news_session_new_for_folder(folder);
498 session_register_ping(SESSION(rfolder->session), nntp_ping);
499 return NEWS_SESSION(rfolder->session);
502 /* Handle port change (also ssl/nossl change) without needing to
503 * restart application. */
504 if (rfolder->session->port != folder->account->nntpport) {
505 session_destroy(rfolder->session);
506 rfolder->session = news_session_new_for_folder(folder);
507 session_register_ping(SESSION(rfolder->session), nntp_ping);
508 goto newsession;
511 if (time(NULL) - rfolder->session->last_access_time <
512 SESSION_TIMEOUT_INTERVAL) {
513 return NEWS_SESSION(rfolder->session);
516 if (!nntp_ping(rfolder->session)) {
517 rfolder->session = news_session_new_for_folder(folder);
518 session_register_ping(SESSION(rfolder->session), nntp_ping);
521 newsession:
522 if (rfolder->session)
523 session_set_access_time(rfolder->session);
525 return NEWS_SESSION(rfolder->session);
528 static void news_remove_cached_msg(Folder *folder, FolderItem *item, MsgInfo *msginfo)
530 gchar *path, *filename;
532 path = folder_item_get_path(item);
534 if (!is_dir_exist(path)) {
535 g_free(path);
536 return;
539 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
540 g_free(path);
542 if (is_file_exist(filename)) {
543 claws_unlink(filename);
545 g_free(filename);
548 static gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
550 gchar *path, *filename;
551 NewsSession *session;
552 gint ok;
554 cm_return_val_if_fail(folder != NULL, NULL);
555 cm_return_val_if_fail(item != NULL, NULL);
557 path = folder_item_get_path(item);
558 if (!is_dir_exist(path))
559 make_dir_hier(path);
560 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
561 g_free(path);
563 if (is_file_exist(filename)) {
564 debug_print("article %d has been already cached.\n", num);
565 return filename;
568 session = news_session_get(folder);
569 if (!session) {
570 g_free(filename);
571 return NULL;
574 ok = news_select_group(folder, item->path, NULL, NULL, NULL);
575 if (ok != NEWSNNTP_NO_ERROR) {
576 if (ok == NEWSNNTP_ERROR_STREAM) {
577 session_destroy(SESSION(session));
578 REMOTE_FOLDER(folder)->session = NULL;
580 g_free(filename);
581 return NULL;
584 debug_print("getting article %d...\n", num);
585 ok = news_get_article(folder,
586 num, filename);
587 if (ok != NEWSNNTP_NO_ERROR) {
588 g_warning("can't read article %d", num);
589 if (ok == NEWSNNTP_ERROR_STREAM) {
590 session_destroy(SESSION(session));
591 REMOTE_FOLDER(folder)->session = NULL;
593 g_free(filename);
594 return NULL;
596 GTK_EVENTS_FLUSH();
597 return filename;
600 static NewsGroupInfo *news_group_info_new(const gchar *name,
601 gint first, gint last, gchar type)
603 NewsGroupInfo *ginfo;
605 ginfo = g_new(NewsGroupInfo, 1);
606 ginfo->name = g_strdup(name);
607 ginfo->first = first;
608 ginfo->last = last;
609 ginfo->type = type;
611 return ginfo;
614 static void news_group_info_free(NewsGroupInfo *ginfo)
616 g_free(ginfo->name);
617 g_free(ginfo);
620 static gint news_group_info_compare(NewsGroupInfo *ginfo1,
621 NewsGroupInfo *ginfo2)
623 return g_ascii_strcasecmp(ginfo1->name, ginfo2->name);
626 GSList *news_get_group_list(Folder *folder)
628 gchar *path, *filename;
629 FILE *fp;
630 GSList *list = NULL;
631 GSList *last = NULL;
632 gchar buf[BUFFSIZE];
634 cm_return_val_if_fail(folder != NULL, NULL);
635 cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
637 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
638 if (!is_dir_exist(path))
639 make_dir_hier(path);
640 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
641 g_free(path);
643 if ((fp = g_fopen(filename, "rb")) == NULL) {
644 NewsSession *session;
645 gint ok;
646 clist *grouplist = NULL;
647 clistiter *cur;
648 fp = g_fopen(filename, "wb");
650 if (!fp) {
651 g_free(filename);
652 return NULL;
654 session = news_session_get(folder);
655 if (!session) {
656 fclose(fp);
657 g_free(filename);
658 return NULL;
661 ok = nntp_threaded_list(folder, &grouplist);
663 if (ok != NEWSNNTP_NO_ERROR) {
664 if (ok == NEWSNNTP_ERROR_STREAM) {
665 session_destroy(SESSION(session));
666 REMOTE_FOLDER(folder)->session = NULL;
668 fclose(fp);
669 g_free(filename);
670 return NULL;
673 if (grouplist) {
674 for (cur = clist_begin(grouplist); cur; cur = clist_next(cur)) {
675 struct newsnntp_group_info *info = (struct newsnntp_group_info *)
676 clist_content(cur);
677 if (fprintf(fp, "%s %d %d %c\n",
678 info->grp_name,
679 info->grp_last,
680 info->grp_first,
681 info->grp_type) < 0) {
682 log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
683 session_destroy(SESSION(session));
684 REMOTE_FOLDER(folder)->session = NULL;
685 fclose(fp);
686 g_free(filename);
687 newsnntp_list_free(grouplist);
688 return NULL;
691 newsnntp_list_free(grouplist);
693 if (fclose(fp) == EOF) {
694 log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n"));
695 session_destroy(SESSION(session));
696 REMOTE_FOLDER(folder)->session = NULL;
697 g_free(filename);
698 return NULL;
701 if ((fp = g_fopen(filename, "rb")) == NULL) {
702 FILE_OP_ERROR(filename, "fopen");
703 g_free(filename);
704 return NULL;
708 while (fgets(buf, sizeof(buf), fp) != NULL) {
709 gchar *p = buf;
710 gchar *name;
711 gint last_num;
712 gint first_num;
713 gchar type;
714 NewsGroupInfo *ginfo;
716 p = strchr(p, ' ');
717 if (!p) continue;
718 *p = '\0';
719 p++;
720 name = buf;
722 if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
723 continue;
725 ginfo = news_group_info_new(name, first_num, last_num, type);
727 if (!last)
728 last = list = g_slist_append(NULL, ginfo);
729 else {
730 last = g_slist_append(last, ginfo);
731 last = last->next;
735 fclose(fp);
736 g_free(filename);
738 list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
740 return list;
743 void news_group_list_free(GSList *group_list)
745 GSList *cur;
747 if (!group_list) return;
749 for (cur = group_list; cur != NULL; cur = cur->next)
750 news_group_info_free((NewsGroupInfo *)cur->data);
751 g_slist_free(group_list);
754 void news_remove_group_list_cache(Folder *folder)
756 gchar *path, *filename;
758 cm_return_if_fail(folder != NULL);
759 cm_return_if_fail(FOLDER_CLASS(folder) == &news_class);
761 path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
762 filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
763 g_free(path);
765 if (is_file_exist(filename)) {
766 if (claws_unlink(filename) < 0)
767 FILE_OP_ERROR(filename, "remove");
769 g_free(filename);
772 gint news_post(Folder *folder, const gchar *file)
774 gint ok;
775 char *contents = file_read_to_str_no_recode(file);
776 NewsSession *session;
778 cm_return_val_if_fail(folder != NULL, -1);
779 cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
780 cm_return_val_if_fail(contents != NULL, -1);
782 session = news_session_get(folder);
783 if (!session) {
784 g_free(contents);
785 return -1;
788 ok = nntp_threaded_post(folder, contents, strlen(contents));
790 g_free(contents);
792 if (ok == NEWSNNTP_ERROR_STREAM) {
793 session_destroy(SESSION(session));
794 REMOTE_FOLDER(folder)->session = NULL;
797 return (ok == NEWSNNTP_NO_ERROR ? 0 : -1);
800 static gint news_get_article(Folder *folder, gint num, gchar *filename)
802 size_t len;
803 char *result = NULL;
804 int r;
806 r = nntp_threaded_article(folder, num, &result, &len);
808 if (r == NEWSNNTP_NO_ERROR) {
809 if (str_write_to_file(result, filename) < 0) {
810 mmap_string_unref(result);
811 return -1;
813 mmap_string_unref(result);
816 return r;
820 * news_select_group:
821 * @session: Active NNTP session.
822 * @group: Newsgroup name.
823 * @num: Estimated number of articles.
824 * @first: First article number.
825 * @last: Last article number.
827 * Select newsgroup @group with the GROUP command if it is not already
828 * selected in @session, or article numbers need to be returned.
830 * Return value: NNTP result code.
832 static gint news_select_group(Folder *folder, const gchar *group,
833 gint *num, gint *first, gint *last)
835 gint ok;
836 gint num_, first_, last_;
837 struct newsnntp_group_info *info = NULL;
838 NewsSession *session = NEWS_SESSION(news_session_get(folder));
840 cm_return_val_if_fail(session != NULL, -1);
842 if (!num || !first || !last) {
843 if (session->group && g_ascii_strcasecmp(session->group, group) == 0)
844 return NEWSNNTP_NO_ERROR;
845 num = &num_;
846 first = &first_;
847 last = &last_;
850 g_free(session->group);
851 session->group = NULL;
853 ok = nntp_threaded_group(folder, group, &info);
855 if (ok == NEWSNNTP_NO_ERROR && info) {
856 session->group = g_strdup(group);
857 *num = info->grp_first;
858 *first = info->grp_first;
859 *last = info->grp_last;
860 newsnntp_group_free(info);
861 } else {
862 log_warning(LOG_PROTOCOL, _("couldn't select group: %s\n"), group);
864 return ok;
867 static MsgInfo *news_parse_xover(struct newsnntp_xover_resp_item *item)
869 MsgInfo *msginfo;
871 /* set MsgInfo */
872 msginfo = procmsg_msginfo_new();
873 msginfo->msgnum = item->ovr_article;
874 msginfo->size = item->ovr_size;
876 msginfo->date = g_strdup(item->ovr_date);
877 msginfo->date_t = procheader_date_parse(NULL, item->ovr_date, 0);
879 msginfo->from = conv_unmime_header(item->ovr_author, NULL, TRUE);
880 msginfo->fromname = procheader_get_fromname(msginfo->from);
882 msginfo->subject = conv_unmime_header(item->ovr_subject, NULL, TRUE);
884 remove_return(msginfo->from);
885 remove_return(msginfo->fromname);
886 remove_return(msginfo->subject);
888 if (item->ovr_message_id) {
889 gchar *tmp = g_strdup(item->ovr_message_id);
890 extract_parenthesis(tmp, '<', '>');
891 remove_space(tmp);
892 if (*tmp != '\0')
893 msginfo->msgid = g_strdup(tmp);
894 g_free(tmp);
897 /* FIXME: this is a quick fix; references' meaning was changed
898 * into having the actual list of references in the References: header.
899 * We need a GSList here, so msginfo_free() and msginfo_copy() can do
900 * their things properly. */
901 if (item->ovr_references && *(item->ovr_references)) {
902 gchar **ref_tokens = g_strsplit(item->ovr_references, " ", -1);
903 guint i = 0;
904 char *tmp;
905 char *p;
906 while (ref_tokens[i]) {
907 gchar *cur_ref = ref_tokens[i];
908 msginfo->references = references_list_append(msginfo->references,
909 cur_ref);
910 i++;
912 g_strfreev(ref_tokens);
914 tmp = g_strdup(item->ovr_references);
915 eliminate_parenthesis(tmp, '(', ')');
916 if ((p = strrchr(tmp, '<')) != NULL) {
917 extract_parenthesis(p, '<', '>');
918 remove_space(p);
919 if (*p != '\0')
920 msginfo->inreplyto = g_strdup(p);
922 g_free(tmp);
925 return msginfo;
928 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
930 gchar * tmp;
931 FILE * tmpfp;
932 gchar buf[BUFFSIZE];
934 tmp = g_strdup_printf("%s%ccancel%p", get_tmp_dir(),
935 G_DIR_SEPARATOR, msginfo);
936 if (tmp == NULL)
937 return -1;
939 if ((tmpfp = g_fopen(tmp, "wb")) == NULL) {
940 FILE_OP_ERROR(tmp, "fopen");
941 return -1;
943 if (change_file_mode_rw(tmpfp, tmp) < 0) {
944 FILE_OP_ERROR(tmp, "chmod");
945 g_warning("can't change file mode");
948 if (prefs_common.hide_timezone)
949 get_rfc822_date_hide_tz(buf, sizeof(buf));
950 else
951 get_rfc822_date(buf, sizeof(buf));
952 if (fprintf(tmpfp, "From: %s\r\n"
953 "Newsgroups: %s\r\n"
954 "Subject: cmsg cancel <%s>\r\n"
955 "Control: cancel <%s>\r\n"
956 "Approved: %s\r\n"
957 "X-Cancelled-by: %s\r\n"
958 "Date: %s\r\n"
959 "\r\n"
960 "removed with Claws Mail\r\n",
961 msginfo->from,
962 msginfo->newsgroups,
963 msginfo->msgid,
964 msginfo->msgid,
965 msginfo->from,
966 msginfo->from,
967 buf) < 0) {
968 FILE_OP_ERROR(tmp, "fprintf");
969 fclose(tmpfp);
970 claws_unlink(tmp);
971 g_free(tmp);
972 return -1;
975 if (fclose(tmpfp) == EOF) {
976 FILE_OP_ERROR(tmp, "fclose");
977 claws_unlink(tmp);
978 g_free(tmp);
979 return -1;
982 news_post(folder, tmp);
983 claws_unlink(tmp);
985 g_free(tmp);
987 return 0;
990 static gchar *news_folder_get_path(Folder *folder)
992 gchar *folder_path;
994 cm_return_val_if_fail(folder->account != NULL, NULL);
996 folder_path = g_strconcat(get_news_cache_dir(),
997 G_DIR_SEPARATOR_S,
998 folder->account->nntp_server,
999 NULL);
1000 return folder_path;
1003 static gchar *news_item_get_path(Folder *folder, FolderItem *item)
1005 gchar *folder_path, *path;
1007 cm_return_val_if_fail(folder != NULL, NULL);
1008 cm_return_val_if_fail(item != NULL, NULL);
1009 folder_path = news_folder_get_path(folder);
1011 cm_return_val_if_fail(folder_path != NULL, NULL);
1012 if (g_path_is_absolute(folder_path)) {
1013 if (item->path)
1014 path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
1015 item->path, NULL);
1016 else
1017 path = g_strdup(folder_path);
1018 } else {
1019 if (item->path)
1020 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1021 folder_path, G_DIR_SEPARATOR_S,
1022 item->path, NULL);
1023 else
1024 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1025 folder_path, NULL);
1027 g_free(folder_path);
1028 #ifdef G_OS_WIN32
1029 while (strchr(path, '/'))
1030 *strchr(path, '/') = '\\';
1031 #endif
1032 return path;
1035 static gint news_get_num_list(Folder *folder, FolderItem *item, GSList **msgnum_list, gboolean *old_uids_valid)
1037 NewsSession *session;
1038 gint i, ok, num, first, last, nummsgs = 0;
1039 gchar *dir;
1041 cm_return_val_if_fail(item != NULL, -1);
1042 cm_return_val_if_fail(item->folder != NULL, -1);
1043 cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, -1);
1045 session = news_session_get(folder);
1046 cm_return_val_if_fail(session != NULL, -1);
1048 *old_uids_valid = TRUE;
1050 news_folder_lock(NEWS_FOLDER(item->folder));
1052 ok = news_select_group(folder, item->path, &num, &first, &last);
1053 if (ok != NEWSNNTP_NO_ERROR) {
1054 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
1055 news_folder_unlock(NEWS_FOLDER(item->folder));
1056 return -1;
1059 dir = news_folder_get_path(folder);
1060 if (num <= 0)
1061 remove_all_numbered_files(dir);
1062 else if (last < first)
1063 log_warning(LOG_PROTOCOL, _("invalid article range: %d - %d\n"),
1064 first, last);
1065 else {
1066 for (i = first; i <= last; i++) {
1067 *msgnum_list = g_slist_prepend(*msgnum_list,
1068 GINT_TO_POINTER(i));
1069 nummsgs++;
1071 debug_print("removing old messages from %d to %d in %s\n",
1072 first, last, dir);
1073 remove_numbered_files(dir, 1, first - 1);
1075 g_free(dir);
1076 news_folder_unlock(NEWS_FOLDER(item->folder));
1077 return nummsgs;
1080 static void news_set_msg_flags(FolderItem *item, MsgInfo *msginfo)
1082 msginfo->flags.tmp_flags = 0;
1083 if (item->folder->account->mark_crosspost_read && msginfo->msgid) {
1084 if (item->folder->newsart &&
1085 g_hash_table_lookup(item->folder->newsart, msginfo->msgid) != NULL) {
1086 msginfo->flags.perm_flags = MSG_COLORLABEL_TO_FLAGS(item->folder->account->crosspost_col);
1088 } else {
1089 if (!item->folder->newsart)
1090 item->folder->newsart = g_hash_table_new(g_str_hash, g_str_equal);
1091 g_hash_table_insert(item->folder->newsart,
1092 g_strdup(msginfo->msgid), GINT_TO_POINTER(1));
1093 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1095 } else {
1096 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
1100 static void news_get_extra_fields(NewsSession *session, FolderItem *item, GSList *msglist)
1102 MsgInfo *msginfo = NULL;
1103 gint ok;
1104 GSList *cur;
1105 clist *hdrlist = NULL;
1106 clistiter *hdr;
1107 gint first = -1, last = -1;
1108 GHashTable *hash_table;
1110 cm_return_if_fail(session != NULL);
1111 cm_return_if_fail(item != NULL);
1112 cm_return_if_fail(item->folder != NULL);
1113 cm_return_if_fail(FOLDER_CLASS(item->folder) == &news_class);
1115 news_folder_lock(NEWS_FOLDER(item->folder));
1117 hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1119 for (cur = msglist; cur; cur = cur->next) {
1120 msginfo = (MsgInfo *)cur->data;
1121 if (first == -1 || msginfo->msgnum < first)
1122 first = msginfo->msgnum;
1123 if (last == -1 || msginfo->msgnum > last)
1124 last = msginfo->msgnum;
1125 g_hash_table_insert(hash_table,
1126 GINT_TO_POINTER(msginfo->msgnum), msginfo);
1129 /* Newsgroups */
1130 ok = nntp_threaded_xhdr(item->folder, "newsgroups", first, last, &hdrlist);
1132 if (ok != NEWSNNTP_NO_ERROR) {
1133 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1134 if (ok == NEWSNNTP_ERROR_STREAM) {
1135 session_destroy(SESSION(session));
1136 REMOTE_FOLDER(item->folder)->session = NULL;
1138 news_folder_unlock(NEWS_FOLDER(item->folder));
1139 return;
1142 for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1143 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1144 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1145 if (msginfo) {
1146 if (msginfo->newsgroups)
1147 g_free(msginfo->newsgroups);
1148 msginfo->newsgroups = g_strdup(hdrval->hdr_value);
1151 newsnntp_xhdr_free(hdrlist);
1153 /* To */
1154 ok = nntp_threaded_xhdr(item->folder, "to", first, last, &hdrlist);
1156 if (ok != NEWSNNTP_NO_ERROR) {
1157 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1158 if (ok == NEWSNNTP_ERROR_STREAM) {
1159 session_destroy(SESSION(session));
1160 REMOTE_FOLDER(item->folder)->session = NULL;
1162 news_folder_unlock(NEWS_FOLDER(item->folder));
1163 return;
1166 for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1167 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1168 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1169 if (msginfo) {
1170 if (msginfo->to)
1171 g_free(msginfo->to);
1172 msginfo->to = g_strdup(hdrval->hdr_value);
1175 newsnntp_xhdr_free(hdrlist);
1177 /* Cc */
1178 ok = nntp_threaded_xhdr(item->folder, "cc", first, last, &hdrlist);
1180 if (ok != NEWSNNTP_NO_ERROR) {
1181 log_warning(LOG_PROTOCOL, _("couldn't get xhdr\n"));
1182 if (ok == NEWSNNTP_ERROR_STREAM) {
1183 session_destroy(SESSION(session));
1184 REMOTE_FOLDER(item->folder)->session = NULL;
1186 news_folder_unlock(NEWS_FOLDER(item->folder));
1187 return;
1190 for (hdr = clist_begin(hdrlist); hdr; hdr = clist_next(hdr)) {
1191 struct newsnntp_xhdr_resp_item *hdrval = clist_content(hdr);
1192 msginfo = g_hash_table_lookup(hash_table, GINT_TO_POINTER(hdrval->hdr_article));
1193 if (msginfo) {
1194 if (msginfo->cc)
1195 g_free(msginfo->cc);
1196 msginfo->cc = g_strdup(hdrval->hdr_value);
1199 newsnntp_xhdr_free(hdrlist);
1202 g_hash_table_destroy(hash_table);
1203 news_folder_unlock(NEWS_FOLDER(item->folder));
1206 static GSList *news_get_msginfos_for_range(NewsSession *session, FolderItem *item, guint begin, guint end)
1208 GSList *newlist = NULL;
1209 GSList *llast = NULL;
1210 MsgInfo *msginfo;
1211 gint ok;
1212 clist *msglist = NULL;
1213 clistiter *cur;
1214 cm_return_val_if_fail(session != NULL, NULL);
1215 cm_return_val_if_fail(item != NULL, NULL);
1217 log_message(LOG_PROTOCOL, _("getting xover %d - %d in %s...\n"),
1218 begin, end, item->path);
1220 news_folder_lock(NEWS_FOLDER(item->folder));
1222 ok = news_select_group(item->folder, item->path, NULL, NULL, NULL);
1223 if (ok != NEWSNNTP_NO_ERROR) {
1224 log_warning(LOG_PROTOCOL, _("couldn't set group: %s\n"), item->path);
1225 news_folder_unlock(NEWS_FOLDER(item->folder));
1226 return NULL;
1229 ok = nntp_threaded_xover(item->folder, begin, end, NULL, &msglist);
1231 if (ok != NEWSNNTP_NO_ERROR) {
1232 log_warning(LOG_PROTOCOL, _("couldn't get xover\n"));
1233 if (ok == NEWSNNTP_ERROR_STREAM) {
1234 session_destroy(SESSION(session));
1235 REMOTE_FOLDER(item->folder)->session = NULL;
1237 news_folder_unlock(NEWS_FOLDER(item->folder));
1238 return NULL;
1241 if (msglist) {
1242 for (cur = clist_begin(msglist); cur; cur = clist_next(cur)) {
1243 struct newsnntp_xover_resp_item *ritem = (struct newsnntp_xover_resp_item *)clist_content(cur);
1244 msginfo = news_parse_xover(ritem);
1246 if (!msginfo) {
1247 log_warning(LOG_PROTOCOL, _("invalid xover line\n"));
1248 continue;
1251 msginfo->folder = item;
1252 news_set_msg_flags(item, msginfo);
1253 msginfo->flags.tmp_flags |= MSG_NEWS;
1255 if (!newlist)
1256 llast = newlist = g_slist_append(newlist, msginfo);
1257 else {
1258 llast = g_slist_append(llast, msginfo);
1259 llast = llast->next;
1262 newsnntp_xover_resp_list_free(msglist);
1265 news_folder_unlock(NEWS_FOLDER(item->folder));
1267 session_set_access_time(SESSION(session));
1269 news_get_extra_fields(session, item, newlist);
1271 return newlist;
1274 static MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
1276 GSList *msglist = NULL;
1277 NewsSession *session;
1278 MsgInfo *msginfo = NULL;
1280 session = news_session_get(folder);
1281 cm_return_val_if_fail(session != NULL, NULL);
1282 cm_return_val_if_fail(item != NULL, NULL);
1283 cm_return_val_if_fail(item->folder != NULL, NULL);
1284 cm_return_val_if_fail(FOLDER_CLASS(item->folder) == &news_class, NULL);
1286 msglist = news_get_msginfos_for_range(session, item, num, num);
1288 if (msglist)
1289 msginfo = msglist->data;
1291 g_slist_free(msglist);
1293 return msginfo;
1296 static GSList *news_get_msginfos(Folder *folder, FolderItem *item, GSList *msgnum_list)
1298 NewsSession *session;
1299 GSList *elem, *msginfo_list = NULL, *tmp_msgnum_list, *tmp_msginfo_list;
1300 guint first, last, next;
1302 cm_return_val_if_fail(folder != NULL, NULL);
1303 cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL);
1304 cm_return_val_if_fail(msgnum_list != NULL, NULL);
1305 cm_return_val_if_fail(item != NULL, NULL);
1307 session = news_session_get(folder);
1308 cm_return_val_if_fail(session != NULL, NULL);
1310 tmp_msgnum_list = g_slist_copy(msgnum_list);
1311 tmp_msgnum_list = g_slist_sort(tmp_msgnum_list, g_int_compare);
1313 progressindicator_start(PROGRESS_TYPE_NETWORK);
1315 first = GPOINTER_TO_INT(tmp_msgnum_list->data);
1316 last = first;
1318 news_folder_lock(NEWS_FOLDER(item->folder));
1320 for(elem = g_slist_next(tmp_msgnum_list); elem != NULL; elem = g_slist_next(elem)) {
1321 next = GPOINTER_TO_INT(elem->data);
1322 if(next != (last + 1)) {
1323 tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1324 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1325 first = next;
1327 last = next;
1330 news_folder_unlock(NEWS_FOLDER(item->folder));
1332 tmp_msginfo_list = news_get_msginfos_for_range(session, item, first, last);
1333 msginfo_list = g_slist_concat(msginfo_list, tmp_msginfo_list);
1335 g_slist_free(tmp_msgnum_list);
1337 progressindicator_stop(PROGRESS_TYPE_NETWORK);
1339 return msginfo_list;
1342 static gboolean news_scan_required(Folder *folder, FolderItem *item)
1344 return TRUE;
1347 void news_synchronise(FolderItem *item, gint days)
1349 news_gtk_synchronise(item, days);
1352 static gint news_rename_folder(Folder *folder, FolderItem *item,
1353 const gchar *name)
1355 gchar *path;
1357 cm_return_val_if_fail(folder != NULL, -1);
1358 cm_return_val_if_fail(item != NULL, -1);
1359 cm_return_val_if_fail(item->path != NULL, -1);
1360 cm_return_val_if_fail(name != NULL, -1);
1362 path = folder_item_get_path(item);
1363 if (!is_dir_exist(path))
1364 make_dir_hier(path);
1366 g_free(item->name);
1367 item->name = g_strdup(name);
1369 return 0;
1372 static gint news_remove_folder(Folder *folder, FolderItem *item)
1374 gchar *path;
1376 cm_return_val_if_fail(folder != NULL, -1);
1377 cm_return_val_if_fail(item != NULL, -1);
1378 cm_return_val_if_fail(item->path != NULL, -1);
1380 path = folder_item_get_path(item);
1381 if (remove_dir_recursive(path) < 0) {
1382 g_warning("can't remove directory '%s'", path);
1383 g_free(path);
1384 return -1;
1387 g_free(path);
1388 folder_item_remove(item);
1389 return 0;
1392 void nntp_disconnect_all(gboolean have_connectivity)
1394 GList *list;
1395 gboolean short_timeout;
1396 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1397 GError *error;
1398 #endif
1400 #ifdef HAVE_NETWORKMANAGER_SUPPORT
1401 error = NULL;
1402 short_timeout = !networkmanager_is_online(&error);
1403 if(error) {
1404 short_timeout = TRUE;
1405 g_error_free(error);
1407 #else
1408 short_timeout = TRUE;
1409 #endif
1411 if(short_timeout)
1412 nntp_main_set_timeout(1);
1414 for (list = account_get_list(); list != NULL; list = list->next) {
1415 PrefsAccount *account = list->data;
1416 if (account->protocol == A_NNTP) {
1417 RemoteFolder *folder = (RemoteFolder *)account->folder;
1418 if (folder && folder->session) {
1419 NewsSession *session = (NewsSession *)folder->session;
1420 if (have_connectivity)
1421 nntp_threaded_disconnect(FOLDER(folder));
1422 SESSION(session)->state = SESSION_DISCONNECTED;
1423 SESSION(session)->sock = NULL;
1424 session_destroy(SESSION(session));
1425 folder->session = NULL;
1430 if(short_timeout)
1431 nntp_main_set_timeout(prefs_common.io_timeout_secs);
1434 #else
1435 #include <glib.h>
1436 #include <glib/gi18n.h>
1437 #include <gtk/gtk.h>
1438 #include "folder.h"
1439 #include "alertpanel.h"
1441 static FolderClass news_class;
1443 static void warn_etpan(void)
1445 static gboolean missing_news_warning = TRUE;
1446 if (missing_news_warning) {
1447 missing_news_warning = FALSE;
1448 alertpanel_error(
1449 _("You have one or more News accounts "
1450 "defined. However this version of "
1451 "Claws Mail has been built without "
1452 "News support; your News accounts are "
1453 "disabled.\n\n"
1454 "You probably need to "
1455 "install libetpan and recompile "
1456 "Claws Mail."));
1459 static Folder *news_folder_new(const gchar *name, const gchar *path)
1461 warn_etpan();
1462 return NULL;
1464 void news_group_list_free(GSList *group_list)
1466 warn_etpan();
1468 void news_remove_group_list_cache(Folder *folder)
1470 warn_etpan();
1472 int news_folder_locked(Folder *folder)
1474 warn_etpan();
1475 return 0;
1477 gint news_post(Folder *folder, const gchar *file)
1479 warn_etpan();
1480 return -1;
1483 gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
1485 warn_etpan();
1486 return -1;
1489 GSList *news_get_group_list(Folder *folder)
1491 warn_etpan();
1492 return NULL;
1496 FolderClass *news_get_class(void)
1498 if (news_class.idstr == NULL) {
1499 news_class.type = F_NEWS;
1500 news_class.idstr = "news";
1501 news_class.uistr = "News";
1503 /* Folder functions */
1504 news_class.new_folder = news_folder_new;
1507 return &news_class;
1510 void nntp_disconnect_all(gboolean have_connectivity)
1514 #endif