RSSyl: Fix Atom parser getting lost within feed/author.
[claws.git] / src / plugins / rssyl / libfeed / parser_rss20.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
25 #include "feed.h"
26 #include "feeditem.h"
27 #include "feeditemenclosure.h"
28 #include "date.h"
29 #include "parser.h"
30
31 void feed_parser_rss20_start(void *data, const gchar *el, const gchar **attr)
32 {
33         FeedParserCtx *ctx = (FeedParserCtx *)data;
34         FeedItemEnclosure *enclosure = NULL;
35         gchar *url, *type, *size_s;
36         gulong size = -1;
37
38         /* ------------------- */
39         if( ctx->depth == 2 ) {
40                 if( !strcmp(el, "item") ) {             /* Start of new item */
41
42                         if( ctx->curitem != NULL )
43                                 feed_item_free(ctx->curitem);
44
45                         ctx->curitem = feed_item_new(ctx->feed);
46
47                 } else ctx->location = 0;
48         /* ------------------- */
49         } else if( ctx->depth == 3 ) {
50                 if( !strcmp(el, "enclosure") ) {        /* Media enclosure */
51
52                         url = feed_parser_get_attribute_value(attr, "url");
53                         type = feed_parser_get_attribute_value(attr, "type");
54                         size_s = feed_parser_get_attribute_value(attr, "length");
55                         if( size_s != NULL )
56                                 size = (gulong)atol(size_s);
57
58                         if( url != NULL && type != NULL && size > 0 ) {
59                                 if( (enclosure = feed_item_enclosure_new(url, type, size)) )
60                                         feed_item_set_enclosure(ctx->curitem, enclosure);
61                         }
62
63                 } else if( !strcmp(el, "guid") ) { /* Unique ID */
64                         type = feed_parser_get_attribute_value(attr, "isPermaLink");
65                         if( type != NULL && !strcmp(type, "false") )
66                                 feed_item_set_id_permalink(ctx->curitem, TRUE);
67                 }
68         } else ctx->location = 0;
69
70         ctx->depth++;
71
72 }
73
74 void feed_parser_rss20_end(void *data, const gchar *el)
75 {
76         FeedParserCtx *ctx = (FeedParserCtx *)data;
77         Feed *feed = ctx->feed;
78         gchar *text = NULL;
79
80         if( ctx->str != NULL )
81                 text = ctx->str->str;
82         else
83                 text = "";
84
85         ctx->depth--;
86
87         switch( ctx->depth ) {
88
89         /* ------------------- */
90                 case 0:
91
92                         if( !strcmp(el, "rss") ) {
93                                 /* we finished parsing the feed */
94                                 ctx->feed->items = g_slist_reverse(ctx->feed->items);
95                         }
96
97                         break;
98
99         /* ------------------- */
100                 case 1:
101
102                         break;  /* nothing to do at this depth */
103
104         /* ------------------- */
105                 case 2:
106
107                         /* decide if we just received </item>, so we can
108                          * add a complete item to feed */
109                         if( !strcmp(el, "item") ) {
110
111                                 /* append the complete feed item, if it is valid
112                                  * "All elements of an item are optional, however at least one
113                                  * of title or description must be present." */
114                                 if( ctx->curitem->title != NULL || ctx->curitem->summary != NULL ) {
115                                         ctx->feed->items = 
116                                                 g_slist_prepend(ctx->feed->items, (gpointer)ctx->curitem);
117                                 }
118                                 
119                                 /* since it's in the linked list, lose this pointer */
120                                 ctx->curitem = NULL;
121
122                         } else if( !strcmp(el, "title") ) {     /* so it wasn't end of item */
123                                 FILL(feed->title)
124                         } else if( !strcmp(el, "description" ) ) {
125                                 FILL(feed->description)
126                         } else if( !strcmp(el, "dc:language") ) {
127                                 FILL(feed->language)
128                         } else if( !strcmp(el, "author") ) {
129                                 FILL(feed->author)
130                         } else if( !strcmp(el, "admin:generatorAgent") ) {
131                                 FILL(feed->generator)
132                         } else if( !strcmp(el, "dc:date") ) {
133                                 feed->date = parseISO8601Date(text);
134                         } else if( !strcmp(el, "pubDate") ) {
135                                 feed->date = parseRFC822Date(text);
136                         }
137
138                         break;
139
140         /* ------------------- */
141                 case 3:
142
143                         if( ctx->curitem == NULL ) {
144                                 break;
145                         }
146
147                         /* decide which field did we just get */
148                         if( !strcmp(el, "title") ) {
149                                 FILL(ctx->curitem->title)
150                         } else if( !strcmp(el, "author") ) {
151                                 FILL(ctx->curitem->author)
152                         } else if( !strcmp(el, "description") ) { 
153                                 FILL(ctx->curitem->summary)
154                         } else if( !strcmp(el, "content:encoded") ) {
155                                 FILL(ctx->curitem->text)
156                         } else if( !strcmp(el, "link") ) {
157                                 FILL(ctx->curitem->url)
158                         } else if( !strcmp(el, "guid") ) {
159                                 FILL(ctx->curitem->id)
160                         } else if( !strcmp(el, "wfw:commentRSS") || !strcmp(el, "wfw:commentRss") ) {
161                                 FILL(ctx->curitem->comments_url)
162                         } else if( !strcmp(el, "dc:date") ) {
163                                 ctx->curitem->date_modified = parseISO8601Date(text);
164                         } else if( !strcmp(el, "pubDate") ) {
165                                 ctx->curitem->date_modified = parseRFC822Date(text);
166                         } else if( !strcmp(el, "dc:creator")) {
167                                 FILL(ctx->curitem->author)
168                         }
169
170                         break;
171
172         }
173
174         if( ctx->str != NULL ) {
175                 g_string_free(ctx->str, TRUE);
176                 ctx->str = NULL;
177         }
178 }