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