summaryrefslogtreecommitdiff
path: root/libpiano/xml.c
blob: a113a1277f4868189b736e863313430ce1d6f2cb (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
Copyright (c) 2008 Lars-Dominik Braun

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "piano.h"
#include "crypt.h"

/*	parses things like this:
 *	<struct>
 *		<member>
 *			<name />
 *			<value />
 *		</member>
 *		<!-- ... -->
 *	</struct>
 *	@author PromyLOPh
 *	@added 2008-06-03
 *	@param xml node named "struct" (or containing a similar structure)
 *	@param who wants to use this data? callback: content of <name> as
 *			string, content of <value> as xmlNode (may contain other nodes
 *			or text), additional data used by callback(); don't forget
 *			to *copy* data taken from <name> or <value> as they will be
 *			freed soon
 *	@param 
 */
void PianoXmlStructParser (xmlNode *structRoot,
		void (*callback) (char *, xmlNode *, void *), void *data) {

	xmlNode *curNode, *memberNode, *valueNode;
	xmlChar *key;

	/* get all <member> nodes */
    for (curNode = structRoot->children; curNode; curNode = curNode->next) {
        if (curNode->type == XML_ELEMENT_NODE &&
				xmlStrEqual ((xmlChar *) "member", curNode->name)) {
			key = NULL;
			valueNode = NULL;
			/* check children for <name> or <value> */
			for (memberNode = curNode->children; memberNode;
					memberNode = memberNode->next) {
				if (memberNode->type == XML_ELEMENT_NODE) {
					if (xmlStrEqual ((xmlChar *) "name", memberNode->name)) {
						key = memberNode->children->content;
					} else if (xmlStrEqual ((xmlChar *) "value",
							memberNode->name)) {
						valueNode = memberNode->children;
					}
				}
			}
			/* this will ignore empty <value /> nodes, but well... */
			if (key != NULL && valueNode != NULL) {
				(*callback) ((char *) key, valueNode, data);
			}
        }
	}
}

/*	get text from <value> nodes; some of them have <boolean>, <string>
 *	or <int> subnodes, just ignore them
 *	@author PromyLOPh
 *	@added 2008-06-13
 *	@param xml node <value>
 */
char *PianoXmlGetNodeText (xmlNode *node) {
	/* FIXME: this is not the correct way; we should check the node type
	 * as well */
	if (node->content != NULL) {
		return (char *) node->content;
	} else if (node->children != NULL &&
			node->children->content != NULL) {
		return (char *) node->children->content;
	}
	return NULL;
}

/*	structParser callback; writes userinfo to PianoUserInfo structure
 *	@author PromyLOPh
 *	@added 2008-06-03
 *	@param value identifier
 *	@param value node
 *	@param pointer to userinfo structure
 *	@return nothing
 */
void PianoXmlParseUserinfoCb (char *key, xmlNode *value, void *data) {
	PianoUserInfo_t *user = data;
	char *valueStr = PianoXmlGetNodeText (value);

	/* FIXME: should be continued later */
	if (strcmp ("webAuthToken", key) == 0) {
		user->webAuthToken = strdup (valueStr);
	} else if (strcmp ("authToken", key) == 0) {
		user->authToken = strdup (valueStr);
	} else if (strcmp ("listenerId", key) == 0) {
		user->listenerId = strdup (valueStr);
	}
}

void PianoXmlParseStationsCb (char *key, xmlNode *value, void *data) {
	PianoStation_t *station = data;
	char *valueStr = PianoXmlGetNodeText (value);

	if (strcmp ("stationName", key) == 0) {
		station->name = strdup (valueStr);
	} else if (strcmp ("stationId", key) == 0) {
		station->id = strdup (valueStr);
	}
}

/* FIXME: copy & waste */
void PianoXmlParsePlaylistCb (char *key, xmlNode *value, void *data) {
	PianoSong_t *song = data;
	char *valueStr = PianoXmlGetNodeText (value);

	if (strcmp ("audioURL", key) == 0) {
		/* last 48 chars of audioUrl are encrypted, but they put the key
		 * into the door's lock; dumb pandora... */
		const char urlTailN = 48;
		char *urlTail, *urlTailCrypted = valueStr + (strlen (valueStr) - urlTailN);
		urlTail = PianoDecryptString (urlTailCrypted);
		//printf ("tail is:\t%s\nwas:\t\t%s (%i)\nurl was:\t %s (%i)\n", urlTail, urlTailCrypted, strlen (urlTailCrypted), valueStr, strlen (valueStr));
		song->audioUrl = calloc (strlen (valueStr) + 1, sizeof (char));
		strncpy (song->audioUrl, valueStr, strlen (valueStr) - urlTailN);
		/* FIXME: the key seems to be broken... so ignore 8 x 0x08 postfix;
		 * urlTailN/2 because the encrypted hex string is now decoded */
		strncat (song->audioUrl, urlTail, (urlTailN/2)-8);
		free (urlTail);

	} else if (strcmp ("artistSummary", key) == 0) {
		song->artist = strdup (valueStr);
	} else if (strcmp ("musicId", key) == 0) {
		song->musicId = strdup (valueStr);
	} else if (strcmp ("matchingSeed", key) == 0) {
		song->matchingSeed = strdup (valueStr);
	} else if (strcmp ("userSeed", key) == 0) {
		song->userSeed = strdup (valueStr);
	} else if (strcmp ("focusTraitId", key) == 0) {
		song->focusTraitId = strdup (valueStr);
	} else if (strcmp ("songTitle", key) == 0) {
		song->title = strdup (valueStr);
	} else if (strcmp ("rating", key) == 0) {
		if (strcmp (valueStr, "1") == 0) {
			song->rating = PIANO_RATE_LOVE;
		} else {
			song->rating = PIANO_RATE_NONE;
		}
	}
}

/*	parses server response and updates handle
 *	@author PromyLOPh
 *	@added 2008-06-03
 *	@param piano handle
 *	@param utf-8 string
 *	@return nothing
 */
void PianoXmlParseUserinfo (PianoHandle_t *ph, char *xml) {
	xmlNode *docRoot = NULL;
	xmlDocPtr doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, 0);

	if (doc == NULL) {
		printf ("whoops... xml parser error\n");
		return;
	}

	docRoot = xmlDocGetRootElement (doc);

	/* <methodResponse> <params> <param> <value> <struct> */
	xmlNode *structRoot = docRoot->children->children->children->children;
	PianoXmlStructParser (structRoot, PianoXmlParseUserinfoCb, &ph->user);

	xmlFreeDoc (doc);
	xmlCleanupParser();
}

/*	parse stations returned by pandora
 *	@author PromyLOPh
 *	@added 2008-06-04
 *	@param piano handle
 *	@param xml returned by pandora
 *	@return nothing
 */
void PianoXmlParseStations (PianoHandle_t *ph, char *xml) {
	/* FIXME: copy & waste */
	xmlNode *docRoot = NULL, *curNode = NULL;
	xmlDocPtr doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, 0);

	if (doc == NULL) {
		printf ("whoops... xml parser error\n");
		return;
	}

	docRoot = xmlDocGetRootElement (doc);

	/* <methodResponse> <params> <param> <value> <array> <data> */
	xmlNode *dataRoot = docRoot->children->children->children->children->children;
    for (curNode = dataRoot->children; curNode; curNode = curNode->next) {
        if (curNode->type == XML_ELEMENT_NODE &&
				xmlStrEqual ((xmlChar *) "value", curNode->name)) {
			PianoStation_t *tmpStation = calloc (1, sizeof (*tmpStation));
			PianoXmlStructParser (curNode->children,
					PianoXmlParseStationsCb, tmpStation);
			/* start new linked list or append */
			if (ph->stations == NULL) {
				ph->stations = tmpStation;
			} else {
				PianoStation_t *curStation = ph->stations;
				while (curStation->next != NULL) {
					curStation = curStation->next;
				}
				curStation->next = tmpStation;
			}
		}
	}

	xmlFreeDoc (doc);
	xmlCleanupParser();
}

/*	parses playlist; used when searching too
 *	@author PromyLOPh
 *	@added 2008-06-12
 *	@param piano handle
 *	@param xml document
 */
void PianoXmlParsePlaylist (PianoHandle_t *ph, char *xml) {
	/* FIXME: copy & waste */
	xmlNode *docRoot = NULL, *curNode = NULL;
	xmlDocPtr doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, 0);

	if (doc == NULL) {
		printf ("whoops... xml parser error\n");
		return;
	}

	docRoot = xmlDocGetRootElement (doc);

	/* <methodResponse> <params> <param> <value> <array> <data> */
	xmlNode *dataRoot = docRoot->children->children->children->children->children;
    for (curNode = dataRoot->children; curNode; curNode = curNode->next) {
        if (curNode->type == XML_ELEMENT_NODE &&
				xmlStrEqual ((xmlChar *) "value", curNode->name)) {
			PianoSong_t *tmpSong = calloc (1, sizeof (*tmpSong));
			PianoXmlStructParser (curNode->children,
					PianoXmlParsePlaylistCb, tmpSong);
			/* begin linked list or append */
			if (ph->playlist == NULL) {
				ph->playlist = tmpSong;
			} else {
				PianoSong_t *curSong = ph->playlist;
				while (curSong->next != NULL) {
					curSong = curSong->next;
				}
				curSong->next = tmpSong;
			}
		}
	}

	xmlFreeDoc (doc);
	xmlCleanupParser();
}

/*	parse simple answers like this: <?xml version="1.0" encoding="UTF-8"?>
 *	<methodResponse><params><param><value>1</value></param></params>
 *	</methodResponse>
 *	@author PromyLOPh
 *	@added 2008-06-10
 *	@param xml string
 *	@return
 */
PianoReturn_t PianoXmlParseSimple (char *xml) {
	xmlNode *docRoot = NULL, *curNode = NULL;
	xmlDocPtr doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, 0);
	PianoReturn_t ret = PIANO_RET_ERR;

	if (doc == NULL) {
		printf ("whoops... xml parser error\n");
		return PIANO_RET_ERR;
	}

	docRoot = xmlDocGetRootElement (doc);

	xmlNode *val = docRoot->children->children->children->children;
	if (xmlStrEqual (val->content, (xmlChar *) "1")) {
		ret = PIANO_RET_OK;
	}

	xmlFreeDoc (doc);
	xmlCleanupParser();

	return ret;
}

/*	xml struct parser callback, used in PianoXmlParseSearchCb
 *	@author PromyLOPh
 *	@added 2008-06-12
 */
void PianoXmlParseSearchArtistCb (char *key, xmlNode *value, void *data) {
	PianoArtist_t *artist = data;
	char *valueStr = PianoXmlGetNodeText (value);

	if (strcmp ("artistName", key) == 0) {
		artist->name = strdup (valueStr);
	} else if (strcmp ("musicId", key) == 0) {
		artist->musicId = strdup (valueStr);
	}
}

/*	callback for xml struct parser used in PianoXmlParseSearch, "switch" for
 *	PianoXmlParseSearchArtistCb and PianoXmlParsePlaylistCb
 *	@author PromyLOPh
 *	@added 2008-06-12
 */
void PianoXmlParseSearchCb (char *key, xmlNode *value, void *data) {
	PianoSearchResult_t *searchResult = data;

	if (strcmp ("artists", key) == 0) {
		xmlNode *curNode;

		/* skip <value><array><data> */
		for (curNode = value->children->children; curNode;
				curNode = curNode->next) {
	        if (curNode->type == XML_ELEMENT_NODE &&
					xmlStrEqual ((xmlChar *) "value", curNode->name)) {

				PianoArtist_t *artist = calloc (1, sizeof (*artist));
				memset (artist, 0, sizeof (*artist));

				PianoXmlStructParser (curNode->children,
						PianoXmlParseSearchArtistCb, artist);

				/* add result to linked list */
				if (searchResult->artists == NULL) {
					searchResult->artists = artist;
				} else {
					PianoArtist_t *curArtist = searchResult->artists;
					while (curArtist->next != NULL) {
						curArtist = curArtist->next;
					}
					curArtist->next = artist;
				}
			}
		}
	} else if (strcmp ("songs", key) == 0) {
		xmlNode *curNode;

		for (curNode = value->children->children; curNode;
				curNode = curNode->next) {
	        if (curNode->type == XML_ELEMENT_NODE &&
					xmlStrEqual ((xmlChar *) "value", curNode->name)) {
				/* FIXME: copy & waste */
				PianoSong_t *tmpSong = calloc (1, sizeof (*tmpSong));
				PianoXmlStructParser (curNode->children,
						PianoXmlParsePlaylistCb, tmpSong);
				/* begin linked list or append */
				if (searchResult->songs == NULL) {
					searchResult->songs = tmpSong;
				} else {
					PianoSong_t *curSong = searchResult->songs;
					while (curSong->next != NULL) {
						curSong = curSong->next;
					}
					curSong->next = tmpSong;
				}
			}
		}
	}
}

/*	parse search result; searchResult is nulled before use
 *	@author PromyLOPh
 *	@added 2008-06-12
 *	@param xml document
 *	@param returns search result
 *	@return nothing yet
 */
void PianoXmlParseSearch (char *searchXml,
		PianoSearchResult_t *searchResult) {
	/* FIXME: copy & waste */
	xmlNode *docRoot = NULL, *curNode = NULL;
	xmlDocPtr doc = xmlReadDoc ((xmlChar *) searchXml, NULL, NULL, 0);

	if (doc == NULL) {
		printf ("whoops... xml parser error\n");
		return;
	}

	docRoot = xmlDocGetRootElement (doc);
	
	xmlNode *structRoot = docRoot->children->children->children->children;
	/* we need a "clean" search result (with null pointers) */
	memset (searchResult, 0, sizeof (*searchResult));
	PianoXmlStructParser (structRoot, PianoXmlParseSearchCb, searchResult);

	xmlFreeDoc (doc);
	xmlCleanupParser();
}

/*	encode reserved xml chars
 *	@author PromyLOPh
 *	@added 2008-06-10
 *	@param encode this
 *	@return encoded string
 */
char *PianoXmlEncodeString (const char *s) {
	char *replacements[] = {"&&amp;", "'&apos;", "\"&quot;", "<&lt;", ">&gt;"};
	char *s_new = NULL;
	unsigned int s_new_n = 0;
	unsigned int i;
	char found = 0;

	s_new_n = strlen (s) + 1;
	s_new = calloc (s_new_n, 1);

	while (*s) {
		found = 0;
		for (i = 0; i < sizeof (replacements) / sizeof (*replacements); i++) {
			if (*s == replacements[i][0]) {
				/* replacements[i]+1 is our replacement, and -1 'cause we
				 * "overwrite" one byte */
				s_new_n += strlen (replacements[i]+1)-1;
				s_new = realloc (s_new, s_new_n);
				strncat (s_new, replacements[i]+1, strlen (replacements[i]+1));
				found = 1;
				/* no need to look for other entities */
				break;
			}
		}
		if (!found) {
			strncat (s_new, s, 1);
		}
		s++;
	}
	return s_new;
}