more sync with sylpheed 0.5.0pre4
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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
33 #include "intl.h"
34 #include "news.h"
35 #include "nntp.h"
36 #include "socket.h"
37 #include "recv.h"
38 #include "procmsg.h"
39 #include "procheader.h"
40 #include "folder.h"
41 #include "session.h"
42 #include "statusbar.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
50 #define NNTP_PORT       119
51
52 static Session *news_session_new         (const gchar   *server,
53                                           gushort        port,
54                                           const gchar   *userid,
55                                           const gchar   *passwd);
56
57 static gint news_get_article_cmd         (NNTPSession   *session,
58                                           const gchar   *cmd,
59                                           gint           num,
60                                           gchar         *filename);
61 static gint news_get_article             (NNTPSession   *session,
62                                           gint           num,
63                                           gchar         *filename);
64 static gint news_get_header              (NNTPSession   *session,
65                                           gint           num,
66                                           gchar         *filename);
67
68 static gint news_select_group            (NNTPSession   *session,
69                                           const gchar   *group);
70 static GSList *news_get_uncached_articles(NNTPSession   *session,
71                                           FolderItem    *item,
72                                           gint           cache_last,
73                                           gint          *rfirst,
74                                           gint          *rlast);
75 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
76 static gchar *news_parse_xhdr            (const gchar   *xhdr_str,
77                                           MsgInfo       *msginfo);
78 static GSList *news_delete_old_articles  (GSList        *alist,
79                                           FolderItem    *item,
80                                           gint           first);
81 static void news_delete_all_articles     (FolderItem    *item);
82
83
84 static Session *news_session_new(const gchar *server, gushort port,
85                                  const gchar *userid, const gchar *passwd)
86 {
87         gchar buf[NNTPBUFSIZE];
88         NNTPSession *session;
89         NNTPSockInfo *nntp_sock;
90
91         g_return_val_if_fail(server != NULL, NULL);
92
93         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
94
95         if (userid && passwd)
96                 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
97         else
98                 nntp_sock = nntp_open(server, port, buf);
99         if (nntp_sock == NULL)
100                 return NULL;
101
102         session = g_new(NNTPSession, 1);
103         SESSION(session)->type      = SESSION_NEWS;
104         SESSION(session)->server    = g_strdup(server);
105         session->nntp_sock          = nntp_sock;
106         SESSION(session)->sock      = nntp_sock->sock;
107         SESSION(session)->connected = TRUE;
108         SESSION(session)->phase     = SESSION_READY;
109         SESSION(session)->data      = NULL;
110         session->group = NULL;
111
112         return SESSION(session);
113 }
114
115 void news_session_destroy(NNTPSession *session)
116 {
117         nntp_close(session->nntp_sock);
118         session->nntp_sock = NULL;
119         SESSION(session)->sock = NULL;
120
121         g_free(session->group);
122 }
123
124 static gchar *news_query_password(const gchar *server, const gchar *user)
125 {
126         gchar *message;
127         gchar *pass;
128
129         message = g_strdup_printf(_("Input password for %s on %s:"),
130                                   user, server);
131         pass = input_dialog_with_invisible(_("Input password"), message, NULL);
132         g_free(message);
133
134         return pass;
135 }
136
137 static Session *news_session_new_for_folder(Folder *folder)
138 {
139         Session *session;
140         PrefsAccount *ac;
141         const gchar *userid = NULL;
142         gchar *passwd = NULL;
143
144         g_return_val_if_fail(folder != NULL, NULL);
145         g_return_val_if_fail(folder->account != NULL, NULL);
146
147         ac = folder->account;
148         if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
149                 userid = ac->userid;
150                 if (ac->passwd && ac->passwd[0])
151                         passwd = g_strdup(ac->passwd);
152                 else
153                         passwd = news_query_password(ac->nntp_server, userid);
154         }
155
156         session = news_session_new(ac->nntp_server,
157                                    ac->set_nntpport ? ac->nntpport : NNTP_PORT,
158                                    userid, passwd);
159         g_free(passwd);
160
161         return session;
162 }
163
164 NNTPSession *news_session_get(Folder *folder)
165 {
166         NNTPSession *session;
167
168         g_return_val_if_fail(folder != NULL, NULL);
169         g_return_val_if_fail(folder->type == F_NEWS, NULL);
170         g_return_val_if_fail(folder->account != NULL, NULL);
171
172         if (!REMOTE_FOLDER(folder)->session) {
173                 REMOTE_FOLDER(folder)->session =
174                         news_session_new_for_folder(folder);
175                 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
176         }
177
178         session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
179
180         if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
181                 log_warning(_("NNTP connection to %s:%d has been"
182                               " disconnected. Reconnecting...\n"),
183                             folder->account->nntp_server,
184                             folder->account->set_nntpport ?
185                             folder->account->nntpport : NNTP_PORT);
186                 session_destroy(REMOTE_FOLDER(folder)->session);
187                 REMOTE_FOLDER(folder)->session =
188                         news_session_new_for_folder(folder);
189         }
190
191         return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
192 }
193
194 GSList *news_get_article_list(Folder *folder, FolderItem *item,
195                               gboolean use_cache)
196 {
197         GSList *alist;
198         NNTPSession *session;
199
200         g_return_val_if_fail(folder != NULL, NULL);
201         g_return_val_if_fail(item != NULL, NULL);
202         g_return_val_if_fail(folder->type == F_NEWS, NULL);
203
204         session = news_session_get(folder);
205
206         if (!session) {
207                 alist = procmsg_read_cache(item, FALSE);
208                 item->last_num = procmsg_get_last_num_in_cache(alist);
209         } else if (use_cache) {
210                 GSList *newlist;
211                 gint cache_last;
212                 gint first, last;
213
214                 alist = procmsg_read_cache(item, FALSE);
215
216                 cache_last = procmsg_get_last_num_in_cache(alist);
217                 newlist = news_get_uncached_articles
218                         (session, item, cache_last, &first, &last);
219                 alist = news_delete_old_articles(alist, item, first);
220
221                 alist = g_slist_concat(alist, newlist);
222                 item->last_num = last;
223         } else {
224                 gint last;
225
226                 alist = news_get_uncached_articles
227                         (session, item, 0, NULL, &last);
228                 news_delete_all_articles(item);
229                 item->last_num = last;
230         }
231
232         procmsg_set_flags(alist, item);
233
234         statusbar_pop_all();
235
236         return alist;
237 }
238
239 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
240 {
241         gchar *path, *filename;
242         gint ok;
243
244         g_return_val_if_fail(folder != NULL, NULL);
245         g_return_val_if_fail(item != NULL, NULL);
246
247         path = folder_item_get_path(item);
248         if (!is_dir_exist(path))
249                 make_dir_hier(path);
250
251         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
252         g_free(path);
253
254         if (is_file_exist(filename)) {
255                 debug_print(_("article %d has been already cached.\n"), num);
256                 return filename;
257         }
258
259         if (!REMOTE_FOLDER(folder)->session) {
260                 g_free(filename);
261                 return NULL;
262         }
263
264         ok = news_select_group(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
265                                item->path);
266         if (ok != NN_SUCCESS) {
267                 g_warning(_("can't select group %s\n"), item->path);
268                 g_free(filename);
269                 return NULL;
270         }
271
272         debug_print(_("getting article %d...\n"), num);
273         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
274                               num, filename);
275         statusbar_pop_all();
276         if (ok < 0) {
277                 g_warning(_("can't read article %d\n"), num);
278                 g_free(filename);
279                 return NULL;
280         }
281
282         return filename;
283 }
284
285 void news_scan_group(Folder *folder, FolderItem *item)
286 {
287 }
288
289 gint news_post(Folder *folder, const gchar *file)
290 {
291         NNTPSession *session;
292         FILE *fp;
293         gint ok;
294
295         g_return_val_if_fail(folder != NULL, -1);
296         g_return_val_if_fail(folder->type == F_NEWS, -1);
297         g_return_val_if_fail(file != NULL, -1);
298
299         session = news_session_get(folder);
300         if (!session) return -1;
301
302         if ((fp = fopen(file, "r")) == NULL) {
303                 FILE_OP_ERROR(file, "fopen");
304                 return -1;
305         }
306
307         ok = nntp_post(session->nntp_sock, fp);
308         if (ok != NN_SUCCESS) {
309                 log_warning(_("can't post article.\n"));
310                 return -1;
311         }
312
313         fclose(fp);
314
315         statusbar_pop_all();
316
317         return 0;
318 }
319
320 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
321                                  gint num, gchar *filename)
322 {
323         gchar *msgid;
324
325         if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
326             != NN_SUCCESS)
327                 return -1;
328
329         debug_print("Message-Id = %s, num = %d\n", msgid, num);
330         g_free(msgid);
331
332         if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
333                 log_warning(_("can't retrieve article %d\n"), num);
334                 return -1;
335         }
336
337         return 0;
338 }
339
340 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
341 {
342         return news_get_article_cmd(session, "ARTICLE", num, filename);
343 }
344
345 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
346 {
347         return news_get_article_cmd(session, "HEAD", num, filename);
348 }
349
350 /**
351  * news_select_group:
352  * @session: Active NNTP session.
353  * @group: Newsgroup name.
354  *
355  * Select newsgroup @group with the GROUP command if it is not already
356  * selected in @session.
357  *
358  * Return value: NNTP result code.
359  **/
360 static gint news_select_group(NNTPSession *session, const gchar *group)
361 {
362         gint ok;
363         gint num, first, last;
364
365         if (session->group && g_strcasecmp(session->group, group) == 0)
366                 return NN_SUCCESS;
367
368         g_free(session->group);
369         session->group = NULL;
370
371         ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
372         if (ok == NN_SUCCESS)
373                 session->group = g_strdup(group);
374
375         return ok;
376 }
377
378 static GSList *news_get_uncached_articles(NNTPSession *session,
379                                           FolderItem *item, gint cache_last,
380                                           gint *rfirst, gint *rlast)
381 {
382         gint ok;
383         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
384         gchar buf[NNTPBUFSIZE];
385         GSList *newlist = NULL;
386         GSList *llast = NULL;
387         MsgInfo *msginfo;
388
389         if (rfirst) *rfirst = 0;
390         if (rlast)  *rlast  = 0;
391
392         g_return_val_if_fail(session != NULL, NULL);
393         g_return_val_if_fail(item != NULL, NULL);
394         g_return_val_if_fail(item->folder != NULL, NULL);
395         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
396
397         g_free(session->group);
398         session->group = NULL;
399
400         ok = nntp_group(session->nntp_sock, item->path,
401                         &num, &first, &last);
402         if (ok != NN_SUCCESS) {
403                 log_warning(_("can't set group: %s\n"), item->path);
404                 return NULL;
405         }
406         session->group = g_strdup(item->path);
407
408         /* calculate getting overview range */
409         if (first > last) {
410                 log_warning(_("invalid article range: %d - %d\n"),
411                             first, last);
412                 return NULL;
413         }
414         if (cache_last < first)
415                 begin = first;
416         else if (last < cache_last)
417                 begin = first;
418         else if (last == cache_last) {
419                 debug_print(_("no new articles.\n"));
420                 return NULL;
421         } else
422                 begin = cache_last + 1;
423         end = last;
424
425         if (rfirst) *rfirst = first;
426         if (rlast)  *rlast  = last;
427
428         if (prefs_common.max_articles > 0 &&
429             end - begin + 1 > prefs_common.max_articles)
430                 begin = end - prefs_common.max_articles + 1;
431
432         log_message(_("getting xover %d - %d in %s...\n"),
433                     begin, end, item->path);
434         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
435                 log_warning(_("can't get xover\n"));
436                 return NULL;
437         }
438
439         for (;;) {
440                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
441                         log_warning(_("error occurred while getting xover.\n"));
442                         return newlist;
443                 }
444
445                 if (buf[0] == '.' && buf[1] == '\r') break;
446
447                 msginfo = news_parse_xover(buf);
448                 if (!msginfo) {
449                         log_warning(_("invalid xover line: %s\n"), buf);
450                         continue;
451                 }
452
453                 msginfo->folder = item;
454                 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
455                 msginfo->newsgroups = g_strdup(item->path);
456
457                 if (!newlist)
458                         llast = newlist = g_slist_append(newlist, msginfo);
459                 else {
460                         llast = g_slist_append(llast, msginfo);
461                         llast = llast->next;
462                 }
463         }
464
465         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
466                 log_warning(_("can't get xhdr\n"));
467                 return newlist;
468         }
469
470         llast = newlist;
471
472         for (;;) {
473                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
474                         log_warning(_("error occurred while getting xhdr.\n"));
475                         return newlist;
476                 }
477
478                 if (buf[0] == '.' && buf[1] == '\r') break;
479                 if (!llast) {
480                         g_warning("llast == NULL\n");
481                         continue;
482                 }
483
484                 msginfo = (MsgInfo *)llast->data;
485                 msginfo->to = news_parse_xhdr(buf, msginfo);
486
487                 llast = llast->next;
488         }
489
490         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
491                 log_warning(_("can't get xhdr\n"));
492                 return newlist;
493         }
494
495         llast = newlist;
496
497         for (;;) {
498                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
499                         log_warning(_("error occurred while getting xhdr.\n"));
500                         return newlist;
501                 }
502
503                 if (buf[0] == '.' && buf[1] == '\r') break;
504                 if (!llast) {
505                         g_warning("llast == NULL\n");
506                         continue;
507                 }
508
509                 msginfo = (MsgInfo *)llast->data;
510                 msginfo->cc = news_parse_xhdr(buf, msginfo);
511
512                 llast = llast->next;
513         }
514
515         return newlist;
516 }
517
518 #define PARSE_ONE_PARAM(p, srcp) \
519 { \
520         p = strchr(srcp, '\t'); \
521         if (!p) return NULL; \
522         else \
523                 *p++ = '\0'; \
524 }
525
526 static MsgInfo *news_parse_xover(const gchar *xover_str)
527 {
528         MsgInfo *msginfo;
529         gchar buf[NNTPBUFSIZE];
530         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
531         gchar *p;
532         gint num, size_int, line_int;
533         gchar *xover_buf;
534
535         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
536         strcpy(xover_buf, xover_str);
537
538         PARSE_ONE_PARAM(subject, xover_buf);
539         PARSE_ONE_PARAM(sender, subject);
540         PARSE_ONE_PARAM(date, sender);
541         PARSE_ONE_PARAM(msgid, date);
542         PARSE_ONE_PARAM(ref, msgid);
543         PARSE_ONE_PARAM(size, ref);
544         PARSE_ONE_PARAM(line, size);
545
546         tmp = strchr(line, '\t');
547         if (!tmp) tmp = strchr(line, '\r');
548         if (!tmp) tmp = strchr(line, '\n');
549         if (tmp) *tmp = '\0';
550
551         num = atoi(xover_str);
552         size_int = atoi(size);
553         line_int = atoi(line);
554
555         /* set MsgInfo */
556         msginfo = g_new0(MsgInfo, 1);
557         msginfo->msgnum = num;
558         msginfo->size = size_int;
559
560         msginfo->date = g_strdup(date);
561         msginfo->date_t = procheader_date_parse(NULL, date, 0);
562
563         conv_unmime_header(buf, sizeof(buf), sender, NULL);
564         msginfo->from = g_strdup(buf);
565         msginfo->fromname = procheader_get_fromname(buf);
566
567         conv_unmime_header(buf, sizeof(buf), subject, NULL);
568         msginfo->subject = g_strdup(buf);
569
570         extract_parenthesis(msgid, '<', '>');
571         remove_space(msgid);
572         if (*msgid != '\0')
573                 msginfo->msgid = g_strdup(msgid);
574
575         msginfo->references = g_strdup(ref);
576         eliminate_parenthesis(ref, '(', ')');
577         if ((p = strrchr(ref, '<')) != NULL) {
578                 extract_parenthesis(p, '<', '>');
579                 remove_space(p);
580                 if (*p != '\0')
581                         msginfo->inreplyto = g_strdup(p);
582         }
583
584         return msginfo;
585 }
586
587 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
588 {
589         gchar buf[NNTPBUFSIZE];
590         gchar *p;
591         gchar *tmp;
592         gint num;
593
594         p = strchr(xhdr_str, ' ');
595         if (!p)
596                 return NULL;
597         else
598                 p++;
599
600         num = atoi(xhdr_str);
601         if (msginfo->msgnum != num) return NULL;
602
603         tmp = strchr(p, '\r');
604         if (!tmp) tmp = strchr(p, '\n');
605
606         if (tmp)
607                 return g_strndup(p, tmp - p);
608         else
609                 return g_strdup(p);
610 }
611
612 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
613                                         gint first)
614 {
615         GSList *cur, *next;
616         MsgInfo *msginfo;
617         gchar *dir;
618
619         g_return_val_if_fail(item != NULL, alist);
620         g_return_val_if_fail(item->folder != NULL, alist);
621         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
622
623         if (first < 2) return alist;
624
625         debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
626
627         dir = folder_item_get_path(item);
628         remove_numbered_files(dir, 1, first - 1);
629         g_free(dir);
630
631         for (cur = alist; cur != NULL; ) {
632                 next = cur->next;
633
634                 msginfo = (MsgInfo *)cur->data;
635                 if (msginfo && msginfo->msgnum < first) {
636                         procmsg_msginfo_free(msginfo);
637                         alist = g_slist_remove(alist, msginfo);
638                 }
639
640                 cur = next;
641         }
642
643         return alist;
644 }
645
646 static void news_delete_all_articles(FolderItem *item)
647 {
648         gchar *dir;
649
650         g_return_if_fail(item != NULL);
651         g_return_if_fail(item->folder != NULL);
652         g_return_if_fail(item->folder->type == F_NEWS);
653
654         debug_print(_("\tDeleting all cached articles... "));
655
656         dir = folder_item_get_path(item);
657         if (!is_dir_exist(dir))
658                 make_dir_hier(dir);
659         remove_all_numbered_files(dir);
660         g_free(dir);
661
662         debug_print(_("done.\n"));
663 }
664
665 /*
666   news_get_group_list returns a strings list.
667   These strings are the names of the newsgroups of a server.
668   item is the FolderItem of the news server.
669   The names of the newsgroups are cached into a file so that
670   when the function is called again, there is no need to make
671   a request to the server.
672  */
673
674 GSList * news_get_group_list(FolderItem *item)
675 {
676         gchar *path, *filename;
677         gint ok;
678         NNTPSession *session;
679         GSList * group_list = NULL;
680         FILE * f;
681         gchar buf[NNTPBUFSIZE];
682         int len;
683
684         if (item == NULL)
685           return NULL;
686
687         path = folder_item_get_path(item);
688
689         if (!is_dir_exist(path))
690                 make_dir_hier(path);
691
692         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
693         g_free(path);
694
695         session = news_session_get(item->folder);
696
697         if (session == NULL)
698           return NULL;
699
700         if (is_file_exist(filename)) {
701                 debug_print(_("group list has been already cached.\n"));
702         }
703         else {
704             ok = nntp_list(session->nntp_sock);
705             if (ok != NN_SUCCESS)
706               return NULL;
707             
708             if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
709               log_warning(_("can't retrieve group list\n"));
710               return NULL;
711             }
712         }
713
714         f = fopen(filename, "r");
715         while (fgets(buf, NNTPBUFSIZE, f)) {
716             char * s;
717
718             len = 0;
719             while ((buf[len] != 0) && (buf[len] != ' '))
720               len++;
721             buf[len] = 0;
722             s = g_strdup(buf);
723
724             group_list = g_slist_append(group_list, s);
725         }
726         fclose(f);
727         g_free(filename);
728
729         group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
730
731         return group_list;
732 }
733
734 /*
735   remove the cache file of the names of the newsgroups.
736  */
737
738 void news_reset_group_list(FolderItem *item)
739 {
740         gchar *path, *filename;
741
742         debug_print(_("\tDeleting cached group list... "));
743         path = folder_item_get_path(item);
744         if (!is_dir_exist(path))
745                 make_dir_hier(path);
746
747         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
748         g_free(path);
749         if (remove(filename) != 0)
750           log_warning(_("can't delete cached group list %s\n"), filename);
751         g_free(filename);
752 }