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