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