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