~ubuntu-branches/ubuntu/vivid/liferea/vivid-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * @file cdf_channel.c CDF channel parsing
 *
 * Copyright (C) 2003-2010 Lars Lindner <lars.lindner@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* CDF is evil. There is only one outdated specification of it and its
   example is not even well-formed XML! Also, it seems to rely on
   things being case insensitive. Some people seem to make the tags
   capitalized, while others are like "Channel". */

#include "cdf_channel.h"

#include <sys/time.h>
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

#include "common.h"
#include "feedlist.h"
#include "cdf_item.h"
#include "metadata.h"
#include "xml.h"

/* note: the tag order has to correspond with the CHANNEL_* defines in the header file */
static GHashTable *channelHash = NULL;

/* method to parse standard tags for the channel element */
static void parseCDFChannel(feedParserCtxtPtr ctxt, xmlNodePtr cur, CDFChannelPtr cp) {
	gchar		*tmp, *tmp2, *tmp3;
	
	cur = cur->xmlChildrenNode;
	while(cur) {
		if(!cur->name || cur->type != XML_ELEMENT_NODE) {
			cur = cur->next;
			continue;
		}

		if((!xmlStrcasecmp(cur->name, BAD_CAST"logo"))) {
			tmp = (gchar *)xmlGetProp(cur, BAD_CAST"HREF");
			if(tmp) {
				tmp = (gchar *)xmlGetProp(cur, BAD_CAST"href");
				metadata_list_set (&ctxt->subscription->metadata, "imageUrl", tmp);
				g_free(tmp);
			}

		} else if((!xmlStrcasecmp(cur->name, BAD_CAST"a"))) {
			xmlChar *value = xmlGetProp(cur, BAD_CAST"HREF");
			if(value) {
				subscription_set_homepage (ctxt->subscription, (gchar *)value);
				xmlFree(value);
			}

		} else if((!xmlStrcasecmp(cur->name, BAD_CAST"item"))) {
			ctxt->item = parseCDFItem(ctxt, cur, cp);
			if(ctxt->item) {
				if(0 == ctxt->item->time)
					ctxt->item->time = cp->time;
				ctxt->items = g_list_append(ctxt->items, ctxt->item);
			}

		} else if(!xmlStrcasecmp(cur->name, BAD_CAST "title")) {
			tmp = (gchar *)xmlNodeListGetString(cur->doc, cur->xmlChildrenNode, TRUE);
			if(tmp) {
				tmp = unhtmlize(tmp);
				
				if(ctxt->title)
					g_free(ctxt->title);
				ctxt->title = tmp;
			}
			
		} else if (!xmlStrcasecmp(cur->name, BAD_CAST "abstract")) {
			tmp = (gchar *)xmlNodeListGetString (cur->doc, cur->xmlChildrenNode, TRUE);
			if (tmp) {
				metadata_list_set (&ctxt->subscription->metadata, "description", tmp);
				xmlFree (tmp);
			}
			
		} else {		
			tmp = g_ascii_strdown((gchar *)cur->name, -1);
			tmp2 = g_hash_table_lookup(channelHash, tmp);
			if(tmp2) {
				tmp3 = (gchar *)xmlNodeListGetString(cur->doc, cur->xmlChildrenNode, TRUE);
				if(tmp3) {
					ctxt->subscription->metadata = metadata_list_append(ctxt->subscription->metadata, tmp2, tmp3);
					g_free(tmp3);
				}
			}
			g_free(tmp);
		}
		
		cur = cur->next;
	}
}

/* reads a CDF feed URL and returns a new channel structure (even if
   the feed could not be read) */
static void cdf_parse(feedParserCtxtPtr ctxt, xmlNodePtr cur) {
	CDFChannelPtr 	cp;
	
	cp = g_new0(struct CDFChannel, 1);
	
	do {
		/* note: we support only one flavour of CDF channels! We will only
		   support the outer channel of the CDF feed. */
		
		/* find outer channel tag */
		while(cur) {
			if(cur->type == XML_ELEMENT_NODE && (!xmlStrcasecmp(cur->name, BAD_CAST"channel"))) {
				cur = cur->xmlChildrenNode;
				break;
			}
			cur = cur->next;
		}

		time(&(cp->time));
		
		/* find first "real" channel tag */
		while(cur) {
			if((!xmlStrcasecmp(cur->name, BAD_CAST"channel"))) {
				parseCDFChannel(ctxt, cur, cp);
				break;
			}
			cur = cur->next;
		}

		/* after parsing we fill in the infos into the subscription structure */		
		subscription_set_default_update_interval(ctxt->subscription, -1);
		
		g_free(cp);
	} while (FALSE);
}

static gboolean cdf_format_check(xmlDocPtr doc, xmlNodePtr cur) {
		
	/* avoid mistaking RSS 1.1 which also uses "Channel" as root tag */
	if((NULL != cur->ns) &&
	   (NULL != cur->ns->href) &&
	   !xmlStrcmp(cur->ns->href, BAD_CAST"http://purl.org/net/rss1.1#"))
	   	return FALSE;
		
	if(!xmlStrcasecmp(cur->name, BAD_CAST"Channel"))
		return TRUE;
		
	return FALSE;
}

/* ---------------------------------------------------------------------------- */
/* initialization		 						*/
/* ---------------------------------------------------------------------------- */

#define CDF_CHANNEL_PUBDATE		4
#define CDF_CHANNEL_WEBMASTER		5
#define CDF_CHANNEL_CATEGORY		6

feedHandlerPtr cdf_init_feed_handler(void) {
	feedHandlerPtr	fhp;
	
	fhp = g_new0(struct feedHandler, 1);
	
	/* there are no name space handlers! */
	if (channelHash == NULL) {
		channelHash = g_hash_table_new(g_str_hash, g_str_equal);
		g_hash_table_insert(channelHash, "copyright", "copyright");
		g_hash_table_insert(channelHash, "publicationdate", "lastBuildDate");
		g_hash_table_insert(channelHash, "publisher", "managingEditor");
		g_hash_table_insert(channelHash, "category", "category");
	}
	
	/* prepare feed handler structure */
	fhp->typeStr = "cdf";
	fhp->feedParser	= cdf_parse;
	fhp->checkFormat = cdf_format_check;
	return fhp;
}