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