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