/
despotify.c
1642 lines (1288 loc) · 44.9 KB
/
despotify.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <zlib.h>
#include "aes.h"
#include "auth.h"
#include "buf.h"
#include "cache.h"
#include "channel.h"
#include "commands.h"
#include "despotify.h"
#include "ezxml.h"
#include "handlers.h"
#include "keyexchange.h"
#include "network.h"
#include "packet.h"
#include "session.h"
#include "sha1.h"
#include "sndqueue.h"
#include "util.h"
#include "xml.h"
#define MAX_BROWSE_REQ 244 /* max entries to load in one browse request */
bool despotify_init()
{
DSFYDEBUG("Initializing networking\n");
if (network_init() != 0) {
DSFYDEBUG("Failed to initialize networking\n");
return false;
}
return true;
}
bool despotify_cleanup()
{
DSFYDEBUG("Uninitializing networking\n");
if (network_cleanup() != 0)
return false;
return true;
}
static void* despotify_thread(void* arg)
{
struct despotify_session* ds = arg;
DSFYDEBUG("Networking thread started\n");
while (1) {
SESSION* s = ds->session;
PHEADER hdr;
unsigned char* payload;
if(packet_read(s, &hdr, &payload))
break;
handle_packet(s, hdr.cmd, payload, hdr.len);
DSFYfree(payload); /* Allocated in packet_read() */
}
DSFYDEBUG("Networking thread exiting\n");
return NULL;
}
struct despotify_session* despotify_init_client(void(*callback)(struct despotify_session*, int, void*, void*), void* callback_data, bool high_bitrate, bool use_cache)
{
struct despotify_session* ds = calloc(1,sizeof(struct despotify_session));
if (!ds)
return NULL;
ds->session = session_init_client();
if (!ds->session)
return NULL;
ds->thread = (pthread_t)0;
pthread_cond_init(&ds->sync_cond, NULL);
pthread_mutex_init(&ds->sync_mutex, NULL);
ds->user_info = &ds->session->user_info;
ds->client_callback = callback;
ds->client_callback_data = callback_data;
ds->high_bitrate = high_bitrate;
ds->use_cache = use_cache;
if (use_cache && !cache_init()) {
DSFYDEBUG("cache_init() failed. Caching disabled!\n");
ds->use_cache = false;
}
return ds;
}
bool despotify_authenticate(struct despotify_session* ds,
const char* user,
const char* password)
{
assert(ds != NULL && ds->session != NULL);
session_auth_set(ds->session, user, password);
if (session_connect(ds->session) < 0)
{
ds->last_error = "Could not connect to server.";
return false;
}
DSFYDEBUG("session_connect() completed\n");
switch (do_key_exchange(ds->session))
{
case 0: /* all ok */
break;
case -11:
ds->last_error = "Client upgrade required";
return false;
case -13:
ds->last_error = "User not found";
return false;
case -14:
ds->last_error = "Account has been disabled";
return false;
case -16:
ds->last_error = "You need to complete your account details";
return false;
case -19:
ds->last_error = "Account/use country mismatch";
return false;
default:
ds->last_error = "Key exchanged failed";
return false;
}
DSFYDEBUG("do_key_exchange() completed\n");
auth_generate_auth_hash(ds->session);
if (key_init(ds->session) < 0)
{
ds->last_error = "Key computation failed.";
return false;
}
if (do_auth(ds->session) < 0)
{
ds->last_error = "Authentication failed. Wrong password?";
return false;
}
DSFYDEBUG("%s", "do_auth() completed\n");
pthread_create(&ds->thread, NULL, &despotify_thread, ds);
pthread_mutex_lock(&ds->session->login_mutex);
pthread_cond_wait(&ds->session->login_cond, &ds->session->login_mutex);
pthread_mutex_unlock(&ds->session->login_mutex);
return true;
}
void despotify_exit(struct despotify_session* ds)
{
DSFYDEBUG("Calling despotify_free() and requesting disconnection\n");
despotify_free(ds, true);
}
void despotify_free(struct despotify_session* ds, bool should_disconnect)
{
int r;
assert(ds != NULL && ds->session != NULL);
if (should_disconnect) {
DSFYDEBUG("Disconnection requested, calling session_disconnect()\n");
session_disconnect(ds->session);
}
if (ds->thread) {
DSFYDEBUG("Canceling despotify networking thread\n");
r = pthread_cancel(ds->thread);
DSFYDEBUG("pthread_cancel() returned %d\n", r);
r = pthread_join(ds->thread, NULL);
DSFYDEBUG("Joined despotify networking thread, return value is %d\n", r);
}
snd_destroy(ds);
session_free(ds->session);
free(ds);
DSFYDEBUG("Session uninitialized and free'd\n");
}
const char* despotify_get_error(struct despotify_session* ds)
{
const char* error;
/* Only session_init_client() failing can cause this. */
if (!ds)
return "Could not allocate memory for a new session.";
error = ds->last_error;
ds->last_error = NULL;
return error;
}
void despotify_set_buffer_size(struct despotify_session* ds, int size)
{
assert(ds != NULL);
if (!ds->fifo)
snd_init(ds);
ds->fifo->maxbytes = size;
}
void despotify_set_watermark(struct despotify_session* ds, int watermark)
{
assert(ds != NULL);
if (!ds->fifo)
snd_init(ds);
ds->fifo->watermark = watermark;
}
/****************************************************
*
* Playback
*
*/
/* called by channel */
static int despotify_aes_callback(CHANNEL* ch,
unsigned char* buf,
unsigned short len)
{
if (ch->state == CHANNEL_DATA) {
struct despotify_session* ds = ch->private;
struct ds_track* t = ds->track;
if (t->key)
DSFYfree(t->key);
t->key = malloc(len);
memcpy(t->key, buf, len);
/* Expand file key */
rijndaelKeySetupEnc (ds->aes.state, t->key, 128);
/* Set initial IV */
memcpy(ds->aes.IV,
"\x72\xe0\x67\xfb\xdd\xcb\xcf\x77"
"\xeb\xe8\xbc\x64\x3f\x63\x0d\x93",
16);
DSFYDEBUG ("Got AES key\n");
/* Add SND_CMD_START to buffer chain,
with a copy of the track struct */
struct ds_track* copy = malloc(sizeof(struct ds_track));
memcpy(copy, ds->track, sizeof(struct ds_track));
snd_ioctl(ds, SND_CMD_START, copy, 0);
}
return 0;
}
static int despotify_substream_callback(CHANNEL * ch,
unsigned char *buf,
unsigned short len)
{
struct despotify_session* ds = ch->private;
switch (ch->state) {
case CHANNEL_HEADER:
DSFYDEBUG("CHANNEL_HEADER\n");
break;
case CHANNEL_DATA: {
int block;
unsigned char* plaintext = (unsigned char *) malloc (len + 1024);
/* Decrypt each 1024 byte block */
for (block = 0; block < len / 1024; block++) {
int i;
/* Deinterleave the 4x256 byte blocks */
unsigned char* ciphertext = plaintext + block * 1024;
unsigned char* w = buf + block * 1024 + 0 * 256;
unsigned char* x = buf + block * 1024 + 1 * 256;
unsigned char* y = buf + block * 1024 + 2 * 256;
unsigned char* z = buf + block * 1024 + 3 * 256;
for (i = 0; i < 1024 && (block * 1024 + i) < len; i += 4) {
*ciphertext++ = *w++;
*ciphertext++ = *x++;
*ciphertext++ = *y++;
*ciphertext++ = *z++;
}
/* Decrypt 1024 bytes block. This will fail for the last block. */
for (i = 0; i < 1024 && (block * 1024 + i) < len; i += 16) {
int j;
/* Produce 16 bytes of keystream from the IV */
rijndaelEncrypt(ds->aes.state, 10,
ds->aes.IV,
ds->aes.keystream);
/* Update IV counter */
for (j = 15; j >= 0; j--) {
ds->aes.IV[j] += 1;
if (ds->aes.IV[j] != 0)
break;
}
/* Produce plaintext by XORing ciphertext with keystream */
for (j = 0; j < 16; j++)
plaintext[block * 1024 + i + j] ^= ds->aes.keystream[j];
}
}
/* Push data onto the sound buffer queue */
snd_ioctl(ds, SND_CMD_DATA, plaintext, len);
break;
}
case CHANNEL_ERROR:
DSFYDEBUG("got CHANNEL_ERROR #%d\n", ds->errorcount);
ds->errorcount += 1;
if (ds->errorcount > 3)
exit(-1);
break;
case CHANNEL_END:
DSFYDEBUG("got CHANNEL_END, processed %d bytes data\n",
ch->total_data_len);
ds->errorcount = 0;
/* Reflect the current offset in the player context */
ds->offset += ch->total_data_len;
if (ch->total_data_len < SUBSTREAM_SIZE) {
DSFYDEBUG("Stream returned short coutn (%d of %d requested), marking END\n",
ch->total_data_len, SUBSTREAM_SIZE);
/* find next playable track */
do {
ds->track = ds->track->next;
} while (ds->track && !ds->track->playable);
/* Add SND_CMD_END to buffer chain */
snd_ioctl(ds, SND_CMD_END, NULL, 0);
ds->offset = 0;
int error = 0;
if (ds->track && ds->play_as_list) {
char fid[20], tid[16];
hex_ascii_to_bytes(ds->track->file_id, fid, sizeof fid);
hex_ascii_to_bytes(ds->track->track_id, tid, sizeof tid);
/* request key for next track */
error = cmd_aeskey(ds->session, fid, tid, despotify_aes_callback, ds);
}
}
snd_ioctl(ds, SND_CMD_CHANNEL_END, NULL, 0);
break;
default:
break;
}
return 0;
}
/* called by pcm_read() when it wants more data */
int despotify_snd_read_stream(struct despotify_session* ds)
{
DSFYDEBUG("Calling cmd_getsubstreams() with offset %d and len %d\n", ds->offset, SUBSTREAM_SIZE);
char fid[20];
hex_ascii_to_bytes(ds->track->file_id, fid, sizeof fid);
if (cmd_getsubstreams(ds->session, fid,
ds->offset, SUBSTREAM_SIZE,
200 * 1000, /* unknown, static value */
despotify_substream_callback, ds))
{
DSFYDEBUG("cmd_getsubstreams() failed for %s\n", ds->track->title);
return -1;
}
return 0;
}
bool despotify_is_track_restricted(struct ds_track* track, const char* country)
{
bool allowed = true;
bool forbidden = false;
if(track->allowed)
allowed = strstr(track->allowed, country) != NULL;
if(track->forbidden)
forbidden = strstr(track->forbidden, country) != NULL;
return !allowed || forbidden;
}
bool despotify_play(struct despotify_session* ds,
struct ds_track* t, bool play_as_list)
{
if (ds->fifo) {
snd_stop(ds);
ds->offset = 0;
}
else
snd_init(ds);
/* notify server we're starting playback */
if (packet_write(ds->session, CMD_TOKENNOTIFY, NULL, 0)) {
DSFYDEBUG("packet_write(CMD_TOKENNOTIFY) failed\n");
return false;
}
ds->track = t;
ds->play_as_list = play_as_list;
char fid[20], tid[16];
hex_ascii_to_bytes(ds->track->file_id, fid, sizeof fid);
hex_ascii_to_bytes(ds->track->track_id, tid, sizeof tid);
int error = cmd_aeskey(ds->session, fid, tid, despotify_aes_callback, ds);
if (error) {
DSFYDEBUG("cmd_aeskey() failed for %s\n", t->title);
return false;
}
/* from here everything is handled in despotify_thread() */
return true;
}
void despotify_next(struct despotify_session* ds)
{
if (ds->fifo) {
snd_stop(ds);
ds->offset = 0;
}
else
snd_init(ds);
if (snd_next(ds))
return;
/* find next playable track */
while (ds->track)
{
ds->track = ds->track->next;
if (ds->track->playable)
break;
}
if (ds->track)
despotify_play(ds, ds->track, ds->play_as_list);
}
bool despotify_stop(struct despotify_session* ds)
{
if (!ds->fifo)
return false;
snd_stop(ds);
ds->offset = 0;
return true;
}
struct ds_track *despotify_get_current_track(struct despotify_session *ds) {
if (ds->track)
return ds->track;
return NULL;
}
static struct buf* despotify_inflate(unsigned char* data, int len)
{
if (!len)
return NULL;
struct z_stream_s z;
memset(&z, 0, sizeof z);
int rc = inflateInit2(&z, -MAX_WBITS);
if (rc != Z_OK) {
DSFYDEBUG("error: inflateInit() returned %d\n", rc);
return NULL;
}
z.next_in = data;
z.avail_in = len;
struct buf* b = buf_new();
buf_extend(b, 4096);
bool loop = true;
int offset = 0;
while (loop) {
z.avail_out = b->size - offset;
z.next_out = b->ptr + offset;
rc = inflate(&z, Z_NO_FLUSH);
switch (rc) {
case Z_OK:
/* inflated fine */
if (z.avail_out == 0) {
/* zlib needs more output buffer */
offset = b->size;
buf_extend(b, b->size * 2);
}
break;
case Z_STREAM_END:
/* end of input */
loop = false;
break;
default:
/* error */
DSFYDEBUG("error: inflate() returned %d\n", rc);
loop = false;
buf_free(b);
b = NULL;
break;
}
}
if (b) {
b->len = b->size - z.avail_out;
buf_append_u8(b, 0); /* null terminate string */
}
inflateEnd(&z);
return b;
}
static int despotify_plain_callback(CHANNEL *ch,
unsigned char *buf,
unsigned short len)
{
struct despotify_session* ds = ch->private;
bool done = false;
switch (ch->state) {
case CHANNEL_DATA:
buf_append_data(ds->response, buf, len);
break;
case CHANNEL_ERROR:
DSFYDEBUG("!!! channel error\n");
done = true;
break;
case CHANNEL_END:
done = true;
break;
default:
break;
}
if (done) {
/* tell caller we're done */
pthread_mutex_lock(&ds->sync_mutex);
pthread_cond_signal(&ds->sync_cond);
pthread_mutex_unlock(&ds->sync_mutex);
}
return 0;
}
static int despotify_gzip_callback(CHANNEL* ch,
unsigned char* buf,
unsigned short len)
{
struct despotify_session* ds = ch->private;
bool done = false;
switch (ch->state) {
case CHANNEL_DATA:
/* Skip a minimal gzip header */
if (ch->total_data_len < 10) {
int skip_len = 10 - ch->total_data_len;
while (skip_len && len) {
skip_len--;
len--;
buf++;
}
if (len == 0)
return 0;
}
buf_append_data(ds->response, buf, len);
break;
case CHANNEL_END:
done = true;
break;
case CHANNEL_ERROR:
DSFYDEBUG("!!! channel error\n");
done = true;
break;
default:
/* unknown state */
break;
}
if (done) {
/* tell caller we're done */
pthread_mutex_lock(&ds->sync_mutex);
pthread_cond_signal(&ds->sync_cond);
pthread_mutex_unlock(&ds->sync_mutex);
}
return 0;
}
/****************************************************
*
* Timeouts
*
*/
bool despotify_wait_timeout(struct despotify_session* ds){
struct timeval tv;
struct timespec ts;
/* Get system time */
gettimeofday(&tv, NULL);
/* Setup timespec structure */
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
ts.tv_sec += TIMEOUT;
/* timed wait until response is ready */
pthread_mutex_lock(&ds->sync_mutex);
if (pthread_cond_timedwait(&ds->sync_cond, &ds->sync_mutex, &ts) == ETIMEDOUT) {
pthread_mutex_unlock(&ds->sync_mutex);
DSFYDEBUG("Timeout while waiting on sync condition\n");
return false;
}
pthread_mutex_unlock(&ds->sync_mutex);
return true;
}
/****************************************************
*
* Search
*
*/
struct ds_search_result* despotify_search(struct despotify_session* ds,
char* searchtext, int maxresults)
{
struct ds_search_result* search = NULL;
ds->response = buf_new();
ds->playlist = calloc(1, sizeof(struct ds_playlist));
char buf[80];
snprintf(buf, sizeof buf, "Search: %s", searchtext);
buf[(sizeof buf)-1] = 0;
DSFYstrncpy(ds->playlist->name, buf, sizeof ds->playlist->name);
DSFYstrncpy(ds->playlist->author, ds->session->username, sizeof ds->playlist->author);
int ret = cmd_search(ds->session, searchtext, 0, maxresults,
despotify_gzip_callback, ds);
if (ret) {
ds->last_error = "cmd_search() failed";
return NULL;
}
/* wait until search is ready */
if (!despotify_wait_timeout(ds)) {
ds->last_error = "Timeout when searching";
return NULL;
}
/* Add tracks */
if (!ds->playlist->tracks)
ds->playlist->tracks = calloc(1, sizeof(struct ds_track));
struct buf* b = despotify_inflate(ds->response->ptr, ds->response->len);
if (b) {
search = calloc(1, sizeof(struct ds_search_result));
DSFYstrncpy(search->query, searchtext, sizeof search->query);
search->playlist = ds->playlist;
search->tracks = ds->playlist->tracks;
ds->playlist->num_tracks = xml_parse_search(search, ds->playlist->tracks, b->ptr, b->len, ds->high_bitrate);
buf_free(b);
}
buf_free(ds->response);
if (!search) {
ds->last_error = "Error when searching";
return NULL;
}
return search;
}
struct ds_search_result* despotify_search_more(struct despotify_session *ds,
struct ds_search_result *search,
int offset, int maxresults)
{
if (!search || !search->tracks)
return NULL;
if (offset >= search->total_tracks)
return search;
ds->response = buf_new();
int ret = cmd_search(ds->session, search->query,
offset, maxresults,
despotify_gzip_callback, ds);
if (ret) {
ds->last_error = "cmd_search() failed";
return NULL;
}
/* wait until search is ready */
if (!despotify_wait_timeout(ds)) {
ds->last_error = "Timeout when searching";
return NULL;
}
struct buf* b = despotify_inflate(ds->response->ptr, ds->response->len);
if (b) {
/* append at end of list */
struct ds_track *t;
for (t = search->tracks; t; t = t->next)
if (!t->next)
break;
t = t->next = calloc(1, sizeof(struct ds_track));
ds->playlist->num_tracks += xml_parse_tracklist(t, b->ptr, b->len,
false, ds->high_bitrate);
buf_free(b);
}
buf_free(ds->response);
return search;
}
void despotify_free_search(struct ds_search_result *search) {
despotify_free_playlist(search->playlist);
xml_free_album(search->albums);
xml_free_artist(search->artists);
free(search);
}
/**************************************************************
*
* Playlists
*
*/
static bool despotify_load_tracks(struct despotify_session *ds, bool cache_do_store)
{
struct ds_playlist* pl = ds->playlist;
if (!pl->num_tracks)
return true;
struct ds_track* t = pl->tracks;
/* construct an array of 16-byte track ids */
char* tracklist = malloc(MAX_BROWSE_REQ * 16);
SHA1_CTX sha_ctx;
unsigned char hash[20], tracks_hash[33];
int track_count = 0;
/* don't request too many tracks at once */
int count;
for (int totcount=0; totcount < pl->num_tracks; totcount += count) {
ds->response = buf_new();
struct ds_track* firsttrack = t;
for (count = 0; t && count < MAX_BROWSE_REQ; t = t->next, count++)
hex_ascii_to_bytes(t->track_id, tracklist + count * 16, 16);
/* create a hash of the tracklist */
SHA1Init(&sha_ctx);
SHA1Update(&sha_ctx, tracklist, count * 16);
SHA1Final(hash, &sha_ctx);
hex_bytes_to_ascii(hash, tracks_hash, 20);
/* check cache */
if (ds->use_cache && cache_contains(tracks_hash)) {
unsigned char* data;
int len;
DSFYDEBUG("Loading cached tracks...\n");
if ((data = cache_load(tracks_hash, &len)) != NULL) {
track_count += xml_parse_tracklist(firsttrack, data, len,
true, ds->high_bitrate);
free(data);
continue;
}
}
int error = cmd_browse(ds->session, BROWSE_TRACK, tracklist, count,
despotify_gzip_callback, ds);
if (error) {
DSFYDEBUG("cmd_browse() failed with %d\n", error);
ds->last_error = "Network error.";
session_disconnect(ds->session);
return false;
}
/* wait until track fetch is ready */
if (!despotify_wait_timeout(ds)) {
ds->last_error = "Timeout while loading tracks";
return NULL;
}
/* add tracks to playlist */
struct buf* b = despotify_inflate(ds->response->ptr, ds->response->len);
if (b) {
/* store tracks xml in cache. */
if (cache_do_store)
cache_store(tracks_hash, b->ptr, b->len);
track_count += xml_parse_tracklist(firsttrack, b->ptr, b->len,
true, ds->high_bitrate);
buf_free(b);
}
buf_free(ds->response);
}
free(tracklist);
/* tracks that still lack meta data are likely duplicates */
if (track_count < pl->num_tracks) {
for (t = pl->tracks; t; t = t->next) {
if (!t->has_meta_data) {
struct ds_track* tt;
for (tt = pl->tracks; tt; tt = tt->next) {
if (tt->has_meta_data &&
!strncmp(tt->track_id, t->track_id, sizeof tt->track_id)) {
struct ds_track* next = t->next;
*t = *tt;
t->next = next;
/* deep copy of artist list */
struct ds_artist* a = calloc(1, sizeof(struct ds_artist));
t->artist = a;
struct ds_artist* ta;
for (ta = tt->artist; ta; ta = ta->next) {
*a = *ta;
if (ta->next)
a = a->next = calloc(1, sizeof(struct ds_artist));
}
/* deep copy of georestrictions. */
if(tt->allowed) {
t->allowed = calloc(strlen(tt->allowed) + 1,
sizeof(char));
DSFYstrncpy(t->allowed, tt->allowed,
strlen(tt->allowed) + 1);
}
if(tt->forbidden) {
t->forbidden = calloc(strlen(tt->forbidden) + 1,
sizeof(char));
DSFYstrncpy(t->forbidden, tt->forbidden,
strlen(tt->forbidden) + 1);
}
t->has_meta_data = true;
track_count++;
break;
}
}
}
}
}
pl->num_tracks = track_count;
return true;
}
struct ds_playlist* despotify_get_playlist(struct despotify_session *ds,
char* playlist_id, bool cache_do_store)
{
ds->playlist = calloc(1, sizeof(struct ds_playlist));
/* check cache */
if (ds->use_cache && playlist_id && cache_contains(playlist_id)) {
unsigned char* data;
int len;
DSFYDEBUG("Loading cached playlist '%s'...\n", playlist_id);
if ((data = cache_load(playlist_id, &len)) != NULL) {
ds->playlist = xml_parse_playlist(ds->playlist, data, len, false);
free(data);
DSFYstrncpy(ds->playlist->playlist_id, playlist_id, sizeof ds->playlist->playlist_id);
/* fill the playlist with track info */
if (!despotify_load_tracks(ds, cache_do_store)) {
ds->last_error = "Failed loading track info";
DSFYDEBUG("%s", ds->last_error);
free(ds->playlist);
return NULL;
}
return ds->playlist;
}
}
ds->response = buf_new();
static const char* load_lists =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<playlist>\n";
buf_append_data(ds->response, (char*)load_lists, strlen(load_lists));
char pid[17];
if (playlist_id)
hex_ascii_to_bytes(playlist_id, pid, 17);
else {
/* enable list_of_lists state */
ds->list_of_lists = true;
memset(pid, 0, sizeof pid);
}
int error = cmd_getplaylist(ds->session, pid, PLAYLIST_CURRENT,
despotify_plain_callback, ds);
if (error) {
DSFYDEBUG("Failed getting playlist\n");
ds->last_error = "Network error.";
session_disconnect(ds->session);
return NULL;
}
/* wait until playlist fetch is ready */
if (!despotify_wait_timeout(ds)) {
ds->last_error = "Timeout while loading playlist";
return NULL;
}
buf_append_u8(ds->response, 0); /* null terminate xml string */
/* store playlist xml in cache. */
if (cache_do_store && playlist_id)
cache_store(playlist_id, ds->response->ptr, ds->response->len);
ds->playlist = xml_parse_playlist(ds->playlist,
ds->response->ptr,
ds->response->len,
ds->list_of_lists);
if (! ds->list_of_lists && playlist_id)
DSFYstrncpy(ds->playlist->playlist_id, playlist_id, sizeof ds->playlist->playlist_id);
ds->list_of_lists = false;
buf_free(ds->response);