f2afa3ff62216d44fa167795f830af3dce6ae4f4
[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                         a = feed_parser_get_attribute_value(attr, "type");
103                         if (a && !strcmp(a, "xhtml")) {
104                                 ctx->curitem->xhtml_content = TRUE;
105                                 ctx->location = FEED_LOC_ATOM10_CONTENT;
106                         }
107                 }
108         }
109
110         ctx->depth++;
111 }
112
113 void feed_parser_atom10_end(void *data, const gchar *el)
114 {
115         FeedParserCtx *ctx = (FeedParserCtx *)data;
116         Feed *feed = ctx->feed;
117         gchar *text = NULL, *tmp;
118
119         if( ctx->str != NULL )
120                 text = g_strstrip(g_strdup(ctx->str->str));
121         else
122                 text = "";
123
124         switch( ctx->depth ) {
125
126                 case 0:
127                         /* Just in case. */
128                         break;
129
130                 case 1:
131
132                         if( !strcmp(el, "feed") ) {
133                                 /* We have finished parsing the feed, reverse the list
134                                  * so it's not upside down. */
135                                 feed->items = g_slist_reverse(ctx->feed->items);
136                         }
137
138                         break;
139
140                 case 2:
141
142                         /* decide if we just received </entry>, so we can
143                          * add a complete item to feed */
144                         if( !strcmp(el, "entry") ) {
145
146                                 /* Fix up URL, if it is relative */
147                                 if (ctx->curitem->url != NULL &&
148                                                 !strstr(ctx->curitem->url, "://") &&
149                                                 ctx->feed->link != NULL) {
150                                         tmp = g_strconcat(ctx->feed->link,
151                                                         (ctx->curitem->url[0] == '/' ? "" : "/"),
152                                                         ctx->curitem->url, NULL);
153                                         feed_item_set_url(ctx->curitem, tmp);
154                                         g_free(tmp);
155                                 }
156
157                                 /* append the complete feed item */
158                                 if( ctx->curitem->id && ctx->curitem->title
159                                                 && ctx->curitem->date_modified ) {
160                                         feed->items = 
161                                                 g_slist_prepend(feed->items, (gpointer)ctx->curitem);
162                                 }
163                                 
164                                 /* since it's in the linked list, lose this pointer */
165                                 ctx->curitem = NULL;
166
167                         } else if( !strcmp(el, "title") ) {     /* so it wasn't end of item */
168                                 FILL(feed->title)
169                         } else if( !strcmp(el, "summary" ) ) {
170                                 FILL(feed->description)
171                         } else if( !strcmp(el, "updated" ) ) {
172                                 feed->date = procheader_date_parse(NULL, text, 0);
173                         }
174                         /* FIXME: add more later */
175
176                         break;
177
178                 case 3:
179
180                         if( ctx->curitem == NULL )
181                                 break;
182
183                         switch(ctx->location) {
184
185                                 /* We're in feed/entry */
186                                 case FEED_LOC_ATOM10_ENTRY:
187                                         if( !strcmp(el, "title") ) {
188                                                 FILL(ctx->curitem->title)
189                                         } else if( !strcmp(el, "summary") ) {
190                                                 FILL(ctx->curitem->summary)
191                                         } else if( !strcmp(el, "content") ) {
192                                                 if (!ctx->curitem->xhtml_content)
193                                                         FILL(ctx->curitem->text)
194                                         } else if( !strcmp(el, "id") ) {
195                                                 FILL(ctx->curitem->id)
196                                                 feed_item_set_id_permalink(ctx->curitem, TRUE);
197                                         } else if( !strcmp(el, "published") ) {
198                                                 ctx->curitem->date_published = procheader_date_parse(NULL, text, 0);
199                                         } else if( !strcmp(el, "updated") ) {
200                                                 ctx->curitem->date_modified = procheader_date_parse(NULL, text, 0);
201                                         }
202
203                                         break;
204
205                                 /* We're in feed/author or about to leave feed/entry/author */
206                                 case FEED_LOC_ATOM10_AUTHOR:
207                                         if( !strcmp(el, "author" ) ) {
208                                                 /* We just finished parsing <author> */
209                                                 ctx->curitem->author = g_strdup_printf("%s%s%s%s%s",
210                                                                 ctx->name ? ctx->name : "",
211                                                                 ctx->name && ctx->mail ? " <" : ctx->mail ? "<" : "",
212                                                                 ctx->mail ? ctx->mail : "",
213                                                                 ctx->mail ? ">" : "",
214                                                                 !ctx->name && !ctx->mail ? "N/A" : "");
215                                                 ctx->location = FEED_LOC_ATOM10_ENTRY;
216                                         } else if( !strcmp(el, "name") ) {
217                                                 FILL(feed->author)
218                                         }
219
220                                         break;
221                         }
222
223                         break;
224
225                 case 4:
226
227                         if( ctx->curitem == NULL )
228                                 break;
229
230                         switch(ctx->location) {
231
232                                 /* We're in feed/entry/author */
233                                 case FEED_LOC_ATOM10_AUTHOR:
234                                         if( !strcmp(el, "name") ) {
235                                                 FILL(ctx->name)
236                                         } else if( !strcmp(el, "email") ) {
237                                                 FILL(ctx->mail)
238                                         }
239
240                                         break;
241
242                                 /* We're in feed/entry/source */
243                                 case FEED_LOC_ATOM10_SOURCE:
244                                         if( !strcmp(el, "title" ) ) {
245                                                 FILL(ctx->curitem->sourcetitle)
246                                         } else if( !strcmp(el, "id" ) ) {
247                                                 FILL(ctx->curitem->sourceid)
248                                         } else if( !strcmp(el, "updated" ) ) {
249                                                 ctx->curitem->sourcedate = procheader_date_parse(NULL, text, 0);
250                                         }
251
252                                         break;
253
254                                 case FEED_LOC_ATOM10_CONTENT:
255                                         if (!strcmp(el, "div") && ctx->curitem->xhtml_content)
256                                                 FILL(ctx->curitem->text)
257                                         break;
258
259                                 }
260
261
262                         break;
263         }
264
265         if( ctx->str != NULL ) {
266                 g_free(text);
267                 g_string_free(ctx->str, TRUE);
268                 ctx->str = NULL;
269         }
270         ctx->str = NULL;
271
272         ctx->depth--;
273 }