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