dropped PSPELL_PATH macro
[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                 statusbar_pop_all();
176                 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
177         }
178
179         session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
180
181         if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
182                 log_warning(_("NNTP connection to %s:%d has been"
183                               " disconnected. Reconnecting...\n"),
184                             folder->account->nntp_server,
185                             folder->account->set_nntpport ?
186                             folder->account->nntpport : NNTP_PORT);
187                 session_destroy(REMOTE_FOLDER(folder)->session);
188                 REMOTE_FOLDER(folder)->session =
189                         news_session_new_for_folder(folder);
190         }
191
192         statusbar_pop_all();
193         return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
194 }
195
196 GSList *news_get_article_list(Folder *folder, FolderItem *item,
197                               gboolean use_cache)
198 {
199         GSList *alist;
200         NNTPSession *session;
201
202         g_return_val_if_fail(folder != NULL, NULL);
203         g_return_val_if_fail(item != NULL, NULL);
204         g_return_val_if_fail(folder->type == F_NEWS, NULL);
205
206         session = news_session_get(folder);
207
208         if (!session) {
209                 alist = procmsg_read_cache(item, FALSE);
210                 item->last_num = procmsg_get_last_num_in_cache(alist);
211         } else if (use_cache) {
212                 GSList *newlist;
213                 gint cache_last;
214                 gint first, last;
215
216                 alist = procmsg_read_cache(item, FALSE);
217
218                 cache_last = procmsg_get_last_num_in_cache(alist);
219                 newlist = news_get_uncached_articles
220                         (session, item, cache_last, &first, &last);
221                 alist = news_delete_old_articles(alist, item, first);
222
223                 alist = g_slist_concat(alist, newlist);
224                 item->last_num = last;
225         } else {
226                 gint last;
227
228                 alist = news_get_uncached_articles
229                         (session, item, 0, NULL, &last);
230                 news_delete_all_articles(item);
231                 item->last_num = last;
232         }
233
234         procmsg_set_flags(alist, item);
235
236         statusbar_pop_all();
237
238         return alist;
239 }
240
241 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
242 {
243         gchar *path, *filename;
244         NNTPSession *session;
245         gint ok;
246
247         g_return_val_if_fail(folder != NULL, NULL);
248         g_return_val_if_fail(item != NULL, NULL);
249
250         path = folder_item_get_path(item);
251         if (!is_dir_exist(path))
252                 make_dir_hier(path);
253         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
254         g_free(path);
255
256         if (is_file_exist(filename)) {
257                 debug_print(_("article %d has been already cached.\n"), num);
258                 return filename;
259         }
260
261         session = news_session_get(folder);
262         if (!session) {
263                 g_free(filename);
264                 return NULL;
265         }
266
267         ok = news_select_group(session, item->path);
268         statusbar_pop_all();
269         if (ok != NN_SUCCESS) {
270                 g_warning(_("can't select group %s\n"), item->path);
271                 g_free(filename);
272                 return NULL;
273         }
274
275         debug_print(_("getting article %d...\n"), num);
276         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
277                               num, filename);
278         statusbar_pop_all();
279         if (ok < 0) {
280                 g_warning(_("can't read article %d\n"), num);
281                 g_free(filename);
282                 return NULL;
283         }
284
285         return filename;
286 }
287
288 void news_scan_group(Folder *folder, FolderItem *item)
289 {
290 }
291
292 GSList *news_get_group_list(Folder *folder)
293 {
294         gchar *path, *filename;
295         FILE *fp;
296         GSList *list = NULL;
297         GSList *last = NULL;
298         gchar buf[NNTPBUFSIZE];
299
300         g_return_val_if_fail(folder != NULL, NULL);
301         g_return_val_if_fail(folder->type == F_NEWS, NULL);
302
303         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
304         if (!is_dir_exist(path))
305                 make_dir_hier(path);
306         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
307         g_free(path);
308
309         if ((fp = fopen(filename, "r")) == NULL) {
310                 NNTPSession *session;
311
312                 session = news_session_get(folder);
313                 if (!session) {
314                         g_free(filename);
315                         return NULL;
316                 }
317
318                 if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
319                         g_free(filename);
320                         statusbar_pop_all();
321                         return NULL;
322                 }
323                 statusbar_pop_all();
324                 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
325                         log_warning(_("can't retrieve newsgroup list\n"));
326                         g_free(filename);
327                         return NULL;
328                 }
329
330                 if ((fp = fopen(filename, "r")) == NULL) {
331                         FILE_OP_ERROR(filename, "fopen");
332                         g_free(filename);
333                         return NULL;
334                 }
335         }
336
337         while (fgets(buf, sizeof(buf), fp) != NULL) {
338                 gchar *p = buf;
339                 while (*p != '\0' && *p != ' ') p++;
340                 *p = '\0';
341                 if (!last)
342                         last = list = g_slist_append(NULL, g_strdup(buf));
343                 else {
344                         last = g_slist_append(last, g_strdup(buf));
345                         last = last->next;
346                 }
347         }
348
349         fclose(fp);
350         g_free(filename);
351
352         list = g_slist_sort(list, (GCompareFunc)g_strcasecmp);
353
354         statusbar_pop_all();
355
356         return list;
357 }
358
359 void news_remove_group_list(Folder *folder)
360 {
361         gchar *path, *filename;
362
363         g_return_if_fail(folder != NULL);
364         g_return_if_fail(folder->type == F_NEWS);
365
366         path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
367         filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
368         g_free(path);
369
370         if (is_file_exist(filename)) {
371                 if (remove(filename) < 0)
372                         FILE_OP_ERROR(filename, "remove");
373         }
374         g_free(filename);
375 }
376
377 gint news_post(Folder *folder, const gchar *file)
378 {
379         NNTPSession *session;
380         FILE *fp;
381         gint ok;
382
383         g_return_val_if_fail(folder != NULL, -1);
384         g_return_val_if_fail(folder->type == F_NEWS, -1);
385         g_return_val_if_fail(file != NULL, -1);
386
387         session = news_session_get(folder);
388         if (!session) return -1;
389
390         if ((fp = fopen(file, "r")) == NULL) {
391                 FILE_OP_ERROR(file, "fopen");
392                 return -1;
393         }
394
395         ok = nntp_post(session->nntp_sock, fp);
396         if (ok != NN_SUCCESS) {
397                 log_warning(_("can't post article.\n"));
398                 return -1;
399         }
400
401         fclose(fp);
402
403         statusbar_pop_all();
404
405         return 0;
406 }
407
408 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
409                                  gint num, gchar *filename)
410 {
411         gchar *msgid;
412
413         if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
414             != NN_SUCCESS)
415                 return -1;
416
417         debug_print("Message-Id = %s, num = %d\n", msgid, num);
418         g_free(msgid);
419
420         if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
421                 log_warning(_("can't retrieve article %d\n"), num);
422                 return -1;
423         }
424
425         return 0;
426 }
427
428 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
429 {
430         return news_get_article_cmd(session, "ARTICLE", num, filename);
431 }
432
433 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
434 {
435         return news_get_article_cmd(session, "HEAD", num, filename);
436 }
437
438 /**
439  * news_select_group:
440  * @session: Active NNTP session.
441  * @group: Newsgroup name.
442  *
443  * Select newsgroup @group with the GROUP command if it is not already
444  * selected in @session.
445  *
446  * Return value: NNTP result code.
447  **/
448 static gint news_select_group(NNTPSession *session, const gchar *group)
449 {
450         gint ok;
451         gint num, first, last;
452
453         if (session->group && g_strcasecmp(session->group, group) == 0)
454                 return NN_SUCCESS;
455
456         g_free(session->group);
457         session->group = NULL;
458
459         ok = nntp_group(session->nntp_sock, group, &num, &first, &last);
460         if (ok == NN_SUCCESS)
461                 session->group = g_strdup(group);
462
463         return ok;
464 }
465
466 static GSList *news_get_uncached_articles(NNTPSession *session,
467                                           FolderItem *item, gint cache_last,
468                                           gint *rfirst, gint *rlast)
469 {
470         gint ok;
471         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
472         gchar buf[NNTPBUFSIZE];
473         GSList *newlist = NULL;
474         GSList *llast = NULL;
475         MsgInfo *msginfo;
476
477         if (rfirst) *rfirst = 0;
478         if (rlast)  *rlast  = 0;
479
480         g_return_val_if_fail(session != NULL, NULL);
481         g_return_val_if_fail(item != NULL, NULL);
482         g_return_val_if_fail(item->folder != NULL, NULL);
483         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
484
485         g_free(session->group);
486         session->group = NULL;
487
488         ok = nntp_group(session->nntp_sock, item->path,
489                         &num, &first, &last);
490         if (ok != NN_SUCCESS) {
491                 log_warning(_("can't set group: %s\n"), item->path);
492                 return NULL;
493         }
494         session->group = g_strdup(item->path);
495
496         /* calculate getting overview range */
497         if (first > last) {
498                 log_warning(_("invalid article range: %d - %d\n"),
499                             first, last);
500                 return NULL;
501         }
502         if (cache_last < first)
503                 begin = first;
504         else if (last < cache_last)
505                 begin = first;
506         else if (last == cache_last) {
507                 debug_print(_("no new articles.\n"));
508                 return NULL;
509         } else
510                 begin = cache_last + 1;
511         end = last;
512
513         if (rfirst) *rfirst = first;
514         if (rlast)  *rlast  = last;
515
516         if (prefs_common.max_articles > 0 &&
517             end - begin + 1 > prefs_common.max_articles)
518                 begin = end - prefs_common.max_articles + 1;
519
520         log_message(_("getting xover %d - %d in %s...\n"),
521                     begin, end, item->path);
522         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
523                 log_warning(_("can't get xover\n"));
524                 return NULL;
525         }
526
527         for (;;) {
528                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
529                         log_warning(_("error occurred while getting xover.\n"));
530                         return newlist;
531                 }
532
533                 if (buf[0] == '.' && buf[1] == '\r') break;
534
535                 msginfo = news_parse_xover(buf);
536                 if (!msginfo) {
537                         log_warning(_("invalid xover line: %s\n"), buf);
538                         continue;
539                 }
540
541                 msginfo->folder = item;
542                 msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
543                 msginfo->flags.tmp_flags = MSG_NEWS;
544                 msginfo->newsgroups = g_strdup(item->path);
545
546                 if (!newlist)
547                         llast = newlist = g_slist_append(newlist, msginfo);
548                 else {
549                         llast = g_slist_append(llast, msginfo);
550                         llast = llast->next;
551                 }
552         }
553
554         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
555                 log_warning(_("can't get xhdr\n"));
556                 return newlist;
557         }
558
559         llast = newlist;
560
561         for (;;) {
562                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
563                         log_warning(_("error occurred while getting xhdr.\n"));
564                         return newlist;
565                 }
566
567                 if (buf[0] == '.' && buf[1] == '\r') break;
568                 if (!llast) {
569                         g_warning("llast == NULL\n");
570                         continue;
571                 }
572
573                 msginfo = (MsgInfo *)llast->data;
574                 msginfo->to = news_parse_xhdr(buf, msginfo);
575
576                 llast = llast->next;
577         }
578
579         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
580                 log_warning(_("can't get xhdr\n"));
581                 return newlist;
582         }
583
584         llast = newlist;
585
586         for (;;) {
587                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
588                         log_warning(_("error occurred while getting xhdr.\n"));
589                         return newlist;
590                 }
591
592                 if (buf[0] == '.' && buf[1] == '\r') break;
593                 if (!llast) {
594                         g_warning("llast == NULL\n");
595                         continue;
596                 }
597
598                 msginfo = (MsgInfo *)llast->data;
599                 msginfo->cc = news_parse_xhdr(buf, msginfo);
600
601                 llast = llast->next;
602         }
603
604         return newlist;
605 }
606
607 #define PARSE_ONE_PARAM(p, srcp) \
608 { \
609         p = strchr(srcp, '\t'); \
610         if (!p) return NULL; \
611         else \
612                 *p++ = '\0'; \
613 }
614
615 static MsgInfo *news_parse_xover(const gchar *xover_str)
616 {
617         MsgInfo *msginfo;
618         gchar buf[NNTPBUFSIZE];
619         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
620         gchar *p;
621         gint num, size_int, line_int;
622         gchar *xover_buf;
623
624         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
625         strcpy(xover_buf, xover_str);
626
627         PARSE_ONE_PARAM(subject, xover_buf);
628         PARSE_ONE_PARAM(sender, subject);
629         PARSE_ONE_PARAM(date, sender);
630         PARSE_ONE_PARAM(msgid, date);
631         PARSE_ONE_PARAM(ref, msgid);
632         PARSE_ONE_PARAM(size, ref);
633         PARSE_ONE_PARAM(line, size);
634
635         tmp = strchr(line, '\t');
636         if (!tmp) tmp = strchr(line, '\r');
637         if (!tmp) tmp = strchr(line, '\n');
638         if (tmp) *tmp = '\0';
639
640         num = atoi(xover_str);
641         size_int = atoi(size);
642         line_int = atoi(line);
643
644         /* set MsgInfo */
645         msginfo = g_new0(MsgInfo, 1);
646         msginfo->msgnum = num;
647         msginfo->size = size_int;
648
649         msginfo->date = g_strdup(date);
650         msginfo->date_t = procheader_date_parse(NULL, date, 0);
651
652         conv_unmime_header(buf, sizeof(buf), sender, NULL);
653         msginfo->from = g_strdup(buf);
654         msginfo->fromname = procheader_get_fromname(buf);
655
656         conv_unmime_header(buf, sizeof(buf), subject, NULL);
657         msginfo->subject = g_strdup(buf);
658
659         extract_parenthesis(msgid, '<', '>');
660         remove_space(msgid);
661         if (*msgid != '\0')
662                 msginfo->msgid = g_strdup(msgid);
663
664         msginfo->references = g_strdup(ref);
665         eliminate_parenthesis(ref, '(', ')');
666         if ((p = strrchr(ref, '<')) != NULL) {
667                 extract_parenthesis(p, '<', '>');
668                 remove_space(p);
669                 if (*p != '\0')
670                         msginfo->inreplyto = g_strdup(p);
671         }
672
673         return msginfo;
674 }
675
676 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
677 {
678         gchar *p;
679         gchar *tmp;
680         gint num;
681
682         p = strchr(xhdr_str, ' ');
683         if (!p)
684                 return NULL;
685         else
686                 p++;
687
688         num = atoi(xhdr_str);
689         if (msginfo->msgnum != num) return NULL;
690
691         tmp = strchr(p, '\r');
692         if (!tmp) tmp = strchr(p, '\n');
693
694         if (tmp)
695                 return g_strndup(p, tmp - p);
696         else
697                 return g_strdup(p);
698 }
699
700 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
701                                         gint first)
702 {
703         GSList *cur, *next;
704         MsgInfo *msginfo;
705         gchar *dir;
706
707         g_return_val_if_fail(item != NULL, alist);
708         g_return_val_if_fail(item->folder != NULL, alist);
709         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
710
711         if (first < 2) return alist;
712
713         debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
714
715         dir = folder_item_get_path(item);
716         remove_numbered_files(dir, 1, first - 1);
717         g_free(dir);
718
719         for (cur = alist; cur != NULL; ) {
720                 next = cur->next;
721
722                 msginfo = (MsgInfo *)cur->data;
723                 if (msginfo && msginfo->msgnum < first) {
724                         procmsg_msginfo_free(msginfo);
725                         alist = g_slist_remove(alist, msginfo);
726                 }
727
728                 cur = next;
729         }
730
731         return alist;
732 }
733
734 static void news_delete_all_articles(FolderItem *item)
735 {
736         gchar *dir;
737
738         g_return_if_fail(item != NULL);
739         g_return_if_fail(item->folder != NULL);
740         g_return_if_fail(item->folder->type == F_NEWS);
741
742         debug_print(_("\tDeleting all cached articles... "));
743
744         dir = folder_item_get_path(item);
745         remove_all_numbered_files(dir);
746         g_free(dir);
747
748         debug_print(_("done.\n"));
749 }