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