| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2026 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <smooth/io/drivers/driver_socket.h> |
| 14 | #include <smooth/io/drivers/driver_https.h> |
| 15 | #include <smooth/io/drivers/driver_socks4.h> |
| 16 | #include <smooth/io/drivers/driver_socks5.h> |
| 17 | |
| 18 | #include <cddb/cddbremote.h> |
| 19 | #include <freac.h> |
| 20 | #include <config.h> |
| 21 | |
| 22 | using namespace BoCA; |
| 23 | |
| 24 | freac::CDDBRemote::CDDBRemote() |
| 25 | { |
| 26 | connected = False; |
| 27 | |
| 28 | socket = NIL; |
| 29 | |
| 30 | in = NIL; |
| 31 | out = NIL; |
| 32 | } |
| 33 | |
| 34 | freac::CDDBRemote::~CDDBRemote() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | String freac::CDDBRemote::SendCommand(const String &iCommand) |
| 39 | { |
| 40 | BoCA::Config *config = BoCA::Config::Get(); |
| 41 | BoCA::Protocol *protocol = BoCA::Protocol::Get("CDDB communication"); |
| 42 | |
| 43 | Int freedbMode = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbModeID, Config::FreedbModeDefault); |
| 44 | |
| 45 | if (!connected && freedbMode == FREEDB_MODE_CDDBP) return "error not connected"; |
| 46 | |
| 47 | String str; |
| 48 | String command = iCommand; |
| 49 | |
| 50 | switch (freedbMode) |
| 51 | { |
| 52 | case FREEDB_MODE_CDDBP: |
| 53 | if (command != NIL) |
| 54 | { |
| 55 | protocol->Write(String("CDDB: > ").Append(command)); |
| 56 | |
| 57 | out->OutputLine(command); |
| 58 | } |
| 59 | |
| 60 | do |
| 61 | { |
| 62 | str = in->InputLine(); |
| 63 | |
| 64 | protocol->Write(String("CDDB: < ").Append(str)); |
| 65 | } |
| 66 | while (str[0] != '2' && str[0] != '3' && str[0] != '4' && str[0] != '5'); |
| 67 | |
| 68 | break; |
| 69 | case FREEDB_MODE_HTTP_GET: |
| 70 | case FREEDB_MODE_HTTP_POST: |
| 71 | if (connected) |
| 72 | { |
| 73 | delete in; |
| 74 | |
| 75 | connected = false; |
| 76 | } |
| 77 | |
| 78 | if (command.StartsWith("proto")) break; |
| 79 | if (command.StartsWith("cddb hello")) break; |
| 80 | if (command.StartsWith("quit")) break; |
| 81 | if (command == NIL) break; |
| 82 | |
| 83 | String freedb_server = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbServerID, Config::FreedbServerDefault); |
| 84 | |
| 85 | Int freedb_http_port = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbHTTPPortID, Config::FreedbHTTPPortDefault); |
| 86 | |
| 87 | Net::Protocols::HTTP http(String("http://").Append(freedb_server).Append(":").Append(String::FromInt(freedb_http_port)).Append(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbQueryPathID, Config::FreedbQueryPathDefault))); |
| 88 | |
| 89 | http.SetParameter("cmd", String(command).Replace(" ", "+")); |
| 90 | http.SetParameter("hello", String("user host ").Append(freac::appName).Append(" ").Append(freac::cddbVersion).Replace(" ", "+")); |
| 91 | http.SetParameter("proto", "6"); |
| 92 | |
| 93 | http.SetHeaderField("User-Email", config->GetStringValue(Config::CategoryFreedbID, Config::FreedbEmailID, Config::FreedbEmailDefault)); |
| 94 | http.SetHeaderField("Charset", "UTF-8"); |
| 95 | |
| 96 | if (freedbMode == FREEDB_MODE_HTTP_GET) http.SetMode(Net::Protocols::HTTP_METHOD_GET); |
| 97 | else http.SetMode(Net::Protocols::HTTP_METHOD_POST); |
| 98 | |
| 99 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyModeID, Config::FreedbProxyModeDefault) != 0) |
| 100 | { |
| 101 | http.SetProxy(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyID, Config::FreedbProxyDefault), config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyPortID, Config::FreedbProxyPortDefault)); |
| 102 | http.SetProxyAuth(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyUserID, Config::FreedbProxyUserDefault), config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyPasswordID, Config::FreedbProxyPasswordDefault)); |
| 103 | |
| 104 | switch (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyModeID, Config::FreedbProxyModeDefault)) |
| 105 | { |
| 106 | case 1: http.SetProxyMode(Net::Protocols::HTTP_PROXY_HTTP); break; |
| 107 | case 2: http.SetProxyMode(Net::Protocols::HTTP_PROXY_HTTPS); break; |
| 108 | case 3: http.SetProxyMode(Net::Protocols::HTTP_PROXY_SOCKS4); break; |
| 109 | case 4: http.SetProxyMode(Net::Protocols::HTTP_PROXY_SOCKS5); break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (http.DownloadToBuffer(httpResultBuffer) == Error()) |
| 114 | { |
| 115 | protocol->Write(String("CDDB: Error connecting to CDDB server at ").Append(freedb_server).Append(":").Append(String::FromInt(freedb_http_port)), MessageTypeError); |
| 116 | |
| 117 | str = "error"; |
| 118 | |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | in = new InStream(STREAM_BUFFER, httpResultBuffer, httpResultBuffer.Size()); |
| 123 | |
| 124 | str = in->InputLine(); |
| 125 | protocol->Write(String("CDDB: < ").Append(str)); |
| 126 | |
| 127 | if (str.StartsWith("210") || str.StartsWith("211")) connected = true; |
| 128 | else delete in; |
| 129 | |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | return str; |
| 134 | } |
| 135 | |
| 136 | Bool freac::CDDBRemote::ConnectToServer() |
| 137 | { |
| 138 | BoCA::Config *config = BoCA::Config::Get(); |
| 139 | BoCA::Protocol *protocol = BoCA::Protocol::Get("CDDB communication"); |
| 140 | |
| 141 | Int freedbMode = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbModeID, Config::FreedbModeDefault); |
| 142 | |
| 143 | if (freedbMode == FREEDB_MODE_CDDBP) |
| 144 | { |
| 145 | String freedb_server = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbServerID, Config::FreedbServerDefault); |
| 146 | |
| 147 | Int freedb_cddbp_port = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbCDDBPPortID, Config::FreedbCDDBPPortDefault); |
| 148 | |
| 149 | String freedb_proxy = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyID, Config::FreedbProxyDefault); |
| 150 | Int freedb_proxy_port = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyPortID, Config::FreedbProxyPortDefault); |
| 151 | |
| 152 | String freedb_proxy_user = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyUserID, Config::FreedbProxyUserDefault); |
| 153 | String freedb_proxy_password = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyPasswordID, Config::FreedbProxyPasswordDefault); |
| 154 | |
| 155 | switch (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyModeID, Config::FreedbProxyModeDefault)) |
| 156 | { |
| 157 | case 0: socket = new DriverSocket(freedb_server, freedb_cddbp_port); break; |
| 158 | case 1: connected = False; return False; |
| 159 | case 2: socket = new DriverHTTPS(freedb_proxy, freedb_proxy_port, freedb_server, freedb_cddbp_port, freedb_proxy_user, freedb_proxy_password); break; |
| 160 | case 3: socket = new DriverSOCKS4(freedb_proxy, freedb_proxy_port, freedb_server, freedb_cddbp_port); break; |
| 161 | case 4: socket = new DriverSOCKS5(freedb_proxy, freedb_proxy_port, freedb_server, freedb_cddbp_port, freedb_proxy_user, freedb_proxy_password); break; |
| 162 | } |
| 163 | |
| 164 | if (socket->GetLastError() != IO_ERROR_OK) |
| 165 | { |
| 166 | protocol->Write(String("CDDB: Error connecting to CDDB server at ").Append(freedb_server).Append(":").Append(String::FromInt(freedb_cddbp_port)), MessageTypeError); |
| 167 | |
| 168 | connected = False; |
| 169 | |
| 170 | delete socket; |
| 171 | |
| 172 | return False; |
| 173 | } |
| 174 | |
| 175 | protocol->Write(String("CDDB: Connected to CDDB server at ").Append(freedb_server).Append(":").Append(freedb_cddbp_port)); |
| 176 | |
| 177 | connected = True; |
| 178 | |
| 179 | in = new InStream(STREAM_DRIVER, socket); |
| 180 | out = new OutStream(STREAM_STREAM, in); |
| 181 | } |
| 182 | |
| 183 | SendCommand(NIL); |
| 184 | SendCommand("proto 6"); |
| 185 | |
| 186 | SendCommand(String("cddb hello user host ").Append(freac::appName).Append(" ").Append(freac::cddbVersion)); |
| 187 | |
| 188 | return True; |
| 189 | } |
| 190 | |
| 191 | Int freac::CDDBRemote::Query(const String &queryString) |
| 192 | { |
| 193 | BoCA::Protocol *protocol = BoCA::Protocol::Get("CDDB communication"); |
| 194 | |
| 195 | String str = SendCommand(queryString); |
| 196 | |
| 197 | ids.RemoveAll(); |
| 198 | titles.RemoveAll(); |
| 199 | categories.RemoveAll(); |
| 200 | |
| 201 | /* No match found. |
| 202 | */ |
| 203 | if (str.StartsWith("202")) return QUERY_RESULT_NONE; |
| 204 | |
| 205 | /* Exact match. |
| 206 | */ |
| 207 | if (str.StartsWith("200")) |
| 208 | { |
| 209 | String id; |
| 210 | String title; |
| 211 | String category; |
| 212 | |
| 213 | for (Int s = 4; s < 256; s++) |
| 214 | { |
| 215 | if (str[s] == ' ') |
| 216 | { |
| 217 | for (Int i = 0; i < 8; i++) id[i] = str[s + i + 1]; |
| 218 | for (Int i = 0; i < (str.Length() - s - 14); i++) title[i] = str[s + i + 10]; |
| 219 | |
| 220 | break; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | category[s - 4] = str[s]; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | ids.Add(StringToDiscID(id)); |
| 229 | titles.Add(title); |
| 230 | categories.Add(category); |
| 231 | |
| 232 | return QUERY_RESULT_SINGLE; |
| 233 | } |
| 234 | |
| 235 | /* Multiple exact matches. |
| 236 | */ |
| 237 | if (str.StartsWith("210") || str.StartsWith("211")) |
| 238 | { |
| 239 | String::InputFormat inputFormat("UTF-8"); |
| 240 | String::OutputFormat outputFormat("UTF-8"); |
| 241 | |
| 242 | do |
| 243 | { |
| 244 | String val = in->InputLine(); |
| 245 | |
| 246 | String id; |
| 247 | String title; |
| 248 | String category; |
| 249 | |
| 250 | protocol->Write(String("CDDB: < ").Append(val)); |
| 251 | |
| 252 | if (val == ".") break; |
| 253 | |
| 254 | for (Int s = 0; s < 256; s++) |
| 255 | { |
| 256 | if (val[s] == ' ') |
| 257 | { |
| 258 | for (Int i = 0; i < 8; i++) id[i] = val[s + i + 1]; |
| 259 | for (Int i = 0; i < (val.Length() - s - 10); i++) title[i] = val[s + i + 10]; |
| 260 | |
| 261 | break; |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | category[s] = val[s]; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | ids.Add(StringToDiscID(id)); |
| 270 | titles.Add(title); |
| 271 | categories.Add(category); |
| 272 | } |
| 273 | while (true); |
| 274 | |
| 275 | if (str[2] == '0' && ids.Length() == 1) return QUERY_RESULT_SINGLE; |
| 276 | else if (str[2] == '0') return QUERY_RESULT_MULTIPLE; |
| 277 | else return QUERY_RESULT_FUZZY; |
| 278 | } |
| 279 | |
| 280 | return QUERY_RESULT_ERROR; |
| 281 | } |
| 282 | |
| 283 | Bool freac::CDDBRemote::Read(const String &category, UnsignedInt32 discID, CDDBInfo &cddbInfo) |
| 284 | { |
| 285 | BoCA::Protocol *protocol = BoCA::Protocol::Get("CDDB communication"); |
| 286 | |
| 287 | /* Check cache of already read entries. |
| 288 | */ |
| 289 | foreach (const CDDBInfo &entry, readEntries) |
| 290 | { |
| 291 | if (entry.category != category || entry.discID != discID) continue; |
| 292 | |
| 293 | cddbInfo = entry; |
| 294 | |
| 295 | return True; |
| 296 | } |
| 297 | |
| 298 | /* Send read command and get result. |
| 299 | */ |
| 300 | String result = SendCommand(String("cddb read ").Append(category).Append(" ").Append(DiscIDToString(discID))); |
| 301 | |
| 302 | if (!result.StartsWith("210")) return False; |
| 303 | |
| 304 | cddbInfo.discID = discID; |
| 305 | cddbInfo.category = category; |
| 306 | |
| 307 | String::InputFormat inputFormat("UTF-8"); |
| 308 | String::OutputFormat outputFormat("UTF-8"); |
| 309 | |
| 310 | result = NIL; |
| 311 | |
| 312 | do |
| 313 | { |
| 314 | String val = in->InputLine(); |
| 315 | |
| 316 | protocol->Write(String("CDDB: < ").Append(val)); |
| 317 | |
| 318 | if (val == ".") break; |
| 319 | |
| 320 | result.Append(val).Append("\n"); |
| 321 | } |
| 322 | while (True); |
| 323 | |
| 324 | /* Parse result and add entry to cache. |
| 325 | */ |
| 326 | if (!ParseCDDBRecord(result, cddbInfo)) return False; |
| 327 | |
| 328 | readEntries.Add(cddbInfo); |
| 329 | |
| 330 | return True; |
| 331 | } |
| 332 | |
| 333 | Bool freac::CDDBRemote::Submit(const CDDBInfo &oCddbInfo) |
| 334 | { |
| 335 | BoCA::Config *config = BoCA::Config::Get(); |
| 336 | BoCA::Protocol *protocol = BoCA::Protocol::Get("CDDB communication"); |
| 337 | |
| 338 | CDDBInfo cddbInfo = oCddbInfo; |
| 339 | |
| 340 | if (!UpdateEntry(cddbInfo)) return False; |
| 341 | |
| 342 | String freedb_server = config->GetStringValue(Config::CategoryFreedbID, Config::FreedbServerID, Config::FreedbServerDefault); |
| 343 | Int freedb_http_port = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbHTTPPortID, Config::FreedbHTTPPortDefault); |
| 344 | |
| 345 | Net::Protocols::HTTP http(String("http://").Append(freedb_server).Append(":").Append(String::FromInt(freedb_http_port)).Append(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbSubmitPathID, Config::FreedbSubmitPathDefault))); |
| 346 | |
| 347 | http.SetHeaderField("Category", cddbInfo.category); |
| 348 | http.SetHeaderField("Discid", DiscIDToString(cddbInfo.discID)); |
| 349 | http.SetHeaderField("User-Email", config->GetStringValue(Config::CategoryFreedbID, Config::FreedbEmailID, Config::FreedbEmailDefault)); |
| 350 | http.SetHeaderField("Submit-Mode", freac::cddbMode); |
| 351 | http.SetHeaderField("Charset", "UTF-8"); |
| 352 | |
| 353 | if (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyModeID, Config::FreedbProxyModeDefault) != 0) |
| 354 | { |
| 355 | http.SetProxy(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyID, Config::FreedbProxyDefault), config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyPortID, Config::FreedbProxyPortDefault)); |
| 356 | http.SetProxyAuth(config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyUserID, Config::FreedbProxyUserDefault), config->GetStringValue(Config::CategoryFreedbID, Config::FreedbProxyPasswordID, Config::FreedbProxyPasswordDefault)); |
| 357 | |
| 358 | switch (config->GetIntValue(Config::CategoryFreedbID, Config::FreedbProxyModeID, Config::FreedbProxyModeDefault)) |
| 359 | { |
| 360 | case 1: http.SetProxyMode(Net::Protocols::HTTP_PROXY_HTTP); break; |
| 361 | case 2: http.SetProxyMode(Net::Protocols::HTTP_PROXY_HTTPS); break; |
| 362 | case 3: http.SetProxyMode(Net::Protocols::HTTP_PROXY_SOCKS4); break; |
| 363 | case 4: http.SetProxyMode(Net::Protocols::HTTP_PROXY_SOCKS5); break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* Format CDDB record and send it. |
| 368 | */ |
| 369 | const String &cddbRecord = FormatCDDBRecord(cddbInfo); |
| 370 | const Array<String> &cddbLines = cddbRecord.Explode("\n"); |
| 371 | |
| 372 | foreach (const String &line, cddbLines) protocol->Write(String("CDDB: > ").Append(line)); |
| 373 | |
| 374 | http.SetContent(cddbRecord); |
| 375 | |
| 376 | if (http.DownloadToBuffer(httpResultBuffer) == Error()) |
| 377 | { |
| 378 | protocol->Write(String("CDDB: Error connecting to CDDB server at ").Append(freedb_server).Append(":").Append(String::FromInt(freedb_http_port)), MessageTypeError); |
| 379 | |
| 380 | return False; |
| 381 | } |
| 382 | |
| 383 | /* Evaluate result and return. |
| 384 | */ |
| 385 | InStream in(STREAM_BUFFER, httpResultBuffer, httpResultBuffer.Size()); |
| 386 | String line = in.InputLine(); |
| 387 | |
| 388 | protocol->Write(String("CDDB: < ").Append(line)); |
| 389 | |
| 390 | if (line.StartsWith("200")) return True; |
| 391 | else return False; |
| 392 | } |
| 393 | |
| 394 | Bool freac::CDDBRemote::CloseConnection() |
| 395 | { |
| 396 | BoCA::Config *config = BoCA::Config::Get(); |
| 397 | |
| 398 | Int freedbMode = config->GetIntValue(Config::CategoryFreedbID, Config::FreedbModeID, Config::FreedbModeDefault); |
| 399 | |
| 400 | if (!connected && freedbMode == FREEDB_MODE_CDDBP) return False; |
| 401 | |
| 402 | SendCommand("quit"); |
| 403 | |
| 404 | if (freedbMode == FREEDB_MODE_CDDBP) |
| 405 | { |
| 406 | delete out; |
| 407 | delete in; |
| 408 | delete socket; |
| 409 | } |
| 410 | |
| 411 | return True; |
| 412 | } |