RSSyl: Handle XHTML content correctly for Atom feeds.
[claws.git] / src / plugins / rssyl / libfeed / parser_atom10.c
1 /*
2  * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #define __USE_GNU
20
21 #include <glib.h>
22 #include <expat.h>
23 #include <string.h>
24 #include <stdio.h>
25
26 #include <procheader.h>
27
28 #include "feed.h"
29 #include "feeditem.h"
30 #include "date.h"
31 #include "parser.h"
32 #include "parser_atom10.h"
33
34 enum {
35         FEED_LOC_ATOM10_NONE,
36         FEED_LOC_ATOM10_ENTRY,
37         FEED_LOC_ATOM10_AUTHOR,
38         FEED_LOC_ATOM10_SOURCE,
39         FEED_LOC_ATOM10_CONTENT
40 } FeedAtom10Locations;
41
42 void feed_parser_atom10_start(void *data, const gchar *el, const gchar **attr)
43 {
44         FeedParserCtx *ctx = (FeedParserCtx *)data;
45         gchar *a = NULL;
46
47         if( ctx->depth == 1 ) {
48
49                 if( !strcmp(el, "entry") ) {
50                         /* Start of new feed item found.
51                          * Create a new FeedItem, freeing the one we already have, if any. */
52                         if( ctx->curitem != NULL )
53                                 feed_item_free(ctx->curitem);
54                         ctx->curitem = feed_item_new(ctx->feed);
55                         ctx->location = FEED_LOC_ATOM10_ENTRY;
56                 } else if( !strcmp(el, "author") ) {
57                         /* Start of author info for the feed found.
58                          * Set correct location. */
59                         ctx->location = FEED_LOC_ATOM10_AUTHOR;
60                 } else if( !strcmp(el, "link") ) {
61                         if (!feed_parser_get_attribute_value(attr, "rel")) {
62                                 /* Link tag for the feed */
63                                 g_free(ctx->feed->link);
64                                 ctx->feed->link =
65                                         g_strdup(feed_parser_get_attribute_value(attr, "href"));
66                         }
67                 } else ctx->location = FEED_LOC_ATOM10_NONE;
68
69         } else if( ctx->depth == 2 ) {
70
71                 /* Make sure we are in one of known locations within the XML structure.
72                  * This condition should never be true on a valid Atom feed. */
73                 if (ctx->location != FEED_LOC_ATOM10_AUTHOR &&
74                                 ctx->location != FEED_LOC_ATOM10_ENTRY) {
75                         ctx->depth++;
76                         return;
77                 }
78
79                 if( !strcmp(el, "author") ) {
80                         /* Start of author info for current feed item.
81                          * Set correct location. */
82                         ctx->location = FEED_LOC_ATOM10_AUTHOR;
83                 } else if( !strcmp(el, "link") ) {
84                         /* Capture item URL, from the "url" XML attribute. */
85                         if (ctx->curitem && ctx->location == FEED_LOC_ATOM10_ENTRY)
86                                 ctx->curitem->url = g_strdup(feed_parser_get_attribute_value(attr, "href"));
87                 } else if( !strcmp(el, "source") ) {
88                         ctx->location = FEED_LOC_ATOM10_SOURCE;
89                 } else ctx->location = FEED_LOC_ATOM10_ENTRY;
90
91                 if( !strcmp(el, "title") && ctx->curitem != NULL) {
92                         a = feed_parser_get_attribute_value(attr, "type");
93                         if( !a || !strcmp(a, "text") )
94                                 ctx->curitem->title_format = FEED_ITEM_TITLE_TEXT;
95                         else if( !strcmp(a, "html") )
96                                 ctx->curitem->title_format = FEED_ITEM_TITLE_HTML;
97                         else if( !strcmp(a, "xhtml") )
98                                 ctx->curitem->title_format = FEED_ITEM_TITLE_XHTML;
99                         else
100                                 ctx->curitem->title_format = FEED_ITEM_TITLE_UNKNOWN;
101                 } else if (!strcmp(el, "content") && ctx->curitem != NULL) {
102                         ctx->location = FEED_LOC_ATOM10_CONTENT;
103                         a = feed_parser_get_attribute_value(attr, "type");
104                         if (a && !strcmp(a, "xhtml")) {
105                                 ctx->curitem->xhtml_content = TRUE;
106                                 ctx->xhtml_str = g_string_new(NULL);
107                         }
108                 }
109         } else if (ctx->depth >= 3) {
110                 if (ctx->curitem->xhtml_content
111                                 && ctx->location == FEED_LOC_ATOM10_CONTENT) {
112                         guint i;
113                         GString *txt = ctx->xhtml_str;
114                         g_string_append_c(txt, '<');
115                         g_string_append(txt, el);
116
117                         for (i = 0; attr[i] != NULL && attr[i+1] != NULL; i += 2) {
118                                 g_string_append_printf(txt, " %s='%s'", attr[i], attr[i+1]);
119                         }
120                         g_string_append_c(txt, '>');
121                 }
122         }
123
124
125         ctx->depth++;
126 }
127
128 void feed_parser_atom10_end(void *data, const gchar *el)
129 {
130         FeedParserCtx *ctx = (FeedParserCtx *)data;
131         Feed *feed = ctx->feed;
132         gchar *text = NULL, *tmp;
133
134         if( ctx->str != NULL )
135                 text = g_strstrip(g_strdup(ctx->str->str));
136         else
137                 text = "";
138
139         switch( ctx->depth ) {
140
141                 case 0:
142                         /* Just in case. */
143                         break;
144
145                 case 1:
146
147                         if( !strcmp(el, "feed") ) {
148                                 /* We have finished parsing the feed, reverse the list
149                                  * so it's not upside down. */
150                                 feed->items = g_slist_reverse(ctx->feed->items);
151                         }
152
153                         break;
154
155                 case 2:
156
157                         /* decide if we just received </entry>, so we can
158                          * add a complete item to feed */
159                         if( !strcmp(el, "entry") ) {
160
161                                 /* Fix up URL, if it is relative */
162                                 if (ctx->curitem->url != NULL &&
163                                                 !strstr(ctx->curitem->url, "://") &&
164                                                 ctx->feed->link != NULL) {
165                                         tmp = g_strconcat(ctx->feed->link,
166                                                         (ctx->curitem->url[0] == '/' ? "" : "/"),
167                                                         ctx->curitem->url, NULL);
168                                         feed_item_set_url(ctx->curitem, tmp);
169                                         g_free(tmp);
170                                 }
171
172                                 /* append the complete feed item */
173                                 if( ctx->curitem->id && ctx->curitem->title
174                                                 && ctx->curitem->date_modified ) {
175                                         feed->items = 
176                                                 g_slist_prepend(feed->items, (gpointer)ctx->curitem);
177                                 }
178                                 
179                                 /* since it's in the linked list, lose this pointer */
180                                 ctx->curitem = NULL;
181
182                         } else if( !strcmp(el, "title") ) {     /* so it wasn't end of item */
183                                 FILL(feed->title)
184                         } else if( !strcmp(el, "summary" ) ) {
185                                 FILL(feed->description)
186                         } else if( !strcmp(el, "updated" ) ) {
187                                 feed->date = procheader_date_parse(NULL, text, 0);
188                         }
189                         /* FIXME: add more later */
190
191                         break;
192
193                 case 3:
194
195                         if( ctx->curitem == NULL )
196                                 break;
197
198                         switch(ctx->location) {
199
200                                 /* We're in feed/entry */
201                                 case FEED_LOC_ATOM10_ENTRY:
202                                         if( !strcmp(el, "title") ) {
203                                                 FILL(ctx->curitem->title)
204                                         } else if( !strcmp(el, "summary") ) {
205                                                 FILL(ctx->curitem->summary)
206                                         } else if( !strcmp(el, "id") ) {
207                                                 FILL(ctx->curitem->id)
208                                                 feed_item_set_id_permalink(ctx->curitem, TRUE);
209                                         } else if( !strcmp(el, "published") ) {
210                                                 ctx->curitem->date_published = procheader_date_parse(NULL, text, 0);
211                                         } else if( !strcmp(el, "updated") ) {
212                                                 ctx->curitem->date_modified = procheader_date_parse(NULL, text, 0);
213                                         }
214
215                                         break;
216
217                                 /* We're in feed/author or about to leave feed/entry/author */
218                                 case FEED_LOC_ATOM10_AUTHOR:
219                                         if( !strcmp(el, "author" ) ) {
220                                                 /* We just finished parsing <author> */
221                                                 ctx->curitem->author = g_strdup_printf("%s%s%s%s%s",
222                                                                 ctx->name ? ctx->name : "",
223                                                                 ctx->name && ctx->mail ? " <" : ctx->mail ? "<" : "",
224                                                                 ctx->mail ? ctx->mail : "",
225                                                                 ctx->mail ? ">" : "",
226                                                                 !ctx->name && !ctx->mail ? "N/A" : "");
227                                                 ctx->location = FEED_LOC_ATOM10_ENTRY;
228                                         } else if( !strcmp(el, "name") ) {
229                                                 FILL(feed->author)
230                                         }
231
232                                         break;
233
234                                 case FEED_LOC_ATOM10_CONTENT:
235                                         if( !strcmp(el, "content") ) {
236                                                 if (ctx->curitem->xhtml_content) {
237                                                         /* Just in case the <content> tag itself also has some
238                                                          * content of its own, not just the <div> it should,
239                                                          * let's append it to the end. */
240                                                         g_string_append(ctx->xhtml_str, text);
241                                                         ctx->curitem->text = g_string_free(ctx->xhtml_str, FALSE);
242                                                         ctx->xhtml_str = NULL;
243                                                 } else {
244                                                         FILL(ctx->curitem->text)
245                                                 }
246                                                 ctx->location = FEED_LOC_ATOM10_ENTRY;
247                                         }
248
249                                         break;
250                         }
251                         break;
252
253                 case 4:
254
255                         if( ctx->curitem == NULL )
256                                 break;
257
258                         switch(ctx->location) {
259
260                                 /* We're in feed/entry/author */
261                                 case FEED_LOC_ATOM10_AUTHOR:
262                                         if( !strcmp(el, "name") ) {
263                                                 FILL(ctx->name)
264                                         } else if( !strcmp(el, "email") ) {
265                                                 FILL(ctx->mail)
266                                         }
267
268                                         break;
269
270                                 /* We're in feed/entry/source */
271                                 case FEED_LOC_ATOM10_SOURCE:
272                                         if( !strcmp(el, "title" ) ) {
273                                                 FILL(ctx->curitem->sourcetitle)
274                                         } else if( !strcmp(el, "id" ) ) {
275                                                 FILL(ctx->curitem->sourceid)
276                                         } else if( !strcmp(el, "updated" ) ) {
277                                                 ctx->curitem->sourcedate = procheader_date_parse(NULL, text, 0);
278                                         }
279
280                                         break;
281
282                                 case FEED_LOC_ATOM10_CONTENT:
283                                         if (ctx->curitem->xhtml_content) {
284                                                 g_string_append(ctx->xhtml_str, text);
285                                                 g_string_append_printf(ctx->xhtml_str, "</%s>", el);
286                                         }
287                                         break;
288
289                                 }
290
291
292                         break;
293
294                 default:
295                         if (ctx->location == FEED_LOC_ATOM10_CONTENT
296                                         && ctx->curitem->xhtml_content) {
297                                 g_string_append(ctx->xhtml_str, text);
298                                 g_string_append_printf(ctx->xhtml_str, "</%s>", el);
299                         }
300                         break;
301         }
302
303         if( ctx->str != NULL ) {
304                 g_free(text);
305                 g_string_free(ctx->str, TRUE);
306                 ctx->str = NULL;
307         }
308         ctx->str = NULL;
309
310         ctx->depth--;
311 }