New RSSyl replacing old one.
[claws.git] / src / plugins / rssyl / parse822.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 /* Global includes */
26 #include <sys/stat.h>
27 #include <glib.h>
28 #include <pthread.h>
29
30 /* Claws Mail includes */
31 #include <common/claws.h>
32 #include <procheader.h>
33 #include <common/utils.h>
34 #include <main.h>
35
36 /* Local includes */
37 #include "libfeed/feed.h"
38 #include "libfeed/feeditem.h"
39 #include "libfeed/date.h"
40 #include "parse822.h"
41 #include "rssyl_feed.h"
42 #include "rssyl_parse_feed.h"
43 #include "strutils.h"
44
45 /* rssyl_parse_folder_item_file()
46  *
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.
49  *
50  */
51 FeedItem *rssyl_parse_folder_item_file(gchar *path)
52 {
53         gchar *contents, **lines, **line, **splid;
54         GError *error = NULL;
55         FeedItem *item;
56         RFeedCtx *ctx;
57         gint i = 0;
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;
61
62         debug_print("RSSyl: parsing '%s'\n", path);
63
64         g_file_get_contents(path, &contents, NULL, &error);
65
66         if( error )
67                 g_warning("GError: '%s'\n", error->message);
68
69         if( contents != NULL ) {
70                 lines = strsplit_no_copy(contents, '\n');
71         } else {
72                 g_warning("Badly formatted file found, ignoring: '%s'\n", path);
73                 return NULL;
74         }
75
76         ctx = g_new0(RFeedCtx, 1);
77         ctx->path = g_strdup(path); /* store filesystem path to source file */
78         ctx->last_seen = 0;
79
80         item = feed_item_new(NULL);
81         item->data = ctx;
82
83         while( lines[i] ) {
84                 if( parsing_headers && lines[i] && !strlen(lines[i]) ) {
85                         parsing_headers = FALSE;
86                         debug_print("RSSyl: finished parsing headers\n");
87                 }
88
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;
94                                 started_link = FALSE;
95                                 started_clink = FALSE;
96
97                                 /* Author */
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;
102                                 }
103
104                                 /* Date */
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" );
109                                 }
110
111                                 /* Title */
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;
116                                 }
117
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;
124                                 }
125
126                                 /* URL */
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));
130                                         started_link = TRUE;
131                                 }
132
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);
137                                 }
138
139                                 /* ID */
140                                 if( !strcmp(line[0], "Message-ID") ) {
141                                         splid = g_strsplit_set(line[1], "<>", 3);
142                                         if( strlen(splid[1]) != 0 )
143                                                 feed_item_set_id(item, splid[1]);
144                                         g_strfreev(splid);
145                                 }
146
147                                 /* Feed comments */
148                                 if( !strcmp(line[0], "X-RSSyl-Comments") ) {
149                                         feed_item_set_comments_url(item, line[1]);
150                                         debug_print("RSSyl: got clink '%s'\n", feed_item_get_comments_url(item));
151                                         started_clink = TRUE;
152                                 }
153
154                                 /* References */
155                                 if( !strcmp(line[0], "References") ) {
156                                         splid = g_strsplit_set(line[1], "<>", 3);
157                                         if( strlen(splid[1]) != 0 )
158                                                 feed_item_set_parent_id(item, line[1]);
159                                         g_strfreev(splid);
160                                 }
161
162                         } else if (lines[i][0] == ' ') {
163                                 gchar *tmp = NULL;
164                                 /* continuation line */
165                                 if (started_author) {
166                                         tmp = g_strdup_printf("%s %s", feed_item_get_author(item), lines[i]+1);
167                                         feed_item_set_author(item, tmp);
168                                         debug_print("RSSyl: updated author to '%s'\n", tmp);
169                                         g_free(tmp);
170                                 } else if (started_subject) {
171                                         tmp = g_strdup_printf("%s %s", feed_item_get_title(item), lines[i]+1);
172                                         feed_item_set_title(item, tmp);
173                                         debug_print("RSSyl: updated title to '%s'\n", tmp);
174                                         g_free(tmp);
175                                 } else if (started_link) {
176                                         tmp = g_strdup_printf("%s%s", feed_item_get_url(item), lines[i]+1);
177                                         feed_item_set_url(item, tmp);
178                                         debug_print("RSSyl: updated link to '%s'\n", tmp);
179                                         g_free(tmp);
180                                 } else if (started_clink) {
181                                         tmp = g_strdup_printf("%s%s", feed_item_get_comments_url(item), lines[i]+1);
182                                         feed_item_set_comments_url(item, tmp);
183                                         debug_print("RSSyl: updated comments_link to '%s'\n", tmp);
184                                 }
185                         }
186                         g_strfreev(line);
187                 } else {
188                         if( !strcmp(lines[i], RSSYL_TEXT_START) ) {
189                                 debug_print("RSSyl: Leading html tag found at line %d\n", i);
190                                 past_html_tag = TRUE;
191                                 i++;
192                                 continue;
193                         }
194                         while( past_html_tag && !past_endhtml_tag && lines[i] ) {
195                                 if( !strcmp(lines[i], RSSYL_TEXT_END) ) {
196                                         debug_print("RSSyl: Trailing html tag found at line %d\n", i);
197                                         past_endhtml_tag = TRUE;
198                                         i++;
199                                         continue;
200                                 }
201                                 if( feed_item_get_text(item) != NULL ) {
202                                         gint e_len, n_len;
203                                         e_len = strlen(item->text);
204                                         n_len = strlen(lines[i]);
205                                         item->text = g_realloc(item->text, e_len + n_len + 2);
206                                         *(item->text+e_len) = '\n';
207                                         strcpy(item->text+e_len+1, lines[i]);
208                                         *(item->text+e_len+n_len+1) = '\0';
209                                 } else {
210                                         item->text = g_strdup(lines[i]);
211                                 }
212                                 i++;
213                         }
214
215                         if( lines[i] == NULL )
216                                 return item;
217                 }
218
219                 i++;
220         }
221         g_free(lines);
222         g_free(contents);
223         return item;
224 }
225
226 static void rssyl_flush_folder_func(gpointer data, gpointer user_data)
227 {
228         FeedItem *item = (FeedItem *)data;
229         RFeedCtx *ctx = (RFeedCtx *)item->data;
230
231         if( ctx != NULL && ctx->path != NULL)
232                 g_free(ctx->path);
233         feed_item_free(item);
234 }
235
236 static void rssyl_folder_read_existing_real(RFolderItem *ritem)
237 {
238         gchar *path = NULL, *fname = NULL;
239         DIR *dp;
240         struct dirent *d;
241         struct stat st;
242         gint num;
243         FeedItem *item = NULL;
244         RFeedCtx *ctx;
245
246         g_return_if_fail(ritem != NULL);
247
248         path = folder_item_get_path(&ritem->item);
249         g_return_if_fail(path != NULL);
250
251         debug_print("RSSyl: reading existing items from '%s'\n", path);
252
253         /* Flush contents if any, so we can add new */
254         if( g_slist_length(ritem->items) > 0 ) {
255                 g_slist_foreach(ritem->items, (GFunc)rssyl_flush_folder_func, NULL);
256                 g_slist_free(ritem->items);
257         }
258         ritem->items = NULL;
259         ritem->last_update = 0;
260
261         if( (dp = opendir(path)) == NULL ) {
262                 FILE_OP_ERROR(path, "opendir");
263                 g_free(path);
264                 return;
265         }
266
267         while( (d = readdir(dp)) != NULL ) {
268                 if( claws_is_exiting() ) {
269                         closedir(dp);
270                         g_free(path);
271                         return;
272                 }
273
274                 if( d->d_name[0] != '.' && (num = to_number(d->d_name)) > 0 ) {
275                         fname = g_strdup_printf("%s%c%s", path, G_DIR_SEPARATOR, d->d_name);
276                         if( g_stat(fname, &st) < 0 ) {
277                                 debug_print("RSSyl: couldn't stat() file '%s', ignoring it\n", fname);
278                                 g_free(fname);
279                                 continue;
280                         }
281
282                         if( !S_ISREG(st.st_mode)) {
283                                 debug_print("RSSyl: not a regular file: '%s', ignoring it\n", fname);
284                                 g_free(fname);
285                                 continue;
286                         }
287
288                         debug_print("RSSyl: starting to parse '%s'\n", d->d_name);
289                         if( (item = rssyl_parse_folder_item_file(fname)) != NULL ) {
290                                 /* Find latest timestamp */
291                                 ctx = (RFeedCtx *)item->data;
292                                 if( ritem->last_update < ctx->last_seen )
293                                         ritem->last_update = ctx->last_seen;
294                                 debug_print("RSSyl: Appending '%s'\n", feed_item_get_title(item));
295                                 ritem->items = g_slist_prepend(ritem->items, item);
296                         }
297                         g_free(fname);
298                 }
299         }
300
301         closedir(dp);
302         g_free(path);
303
304         ritem->items = g_slist_reverse(ritem->items);
305 }
306
307 #ifdef USE_PTHREAD
308 static void *rssyl_read_existing_thr(void *arg)
309 {
310         RParseCtx *ctx = (RParseCtx *)arg;
311
312         rssyl_folder_read_existing_real(ctx->ritem);
313         ctx->ready = TRUE;
314         return NULL;
315 }
316 #endif
317
318 void rssyl_folder_read_existing(RFolderItem *ritem)
319 {
320 #ifdef USE_PTHREAD
321         RParseCtx *ctx;
322         pthread_t pt;
323 #endif
324
325         g_return_if_fail(ritem != NULL);
326
327
328 #ifdef USE_PTHREAD
329         ctx = g_new0(RParseCtx, 1);
330         ctx->ritem = ritem;
331         ctx->ready = FALSE;
332
333         if( pthread_create(&pt, PTHREAD_CREATE_JOINABLE, rssyl_read_existing_thr,
334                                 (void *)ctx) != 0 ) {
335                 /* Couldn't create thread, let's continue non-threaded. */
336                 rssyl_folder_read_existing_real(ritem);
337         } else {
338                 /* Thread started, wait until it is done. */
339                 debug_print("RSSyl: waiting for thread to finish\n");
340                 while( !ctx->ready ) {
341                         claws_do_idle();
342                 }
343
344                 debug_print("RSSyl: thread finished\n");
345                 pthread_join(pt, NULL);
346         }
347
348         g_free(ctx);
349 #else
350         rssyl_folder_read_existing_real(ritem);
351 #endif
352 }