finished matching UI
[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
443                 if (!newlist)
444                         llast = newlist = g_slist_append(newlist, msginfo);
445                 else {
446                         llast = g_slist_append(llast, msginfo);
447                         llast = llast->next;
448                 }
449         }
450
451         if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
452                 log_warning(_("can't get xhdr\n"));
453                 return NULL;
454         }
455
456         llast = newlist;
457
458         for (;;) {
459                 gchar * value;
460                 
461                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
462                         log_warning(_("error occurred while getting xover.\n"));
463                         return newlist;
464                 }
465
466                 if (buf[0] == '.' && buf[1] == '\r') break;
467
468                 msginfo = (MsgInfo *) llast->data;
469
470                 value = news_parse_xhdr(buf, msginfo);
471
472                 if (value)
473                         msginfo->to = value;
474
475                 llast = llast->next;
476                 
477         }
478
479         if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
480                 log_warning(_("can't get xhdr\n"));
481                 return NULL;
482         }
483
484         llast = newlist;
485
486         for (;;) {
487                 gchar * value;
488                 
489                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
490                         log_warning(_("error occurred while getting xover.\n"));
491                         return newlist;
492                 }
493
494                 if (buf[0] == '.' && buf[1] == '\r') break;
495
496                 msginfo = (MsgInfo *) llast->data;
497
498                 value = news_parse_xhdr(buf, msginfo);
499
500                 if (value)
501                         msginfo->cc = value;
502
503                 llast = llast->next;
504         }
505
506         if (rfirst) *rfirst = first;
507         if (rlast)  *rlast  = last;
508         return newlist;
509 }
510
511 #define PARSE_ONE_PARAM(p, srcp) \
512 { \
513         p = strchr(srcp, '\t'); \
514         if (!p) return NULL; \
515         else \
516                 *p++ = '\0'; \
517 }
518
519 static MsgInfo *news_parse_xover(const gchar *xover_str)
520 {
521         MsgInfo *msginfo;
522         gchar buf[NNTPBUFSIZE];
523         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
524         gchar *p;
525         gint num, size_int, line_int;
526         gchar *xover_buf;
527
528         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
529         strcpy(xover_buf, xover_str);
530
531         PARSE_ONE_PARAM(subject, xover_buf);
532         PARSE_ONE_PARAM(sender, subject);
533         PARSE_ONE_PARAM(date, sender);
534         PARSE_ONE_PARAM(msgid, date);
535         PARSE_ONE_PARAM(ref, msgid);
536         PARSE_ONE_PARAM(size, ref);
537         PARSE_ONE_PARAM(line, size);
538
539         tmp = strchr(line, '\t');
540         if (!tmp) tmp = strchr(line, '\r');
541         if (!tmp) tmp = strchr(line, '\n');
542         if (tmp) *tmp = '\0';
543
544         num = atoi(xover_str);
545         size_int = atoi(size);
546         line_int = atoi(line);
547
548         /* set MsgInfo */
549         msginfo = g_new0(MsgInfo, 1);
550         msginfo->msgnum = num;
551         msginfo->size = size_int;
552
553         msginfo->date = g_strdup(date);
554         msginfo->date_t = procheader_date_parse(NULL, date, 0);
555
556         conv_unmime_header(buf, sizeof(buf), sender, NULL);
557         msginfo->from = g_strdup(buf);
558         msginfo->fromname = procheader_get_fromname(buf);
559
560         conv_unmime_header(buf, sizeof(buf), subject, NULL);
561         msginfo->subject = g_strdup(buf);
562
563         extract_parenthesis(msgid, '<', '>');
564         remove_space(msgid);
565         if (*msgid != '\0')
566                 msginfo->msgid = g_strdup(msgid);
567
568         eliminate_parenthesis(ref, '(', ')');
569         if ((p = strrchr(ref, '<')) != NULL) {
570                 extract_parenthesis(p, '<', '>');
571                 remove_space(p);
572                 if (*p != '\0')
573                         msginfo->inreplyto = g_strdup(p);
574         }
575
576         return msginfo;
577 }
578
579 static gchar * news_parse_xhdr(const gchar *xover_str, MsgInfo * info)
580 {
581         gchar buf[NNTPBUFSIZE];
582         gchar *p;
583         gint num;
584
585         p = strchr(xover_str, ' ');
586         if (!p)
587                 return NULL;
588         else
589                 p++;
590
591         num = atoi(xover_str);
592
593         if (info->msgnum != num)
594                 return NULL;
595
596         return g_strdup(p);
597 }
598
599 static GSList *news_delete_old_article(GSList *alist, gint first)
600 {
601         GSList *cur, *next;
602         MsgInfo *msginfo;
603         gchar *cache_file;
604
605         if (first < 2) return alist;
606
607         for (cur = alist; cur != NULL; ) {
608                 next = cur->next;
609
610                 msginfo = (MsgInfo *)cur->data;
611                 if (msginfo && msginfo->msgnum < first) {
612                         debug_print(_("deleting article %d...\n"),
613                                     msginfo->msgnum);
614
615                         cache_file = procmsg_get_message_file_path(msginfo);
616                         if (is_file_exist(cache_file)) unlink(cache_file);
617                         g_free(cache_file);
618
619                         procmsg_msginfo_free(msginfo);
620                         alist = g_slist_remove(alist, msginfo);
621                 }
622
623                 cur = next;
624         }
625
626         return alist;
627 }
628
629 static void news_delete_all_article(FolderItem *item)
630 {
631         DIR *dp;
632         struct dirent *d;
633         gchar *dir;
634         gchar *file;
635
636         dir = folder_item_get_path(item);
637         if ((dp = opendir(dir)) == NULL) {
638                 FILE_OP_ERROR(dir, "opendir");
639                 g_free(dir);
640                 return;
641         }
642
643         debug_print(_("\tDeleting all cached articles... "));
644
645         while ((d = readdir(dp)) != NULL) {
646                 if (to_number(d->d_name) < 0) continue;
647
648                 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
649
650                 if (is_file_exist(file)) {
651                         if (unlink(file) < 0)
652                                 FILE_OP_ERROR(file, "unlink");
653                 }
654
655                 g_free(file);
656         }
657
658         closedir(dp);
659         g_free(dir);
660
661         debug_print(_("done.\n"));
662 }
663
664 /*
665   news_get_group_list returns a strings list.
666   These strings are the names of the newsgroups of a server.
667   item is the FolderItem of the news server.
668   The names of the newsgroups are cached into a file so that
669   when the function is called again, there is no need to make
670   a request to the server.
671  */
672
673 GSList * news_get_group_list(FolderItem *item)
674 {
675         gchar *path, *filename;
676         gint ok;
677         NNTPSession *session;
678         GSList * group_list = NULL;
679         FILE * f;
680         gchar buf[NNTPBUFSIZE];
681         int len;
682
683         if (item == NULL)
684           return NULL;
685
686         path = folder_item_get_path(item);
687
688         if (!is_dir_exist(path))
689                 make_dir_hier(path);
690
691         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
692         g_free(path);
693
694         session = news_session_get(item->folder);
695
696         if (session == NULL)
697           return NULL;
698
699         if (is_file_exist(filename)) {
700                 debug_print(_("group list has been already cached.\n"));
701         }
702         else {
703             ok = nntp_list(session->nntp_sock);
704             if (ok != NN_SUCCESS)
705               return NULL;
706             
707             if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
708               log_warning(_("can't retrieve group list\n"));
709               return NULL;
710             }
711         }
712
713         f = fopen(filename, "r");
714         while (fgets(buf, NNTPBUFSIZE, f)) {
715             char * s;
716
717             len = 0;
718             while ((buf[len] != 0) && (buf[len] != ' '))
719               len++;
720             buf[len] = 0;
721             s = g_strdup(buf);
722
723             group_list = g_slist_append(group_list, s);
724         }
725         fclose(f);
726         g_free(filename);
727
728         group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
729
730         return group_list;
731 }
732
733 /*
734   remove the cache file of the names of the newsgroups.
735  */
736
737 void news_reset_group_list(FolderItem *item)
738 {
739         gchar *path, *filename;
740
741         debug_print(_("\tDeleting cached group list... "));
742         path = folder_item_get_path(item);
743         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
744         g_free(path);
745         if (remove(filename) != 0)
746           log_warning(_("can't delete cached group list %s\n"), filename);
747         g_free(filename);
748 }