-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcddb_fill.cc
More file actions
1034 lines (844 loc) · 29.2 KB
/
cddb_fill.cc
File metadata and controls
1034 lines (844 loc) · 29.2 KB
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
/** Hey emacs! This is: -*- adrian-c -*-
kover - Kover is an easy to use WYSIWYG CD cover printer with CDDB support.
Copyright (C) 1999-2000 by Denis Oliver Kropp
Copyright (C) 2000-2003 by Adrian Reber
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., 675 Mass Ave, Cambridge, MA 02139, USA.
File: cddb_fill.cc
Description: the cddb code
Changes:
14 Dec 1998: Initial release
11 Jan 2001: cddb over http
17 Jan 2001: threading support
14 Feb 2001: threading support disabled :(
20 Feb 2001: proxy support
17 Apr 2001: track duration
15 Jul 2001: 211(inexact match) support
30 Sep 2001: proxy authentification support
30 Oct 2001: Better error handling
13 Nov 2001: CDDB without CD
*/
/* $Id: cddb_fill.cc,v 1.23 2004/04/20 21:04:28 adrian Exp $ */
#include "cddb_fill.moc"
#include "cddb_fill.h"
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "cddb_211_item.h"
#include "inexact_dialog.h"
#include "proxy_auth.h"
#include "categories.h"
#ifdef __FreeBSD__
#include <sys/cdio.h>
#define CDROM_LEADOUT 0xAA
#endif
extern "C" {
#ifdef HAVE_LINUX_CDROM_H
#include <linux/cdrom.h>
#endif
#ifdef HAVE_LINUX_UCDROM_H
#include <linux/ucdrom.h>
#endif
}
track_info::track_info(int _track, int _min, int _sec, int _frame)
{
track = _track;
min = _min;
sec = _sec;
length = min * 60 + sec;
start = length * 75 + _frame;
}
CD_Info::CD_Info()
{
trk.setAutoDelete(true);
}
CDDB_Fill::CDDB_Fill(KoverFile * _kover_file):QObject()
{
kover_file = _kover_file;
cd_fd = -1;
code = 0;
}
CDDB_Fill::~CDDB_Fill()
{
}
bool CDDB_Fill::execute()
{
char *message;
if (!openCD()) {
closeCD();
return false;
}
if (!readTOC()) {
closeCD();
return false;
}
if (!getCDDBFromLocalFile()) {
if (!cddb_connect()) {
if (!cddb_query()) {
closeCD();
cddb_disconnect();
return false;
}
cddb_disconnect();
} else {
if (errno < sys_nerr) {
message =
(char *) malloc(strlen(strerror(errno)) +
strlen("Connecting to CDDB server failed: "));
sprintf(message, "Connecting to CDDB server failed: %s",
strerror(errno));
emit statusText(message);
//free (message);
} else if (errno == sys_nerr + 101) {
emit statusText(tr
("No http_proxy environment variable found... Giving up..."));
} else if (errno == sys_nerr + 101) {
emit statusText(tr
("Don't understand http_proxy environment variable... Giving up..."));
}
closeCD();
cddb_disconnect();
return false;
}
}
closeCD();
return true;
}
bool CDDB_Fill::execute_without_cd(const char *id, int cat)
{
char *message;
bool without = true;
categories *category = new categories();
cdinfo.cddb_id = strtoul(id, NULL, 16);
cdinfo.category =
(QString((category->get_category(cat)).c_str())).lower();
code = 200;
cdinfo.artist = "Artist";
cdinfo.cdname = "Title";
cdinfo.length = 0;
cdinfo.ntracks = 0;
cdinfo.trk.clear();
if (!getCDDBFromLocalFile(true)) {
if (!cddb_connect()) {
without = cddb_readcdinfo(sk_2, false, true, without);
cddb_disconnect();
} else {
if (errno < sys_nerr) {
message =
(char *) malloc(strlen(strerror(errno)) +
strlen("Connecting to CDDB server failed: "));
sprintf(message, "Connecting to CDDB server failed: %s",
strerror(errno));
emit statusText(message);
//free (message);
} else if (errno == sys_nerr + 101) {
emit statusText(tr
("No http_proxy environment variable found... Giving up..."));
} else if (errno == sys_nerr + 102) {
emit statusText(tr
("Don't understand http_proxy environment variable... Giving up..."));
}
cddb_disconnect();
if (category) {
delete(category);
category = NULL;
}
return false;
}
}
if (category) {
delete(category);
category = NULL;
}
return without;
}
void CDDB_Fill::setTitleAndContents()
{
QString tracks, contents, cddb_id;
kover_file->setTitle(cdinfo.artist + "\n" + cdinfo.cdname);
for (int i = 0; i < cdinfo.ntracks; i++) {
if (globals.display_track_duration) {
int m = 0;
int n = 0;
m = cdinfo.trk.at(i)->length / 60;
n = cdinfo.trk.at(i)->length - m * 60;
tracks.sprintf("(%.2d:%.2d)-%.2d. ", m, n, i + 1);
} else
tracks.sprintf("%.2d. ", i + 1);
tracks.append(cdinfo.trk.at(i)->songname);
if (i != cdinfo.ntracks - 1)
tracks.append("\n");
contents.append(tracks);
}
kover_file->setContents(contents);
cddb_id.sprintf("0x%lx", cdinfo.cddb_id);
kover_file->set_cddb_id(cddb_id);
}
void CDDB_Fill::cdInfo()
{
QString str;
str.sprintf
(tr("CD contains %d tracks, total time is %d:%02d, the magic number is 0x%lx"),
cdinfo.ntracks, cdinfo.length / 60, cdinfo.length % 60,
cdinfo.cddb_id);
_DEBUG_ fprintf(stderr, "%s:%s\n", PACKAGE, str.latin1());
emit statusText(str);
emit update_id(cdinfo.cddb_id);
}
int CDDB_Fill::openCD()
{
int ds;
_DEBUG_ fprintf(stderr, "CD opening\n");
if (cd_fd != -1) {
emit statusText(tr
("Internal error: Filedescriptor is not -1, already opened?"));
return false;
}
if (!globals.cdrom_device)
globals.cdrom_device = strdup("/dev/cdrom");
if ((cd_fd = open(globals.cdrom_device, O_RDONLY | O_NONBLOCK)) < 0) {
#ifdef __FreeBSD__
emit statusText(QString(tr("Error while opening "))
+ QString(globals.cdrom_device));
#else
switch (errno) {
case EACCES:
emit statusText(QString(tr
("You don´t have permission to read from "))
+ QString(globals.cdrom_device));
break;
case ENOMEDIUM:
emit statusText(QString(tr("There´s no medium in "))
+ QString(globals.cdrom_device));
break;
case EBUSY:
emit statusText(QString(globals.cdrom_device)
+ QString(tr(" is busy!")));
break;
default:
emit statusText(QString(tr("Unknown error while opening "))
+ QString(globals.cdrom_device));
}
#endif
return false;
}
#ifndef __FreeBSD__
ds = ioctl(cd_fd, CDROM_DISC_STATUS);
switch (ds) {
case CDS_AUDIO:
case CDS_MIXED:
break;
case CDS_NO_INFO:
emit statusText(tr
("Oops. No information about disc. Will keep trying..."));
break;
default:
emit statusText(QString(tr("There´s no audio cd in "))
+ QString(globals.cdrom_device) + QString(tr("! (ignoring)")));
return false;
}
_DEBUG_ fprintf(stderr, "CD opened: %d\n", ds);
#endif
return true;
}
void CDDB_Fill::closeCD()
{
if (cd_fd != -1) {
close(cd_fd);
cd_fd = -1;
}
}
bool CDDB_Fill::readTOC()
{
#ifdef __FreeBSD__
ioc_toc_header hdr;
ioc_read_toc_single_entry entry;
#else
cdrom_tochdr hdr;
cdrom_tocentry entry;
#endif
int i, pos;
_DEBUG_ fprintf(stderr, "Reading TOC\n");
if (cd_fd < 0) {
emit statusText(tr
("Internal error: Filedescriptor is -1, not opened?"));
return false;
}
emit statusText(tr("Reading table of contents..."));
#ifdef __FreeBSD__
if (ioctl(cd_fd, CDIOREADTOCHEADER, &hdr) == -1) {
#else
if (ioctl(cd_fd, CDROMREADTOCHDR, &hdr)) {
#endif
emit statusText(tr("Error while reading table of contents!"));
return false;
}
cdinfo.artist = "Artist";
cdinfo.cdname = "Title";
cdinfo.length = 0;
#ifdef __FreeBSD__
cdinfo.ntracks = hdr.ending_track;
#else
cdinfo.ntracks = hdr.cdth_trk1;
#endif
cdinfo.trk.clear();
for (i = 0; i <= cdinfo.ntracks; i++) {
if (i == cdinfo.ntracks)
#ifdef __FreeBSD__
entry.track = CDROM_LEADOUT;
else
entry.track = i + 1;
entry.address_format = CD_MSF_FORMAT;
if (ioctl(cd_fd, CDIOREADTOCENTRY, &entry) == -1) {
#else
entry.cdte_track = CDROM_LEADOUT;
else
entry.cdte_track = i + 1;
entry.cdte_format = CDROM_MSF;
if (ioctl(cd_fd, CDROMREADTOCENTRY, &entry)) {
#endif
emit statusText(tr("Error while reading TOC entry!"));
return false;
}
#ifdef __FreeBSD__
cdinfo.trk.append(new track_info(i + 1, entry.entry.addr.msf.minute,
entry.entry.addr.msf.second, entry.entry.addr.msf.frame));
#else
cdinfo.trk.append(new track_info(i + 1, entry.cdte_addr.msf.minute,
entry.cdte_addr.msf.second, entry.cdte_addr.msf.frame));
#endif
}
pos = cdinfo.trk.first()->length;
for (i = 0; i < cdinfo.ntracks; i++) {
cdinfo.trk.at(i)->length = cdinfo.trk.at(i + 1)->length - pos;
pos = cdinfo.trk.at(i + 1)->length;
}
cdinfo.length = cdinfo.trk.last()->length;
cdinfo.cddb_id = calcID();
emit statusText(tr("Table of contents successfully read"));
_DEBUG_ fprintf(stderr, "Table of contents successfully read: %08lx\n",
cdinfo.cddb_id);
return true;
}
int CDDB_Fill::cddb_sum(int n)
{
char buf[12], *p;
uint ret = 0;
sprintf(buf, "%lu", (unsigned long) n);
for (p = buf; *p != '\0'; p++)
ret += (*p - '0');
return (ret);
}
unsigned long CDDB_Fill::calcID()
{
int i, t = 0, n = 0;
for (i = 0; i < cdinfo.ntracks; i++) {
n += cddb_sum((cdinfo.trk.at(i)->min * 60) + cdinfo.trk.at(i)->sec);
}
t = ((cdinfo.trk.last()->min * 60) + cdinfo.trk.last()->sec) -
((cdinfo.trk.first()->min * 60) + cdinfo.trk.first()->sec);
return ((n % 0xff) << 24 | t << 8 | cdinfo.ntracks);
}
void CDDB_Fill::parse_trails(char *ss)
{
unsigned int i;
for (i = 0; i < strlen(ss); i++) {
if (ss[i] == '\r' || ss[i] == '\n')
ss[i] = 0;
}
}
int CDDB_Fill::cddb_connect()
{
if (globals.use_proxy)
emit statusText(QString(tr("Connecting to "))
+ QString(globals.proxy_server) + "...");
else
emit statusText(QString(tr("Connecting to "))
+ QString(globals.cddb_server) + "...");
return net::connect();
}
void CDDB_Fill::cddb_disconnect()
{
net::disconnect();
}
char *CDDB_Fill::cddbHello()
{
return cddb_hello();
}
/** getCDDBFromLocalFile checks if there is already a file containing the disc info we are looking for.
* Currently the .cddb directory is used to check for available cddb entries
*
*
*/
bool CDDB_Fill::getCDDBFromLocalFile(bool without)
{
if (!globals.read_local_cddb)
return false;
char *cddb_file;
FILE *cddb_file_descriptor = NULL;
char help_string[10];
if (globals.cddb_path) {
_DEBUG_ fprintf(stderr, "CDDBDIR=%s\n", globals.cddb_path);
//if not locally, then category is = NULL
//FIXME
cdinfo.category = "";
cddb_file =
(char *) malloc(strlen(globals.cddb_path) + 10 +
strlen(cdinfo.category));
strcpy(cddb_file, globals.cddb_path);
strncat(cddb_file, cdinfo.category, strlen(cdinfo.category));
strncat(cddb_file, "/", 1);
snprintf(help_string, 9, "%08lx", cdinfo.cddb_id);
strncat(cddb_file, help_string, 8);
_DEBUG_ fprintf(stderr, "file : %s\n", cddb_file);
cddb_file_descriptor = fopen(cddb_file, "r");
free(cddb_file);
if (cddb_file_descriptor) {
emit statusText(tr("Using local values for disc"));
cddb_readcdinfo(cddb_file_descriptor, true, true, without);
fclose(cddb_file_descriptor);
return true;
}
}
return false;
}
/* Sends query to server -- this is the first thing to be done */
bool CDDB_Fill::cddb_query()
{
char *code_string = NULL;
char *query_buffer = NULL;
char *http_buffer = NULL;
char *offset_buffer = NULL;
char *ss, *sss, *ssss;
int i;
int tot_len, len;
emit statusText(tr("Querying database..."));
/* Figure out a good buffer size -- 7 chars per track, plus 256 for the rest
of the query */
tot_len = (cdinfo.ntracks * 7) + 256;
offset_buffer = (char *) malloc(tot_len);
len = 0;
// number ot tracks
len = snprintf(offset_buffer, tot_len, "%d", cdinfo.ntracks);
//all the offsets
for (i = 0; i < cdinfo.ntracks; i++)
len +=
snprintf(offset_buffer + len, tot_len - len, "+%d",
cdinfo.trk.at(i)->start);
query_buffer = (char *) malloc(tot_len);
//the query string
len += snprintf(query_buffer, tot_len, "cddb+query+%08x+%s+%d",
(unsigned int) cdinfo.cddb_id, offset_buffer, cdinfo.length);
_DEBUG_ fprintf(stderr, "the query string : %s\n", query_buffer);
http_buffer = make_cddb_request(query_buffer);
if (!http_buffer)
return false;
_DEBUG_ fprintf(stderr, "Query is [%s]\n", http_buffer);
write(socket_1, http_buffer, strlen(http_buffer));
/* free free free */
free(http_buffer);
free(offset_buffer);
// now using code of the http answer
// to determine if we need a user name and pw
code = 0;
code = CDDBSkipHTTP(socket_1);
_DEBUG_ fprintf(stderr, "kover:CDDB_Fill::cddb_query():http code:%d\n",
code);
switch (code) {
case 407:
if (!do_authentification(query_buffer, socket_1))
return false;
break;
case 200:
_DEBUG_ fprintf(stderr, "kover:sweeeeet!\n");
// proceeding with standard operation
break;
default:
_DEBUG_ fprintf(stderr, "O, I die, Horatio!\n");
emit statusText("O, I die, Horatio!");
fprintf(stderr, "kover:%s:%d: this should not happen\n", __FILE__,
__LINE__);
return false;
}
free(query_buffer);
// determining the code of the cddb answer see http://www.freedb.org/sections.php?op=viewarticle&artid=28 for details
code = 0;
// finally some STL
list < cddb_211_item * >inexact_list;
inexact_dialog *inexact;
int aber = 0;
int bye = 1;
//what category
while (bye) {
code_string = readline(socket_1);
code = atoi(code_string);
if (strlen(code_string) < 5)
continue;
_DEBUG_ fprintf(stderr, "answer: %d %s \n", code, code_string);
if (strchr(code_string, 32))
strcpy(cddb_msg, strchr(code_string, 32) + 1);
free(code_string);
switch (code) {
case 200: /* Success, get the category ID */
//cddb_msg looks like : "newage 670db908....."
_DEBUG_ fprintf(stderr, "code 200... %s\n", cddb_msg);
ss = strchr(cddb_msg, 32); //searching for " "
*ss = 0; // terminating the string cddb_msg after the first " "
cdinfo.category = cddb_msg; //bla bla. cddb_msg now contains only the category
bye = 0; // leave the while loop
break;
case 211:
_DEBUG_ fprintf(stderr,
"Found inexact matches, list follows (until terminating marker)\n");
emit statusText(tr
("Found inexact matches, list follows (until terminating marker)"));
char s[256];
s[0] = 0;
cddb_211_item *ref_211;
//reading all possible matches into ref_211
while (strncmp(s, ".", 1) != 0) {
if (!fgets(s, 255, sk_1)) {
bye = 0; // leave the while loop
break;
}
//not the first element
if (!aber++)
continue;
if (s[0] != 48) {
ss = strchr(s, 13); // searching \r
*ss = 0;
ref_211 = new cddb_211_item(s);
_DEBUG_ fprintf(stderr, "%s:read:%s\n", PACKAGE, s);
//pushing everything in a list
inexact_list.push_back(ref_211);
}
}
//removing the last element. it is just a "."
inexact_list.remove(ref_211);
inexact = new inexact_dialog(inexact_list);
//dialog to choose one of the matches
aber = inexact->exec();
_DEBUG_ fprintf(stderr, "kover:inexact_dialog returns: %d\n",
aber);
if (aber == -1) {
emit statusText("Our duty to your honour.");
return false;
}
//getting the string
ssss = inexact->get(aber);
if (!ssss) {
fprintf(stderr, "kover:%s:%d: this should not happen\n",
__FILE__, __LINE__);
emit statusText("Why does the drum come hither?");
return false;
}
_DEBUG_ fprintf(stderr, "kover:inexact->get(%d) returns:%s\n",
aber, ssss);
//string looks like : rock bd09280d Pink Floyd / The Wall (CD1)
//searching first space
ss = strchr(ssss, 32);
*ss = 0; // terminating the string after the first " "
//cdinfo.category is now "rock"
cdinfo.category = ssss;
free(ssss);
ssss = inexact->get(aber);
if (!ssss) {
fprintf(stderr, "kover:%s:%d: this should not happen\n",
__FILE__, __LINE__);
emit statusText("Why does the drum come hither?");
return false;
}
ss = strchr(ssss, 32); //searching for " "
//ss is now " bd09280d Pink Floyd / The Wall (CD1)"
//watch out, a leading space (32)
sss = strchr(ss + 1, 32);
*sss = 0;
//strtoul - convert a string to an unsigned long integer
cdinfo.cddb_id = strtoul(ss + 1, NULL, 16);
free(ssss);
if (cdinfo.cddb_id == ULONG_MAX) {
fprintf(stderr, "kover:%s:%d: this should not happen\n",
__FILE__, __LINE__);
emit statusText("Why does the drum come hither?");
return false;
}
_DEBUG_ fprintf(stderr, "kover:new id:0x%lx:category:%s\n",
cdinfo.cddb_id, cdinfo.category.latin1());
delete inexact;
bye = 0; // leave the while loop
break;
//return false;
case 202:
emit statusText(tr("No match found."));
return false;
case 403:
emit statusText(tr("Database entry is corrupt."));
return false;
case 409:
emit statusText(tr("No handshake."));
return false;
default:
cddb_msg[strlen(cddb_msg) - 1] = 0;
_DEBUG_ fprintf(stderr, "here(%02d): %s\n", code, cddb_msg);
}
}
cddb_readcdinfo(sk_2, false, true);
return true;
}
bool CDDB_Fill::cddb_readcdinfo(FILE * desc, bool local, bool save_as_file,
bool without)
{
char *cddb_file = NULL;
FILE *cddb_file_descriptor = NULL;
char *query_buffer = NULL;
char *cddb_request = NULL;
char help_string[10];
bool file_opened = false;
char s[256], *ss;
int t;
int track = 0;
char *code_string = NULL;
if (!local) {
if (code == 200 || code == 211) {
// cddb_query was a success, request info
emit statusText(tr("Downloading CD info..."));
t = strlen("cddb+read+%s+%08x") + strlen(cdinfo.category) +
sizeof(cdinfo.cddb_id) + 10;
query_buffer = (char *) malloc(t);
snprintf(query_buffer, t, "cddb+read+%s+%08x",
(const char *) cdinfo.category,
(unsigned int) cdinfo.cddb_id);
if (!globals.base64encoded)
cddb_request = make_cddb_request(query_buffer);
else
cddb_request = make_cddb_request(query_buffer, true);
if (!cddb_request) {
without = false;
return without;
}
_DEBUG_ fprintf(stderr, "Reading : %s\n", cddb_request);
write(socket_2, cddb_request, strlen(cddb_request));
free(cddb_request);
// use proxy stuff needs to be included ... done
code = CDDBSkipHTTP(socket_2);
_DEBUG_ fprintf(stderr,
"kover:CDDB_Fill::cddb_readcdinfo():http code:%d\n", code);
switch (code) {
case 200:
_DEBUG_ fprintf(stderr, "kover:no comment\n");
break;
case 407:
_DEBUG_ fprintf(stderr,
"kover:once more we need some authorization.\n");
if (!do_authentification(query_buffer, socket_2))
return false;
desc = sk_2;
_DEBUG_ fprintf(stderr,
"kover:CDDB_Fill::cddb_readcdinfo():http code:%d\n",
code);
break;
default:
fprintf(stderr, "kover:%s:%d: this should not happen\n",
__FILE__, __LINE__);
return false;
}
free(query_buffer);
int bye = 1;
while (bye) {
code_string = readline(socket_2);
code = atoi(code_string);
_DEBUG_ fprintf(stderr, "answer: %d %s \n", code,
code_string);
if (strlen(code_string) < 5)
continue;
if (strchr(code_string, 32))
strcpy(cddb_msg, strchr(code_string, 32) + 1);
free(code_string);
switch (code) {
case 210:
emit statusText(tr("OK, CDDB database entry follows."));
bye = 0; // leave the while loop
break;
case 401:
emit statusText(tr("Specified CDDB entry not found."));
// maybe here without + search in all categories...
return false;
case 402:
emit statusText(tr("Server error."));
return false;
case 403:
emit statusText(tr("Database entry is corrupt."));
return false;
case 409:
emit statusText(tr("No handshake."));
return false;
default:
break;
}
}
} else {
fprintf(stderr, "kover:%s:%d: this should not happen\n",
__FILE__, __LINE__);
return false;
}
}
s[0] = 0;
while (strncmp(s, ".", 1) != 0) {
if (!fgets(s, 255, desc))
break;
_DEBUG_ fprintf(stderr, "answer: %s", s);
if (!local && save_as_file && globals.write_local_cddb) {
if (!file_opened) {
if (globals.cddb_path) {
cddb_file =
(char *) malloc(strlen(globals.cddb_path) + 10 +
strlen(cdinfo.category));
strcpy(cddb_file, globals.cddb_path);
strncat(cddb_file, cdinfo.category,
strlen(cdinfo.category));
strncat(cddb_file, "/", 1);
snprintf(help_string, 9, "%08lx", cdinfo.cddb_id);
strncat(cddb_file, help_string, 8);
_DEBUG_ fprintf(stderr, "using file: %s\n", cddb_file);
cddb_file_descriptor = fopen(cddb_file, "w");
free(cddb_file);
if (cddb_file_descriptor) {
file_opened = true;
}
}
}
if (cddb_file_descriptor) {
if (strstr(s, "#") || strstr(s, "=")) {
if (strstr(s, "# Submitted via:"))
fprintf(cddb_file_descriptor,
"# Submitted via: %s %s\n", PACKAGE, VERSION);
else if (strstr(s, "# xmcd CD database file"))
fprintf(cddb_file_descriptor,
"# xmcd CD database file generated by %s %s\n",
PACKAGE, VERSION);
else if (strstr(s, "# Processed by")
|| strstr(s, "# Generated: ")
|| strstr(s, "Copyright (C)"))
fprintf(cddb_file_descriptor,
"#\n# Revenge his foul and most unnatural murder. (Hamlet I.5.25)\n#\n");
else if (strstr(s, "DISCID"))
fprintf(cddb_file_descriptor, "DISCID=%08lx\n",
cdinfo.cddb_id);
else
fprintf(cddb_file_descriptor, "%s", s);
}
}
}
if ((ss = strstr(s, "DTITLE")) != NULL) {
ss += 7;
ss[strlen(ss) - 1] = 0;
cdinfo.cdnames = ss;
ss = strchr(s, '/');
*ss = 0;
cdinfo.artist = s + 7;
cdinfo.cdname = ss + 2;
}
if (((ss = strstr(s, "Disc length:")) != NULL) && without) {
cdinfo.length = atoi(ss + 13);
_DEBUG_ fprintf(stderr, "%s:length:%d\n", PACKAGE, cdinfo.length);
}
if ((ss = strstr(s, "TTITLE")) == NULL)
continue;
/* Yeah, yeah. Cheap hack, but it's guarenteed to work! :)
* The following just hacks the returned track name into the variable
*/
ss += 6;
t = atoi(ss);
if (t < 100)
ss += t < 10 ? 2 : 3;
else
ss += 4;
parse_trails(ss);
if (without) {
cdinfo.trk.append(new track_info(++track, 0, 0, 0));
cdinfo.ntracks++;
}
if (cdinfo.trk.at(t)->songname.length())
cdinfo.trk.at(t)->songname += ss;
else
cdinfo.trk.at(t)->songname = ss;
}
if (file_opened && cddb_file_descriptor)
fclose(cddb_file_descriptor);
return true;
}
int CDDB_Fill::CDDBSkipHTTP(int socket)
{
return skip_http_header(socket);
}
char *CDDB_Fill::make_cddb_request(char *query_me, bool use_auth)
{
char *return_me = NULL;
return_me = cddb::make_cddb_request(query_me, use_auth);
if (!strcmp(return_me, "ERRGREET")) {
emit statusText(tr("Trouble creating cddb greeting... Giving up..."));
free(return_me);
return NULL;
}
return return_me;
}
bool CDDB_Fill::do_authentification(char *query_buffer, int socket)
{
proxy_auth *proxy_auth_dialog = NULL;
int aber = 0;
int code = 0;
char *http_buffer = NULL;
//loop until the correct password is entered
while (1) {
if (!globals.base64encoded) {
_DEBUG_ fprintf(stderr, "kover:now requesting authorization.\n");
emit statusText(tr("Initializing authorization"));
proxy_auth_dialog =
new proxy_auth(globals.proxy_server, globals.proxy_port);
// invoking passwd dialog...
aber = proxy_auth_dialog->exec();
_DEBUG_ fprintf(stderr, "kover:proxy_auth returns: %d\n", aber);