RSSyl: make sure we do not operate with NULL text in libfeed parsers' end-handlers.
[claws.git] / src / plugins / rssyl / libfeed / parser_rdf.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 "date.h"
27 #include "parser_rdf.h"
28
29 void feed_parser_rdf_start(void *data, const gchar *el, const gchar **attr)
30 {
31         FeedParserCtx *ctx = (FeedParserCtx *)data;
32
33         if( ctx->depth == 1 ) {
34                 if( !strcmp(el, "channel") ) {
35                         ctx->location = FEED_LOC_RDF_CHANNEL;
36                 } else if( !strcmp(el, "item") ) {
37
38                         if( ctx->curitem != NULL )
39                                 feed_item_free(ctx->curitem);
40
41                         ctx->curitem = feed_item_new(ctx->feed);
42                         ctx->location = FEED_LOC_RDF_ITEM;
43
44                 } else ctx->location = 0;
45         }
46
47         ctx->depth++;
48
49 }
50
51 void feed_parser_rdf_end(void *data, const gchar *el)
52 {
53         FeedParserCtx *ctx = (FeedParserCtx *)data;
54         Feed *feed = ctx->feed;
55         gchar *text = NULL;
56
57         if( ctx->str != NULL )
58                 text = ctx->str->str;
59         else
60                 text = "";
61
62         ctx->depth--;
63         
64         switch( ctx->depth ) {
65
66                 case 0:
67
68                         if( !strcmp(el, "rdf") ) {
69                                 /* we finished parsing the feed */
70                                 ctx->feed->items = g_slist_reverse(ctx->feed->items);
71                         }
72
73                         break;
74
75                 case 1:
76
77                         /* <item></item> block just ended, so ... */
78                         if( !strcmp(el, "item") ) {
79
80                                 /* add the complete feed item to our feed struct */
81                                 ctx->feed->items = 
82                                         g_slist_prepend(ctx->feed->items, (gpointer)ctx->curitem);
83                                 
84                                 /* since it's in the linked list, lose this pointer */
85                                 ctx->curitem = NULL;
86                         }
87
88                         break;
89
90                 case 2:
91
92                         switch(ctx->location) {
93
94                                 /* We're inside introductory <channel></channel> */
95                                 case FEED_LOC_RDF_CHANNEL:
96                                         if( !strcmp(el, "title") ) {
97                                                 FILL(feed->title)
98                                         } else if( !strcmp(el, "description" ) ) {
99                                                 FILL(feed->description)
100                                         } else if( !strcmp(el, "dc:language") ) {
101                                                 FILL(feed->language)
102                                         } else if( !strcmp(el, "dc:creator") ) {
103                                                 FILL(feed->author)
104                                         } else if( !strcmp(el, "dc:date") ) {
105                                                 feed->date = parseISO8601Date(text);
106                                         } else if( !strcmp(el, "pubDate") ) {
107                                                 feed->date = parseRFC822Date(text);
108                                         }
109
110                                         break;
111
112                                 /* We're inside an <item></item> */
113                                 case FEED_LOC_RDF_ITEM:
114                                         if( ctx->curitem == NULL ) {
115                                                 break;
116                                         }
117
118                                         /* decide which field did we just get */
119                                         if( !strcmp(el, "title") ) {
120                                                 FILL(ctx->curitem->title)
121                                         } else if( !strcmp(el, "dc:creator") ) {
122                                                 FILL(ctx->curitem->author)
123                                         } else if( !strcmp(el, "description") ) {
124                                                 FILL(ctx->curitem->summary)
125                                         } else if( !strcmp(el, "content:encoded") ) {
126                                                 FILL(ctx->curitem->text)
127                                         } else if( !strcmp(el, "link") ) {
128                                                 FILL(ctx->curitem->url)
129                                         } else if( !strcmp(el, "dc:date") ) {
130                                                 ctx->curitem->date_modified = parseISO8601Date(text);
131                                         } else if( !strcmp(el, "pubDate") ) {
132                                                 ctx->curitem->date_modified = parseRFC822Date(text);
133                                         }
134
135                                         break;
136                         }
137
138                         break;
139
140         }
141
142         if( ctx->str != NULL ) {
143                 g_string_free(ctx->str, TRUE);
144                 ctx->str = NULL;
145         }
146 }