summaryrefslogtreecommitdiff
path: root/src/libpiano/piano.c
blob: 74ab3ede284dc9c7875f264ff992ba0862ae951e (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
/*
Copyright (c) 2008-2012
	Lars-Dominik Braun <lars@6xq.net>

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.
*/

#ifndef __FreeBSD__
#define _BSD_SOURCE /* required by strdup() */
#define _DARWIN_C_SOURCE /* strdup() on OS X */
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>

#include "piano_private.h"
#include "piano.h"
#include "config.h"

/*	initialize piano handle
 *	@param piano handle
 *	@return nothing
 */
void PianoInit (PianoHandle_t *ph, const char *partnerUser,
		const char *partnerPassword, const char *device, const char *inkey,
		const char *outkey) {
	memset (ph, 0, sizeof (*ph));
	ph->partner.user = strdup (partnerUser);
	ph->partner.password = strdup (partnerPassword);
	ph->partner.device = strdup (device);

	gcry_cipher_open (&ph->partner.in, GCRY_CIPHER_BLOWFISH,
			GCRY_CIPHER_MODE_ECB, 0);
	gcry_cipher_setkey (ph->partner.in, (const unsigned char *) inkey,
			strlen (inkey));

	gcry_cipher_open (&ph->partner.out, GCRY_CIPHER_BLOWFISH,
			GCRY_CIPHER_MODE_ECB, 0);
	gcry_cipher_setkey (ph->partner.out, (const unsigned char *) outkey,
			strlen (outkey));
}

/*	destroy artist linked list
 */
static void PianoDestroyArtists (PianoArtist_t *artists) {
	PianoArtist_t *curArtist, *lastArtist;

	curArtist = artists;
	while (curArtist != NULL) {
		free (curArtist->name);
		free (curArtist->musicId);
		free (curArtist->seedId);
		lastArtist = curArtist;
		curArtist = curArtist->next;
		free (lastArtist);
	}
}

/*	free complete search result
 *	@public yes
 *	@param search result
 */
void PianoDestroySearchResult (PianoSearchResult_t *searchResult) {
	PianoDestroyArtists (searchResult->artists);
	PianoDestroyPlaylist (searchResult->songs);
}

/*	free single station
 *	@param station
 */
void PianoDestroyStation (PianoStation_t *station) {
	free (station->name);
	free (station->id);
	free (station->seedId);
	memset (station, 0, sizeof (*station));
}

/*	free complete station list
 *	@param piano handle
 */
static void PianoDestroyStations (PianoStation_t *stations) {
	PianoStation_t *curStation, *lastStation;

	curStation = stations;
	while (curStation != NULL) {
		lastStation = curStation;
		curStation = curStation->next;
		PianoDestroyStation (lastStation);
		free (lastStation);
	}
}

/* FIXME: copy & waste */
/*	free _all_ elements of playlist
 *	@param piano handle
 *	@return nothing
 */
void PianoDestroyPlaylist (PianoSong_t *playlist) {
	PianoSong_t *curSong, *lastSong;

	curSong = playlist;
	while (curSong != NULL) {
		free (curSong->audioUrl);
		free (curSong->coverArt);
		free (curSong->artist);
		free (curSong->musicId);
		free (curSong->title);
		free (curSong->stationId);
		free (curSong->album);
		free (curSong->feedbackId);
		free (curSong->seedId);
		free (curSong->detailUrl);
		free (curSong->trackToken);
		lastSong = curSong;
		curSong = curSong->next;
		free (lastSong);
	}
}

void PianoDestroyStationInfo (PianoStationInfo_t *info) {
	PianoDestroyPlaylist (info->feedback);
	PianoDestroyPlaylist (info->songSeeds);
	PianoDestroyArtists (info->artistSeeds);
	PianoDestroyStations (info->stationSeeds);
}

/*	destroy genre linked list
 */
static void PianoDestroyGenres (PianoGenre_t *genres) {
	PianoGenre_t *curGenre, *lastGenre;

	curGenre = genres;
	while (curGenre != NULL) {
		free (curGenre->name);
		free (curGenre->musicId);
		lastGenre = curGenre;
		curGenre = curGenre->next;
		free (lastGenre);
	}
}

/*	destroy user information
 */
void PianoDestroyUserInfo (PianoUserInfo_t *user) {
	free (user->authToken);
	free (user->listenerId);
}

/*	destroy partner
 */
static void PianoDestroyPartner (PianoPartner_t *partner) {
	free (partner->user);
	free (partner->password);
	free (partner->device);
	free (partner->authToken);
	gcry_cipher_close (partner->in);
	gcry_cipher_close (partner->out);
	memset (partner, 0, sizeof (*partner));
}

/*	frees the whole piano handle structure
 *	@param piano handle
 *	@return nothing
 */
void PianoDestroy (PianoHandle_t *ph) {
	PianoDestroyUserInfo (&ph->user);
	PianoDestroyStations (ph->stations);
	PianoDestroyPartner (&ph->partner);
	/* destroy genre stations */
	PianoGenreCategory_t *curGenreCat = ph->genreStations, *lastGenreCat;
	while (curGenreCat != NULL) {
		PianoDestroyGenres (curGenreCat->genres);
		free (curGenreCat->name);
		lastGenreCat = curGenreCat;
		curGenreCat = curGenreCat->next;
		free (lastGenreCat);
	}
	memset (ph, 0, sizeof (*ph));
}

/*	destroy request, free post data. req->responseData is *not* freed here, as
 *	it might be allocated by something else than malloc!
 *	@param piano request
 */
void PianoDestroyRequest (PianoRequest_t *req) {
	free (req->postData);
	memset (req, 0, sizeof (*req));
}

/*	get station from list by id
 *	@param search here
 *	@param search for this
 *	@return the first station structure matching the given id
 */
PianoStation_t *PianoFindStationById (PianoStation_t *stations,
		const char *searchStation) {
	while (stations != NULL) {
		if (strcmp (stations->id, searchStation) == 0) {
			return stations;
		}
		stations = stations->next;
	}
	return NULL;
}

/*	convert return value to human-readable string
 *	@param enum
 *	@return error string
 */
const char *PianoErrorToStr (PianoReturn_t ret) {
	switch (ret) {
		case PIANO_RET_OK:
			return "Everything is fine :)";
			break;

		case PIANO_RET_ERR:
			return "Unknown.";
			break;

		case PIANO_RET_INVALID_RESPONSE:
			return "Invalid response.";
			break;

		case PIANO_RET_CONTINUE_REQUEST:
			/* never shown to the user */
			assert (0);
			return "Fix your program.";
			break;

		case PIANO_RET_OUT_OF_MEMORY:
			return "Out of memory.";
			break;

		case PIANO_RET_INVALID_LOGIN:
			return "Wrong email address or password.";
			break;

		/* pandora error messages */
		case PIANO_RET_P_INTERNAL:
			return "Internal error.";
			break;

		case PIANO_RET_P_CALL_NOT_ALLOWED:
			return "Call not allowed.";
			break;

		case PIANO_RET_P_INVALID_AUTH_TOKEN:
			return "Invalid auth token.";
			break;

		case PIANO_RET_P_MAINTENANCE_MODE:
			return "Maintenance mode.";
			break;

		case PIANO_RET_P_MAX_STATIONS_REACHED:
			return "Max number of stations reached.";
			break;

		case PIANO_RET_P_READ_ONLY_MODE:
			return "Read only mode. Try again later.";
			break;

		case PIANO_RET_P_STATION_DOES_NOT_EXIST:
			return "Station does not exist.";
			break;

		case PIANO_RET_P_INVALID_PARTNER_LOGIN:
			return "Invalid partner login.";
			break;

		case PIANO_RET_P_LICENSING_RESTRICTIONS:
			return "Pandora is not available in your country. "
					"Set up a control proxy (see manpage).";
			break;

		case PIANO_RET_P_PARTNER_NOT_AUTHORIZED:
			return "Invalid partner credentials.";
			break;

		case PIANO_RET_P_LISTENER_NOT_AUTHORIZED:
			return "Listener not authorized.";
			break;

		default:
			return "No error message available.";
			break;
	}
}