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