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