Statistics
| Revision:

root / src / news.c @ 1

History | View | Annotate | Download (23.6 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 */
19

    
20
#ifdef HAVE_CONFIG_H
21
#  include "config.h"
22
#endif
23

    
24
#include "defs.h"
25

    
26
#include <glib.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include <dirent.h>
31
#include <unistd.h>
32
#include <time.h>
33

    
34
#include "intl.h"
35
#include "news.h"
36
#include "nntp.h"
37
#include "socket.h"
38
#include "recv.h"
39
#include "procmsg.h"
40
#include "procheader.h"
41
#include "folder.h"
42
#include "session.h"
43
#include "codeconv.h"
44
#include "utils.h"
45
#include "prefs_common.h"
46
#include "prefs_account.h"
47
#include "inputdialog.h"
48
#include "alertpanel.h"
49
#if USE_SSL
50
#  include "ssl.h"
51
#endif
52

    
53
#define NNTP_PORT        119
54
#if USE_SSL
55
#define NNTPS_PORT        563
56
#endif
57

    
58
static void news_folder_init                 (Folder        *folder,
59
                                          const gchar        *name,
60
                                          const gchar        *path);
61

    
62
static Folder        *news_folder_new        (const gchar        *name,
63
                                         const gchar        *folder);
64
static void         news_folder_destroy        (Folder                *folder);
65

    
66
static GSList *news_get_article_list        (Folder                *folder,
67
                                         FolderItem        *item,
68
                                         gboolean         use_cache);
69
static gchar *news_fetch_msg                (Folder                *folder,
70
                                         FolderItem        *item,
71
                                         gint                 num);
72
static MsgInfo *news_get_msginfo        (Folder                *folder,
73
                                         FolderItem        *item,
74
                                         gint                 num);
75

    
76
static gint news_close                        (Folder                *folder,
77
                                         FolderItem        *item);
78

    
79
static gint news_scan_group                (Folder                *folder,
80
                                         FolderItem        *item);
81

    
82
#if USE_SSL
83
static Session *news_session_new         (const gchar        *server,
84
                                          gushort         port,
85
                                          const gchar        *userid,
86
                                          const gchar        *passwd,
87
                                          SSLType         ssl_type);
88
#else
89
static Session *news_session_new         (const gchar        *server,
90
                                          gushort         port,
91
                                          const gchar        *userid,
92
                                          const gchar        *passwd);
93
#endif
94

    
95
static gint news_get_article_cmd         (NNTPSession        *session,
96
                                          const gchar        *cmd,
97
                                          gint                 num,
98
                                          gchar                *filename);
99
static gint news_get_article                 (NNTPSession        *session,
100
                                          gint                 num,
101
                                          gchar                *filename);
102
#if 0
103
static gint news_get_header                 (NNTPSession        *session,
104
                                          gint                 num,
105
                                          gchar                *filename);
106
#endif
107

    
108
static gint news_select_group                 (NNTPSession        *session,
109
                                          const gchar        *group,
110
                                          gint                *num,
111
                                          gint                *first,
112
                                          gint                *last);
113
static GSList *news_get_uncached_articles(NNTPSession        *session,
114
                                          FolderItem        *item,
115
                                          gint                 cache_last,
116
                                          gint                *rfirst,
117
                                          gint                *rlast);
118
static MsgInfo *news_parse_xover         (const gchar        *xover_str);
119
static gchar *news_parse_xhdr                 (const gchar        *xhdr_str,
120
                                          MsgInfo        *msginfo);
121
static GSList *news_delete_old_articles         (GSList        *alist,
122
                                          FolderItem        *item,
123
                                          gint                 first);
124
static void news_delete_all_articles         (FolderItem        *item);
125
static void news_delete_expired_caches         (GSList        *alist,
126
                                          FolderItem        *item);
127

    
128
static FolderClass news_class =
129
{
130
        F_NEWS,
131

    
132
        news_folder_new,
133
        news_folder_destroy,
134

    
135
        NULL,
136
        NULL,
137

    
138
        news_get_article_list,
139
        news_fetch_msg,
140
        news_get_msginfo,
141
        NULL,
142
        NULL,
143
        NULL,
144
        NULL,
145
        NULL,
146
        NULL,
147
        NULL,
148
        NULL,
149
        NULL,
150
        NULL,
151
        news_close,
152
        news_scan_group,
153

    
154
        NULL,
155
        NULL,
156
        NULL
157
};
158

    
159

    
160
FolderClass *news_get_class(void)
161
{
162
        return &news_class;
163
}
164

    
165
static Folder *news_folder_new(const gchar *name, const gchar *path)
166
{
167
        Folder *folder;
168

    
169
        folder = (Folder *)g_new0(NewsFolder, 1);
170
        news_folder_init(folder, name, path);
171

    
172
        return folder;
173
}
174

    
175
static void news_folder_destroy(Folder *folder)
176
{
177
        gchar *dir;
178

    
179
        dir = folder_get_path(folder);
180
        if (is_dir_exist(dir))
181
                remove_dir_recursive(dir);
182
        g_free(dir);
183

    
184
        folder_remote_folder_destroy(REMOTE_FOLDER(folder));
185
}
186

    
187
static void news_folder_init(Folder *folder, const gchar *name,
188
                             const gchar *path)
189
{
190
        folder->klass = news_get_class();
191
        folder_remote_folder_init(folder, name, path);
192
}
193

    
194
#if USE_SSL
195
static Session *news_session_new(const gchar *server, gushort port,
196
                                 const gchar *userid, const gchar *passwd,
197
                                 SSLType ssl_type)
198
#else
199
static Session *news_session_new(const gchar *server, gushort port,
200
                                 const gchar *userid, const gchar *passwd)
201
#endif
202
{
203
        gchar buf[NNTPBUFSIZE];
204
        Session *session;
205

    
206
        g_return_val_if_fail(server != NULL, NULL);
207

    
208
        log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
209

    
210
#if USE_SSL
211
        session = nntp_session_new(server, port, buf, userid, passwd, ssl_type);
212
#else
213
        session = nntp_session_new(server, port, buf, userid, passwd);
214
#endif
215

    
216
        return session;
217
}
218

    
219
static Session *news_session_new_for_folder(Folder *folder)
220
{
221
        Session *session;
222
        PrefsAccount *ac;
223
        const gchar *userid = NULL;
224
        gchar *passwd = NULL;
225
        gushort port;
226

    
227
        g_return_val_if_fail(folder != NULL, NULL);
228
        g_return_val_if_fail(folder->account != NULL, NULL);
229

    
230
        ac = folder->account;
231
        if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
232
                userid = ac->userid;
233
                if (ac->passwd && ac->passwd[0])
234
                        passwd = g_strdup(ac->passwd);
235
                else
236
                        passwd = input_dialog_query_password(ac->nntp_server,
237
                                                             userid);
238
        }
239

    
240
#if USE_SSL
241
        port = ac->set_nntpport ? ac->nntpport
242
                : ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
243
        session = news_session_new(ac->nntp_server, port, userid, passwd,
244
                                   ac->ssl_nntp);
245
#else
246
        port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
247
        session = news_session_new(ac->nntp_server, port, userid, passwd);
248
#endif
249

    
250
        g_free(passwd);
251

    
252
        return session;
253
}
254

    
255
static NNTPSession *news_session_get(Folder *folder)
256
{
257
        RemoteFolder *rfolder = REMOTE_FOLDER(folder);
258

    
259
        g_return_val_if_fail(folder != NULL, NULL);
260
        g_return_val_if_fail(FOLDER_TYPE(folder) == F_NEWS, NULL);
261
        g_return_val_if_fail(folder->account != NULL, NULL);
262

    
263
        if (!prefs_common.online_mode)
264
                return NULL;
265

    
266
        if (!rfolder->session) {
267
                rfolder->session = news_session_new_for_folder(folder);
268
                return NNTP_SESSION(rfolder->session);
269
        }
270

    
271
        if (time(NULL) - rfolder->session->last_access_time <
272
                SESSION_TIMEOUT_INTERVAL) {
273
                return NNTP_SESSION(rfolder->session);
274
        }
275

    
276
        if (nntp_mode(NNTP_SESSION(rfolder->session), FALSE)
277
            != NN_SUCCESS) {
278
                log_warning(_("NNTP connection to %s:%d has been"
279
                              " disconnected. Reconnecting...\n"),
280
                            folder->account->nntp_server,
281
                            folder->account->set_nntpport ?
282
                            folder->account->nntpport : NNTP_PORT);
283
                session_destroy(rfolder->session);
284
                rfolder->session = news_session_new_for_folder(folder);
285
        }
286

    
287
        if (rfolder->session)
288
                session_set_access_time(rfolder->session);
289

    
290
        return NNTP_SESSION(rfolder->session);
291
}
292

    
293
static GSList *news_get_article_list(Folder *folder, FolderItem *item,
294
                                     gboolean use_cache)
295
{
296
        GSList *alist;
297
        NNTPSession *session;
298

    
299
        g_return_val_if_fail(folder != NULL, NULL);
300
        g_return_val_if_fail(item != NULL, NULL);
301
        g_return_val_if_fail(FOLDER_TYPE(folder) == F_NEWS, NULL);
302

    
303
        session = news_session_get(folder);
304

    
305
        if (!session) {
306
                alist = procmsg_read_cache(item, FALSE);
307
                item->last_num = procmsg_get_last_num_in_msg_list(alist);
308
        } else if (use_cache) {
309
                GSList *newlist;
310
                gint cache_last;
311
                gint first, last;
312

    
313
                alist = procmsg_read_cache(item, FALSE);
314

    
315
                cache_last = procmsg_get_last_num_in_msg_list(alist);
316
                newlist = news_get_uncached_articles
317
                        (session, item, cache_last, &first, &last);
318
                if (first == 0 && last == 0) {
319
                        news_delete_all_articles(item);
320
                        procmsg_msg_list_free(alist);
321
                        alist = NULL;
322
                } else {
323
                        alist = news_delete_old_articles(alist, item, first);
324
                        news_delete_expired_caches(alist, item);
325
                }
326

    
327
                alist = g_slist_concat(alist, newlist);
328

    
329
                item->last_num = last;
330
        } else {
331
                gint last;
332

    
333
                alist = news_get_uncached_articles
334
                        (session, item, 0, NULL, &last);
335
                news_delete_all_articles(item);
336
                item->last_num = last;
337
        }
338

    
339
        procmsg_set_flags(alist, item);
340

    
341
        alist = procmsg_sort_msg_list(alist, item->sort_key, item->sort_type);
342

    
343
        return alist;
344
}
345

    
346
static gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
347
{
348
        gchar *path, *filename;
349
        NNTPSession *session;
350
        gint ok;
351

    
352
        g_return_val_if_fail(folder != NULL, NULL);
353
        g_return_val_if_fail(item != NULL, NULL);
354

    
355
        path = folder_item_get_path(item);
356
        if (!is_dir_exist(path))
357
                make_dir_hier(path);
358
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
359
        g_free(path);
360

    
361
        if (is_file_exist(filename)) {
362
                debug_print(_("article %d has been already cached.\n"), num);
363
                return filename;
364
        }
365

    
366
        session = news_session_get(folder);
367
        if (!session) {
368
                g_free(filename);
369
                return NULL;
370
        }
371

    
372
        ok = news_select_group(session, item->path, NULL, NULL, NULL);
373
        if (ok != NN_SUCCESS) {
374
                if (ok == NN_SOCKET) {
375
                        session_destroy(SESSION(session));
376
                        REMOTE_FOLDER(folder)->session = NULL;
377
                }
378
                g_free(filename);
379
                return NULL;
380
        }
381

    
382
        debug_print(_("getting article %d...\n"), num);
383
        ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
384
                              num, filename);
385
        if (ok != NN_SUCCESS) {
386
                g_warning(_("can't read article %d\n"), num);
387
                if (ok == NN_SOCKET) {
388
                        session_destroy(SESSION(session));
389
                        REMOTE_FOLDER(folder)->session = NULL;
390
                }
391
                g_free(filename);
392
                return NULL;
393
        }
394

    
395
        return filename;
396
}
397

    
398
static MsgInfo *news_get_msginfo(Folder *folder, FolderItem *item, gint num)
399
{
400
        MsgInfo *msginfo;
401
        MsgFlags flags = {0, 0};
402
        gchar *file;
403

    
404
        g_return_val_if_fail(folder != NULL, NULL);
405
        g_return_val_if_fail(item != NULL, NULL);
406

    
407
        file = news_fetch_msg(folder, item, num);
408
        if (!file) return NULL;
409

    
410
        msginfo = procheader_parse_file(file, flags, FALSE);
411

    
412
        g_free(file);
413

    
414
        return msginfo;
415
}
416

    
417
static gint news_close(Folder *folder, FolderItem *item)
418
{
419
        return 0;
420
}
421

    
422
static gint news_scan_group(Folder *folder, FolderItem *item)
423
{
424
        NNTPSession *session;
425
        gint num = 0, first = 0, last = 0;
426
        gint new = 0, unread = 0, total = 0;
427
        gint min = 0, max = 0;
428
        gint ok;
429

    
430
        g_return_val_if_fail(folder != NULL, -1);
431
        g_return_val_if_fail(item != NULL, -1);
432

    
433
        session = news_session_get(folder);
434
        if (!session) return -1;
435

    
436
        ok = news_select_group(session, item->path, &num, &first, &last);
437
        if (ok != NN_SUCCESS) {
438
                if (ok == NN_SOCKET) {
439
                        session_destroy(SESSION(session));
440
                        REMOTE_FOLDER(folder)->session = NULL;
441
                }
442
                return -1;
443
        }
444

    
445
        if (num == 0) {
446
                item->new = item->unread = item->total = item->last_num = 0;
447
                return 0;
448
        }
449

    
450
        procmsg_get_mark_sum(item, &new, &unread, &total, &min, &max, first);
451

    
452
        if (max < first || last < min)
453
                new = unread = total = num;
454
        else {
455
                if (min < first)
456
                        min = first;
457

    
458
                if (last < max)
459
                        max = last;
460
                else if (max < last) {
461
                        new += last - max;
462
                        unread += last - max;
463
                }
464

    
465
                if (new > num) new = num;
466
                if (unread > num) unread = num;
467
        }
468

    
469
        item->new = new;
470
        item->unread = unread;
471
        item->total = num;
472
        item->last_num = last;
473

    
474
        return 0;
475
}
476

    
477
static NewsGroupInfo *news_group_info_new(const gchar *name,
478
                                          gint first, gint last, gchar type)
479
{
480
        NewsGroupInfo *ginfo;
481

    
482
        ginfo = g_new(NewsGroupInfo, 1);
483
        ginfo->name = g_strdup(name);
484
        ginfo->first = first;
485
        ginfo->last = last;
486
        ginfo->type = type;
487

    
488
        return ginfo;
489
}
490

    
491
static void news_group_info_free(NewsGroupInfo *ginfo)
492
{
493
        g_free(ginfo->name);
494
        g_free(ginfo);
495
}
496

    
497
static gint news_group_info_compare(NewsGroupInfo *ginfo1,
498
                                    NewsGroupInfo *ginfo2)
499
{
500
        return g_strcasecmp(ginfo1->name, ginfo2->name);
501
}
502

    
503
GSList *news_get_group_list(Folder *folder)
504
{
505
        gchar *path, *filename;
506
        FILE *fp;
507
        GSList *list = NULL;
508
        GSList *last = NULL;
509
        gchar buf[NNTPBUFSIZE];
510

    
511
        g_return_val_if_fail(folder != NULL, NULL);
512
        g_return_val_if_fail(FOLDER_TYPE(folder) == F_NEWS, NULL);
513

    
514
        path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
515
        if (!is_dir_exist(path))
516
                make_dir_hier(path);
517
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
518
        g_free(path);
519

    
520
        if ((fp = fopen(filename, "rb")) == NULL) {
521
                NNTPSession *session;
522
                gint ok;
523

    
524
                session = news_session_get(folder);
525
                if (!session) {
526
                        g_free(filename);
527
                        return NULL;
528
                }
529

    
530
                ok = nntp_list(session);
531
                if (ok != NN_SUCCESS) {
532
                        if (ok == NN_SOCKET) {
533
                                session_destroy(SESSION(session));
534
                                REMOTE_FOLDER(folder)->session = NULL;
535
                        }
536
                        g_free(filename);
537
                        return NULL;
538
                }
539
                if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
540
                        log_warning(_("can't retrieve newsgroup list\n"));
541
                        session_destroy(SESSION(session));
542
                        REMOTE_FOLDER(folder)->session = NULL;
543
                        g_free(filename);
544
                        return NULL;
545
                }
546

    
547
                if ((fp = fopen(filename, "rb")) == NULL) {
548
                        FILE_OP_ERROR(filename, "fopen");
549
                        g_free(filename);
550
                        return NULL;
551
                }
552
        }
553

    
554
        while (fgets(buf, sizeof(buf), fp) != NULL) {
555
                gchar *p = buf;
556
                gchar *name;
557
                gint last_num;
558
                gint first_num;
559
                gchar type;
560
                NewsGroupInfo *ginfo;
561

    
562
                p = strchr(p, ' ');
563
                if (!p) continue;
564
                *p = '\0';
565
                p++;
566
                name = buf;
567

    
568
                if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
569
                        continue;
570

    
571
                ginfo = news_group_info_new(name, first_num, last_num, type);
572

    
573
                if (!last)
574
                        last = list = g_slist_append(NULL, ginfo);
575
                else {
576
                        last = g_slist_append(last, ginfo);
577
                        last = last->next;
578
                }
579
        }
580

    
581
        fclose(fp);
582
        g_free(filename);
583

    
584
        list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
585

    
586
        return list;
587
}
588

    
589
void news_group_list_free(GSList *group_list)
590
{
591
        GSList *cur;
592

    
593
        if (!group_list) return;
594

    
595
        for (cur = group_list; cur != NULL; cur = cur->next)
596
                news_group_info_free((NewsGroupInfo *)cur->data);
597
        g_slist_free(group_list);
598
}
599

    
600
void news_remove_group_list_cache(Folder *folder)
601
{
602
        gchar *path, *filename;
603

    
604
        g_return_if_fail(folder != NULL);
605
        g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
606

    
607
        path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
608
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
609
        g_free(path);
610

    
611
        if (is_file_exist(filename)) {
612
                if (remove(filename) < 0)
613
                        FILE_OP_ERROR(filename, "remove");
614
        }
615
        g_free(filename);
616
}
617

    
618
gint news_post(Folder *folder, const gchar *file)
619
{
620
        FILE *fp;
621
        gint ok;
622

    
623
        g_return_val_if_fail(folder != NULL, -1);
624
        g_return_val_if_fail(FOLDER_TYPE(folder) == F_NEWS, -1);
625
        g_return_val_if_fail(file != NULL, -1);
626

    
627
        if ((fp = fopen(file, "rb")) == NULL) {
628
                FILE_OP_ERROR(file, "fopen");
629
                return -1;
630
        }
631

    
632
        ok = news_post_stream(folder, fp);
633

    
634
        fclose(fp);
635

    
636
        return ok;
637
}
638

    
639
gint news_post_stream(Folder *folder, FILE *fp)
640
{
641
        NNTPSession *session;
642
        gint ok;
643

    
644
        g_return_val_if_fail(folder != NULL, -1);
645
        g_return_val_if_fail(FOLDER_TYPE(folder) == F_NEWS, -1);
646
        g_return_val_if_fail(fp != NULL, -1);
647

    
648
        session = news_session_get(folder);
649
        if (!session) return -1;
650

    
651
        ok = nntp_post(session, fp);
652
        if (ok != NN_SUCCESS) {
653
                log_warning(_("can't post article.\n"));
654
                if (ok == NN_SOCKET) {
655
                        session_destroy(SESSION(session));
656
                        REMOTE_FOLDER(folder)->session = NULL;
657
                }
658
                return -1;
659
        }
660

    
661
        return 0;
662
}
663

    
664
static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
665
                                 gint num, gchar *filename)
666
{
667
        gchar *msgid;
668
        gint ok;
669

    
670
        ok = nntp_get_article(session, cmd, num, &msgid);
671
        if (ok != NN_SUCCESS)
672
                return ok;
673

    
674
        debug_print("Message-Id = %s, num = %d\n", msgid, num);
675
        g_free(msgid);
676

    
677
        ok = recv_write_to_file(SESSION(session)->sock, filename);
678
        if (ok < 0) {
679
                log_warning(_("can't retrieve article %d\n"), num);
680
                if (ok == -2)
681
                        return NN_SOCKET;
682
                else
683
                        return NN_IOERR;
684
        }
685

    
686
        return NN_SUCCESS;
687
}
688

    
689
static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
690
{
691
        return news_get_article_cmd(session, "ARTICLE", num, filename);
692
}
693

    
694
#if 0
695
static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
696
{
697
        return news_get_article_cmd(session, "HEAD", num, filename);
698
}
699
#endif
700

    
701
/**
702
 * news_select_group:
703
 * @session: Active NNTP session.
704
 * @group: Newsgroup name.
705
 * @num: Estimated number of articles.
706
 * @first: First article number.
707
 * @last: Last article number.
708
 *
709
 * Select newsgroup @group with the GROUP command if it is not already
710
 * selected in @session, or article numbers need to be returned.
711
 *
712
 * Return value: NNTP result code.
713
 **/
714
static gint news_select_group(NNTPSession *session, const gchar *group,
715
                              gint *num, gint *first, gint *last)
716
{
717
        gint ok;
718
        gint num_, first_, last_;
719

    
720
        if (!num || !first || !last) {
721
                if (session->group && g_strcasecmp(session->group, group) == 0)
722
                        return NN_SUCCESS;
723
                num = &num_;
724
                first = &first_;
725
                last = &last_;
726
        }
727

    
728
        g_free(session->group);
729
        session->group = NULL;
730

    
731
        ok = nntp_group(session, group, num, first, last);
732
        if (ok == NN_SUCCESS)
733
                session->group = g_strdup(group);
734
        else
735
                log_warning(_("can't select group: %s\n"), group);
736

    
737
        return ok;
738
}
739

    
740
static GSList *news_get_uncached_articles(NNTPSession *session,
741
                                          FolderItem *item, gint cache_last,
742
                                          gint *rfirst, gint *rlast)
743
{
744
        gint ok;
745
        gint num = 0, first = 0, last = 0, begin = 0, end = 0;
746
        gchar buf[NNTPBUFSIZE];
747
        GSList *newlist = NULL;
748
        GSList *llast = NULL;
749
        MsgInfo *msginfo;
750

    
751
        if (rfirst) *rfirst = -1;
752
        if (rlast)  *rlast  = -1;
753

    
754
        g_return_val_if_fail(session != NULL, NULL);
755
        g_return_val_if_fail(item != NULL, NULL);
756
        g_return_val_if_fail(item->folder != NULL, NULL);
757
        g_return_val_if_fail(FOLDER_TYPE(item->folder) == F_NEWS, NULL);
758

    
759
        ok = news_select_group(session, item->path, &num, &first, &last);
760
        if (ok != NN_SUCCESS) {
761
                if (ok == NN_SOCKET) {
762
                        session_destroy(SESSION(session));
763
                        REMOTE_FOLDER(item->folder)->session = NULL;
764
                }
765
                return NULL;
766
        }
767

    
768
        /* calculate getting overview range */
769
        if (first > last) {
770
                log_warning(_("invalid article range: %d - %d\n"),
771
                            first, last);
772
                return NULL;
773
        }
774

    
775
        if (rfirst) *rfirst = first;
776
        if (rlast)  *rlast  = last;
777

    
778
        if (cache_last < first)
779
                begin = first;
780
        else if (last < cache_last)
781
                begin = first;
782
        else if (last == cache_last) {
783
                debug_print(_("no new articles.\n"));
784
                return NULL;
785
        } else
786
                begin = cache_last + 1;
787
        end = last;
788

    
789
        if (prefs_common.max_articles > 0 &&
790
            end - begin + 1 > prefs_common.max_articles)
791
                begin = end - prefs_common.max_articles + 1;
792

    
793
        log_message(_("getting xover %d - %d in %s...\n"),
794
                    begin, end, item->path);
795
        ok = nntp_xover(session, begin, end);
796
        if (ok != NN_SUCCESS) {
797
                log_warning(_("can't get xover\n"));
798
                if (ok == NN_SOCKET) {
799
                        session_destroy(SESSION(session));
800
                        REMOTE_FOLDER(item->folder)->session = NULL;
801
                }
802
                return NULL;
803
        }
804

    
805
        for (;;) {
806
                if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
807
                        log_warning(_("error occurred while getting xover.\n"));
808
                        session_destroy(SESSION(session));
809
                        REMOTE_FOLDER(item->folder)->session = NULL;
810
                        return newlist;
811
                }
812

    
813
                if (buf[0] == '.' && buf[1] == '\r') break;
814

    
815
                msginfo = news_parse_xover(buf);
816
                if (!msginfo) {
817
                        log_warning(_("invalid xover line: %s\n"), buf);
818
                        continue;
819
                }
820

    
821
                msginfo->folder = item;
822
                msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
823
                msginfo->flags.tmp_flags = MSG_NEWS;
824
                msginfo->newsgroups = g_strdup(item->path);
825

    
826
                if (!newlist)
827
                        llast = newlist = g_slist_append(newlist, msginfo);
828
                else {
829
                        llast = g_slist_append(llast, msginfo);
830
                        llast = llast->next;
831
                }
832
        }
833

    
834
        ok = nntp_xhdr(session, "to", begin, end);
835
        if (ok != NN_SUCCESS) {
836
                log_warning(_("can't get xhdr\n"));
837
                if (ok == NN_SOCKET) {
838
                        session_destroy(SESSION(session));
839
                        REMOTE_FOLDER(item->folder)->session = NULL;
840
                }
841
                return newlist;
842
        }
843

    
844
        llast = newlist;
845

    
846
        for (;;) {
847
                if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
848
                        log_warning(_("error occurred while getting xhdr.\n"));
849
                        session_destroy(SESSION(session));
850
                        REMOTE_FOLDER(item->folder)->session = NULL;
851
                        return newlist;
852
                }
853

    
854
                if (buf[0] == '.' && buf[1] == '\r') break;
855
                if (!llast) {
856
                        g_warning("llast == NULL\n");
857
                        continue;
858
                }
859

    
860
                msginfo = (MsgInfo *)llast->data;
861
                msginfo->to = news_parse_xhdr(buf, msginfo);
862

    
863
                llast = llast->next;
864
        }
865

    
866
        ok = nntp_xhdr(session, "cc", begin, end);
867
        if (ok != NN_SUCCESS) {
868
                log_warning(_("can't get xhdr\n"));
869
                if (ok == NN_SOCKET) {
870
                        session_destroy(SESSION(session));
871
                        REMOTE_FOLDER(item->folder)->session = NULL;
872
                }
873
                return newlist;
874
        }
875

    
876
        llast = newlist;
877

    
878
        for (;;) {
879
                if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
880
                        log_warning(_("error occurred while getting xhdr.\n"));
881
                        session_destroy(SESSION(session));
882
                        REMOTE_FOLDER(item->folder)->session = NULL;
883
                        return newlist;
884
                }
885

    
886
                if (buf[0] == '.' && buf[1] == '\r') break;
887
                if (!llast) {
888
                        g_warning("llast == NULL\n");
889
                        continue;
890
                }
891

    
892
                msginfo = (MsgInfo *)llast->data;
893
                msginfo->cc = news_parse_xhdr(buf, msginfo);
894

    
895
                llast = llast->next;
896
        }
897

    
898
        session_set_access_time(SESSION(session));
899

    
900
        return newlist;
901
}
902

    
903
#define PARSE_ONE_PARAM(p, srcp) \
904
{ \
905
        p = strchr(srcp, '\t'); \
906
        if (!p) return NULL; \
907
        else \
908
                *p++ = '\0'; \
909
}
910

    
911
static MsgInfo *news_parse_xover(const gchar *xover_str)
912
{
913
        MsgInfo *msginfo;
914
        gchar buf[NNTPBUFSIZE];
915
        gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
916
        gchar *p;
917
        gint num, size_int, line_int;
918
        gchar *xover_buf;
919

    
920
        Xstrdup_a(xover_buf, xover_str, return NULL);
921

    
922
        PARSE_ONE_PARAM(subject, xover_buf);
923
        PARSE_ONE_PARAM(sender, subject);
924
        PARSE_ONE_PARAM(date, sender);
925
        PARSE_ONE_PARAM(msgid, date);
926
        PARSE_ONE_PARAM(ref, msgid);
927
        PARSE_ONE_PARAM(size, ref);
928
        PARSE_ONE_PARAM(line, size);
929

    
930
        tmp = strchr(line, '\t');
931
        if (!tmp) tmp = strchr(line, '\r');
932
        if (!tmp) tmp = strchr(line, '\n');
933
        if (tmp) *tmp = '\0';
934

    
935
        num = atoi(xover_str);
936
        size_int = atoi(size);
937
        line_int = atoi(line);
938

    
939
        /* set MsgInfo */
940
        msginfo = g_new0(MsgInfo, 1);
941
        msginfo->msgnum = num;
942
        msginfo->size = size_int;
943

    
944
        msginfo->date = g_strdup(date);
945
        msginfo->date_t = procheader_date_parse(NULL, date, 0);
946

    
947
        conv_unmime_header(buf, sizeof(buf), sender, NULL);
948
        msginfo->from = g_strdup(buf);
949
        msginfo->fromname = procheader_get_fromname(buf);
950

    
951
        conv_unmime_header(buf, sizeof(buf), subject, NULL);
952
        msginfo->subject = g_strdup(buf);
953

    
954
        extract_parenthesis(msgid, '<', '>');
955
        remove_space(msgid);
956
        if (*msgid != '\0')
957
                msginfo->msgid = g_strdup(msgid);
958

    
959
        eliminate_parenthesis(ref, '(', ')');
960
        if ((p = strrchr(ref, '<')) != NULL) {
961
                extract_parenthesis(p, '<', '>');
962
                remove_space(p);
963
                if (*p != '\0')
964
                        msginfo->inreplyto = g_strdup(p);
965
        }
966

    
967
        return msginfo;
968
}
969

    
970
static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
971
{
972
        gchar *p;
973
        gchar *tmp;
974
        gint num;
975

    
976
        p = strchr(xhdr_str, ' ');
977
        if (!p)
978
                return NULL;
979
        else
980
                p++;
981

    
982
        num = atoi(xhdr_str);
983
        if (msginfo->msgnum != num) return NULL;
984

    
985
        tmp = strchr(p, '\r');
986
        if (!tmp) tmp = strchr(p, '\n');
987

    
988
        if (tmp)
989
                return g_strndup(p, tmp - p);
990
        else
991
                return g_strdup(p);
992
}
993

    
994
static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
995
                                        gint first)
996
{
997
        GSList *cur, *next;
998
        MsgInfo *msginfo;
999
        gchar *dir;
1000

    
1001
        g_return_val_if_fail(item != NULL, alist);
1002
        g_return_val_if_fail(item->folder != NULL, alist);
1003
        g_return_val_if_fail(FOLDER_TYPE(item->folder) == F_NEWS, alist);
1004

    
1005
        if (first < 2) return alist;
1006

    
1007
        debug_print("Deleting cached articles 1 - %d ...\n", first - 1);
1008

    
1009
        dir = folder_item_get_path(item);
1010
        remove_numbered_files(dir, 1, first - 1);
1011
        g_free(dir);
1012

    
1013
        for (cur = alist; cur != NULL; ) {
1014
                next = cur->next;
1015

    
1016
                msginfo = (MsgInfo *)cur->data;
1017
                if (msginfo && msginfo->msgnum < first) {
1018
                        procmsg_msginfo_free(msginfo);
1019
                        alist = g_slist_remove(alist, msginfo);
1020
                }
1021

    
1022
                cur = next;
1023
        }
1024

    
1025
        return alist;
1026
}
1027

    
1028
static void news_delete_all_articles(FolderItem *item)
1029
{
1030
        gchar *dir;
1031

    
1032
        g_return_if_fail(item != NULL);
1033
        g_return_if_fail(item->folder != NULL);
1034
        g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
1035

    
1036
        debug_print("Deleting all cached articles...\n");
1037

    
1038
        dir = folder_item_get_path(item);
1039
        remove_all_numbered_files(dir);
1040
        g_free(dir);
1041
}
1042

    
1043
static void news_delete_expired_caches(GSList *alist, FolderItem *item)
1044
{
1045
        gchar *dir;
1046

    
1047
        g_return_if_fail(item != NULL);
1048
        g_return_if_fail(item->folder != NULL);
1049
        g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
1050

    
1051
        debug_print("Deleting expired cached articles...\n");
1052

    
1053
        dir = folder_item_get_path(item);
1054
        remove_expired_files(dir, 24 * 7);
1055
        g_free(dir);
1056
}