2 * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4 * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 /* Claws Mail includes */
31 #include <common/claws.h>
32 #include <procheader.h>
33 #include <common/utils.h>
37 #include "libfeed/feed.h"
38 #include "libfeed/feeditem.h"
39 #include "libfeed/date.h"
41 #include "rssyl_feed.h"
42 #include "rssyl_parse_feed.h"
45 /* rssyl_parse_folder_item_file()
47 * Parse a RFC822-formatted feed item given by "path", and returns a
48 * pointer to a newly-allocated FeedItem struct, which contains all required data.
51 FeedItem *rssyl_parse_folder_item_file(gchar *path)
53 gchar *contents, **lines, **line, **splid, *tmp, *tmp2;
58 gboolean parsing_headers = TRUE, past_html_tag = FALSE, past_endhtml_tag = FALSE;
59 gboolean started_author = FALSE, started_subject = FALSE;
60 gboolean started_link = FALSE, started_clink = FALSE, got_original_title = FALSE;
62 debug_print("RSSyl: parsing '%s'\n", path);
64 g_file_get_contents(path, &contents, NULL, &error);
67 g_warning("GError: '%s'\n", error->message);
69 if( contents != NULL ) {
70 lines = strsplit_no_copy(contents, '\n');
72 g_warning("Badly formatted file found, ignoring: '%s'\n", path);
76 ctx = g_new0(RFeedCtx, 1);
77 ctx->path = g_strdup(path); /* store filesystem path to source file */
80 item = feed_item_new(NULL);
84 if( parsing_headers && lines[i] && !strlen(lines[i]) ) {
85 parsing_headers = FALSE;
86 debug_print("RSSyl: finished parsing headers\n");
89 if( parsing_headers ) {
90 line = g_strsplit(lines[i], ": ", 2);
91 if( line[0] && line[1] && strlen(line[0]) && lines[i][0] != ' ') {
92 started_author = FALSE;
93 started_subject = FALSE;
95 started_clink = FALSE;
98 if( !strcmp(line[0], "From") ) {
99 feed_item_set_author(item, line[1]);
100 debug_print("RSSyl: got author '%s'\n", feed_item_get_author(item));
101 started_author = TRUE;
105 if( !strcmp(line[0], "Date") ) {
106 feed_item_set_date_modified(item,
107 procheader_date_parse(NULL, line[1], 0));
108 debug_print("RSSyl: got date \n" );
112 if( !strcmp(line[0], "Subject") && !got_original_title ) {
113 feed_item_set_title(item,line[1]);
114 debug_print("RSSyl: got title '%s'\n", feed_item_get_title(item));
115 started_subject = TRUE;
118 /* Original (including HTML) title - Atom feeds */
119 if( !strcmp(line[0], "X-RSSyl-OrigTitle") ) {
120 feed_item_set_title(item, line[1]);
121 debug_print("RSSyl: got original title '%s'\n",
122 feed_item_get_title(item));
123 got_original_title = TRUE;
127 if( !strcmp(line[0], "X-RSSyl-URL") ) {
128 feed_item_set_url(item, line[1]);
129 debug_print("RSSyl: got link '%s'\n", feed_item_get_url(item));
133 /* Last-Seen timestamp */
134 if( !strcmp(line[0], "X-RSSyl-Last-Seen") ) {
135 ctx->last_seen = atol(line[1]);
136 debug_print("RSSyl: got last_seen timestamp %ld\n", ctx->last_seen);
140 if( !strcmp(line[0], "Message-ID") ) {
141 if (line[1][0] != '<' || line[1][strlen(line[1])-1] != '>') {
142 debug_print("RSSyl: malformed Message-ID, ignoring...\n");
144 /* Get the ID from within < and >. */
146 tmp2 = g_strndup(tmp, strlen(tmp) - 1);
147 feed_item_set_id(item, tmp2);
153 if( !strcmp(line[0], "X-RSSyl-Comments") ) {
154 feed_item_set_comments_url(item, line[1]);
155 debug_print("RSSyl: got clink '%s'\n", feed_item_get_comments_url(item));
156 started_clink = TRUE;
160 if( !strcmp(line[0], "References") ) {
161 splid = g_strsplit_set(line[1], "<>", 3);
162 if( strlen(splid[1]) != 0 )
163 feed_item_set_parent_id(item, line[1]);
167 } else if (lines[i][0] == ' ') {
169 /* continuation line */
170 if (started_author) {
171 tmp = g_strdup_printf("%s %s", feed_item_get_author(item), lines[i]+1);
172 feed_item_set_author(item, tmp);
173 debug_print("RSSyl: updated author to '%s'\n", tmp);
175 } else if (started_subject) {
176 tmp = g_strdup_printf("%s %s", feed_item_get_title(item), lines[i]+1);
177 feed_item_set_title(item, tmp);
178 debug_print("RSSyl: updated title to '%s'\n", tmp);
180 } else if (started_link) {
181 tmp = g_strdup_printf("%s%s", feed_item_get_url(item), lines[i]+1);
182 feed_item_set_url(item, tmp);
183 debug_print("RSSyl: updated link to '%s'\n", tmp);
185 } else if (started_clink) {
186 tmp = g_strdup_printf("%s%s", feed_item_get_comments_url(item), lines[i]+1);
187 feed_item_set_comments_url(item, tmp);
188 debug_print("RSSyl: updated comments_link to '%s'\n", tmp);
193 if( !strcmp(lines[i], RSSYL_TEXT_START) ) {
194 debug_print("RSSyl: Leading html tag found at line %d\n", i);
195 past_html_tag = TRUE;
199 while( past_html_tag && !past_endhtml_tag && lines[i] ) {
200 if( !strcmp(lines[i], RSSYL_TEXT_END) ) {
201 debug_print("RSSyl: Trailing html tag found at line %d\n", i);
202 past_endhtml_tag = TRUE;
206 if( feed_item_get_text(item) != NULL ) {
208 e_len = strlen(item->text);
209 n_len = strlen(lines[i]);
210 item->text = g_realloc(item->text, e_len + n_len + 2);
211 *(item->text+e_len) = '\n';
212 strcpy(item->text+e_len+1, lines[i]);
213 *(item->text+e_len+n_len+1) = '\0';
215 item->text = g_strdup(lines[i]);
220 if( lines[i] == NULL )
231 static void rssyl_flush_folder_func(gpointer data, gpointer user_data)
233 FeedItem *item = (FeedItem *)data;
234 RFeedCtx *ctx = (RFeedCtx *)item->data;
236 if( ctx != NULL && ctx->path != NULL)
238 feed_item_free(item);
241 static void rssyl_folder_read_existing_real(RFolderItem *ritem)
243 gchar *path = NULL, *fname = NULL;
248 FeedItem *item = NULL;
251 g_return_if_fail(ritem != NULL);
253 path = folder_item_get_path(&ritem->item);
254 g_return_if_fail(path != NULL);
256 debug_print("RSSyl: reading existing items from '%s'\n", path);
258 /* Flush contents if any, so we can add new */
259 if( g_slist_length(ritem->items) > 0 ) {
260 g_slist_foreach(ritem->items, (GFunc)rssyl_flush_folder_func, NULL);
261 g_slist_free(ritem->items);
264 ritem->last_update = 0;
266 if( (dp = opendir(path)) == NULL ) {
267 FILE_OP_ERROR(path, "opendir");
272 while( (d = readdir(dp)) != NULL ) {
273 if( claws_is_exiting() ) {
279 if( d->d_name[0] != '.' && (num = to_number(d->d_name)) > 0 ) {
280 fname = g_strdup_printf("%s%c%s", path, G_DIR_SEPARATOR, d->d_name);
281 if( g_stat(fname, &st) < 0 ) {
282 debug_print("RSSyl: couldn't stat() file '%s', ignoring it\n", fname);
287 if( !S_ISREG(st.st_mode)) {
288 debug_print("RSSyl: not a regular file: '%s', ignoring it\n", fname);
293 debug_print("RSSyl: starting to parse '%s'\n", d->d_name);
294 if( (item = rssyl_parse_folder_item_file(fname)) != NULL ) {
295 /* Find latest timestamp */
296 ctx = (RFeedCtx *)item->data;
297 if( ritem->last_update < ctx->last_seen )
298 ritem->last_update = ctx->last_seen;
299 debug_print("RSSyl: Appending '%s'\n", feed_item_get_title(item));
300 ritem->items = g_slist_prepend(ritem->items, item);
309 ritem->items = g_slist_reverse(ritem->items);
313 static void *rssyl_read_existing_thr(void *arg)
315 RParseCtx *ctx = (RParseCtx *)arg;
317 rssyl_folder_read_existing_real(ctx->ritem);
323 void rssyl_folder_read_existing(RFolderItem *ritem)
330 g_return_if_fail(ritem != NULL);
334 ctx = g_new0(RParseCtx, 1);
338 if( pthread_create(&pt, PTHREAD_CREATE_JOINABLE, rssyl_read_existing_thr,
339 (void *)ctx) != 0 ) {
340 /* Couldn't create thread, let's continue non-threaded. */
341 rssyl_folder_read_existing_real(ritem);
343 /* Thread started, wait until it is done. */
344 debug_print("RSSyl: waiting for thread to finish\n");
345 while( !ctx->ready ) {
349 debug_print("RSSyl: thread finished\n");
350 pthread_join(pt, NULL);
355 rssyl_folder_read_existing_real(ritem);