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