fixed segfault
[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.perm_flags = MSG_NEW|MSG_UNREAD;
542                 msginfo->flags.tmp_flags = MSG_NEWS;
543                 msginfo->newsgroups = g_strdup(item->path);
544
545                 if (!newlist)
546                         llast = newlist = g_slist_append(newlist, msginfo);
547                 else {
548                         llast = g_slist_append(llast, msginfo);
549                         llast = llast->next;
550                 }
551         }
552
553         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
554                 log_warning(_("can't get xhdr\n"));
555                 return newlist;
556         }
557
558         llast = newlist;
559
560         for (;;) {
561                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
562                         log_warning(_("error occurred while getting xhdr.\n"));
563                         return newlist;
564                 }
565
566                 if (buf[0] == '.' && buf[1] == '\r') break;
567                 if (!llast) {
568                         g_warning("llast == NULL\n");
569                         continue;
570                 }
571
572                 msginfo = (MsgInfo *)llast->data;
573                 msginfo->to = news_parse_xhdr(buf, msginfo);
574
575                 llast = llast->next;
576         }
577
578         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
579                 log_warning(_("can't get xhdr\n"));
580                 return newlist;
581         }
582
583         llast = newlist;
584
585         for (;;) {
586                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
587                         log_warning(_("error occurred while getting xhdr.\n"));
588                         return newlist;
589                 }
590
591                 if (buf[0] == '.' && buf[1] == '\r') break;
592                 if (!llast) {
593                         g_warning("llast == NULL\n");
594                         continue;
595                 }
596
597                 msginfo = (MsgInfo *)llast->data;
598                 msginfo->cc = news_parse_xhdr(buf, msginfo);
599
600                 llast = llast->next;
601         }
602
603         return newlist;
604 }
605
606 #define PARSE_ONE_PARAM(p, srcp) \
607 { \
608         p = strchr(srcp, '\t'); \
609         if (!p) return NULL; \
610         else \
611                 *p++ = '\0'; \
612 }
613
614 static MsgInfo *news_parse_xover(const gchar *xover_str)
615 {
616         MsgInfo *msginfo;
617         gchar buf[NNTPBUFSIZE];
618         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
619         gchar *p;
620         gint num, size_int, line_int;
621         gchar *xover_buf;
622
623         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
624         strcpy(xover_buf, xover_str);
625
626         PARSE_ONE_PARAM(subject, xover_buf);
627         PARSE_ONE_PARAM(sender, subject);
628         PARSE_ONE_PARAM(date, sender);
629         PARSE_ONE_PARAM(msgid, date);
630         PARSE_ONE_PARAM(ref, msgid);
631         PARSE_ONE_PARAM(size, ref);
632         PARSE_ONE_PARAM(line, size);
633
634         tmp = strchr(line, '\t');
635         if (!tmp) tmp = strchr(line, '\r');
636         if (!tmp) tmp = strchr(line, '\n');
637         if (tmp) *tmp = '\0';
638
639         num = atoi(xover_str);
640         size_int = atoi(size);
641         line_int = atoi(line);
642
643         /* set MsgInfo */
644         msginfo = g_new0(MsgInfo, 1);
645         msginfo->msgnum = num;
646         msginfo->size = size_int;
647
648         msginfo->date = g_strdup(date);
649         msginfo->date_t = procheader_date_parse(NULL, date, 0);
650
651         conv_unmime_header(buf, sizeof(buf), sender, NULL);
652         msginfo->from = g_strdup(buf);
653         msginfo->fromname = procheader_get_fromname(buf);
654
655         conv_unmime_header(buf, sizeof(buf), subject, NULL);
656         msginfo->subject = g_strdup(buf);
657
658         extract_parenthesis(msgid, '<', '>');
659         remove_space(msgid);
660         if (*msgid != '\0')
661                 msginfo->msgid = g_strdup(msgid);
662
663         msginfo->references = g_strdup(ref);
664         eliminate_parenthesis(ref, '(', ')');
665         if ((p = strrchr(ref, '<')) != NULL) {
666                 extract_parenthesis(p, '<', '>');
667                 remove_space(p);
668                 if (*p != '\0')
669                         msginfo->inreplyto = g_strdup(p);
670         }
671
672         return msginfo;
673 }
674
675 static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
676 {
677         gchar *p;
678         gchar *tmp;
679         gint num;
680
681         p = strchr(xhdr_str, ' ');
682         if (!p)
683                 return NULL;
684         else
685                 p++;
686
687         num = atoi(xhdr_str);
688         if (msginfo->msgnum != num) return NULL;
689
690         tmp = strchr(p, '\r');
691         if (!tmp) tmp = strchr(p, '\n');
692
693         if (tmp)
694                 return g_strndup(p, tmp - p);
695         else
696                 return g_strdup(p);
697 }
698
699 static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
700                                         gint first)
701 {
702         GSList *cur, *next;
703         MsgInfo *msginfo;
704         gchar *dir;
705
706         g_return_val_if_fail(item != NULL, alist);
707         g_return_val_if_fail(item->folder != NULL, alist);
708         g_return_val_if_fail(item->folder->type == F_NEWS, alist);
709
710         if (first < 2) return alist;
711
712         debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
713
714         dir = folder_item_get_path(item);
715         remove_numbered_files(dir, 1, first - 1);
716         g_free(dir);
717
718         for (cur = alist; cur != NULL; ) {
719                 next = cur->next;
720
721                 msginfo = (MsgInfo *)cur->data;
722                 if (msginfo && msginfo->msgnum < first) {
723                         procmsg_msginfo_free(msginfo);
724                         alist = g_slist_remove(alist, msginfo);
725                 }
726
727                 cur = next;
728         }
729
730         return alist;
731 }
732
733 static void news_delete_all_articles(FolderItem *item)
734 {
735         gchar *dir;
736
737         g_return_if_fail(item != NULL);
738         g_return_if_fail(item->folder != NULL);
739         g_return_if_fail(item->folder->type == F_NEWS);
740
741         debug_print(_("\tDeleting all cached articles... "));
742
743         dir = folder_item_get_path(item);
744         if (!is_dir_exist(dir))
745                 make_dir_hier(dir);
746         remove_all_numbered_files(dir);
747         g_free(dir);
748
749         debug_print(_("done.\n"));
750 }