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