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