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