summaryrefslogtreecommitdiff
path: root/cddb_lookup.c
blob: 4911a762341baf35453314c9a070cdd91ee21c3c (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
/*
cdcd - Command Driven CD player
Copyright (C)1998-99 Tony Arcieri

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.
*/
/* modified for Audio::CD by dougm */

#include "cdaudio.h"
#include "cddb_lookup.h"
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <signal.h>

#define PACKAGE "Audio::CD"

static int verbosity = 0;
static int timestamp = 0;

/* Timestamped discid */
static int timestamped_discid = 0;

void cddb_verbose(void *h, int flag)
{
    verbosity = flag;
}

static cddb_inexact_selection_func_t ixs_func = NULL;

void cddb_inexact_selection_set(cddb_inexact_selection_func_t func)
{
    ixs_func = func;
}

static int inexact_selection(void)
{
    if (ixs_func) {
	return (*ixs_func)();
    }
    else {
	char inbuffer[256];
	fgets(inbuffer, sizeof(inbuffer), stdin);
	return strtol(inbuffer, NULL, 10);
    }
}

int cdcd_cd_stat(int cd_desc, struct disc_info *disc)
{
   cd_stat(cd_desc, disc);
   if(!disc->disc_present) {
      cd_close(cd_desc);
      cd_stat(cd_desc, disc);
      if(!disc->disc_present) {
	 if (verbosity) puts("No disc in drive");
	 return -1;
      }
   }
    
   return 0;
}

void cddb_lookup(int cd_desc, struct disc_data *data)
{
   int index, serverindex, selection, sock = -1;
   struct disc_info disc;
   struct cddb_conf conf;
   struct cddb_serverlist list;
   struct cddb_server *proxy;
   struct cddb_entry entry;
   struct cddb_hello hello;
   struct cddb_query query;
   char http_string[512], discid[CDINDEX_ID_SIZE];
   
   if(cdcd_cd_stat(cd_desc, &disc) < 0)
     return;
   
   if(0)
     cddb_read_disc_data(cd_desc, data);
   else {
      cddb_stat_disc_data(cd_desc, &entry);
      
      if(entry.entry_present) {
	 if(entry.entry_timestamp == timestamp && entry.entry_id == timestamped_discid)
	   return;
	 
	 cddb_read_disc_data(cd_desc, data);
	 timestamp = entry.entry_timestamp;
	 timestamped_discid = entry.entry_id;
      } else {
	 proxy = (struct cddb_server *)malloc(sizeof(struct cddb_server));
	 cddb_read_serverlist(&conf, &list, proxy);
	 if(conf.conf_access == CDDB_ACCESS_LOCAL) {
	    free(proxy);
	    cddb_generate_unknown_entry(cd_desc, data);
	    return;
	 }
	 if(!conf.conf_proxy) {
	    free(proxy);
	    proxy = NULL;
	 } else 
	    if (verbosity) printf("Using proxy http://%s:%d/\n", proxy->server_name, proxy->server_port);	
	      		
	 strncpy(hello.hello_program, PACKAGE, 256);
	 strncpy(hello.hello_version, VERSION, 256);
	 
	 serverindex = 0;
	      
         do {
	    switch(list.list_host[serverindex].host_protocol) {
	     case CDDB_MODE_CDDBP:
	       if (verbosity) printf("Trying CDDB server cddbp://%s:%d/\n", list.list_host[serverindex].host_server.server_name, list.list_host[serverindex].host_server.server_port);
	       sock = cddb_connect_server(list.list_host[serverindex++], proxy, hello);
	       break;
	     case CDDB_MODE_HTTP:
	       if (verbosity) printf("Trying CDDB server http://%s:%d/%s\n", list.list_host[serverindex].host_server.server_name, list.list_host[serverindex].host_server.server_port, list.list_host[serverindex].host_addressing);
	       sock = cddb_connect_server(list.list_host[serverindex++], proxy, hello, http_string, 512);
	       break;
	     case CDINDEX_MODE_HTTP:
	       if (verbosity) printf("Trying CD Index server http://%s:%d/%s\n", list.list_host[serverindex].host_server.server_name, list.list_host[serverindex].host_server.server_port, list.list_host[serverindex].host_addressing);
	       sock = cdindex_connect_server(list.list_host[serverindex++], proxy, http_string, 512);
	       break;
	     default:
	       if (verbosity) puts("Invalid protocol selected!");
	       return;
	    }
	    if(sock == -1) fprintf(stderr, "Connection error: %s\n", cddb_message);
	 } while(serverindex < list.list_len && sock == -1);
	 
	 if(sock == -1) {
	    if (verbosity) puts("Could not establish connection with any CDDB servers!");
	    if(conf.conf_proxy) free(proxy);
	    cddb_generate_unknown_entry(cd_desc, data);
	    return;
	 }
	 serverindex--;
         if (verbosity) puts("Connection established.");
         
	 switch(list.list_host[serverindex].host_protocol) {
	  case CDDB_MODE_CDDBP:
	    if (verbosity) printf("Retrieving information on %02lx.\n", cddb_discid(cd_desc));
            if(cddb_query(cd_desc, sock, CDDB_MODE_CDDBP, &query) < 0) { 
	       fprintf(stderr, "CDDB query error: %s", cddb_message);
	       if(conf.conf_proxy) free(proxy);
	       cddb_generate_unknown_entry(cd_desc, data);
	       return;
	    }
            break;
	  case CDDB_MODE_HTTP:
	    if (verbosity) printf("Retrieving information on %02lx.\n", cddb_discid(cd_desc));
	    if(cddb_query(cd_desc, sock, CDDB_MODE_HTTP, &query, http_string) < 0) {
	       fprintf(stderr, "CDDB query error: %s", cddb_message);
	       if(conf.conf_proxy) free(proxy);
	       cddb_generate_unknown_entry(cd_desc, data);
	       return;
	    }
	    shutdown(sock, 2);
	    close(sock);
		 
	    if((sock = cddb_connect_server(list.list_host[serverindex], proxy, hello, http_string, 512)) < 0) {
	       perror("HTTP server reconnection error");
	       if(conf.conf_proxy) free(proxy);
	       cddb_generate_unknown_entry(cd_desc, data);
	       return;
	    }
	    break;
	  case CDINDEX_MODE_HTTP:
	    cdindex_discid(cd_desc, discid, CDINDEX_ID_SIZE);
	    if (verbosity) printf("Retrieving information on %s.\n", discid);
	    if(cdindex_read(cd_desc, sock, data, http_string) < 0) {
	       if (verbosity) printf("No match for %s.\n", discid);
	       if(conf.conf_proxy) free(proxy);
	       cddb_generate_unknown_entry(cd_desc, data);
	       return;
	    }
	    if (verbosity) printf("Match for %s: %s / %s\nDownloading data...\n", discid, data->data_artist, data->data_title);
	    cddb_write_data(cd_desc, data);
	    return;
	 }
	 
	 if(conf.conf_proxy) free(proxy);
	 
	 if(list.list_host[serverindex].host_protocol == CDINDEX_MODE_HTTP);
	 
         switch(query.query_match) {
          case QUERY_EXACT:
	    if(strlen(query.query_list[0].list_artist) > 0)
	      if (verbosity) printf("Match for %02lx: %s / %s\nDownloading data...\n", cddb_discid(cd_desc), query.query_list[0].list_artist, query.query_list[0].list_title);
	    else
	      if (verbosity) printf("Match for %02lx: %s\nDownloading data...\n", cddb_discid(cd_desc), query.query_list[0].list_title);
	    entry.entry_genre = query.query_list[0].list_genre;
	    entry.entry_id = query.query_list[0].list_id;
	    switch(list.list_host[serverindex].host_protocol) {
	      case CDDB_MODE_CDDBP:
		if(cddb_read(cd_desc, sock, CDDB_MODE_CDDBP, entry, data) < 0) {
		   perror("CDDB read error");
		   cddb_generate_unknown_entry(cd_desc, data);
		   return;
		}	
		cddb_quit(sock);
		break;
	      case CDDB_MODE_HTTP:
		if(cddb_read(cd_desc, sock, CDDB_MODE_HTTP, entry, data, http_string) < 0) {
		   perror("CDDB read error");
		   cddb_generate_unknown_entry(cd_desc, data);
		   return;
		}
		    
		shutdown(sock, 2);
		close(sock);
		break;
	    }
	    break;
          case QUERY_INEXACT:
	    if (verbosity) printf("Inexact match for %02lx.\n", cddb_discid(cd_desc));
	    if (verbosity) puts("Please choose from the following inexact matches:");
	    for(index = 0; index < query.query_matches; index++)
	      if(strlen(query.query_list[index].list_artist) < 1)
		if (verbosity) printf("%d: %s\n", index + 1, query.query_list[index].list_title);
	      else
	        if (verbosity) printf("%d: %s / %s\n", index + 1, query.query_list[index].list_artist, query.query_list[index].list_title);
	    if (verbosity) printf("%d: None of the above.\n", index + 1);
	    if (verbosity) printf("> ");

	    selection = inexact_selection();

	    if(selection > 0 && selection <= query.query_matches) {
	       entry.entry_genre = query.query_list[selection - 1].list_genre;
	       entry.entry_id = query.query_list[selection - 1].list_id;
	       if (verbosity) puts("Downloading data...");
	       switch(list.list_host[serverindex].host_protocol) {
	         case CDDB_MODE_CDDBP:
		   if(cddb_read(cd_desc, sock, CDDB_MODE_CDDBP, entry, data) < 0) {
		      perror("CDDB read error");
		      cddb_generate_unknown_entry(cd_desc, data);
		      return;
		   }
		   cddb_quit(sock);
		   break;
	         case CDDB_MODE_HTTP:
		   if(cddb_read(cd_desc, sock, CDDB_MODE_HTTP, entry, data, http_string) < 0) {
		      perror("CDDB read error");
		      cddb_generate_unknown_entry(cd_desc, data);
		      return;
		   }
		   shutdown(sock, 2);
		   close(sock);
		   break;
	       }
	       break;
	    }
          case QUERY_NOMATCH:
	    if (verbosity) printf("No match for %02lx.\n", cddb_discid(cd_desc));
	    cddb_generate_unknown_entry(cd_desc, data);
         }
         close(sock);
         cddb_write_data(cd_desc, data);
      }
   }
   return; 
}