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