-
Notifications
You must be signed in to change notification settings - Fork 2
/
spshell.c
289 lines (239 loc) · 8.02 KB
/
spshell.c
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
/**
* Copyright (c) 2006-2010 Spotify Ltd
*
* 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 <string.h>
#include "spshell.h"
#include "cmd.h"
sp_session *g_session;
void (*metadata_updated_fn)(void);
int is_logged_out;
int log_to_stderr;
/**
* This callback is called when the user was logged in, but the connection to
* Spotify was dropped for some reason.
*
* @sa sp_session_callbacks#connection_error
*/
static void connection_error(sp_session *session, sp_error error)
{
fprintf(stderr, "Connection to Spotify failed: %s\n",
sp_error_message(error));
}
/**
* This callback is called when an attempt to login has succeeded or failed.
*
* @sa sp_session_callbacks#logged_in
*/
static void logged_in(sp_session *session, sp_error error)
{
sp_user *me;
const char *my_name;
int cc;
if (SP_ERROR_OK != error) {
fprintf(stderr, "failed to log in to Spotify: %s\n",
sp_error_message(error));
sp_session_release(session);
exit(4);
}
// Let us print the nice message...
me = sp_session_user(session);
my_name = (sp_user_is_loaded(me) ? sp_user_display_name(me) : sp_user_canonical_name(me));
cc = sp_session_user_country(session);
fprintf(stderr, "Logged in to Spotify as user %s (registered in country: %c%c)\n", my_name, cc >> 8, cc & 0xff);
start_recv();
}
/**
* This callback is called when the session has logged out of Spotify.
*
* @sa sp_session_callbacks#logged_out
*/
static void logged_out(sp_session *session)
{
is_logged_out = 1; // Will exit mainloop
}
/**
* This callback is called for log messages.
*
* @sa sp_session_callbacks#log_message
*/
static void log_message(sp_session *session, const char *data)
{
if (log_to_stderr)
fprintf(stderr, "%s", data);
}
/**
* replaces find with replacement,
* to be used in the tokenizer later.
* @todo: replace this function with a proper uri parser
*/
char * replace( char const * const original, char const * const pattern, char const * const replacement)
{
size_t const replen = strlen(replacement);
size_t const patlen = strlen(pattern);
size_t const orilen = strlen(original);
size_t patcnt = 0;
const char * oriptr;
const char * patloc;
// find how many times the pattern occurs in the original string
for ( oriptr = original; ( patloc = strstr(oriptr, pattern) ); oriptr = patloc + patlen )
{
patcnt++;
}
{
// allocate memory for the new string
size_t const retlen = orilen + patcnt * (replen - patlen);
char * const returned = (char *) malloc( sizeof(char) * (retlen + 1) );
if (returned != NULL)
{
// copy the original string,
// replacing all the instances of the pattern
char * retptr = returned;
for (oriptr = original; ( patloc = strstr(oriptr, pattern) ); oriptr = patloc + patlen )
{
size_t const skplen = patloc - oriptr;
// copy the section until the occurence of the pattern
strncpy(retptr, oriptr, skplen);
retptr += skplen;
// copy the replacement
strncpy(retptr, replacement, replen);
retptr += replen;
}
// copy the rest of the string.
strcpy(retptr, oriptr);
}
return returned;
}
}
/**
* Callback called when libspotify has new metadata available
*
* Not used in this example (but available to be able to reuse the session.c file
* for other examples.)
*
* @sa sp_session_callbacks#metadata_updated
*/
static void metadata_updated(sp_session *sess)
{
if(metadata_updated_fn)
metadata_updated_fn();
}
/**
* Session callbacks
*/
static sp_session_callbacks callbacks = {
&logged_in,
&logged_out,
&metadata_updated,
&connection_error,
NULL,
¬ify_main_thread,
NULL,
NULL,
&log_message,
NULL,
NULL, // streaming error
NULL, // userinfo update
NULL, // start_playback
NULL, // stop_playback
NULL, // get_audio_buffer_stats
NULL,
NULL, // offline error
};
/**
*
*/
int spshell_init(const char *username, const char *password)
{
sp_session_config config;
sp_error error;
sp_session *session;
/// The application key is specific to each project, and allows Spotify
/// to produce statistics on how our service is used.
extern const char g_appkey[];
/// The size of the application key.
extern const size_t g_appkey_size;
memset(&config, 0, sizeof(config));
// Always do this. It allows libspotify to check for
// header/library inconsistencies.
config.api_version = SPOTIFY_API_VERSION;
// The path of the directory to store the cache. This must be specified.
// Please read the documentation on preferred values.
config.cache_location = "tmp";
// The path of the directory to store the settings.
// This must be specified.
// Please read the documentation on preferred values.
config.settings_location = "tmp";
// The key of the application. They are generated by Spotify,
// and are specific to each application using libspotify.
config.application_key = g_appkey;
config.application_key_size = g_appkey_size;
// This identifies the application using some
// free-text string [1, 255] characters.
config.user_agent = "spshell";
// Register the callbacks.
config.callbacks = &callbacks;
error = sp_session_create(&config, &session);
if (SP_ERROR_OK != error) {
fprintf(stderr, "failed to create session: %s\n",
sp_error_message(error));
return 2;
}
// Login using the credentials given on the command line.
if (username == NULL) {
char reloginname[256];
if (sp_session_relogin(session) == SP_ERROR_NO_CREDENTIALS) {
fprintf(stderr, "No stored credentials\n");
return 3;
}
sp_session_remembered_user(session, reloginname, sizeof(reloginname));
fprintf(stderr, "Trying to relogin as user %s\n", reloginname);
} else {
sp_session_login(session, username, password, 1, NULL);
}
log_to_stderr = 1;
g_session = session;
return 0;
}
/**
*
*/
int cmd_logout(int argc, char **argv)
{
if(argc == 2 && !strcmp(argv[1], "permanent")) {
fprintf(stderr, "Dropping stored credentials\n");
sp_session_forget_me(g_session);
}
sp_session_logout(g_session);
return 0;
}
/**
*
*/
int cmd_log(int argc, char **argv)
{
if(argc != 2) {
fprintf(stderr, "log enable|disable\n");
return -1;
}
log_to_stderr = !strcmp(argv[1], "enable");
return 1;
}