sync with sylpheed 0.4.65cvs10
[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 <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <dirent.h>
29 #include <unistd.h>
30
31 #include "defs.h"
32 #include "intl.h"
33 #include "news.h"
34 #include "nntp.h"
35 #include "socket.h"
36 #include "recv.h"
37 #include "procmsg.h"
38 #include "procheader.h"
39 #include "folder.h"
40 #include "session.h"
41 #include "statusbar.h"
42 #include "codeconv.h"
43 #include "utils.h"
44 #include "prefs_common.h"
45 #include "prefs_account.h"
46 #include "inputdialog.h"
47 #include "alertpanel.h"
48
49 static gint news_get_article_cmd         (NNTPSession   *session,
50                                           const gchar   *cmd,
51                                           gint           num,
52                                           gchar         *filename);
53 static gint news_get_article             (NNTPSession   *session,
54                                           gint           num,
55                                           gchar         *filename);
56 static gint news_get_header              (NNTPSession   *session,
57                                           gint           num,
58                                           gchar         *filename);
59
60 static GSList *news_get_uncached_articles(NNTPSession   *session,
61                                           FolderItem    *item,
62                                           gint           cache_last,
63                                           gint          *rfirst,
64                                           gint          *rlast);
65 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
66 static GSList *news_delete_old_article   (GSList        *alist,
67                                           gint           first);
68 static void news_delete_all_article      (FolderItem    *item);
69
70
71 Session *news_session_new(const gchar *server, gushort port)
72 {
73         gchar buf[NNTPBUFSIZE];
74         NNTPSession *session;
75         SockInfo *nntp_sock;
76
77         g_return_val_if_fail(server != NULL, NULL);
78
79         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
80
81         if ((nntp_sock = nntp_open(server, port, buf)) < 0)
82                 return NULL;
83
84         session = g_new(NNTPSession, 1);
85         SESSION(session)->type      = SESSION_NEWS;
86         SESSION(session)->server    = g_strdup(server);
87         SESSION(session)->sock      = nntp_sock;
88         SESSION(session)->connected = TRUE;
89         SESSION(session)->phase     = SESSION_READY;
90         SESSION(session)->data      = NULL;
91         session->group = NULL;
92
93         return SESSION(session);
94 }
95
96 void news_session_destroy(NNTPSession *session)
97 {
98         sock_close(SESSION(session)->sock);
99         SESSION(session)->sock = NULL;
100
101         g_free(session->group);
102 }
103
104 NNTPSession *news_session_get(Folder *folder)
105 {
106         g_return_val_if_fail(folder != NULL, NULL);
107         g_return_val_if_fail(folder->type == F_NEWS, NULL);
108         g_return_val_if_fail(folder->account != NULL, NULL);
109
110         if (!REMOTE_FOLDER(folder)->session) {
111                 REMOTE_FOLDER(folder)->session =
112                         news_session_new(folder->account->nntp_server, 119);
113         } else {
114                 if (nntp_mode(REMOTE_FOLDER(folder)->session->sock, FALSE)
115                     != NN_SUCCESS) {
116                         log_warning(_("NNTP connection to %s:%d has been"
117                                       " disconnected. Reconnecting...\n"),
118                                     folder->account->nntp_server, 119);
119                         session_destroy(REMOTE_FOLDER(folder)->session);
120                         REMOTE_FOLDER(folder)->session =
121                                 news_session_new(folder->account->nntp_server,
122                                                  119);
123                 }
124         }
125
126         return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
127 }
128
129 GSList *news_get_article_list(Folder *folder, FolderItem *item,
130                               gboolean use_cache)
131 {
132         GSList *alist;
133         NNTPSession *session;
134
135         g_return_val_if_fail(folder != NULL, NULL);
136         g_return_val_if_fail(item != NULL, NULL);
137         g_return_val_if_fail(folder->type == F_NEWS, NULL);
138
139         session = news_session_get(folder);
140
141         if (!session) {
142                 alist = procmsg_read_cache(item, FALSE);
143                 item->last_num = procmsg_get_last_num_in_cache(alist);
144         } else if (use_cache) {
145                 GSList *newlist;
146                 gint cache_last;
147                 gint first, last;
148
149                 alist = procmsg_read_cache(item, FALSE);
150
151                 cache_last = procmsg_get_last_num_in_cache(alist);
152                 newlist = news_get_uncached_articles
153                         (session, item, cache_last, &first, &last);
154                 alist = news_delete_old_article(alist, first);
155
156                 alist = g_slist_concat(alist, newlist);
157                 item->last_num = last;
158         } else {
159                 gint last;
160
161                 alist = news_get_uncached_articles
162                         (session, item, 0, NULL, &last);
163                 news_delete_all_article(item);
164                 item->last_num = last;
165         }
166
167         procmsg_set_flags(alist, item);
168
169         statusbar_pop_all();
170
171         return alist;
172 }
173
174 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
175 {
176         gchar *path, *filename;
177         gint ok;
178
179         g_return_val_if_fail(folder != NULL, NULL);
180         g_return_val_if_fail(item != NULL, NULL);
181
182         path = folder_item_get_path(item);
183         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
184         g_free(path);
185
186         if (is_file_exist(filename)) {
187                 debug_print(_("article %d has been already cached.\n"), num);
188                 return filename;
189         }
190
191         if (!REMOTE_FOLDER(folder)->session) {
192                 g_free(filename);
193                 return NULL;
194         }
195
196         debug_print(_("getting article %d...\n"), num);
197         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
198                               num, filename);
199         statusbar_pop_all();
200         if (ok < 0) {
201                 g_warning(_("can't read article %d\n"), num);
202                 g_free(filename);
203                 return NULL;
204         }
205
206         return filename;
207 }
208
209 void news_scan_group(Folder *folder, FolderItem *item)
210 {
211 }
212
213 gint news_post(Folder *folder, const gchar *file)
214 {
215         NNTPSession *session;
216         FILE *fp;
217         gint ok;
218
219         g_return_val_if_fail(folder != NULL, -1);
220         g_return_val_if_fail(folder->type == F_NEWS, -1);
221         g_return_val_if_fail(file != NULL, -1);
222
223         session = news_session_get(folder);
224         if (!session) return -1;
225
226         if ((fp = fopen(file, "r")) == NULL) {
227                 FILE_OP_ERROR(file, "fopen");
228                 return -1;
229         }
230
231         ok = nntp_post(SESSION(session)->sock, fp);
232         if (ok != NN_SUCCESS) {
233                 log_warning(_("can't post article.\n"));
234                 return -1;
235         }
236
237         fclose(fp);
238
239         statusbar_pop_all();
240
241         return 0;
242 }
243
244 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
245                                  gint num, gchar *filename)
246 {
247         gchar *msgid;
248
249         if (nntp_get_article(SESSION(session)->sock, cmd, num, &msgid)
250             != NN_SUCCESS)
251                 return -1;
252
253         debug_print("Message-Id = %s, num = %d\n", msgid, num);
254         g_free(msgid);
255
256         if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
257                 log_warning(_("can't retrieve article %d\n"), num);
258                 return -1;
259         }
260
261         return 0;
262 }
263
264 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
265 {
266         return news_get_article_cmd(session, "ARTICLE", num, filename);
267 }
268
269 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
270 {
271         return news_get_article_cmd(session, "HEAD", num, filename);
272 }
273
274 static gchar *news_query_password(NNTPSession *session,
275                                   const gchar *user)
276 {
277         gchar *message;
278         gchar *pass;
279
280         message = g_strdup_printf
281                 (_("Input password for %s on %s:"),
282                  user,
283                  SESSION(session)->server);
284
285         pass = input_dialog_with_invisible(_("Input password"),
286                                            message, NULL);
287         g_free(message);
288 /*      manage_window_focus_in(inc_dialog->mainwin->window, */
289 /*                             NULL, NULL); */
290         return pass;
291 }
292
293 static gint news_authenticate(NNTPSession *session,
294                               FolderItem *item)
295 {
296         gint ok;
297         const gchar *user;
298         gchar *pass;
299         gboolean need_free_pass = FALSE;
300
301         debug_print(_("news server requested authentication\n"));
302         if (!item || !item->folder || !item->folder->account)
303                 return NN_ERROR;
304         user = item->folder->account->userid;
305         if (!user)
306                 return NN_ERROR;
307         ok = nntp_authinfo_user(SESSION(session)->sock, user);
308         if (ok == NN_AUTHCONT) {
309                 pass = item->folder->account->passwd;
310                 if (!pass || !pass[0]) {
311                         pass = news_query_password(session, user);
312                         need_free_pass = TRUE;
313                 }
314                 ok = nntp_authinfo_pass(SESSION(session)->sock, pass);
315                 if (need_free_pass)
316                         g_free(pass);
317         }
318         if (ok != NN_SUCCESS) {
319                 log_warning(_("NNTP authentication failed\n"));
320                 alertpanel_error(_("Authentication for %s on %s failed."),
321                                  user, SESSION(session)->server);
322         }
323         return ok;
324 }
325
326 static GSList *news_get_uncached_articles(NNTPSession *session,
327                                           FolderItem *item, gint cache_last,
328                                           gint *rfirst, gint *rlast)
329 {
330         gint ok;
331         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
332         gchar buf[NNTPBUFSIZE];
333         GSList *newlist = NULL;
334         GSList *llast = NULL;
335         MsgInfo *msginfo;
336
337         if (rfirst) *rfirst = 0;
338         if (rlast)  *rlast  = 0;
339
340         g_return_val_if_fail(session != NULL, NULL);
341         g_return_val_if_fail(item != NULL, NULL);
342         g_return_val_if_fail(item->folder != NULL, NULL);
343         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
344
345         ok = nntp_group(SESSION(session)->sock, item->path,
346                         &num, &first, &last);
347         if (ok == NN_AUTHREQ) {
348                 ok = news_authenticate(session, item);
349                 if (ok != NN_SUCCESS)
350                         return NULL;
351                 ok = nntp_group(SESSION(session)->sock, item->path,
352                                 &num, &first, &last);
353         }
354         if (ok != NN_SUCCESS) {
355                 log_warning(_("can't set group: %s\n"), item->path);
356                 return NULL;
357         }
358
359         /* calculate getting overview range */
360         if (first > last) {
361                 log_warning(_("invalid article range: %d - %d\n"),
362                             first, last);
363                 return NULL;
364         }
365         if (cache_last < first)
366                 begin = first;
367         else if (last < cache_last)
368                 begin = first;
369         else if (last == cache_last) {
370                 debug_print(_("no new articles.\n"));
371                 return NULL;
372         } else
373                 begin = cache_last + 1;
374         end = last;
375
376         if (prefs_common.max_articles > 0 &&
377             end - begin + 1 > prefs_common.max_articles)
378                 begin = end - prefs_common.max_articles + 1;
379
380         log_message(_("getting xover %d - %d in %s...\n"),
381                     begin, end, item->path);
382         if (nntp_xover(SESSION(session)->sock, begin, end) != NN_SUCCESS) {
383                 log_warning(_("can't get xover\n"));
384                 return NULL;
385         }
386
387         for (;;) {
388                 if (sock_read(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
389                         log_warning(_("error occurred while getting xover.\n"));
390                         return newlist;
391                 }
392
393                 if (buf[0] == '.' && buf[1] == '\r') break;
394
395                 msginfo = news_parse_xover(buf);
396                 if (!msginfo) {
397                         log_warning(_("invalid xover line: %s\n"), buf);
398                         continue;
399                 }
400
401                 msginfo->folder = item;
402                 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
403
404                 if (!newlist)
405                         llast = newlist = g_slist_append(newlist, msginfo);
406                 else {
407                         llast = g_slist_append(llast, msginfo);
408                         llast = llast->next;
409                 }
410         }
411
412         if (rfirst) *rfirst = first;
413         if (rlast)  *rlast  = last;
414         return newlist;
415 }
416
417 #define PARSE_ONE_PARAM(p, srcp) \
418 { \
419         p = strchr(srcp, '\t'); \
420         if (!p) return NULL; \
421         else \
422                 *p++ = '\0'; \
423 }
424
425 static MsgInfo *news_parse_xover(const gchar *xover_str)
426 {
427         MsgInfo *msginfo;
428         gchar buf[NNTPBUFSIZE];
429         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
430         gchar *p;
431         gint num, size_int, line_int;
432         gchar *xover_buf;
433
434         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
435         strcpy(xover_buf, xover_str);
436
437         PARSE_ONE_PARAM(subject, xover_buf);
438         PARSE_ONE_PARAM(sender, subject);
439         PARSE_ONE_PARAM(date, sender);
440         PARSE_ONE_PARAM(msgid, date);
441         PARSE_ONE_PARAM(ref, msgid);
442         PARSE_ONE_PARAM(size, ref);
443         PARSE_ONE_PARAM(line, size);
444
445         tmp = strchr(line, '\t');
446         if (!tmp) tmp = strchr(line, '\r');
447         if (!tmp) tmp = strchr(line, '\n');
448         if (tmp) *tmp = '\0';
449
450         num = atoi(xover_str);
451         size_int = atoi(size);
452         line_int = atoi(line);
453
454         /* set MsgInfo */
455         msginfo = g_new0(MsgInfo, 1);
456         msginfo->msgnum = num;
457         msginfo->size = size_int;
458
459         msginfo->date = g_strdup(date);
460         msginfo->date_t = procheader_date_parse(NULL, date, 0);
461
462         conv_unmime_header(buf, sizeof(buf), sender, NULL);
463         msginfo->from = g_strdup(buf);
464         msginfo->fromname = procheader_get_fromname(buf);
465
466         conv_unmime_header(buf, sizeof(buf), subject, NULL);
467         msginfo->subject = g_strdup(buf);
468
469         extract_parenthesis(msgid, '<', '>');
470         remove_space(msgid);
471         if (*msgid != '\0')
472                 msginfo->msgid = g_strdup(msgid);
473
474         eliminate_parenthesis(ref, '(', ')');
475         if ((p = strrchr(ref, '<')) != NULL) {
476                 extract_parenthesis(p, '<', '>');
477                 remove_space(p);
478                 if (*p != '\0')
479                         msginfo->inreplyto = g_strdup(p);
480         }
481
482         return msginfo;
483 }
484
485 static GSList *news_delete_old_article(GSList *alist, gint first)
486 {
487         GSList *cur, *next;
488         MsgInfo *msginfo;
489         gchar *cache_file;
490
491         if (first < 2) return alist;
492
493         for (cur = alist; cur != NULL; ) {
494                 next = cur->next;
495
496                 msginfo = (MsgInfo *)cur->data;
497                 if (msginfo && msginfo->msgnum < first) {
498                         debug_print(_("deleting article %d...\n"),
499                                     msginfo->msgnum);
500
501                         cache_file = procmsg_get_message_file_path(msginfo);
502                         if (is_file_exist(cache_file)) unlink(cache_file);
503                         g_free(cache_file);
504
505                         procmsg_msginfo_free(msginfo);
506                         alist = g_slist_remove(alist, msginfo);
507                 }
508
509                 cur = next;
510         }
511
512         return alist;
513 }
514
515 static void news_delete_all_article(FolderItem *item)
516 {
517         DIR *dp;
518         struct dirent *d;
519         gchar *dir;
520         gchar *file;
521
522         dir = folder_item_get_path(item);
523         if ((dp = opendir(dir)) == NULL) {
524                 FILE_OP_ERROR(dir, "opendir");
525                 g_free(dir);
526                 return;
527         }
528
529         debug_print(_("\tDeleting all cached articles... "));
530
531         while ((d = readdir(dp)) != NULL) {
532                 if (to_number(d->d_name) < 0) continue;
533
534                 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
535
536                 if (is_file_exist(file)) {
537                         if (unlink(file) < 0)
538                                 FILE_OP_ERROR(file, "unlink");
539                 }
540
541                 g_free(file);
542         }
543
544         closedir(dp);
545         g_free(dir);
546
547         debug_print(_("done.\n"));
548 }
549
550 /*
551   news_get_group_list returns a strings list.
552   These strings are the names of the newsgroups of a server.
553   item is the FolderItem of the news server.
554   The names of the newsgroups are cached into a file so that
555   when the function is called again, there is no need to make
556   a request to the server.
557  */
558
559 GSList * news_get_group_list(FolderItem *item)
560 {
561         gchar *path, *filename;
562         gint ok;
563         NNTPSession *session;
564         GSList * group_list = NULL;
565         FILE * f;
566         gchar buf[NNTPBUFSIZE];
567         int len;
568
569         if (item == NULL)
570           return NULL;
571
572         path = folder_item_get_path(item);
573
574         if (!is_dir_exist(path))
575                 make_dir_hier(path);
576
577         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
578         g_free(path);
579
580         session = news_session_get(item->folder);
581
582         if (session == NULL)
583           return NULL;
584
585         if (is_file_exist(filename)) {
586                 debug_print(_("group list has been already cached.\n"));
587         }
588         else {
589             ok = nntp_list(SESSION(session)->sock);
590             if (ok != NN_SUCCESS)
591               return NULL;
592             
593             if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
594               log_warning(_("can't retrieve group list\n"));
595               return NULL;
596             }
597         }
598
599         f = fopen(filename, "r");
600         while (fgets(buf, NNTPBUFSIZE, f)) {
601             char * s;
602
603             len = 0;
604             while ((buf[len] != 0) && (buf[len] != ' '))
605               len++;
606             buf[len] = 0;
607             s = g_strdup(buf);
608
609             group_list = g_slist_append(group_list, s);
610         }
611         fclose(f);
612         g_free(filename);
613
614         group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
615
616         return group_list;
617 }
618
619 /*
620   remove the cache file of the names of the newsgroups.
621  */
622
623 void news_reset_group_list(FolderItem *item)
624 {
625         gchar *path, *filename;
626
627         debug_print(_("\tDeleting cached group list... "));
628         path = folder_item_get_path(item);
629         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
630         g_free(path);
631         if (remove(filename) != 0)
632           log_warning(_("can't delete cached group list %s\n"), filename);
633         g_free(filename);
634 }