annotate Plugins/General/scrobbler/scrobbler.c @ 1340:eaa1b6342e75 trunk

[svn] - itemtag buffer was too short. e.g. 256 bytes could hold only 28 url encoded kanji characters.
author yaz
date Tue, 27 Jun 2006 23:14:47 -0700
parents 8a2d526864c2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
1 #include <pthread.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
2 #include <limits.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
3 #include <stdlib.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
4 #include <string.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
5 #include <stdarg.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
6 #include <curl/curl.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
7 #include <stdio.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
8 #include "fmt.h"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
9 #include "md5.h"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
10 #include "queue.h"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
11 #include "scrobbler.h"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
12 #include "config.h"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
13 #include <glib.h>
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
14
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
15 #define SCROBBLER_HS_URL "http://post.audioscrobbler.com"
690
85fa136c9edf [svn] - Change client id from XMMS-Scrobbler to Audacious (we now have our own ID)
nenolod
parents: 688
diff changeset
16 #define SCROBBLER_CLI_ID "aud"
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
17 #define SCROBBLER_HS_WAIT 1800
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
18 #define SCROBBLER_SB_WAIT 10
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
19 #define SCROBBLER_VERSION "1.1"
690
85fa136c9edf [svn] - Change client id from XMMS-Scrobbler to Audacious (we now have our own ID)
nenolod
parents: 688
diff changeset
20 #define SCROBBLER_IMPLEMENTATION "0.1" /* This is the implementation, not the player version. */
1340
eaa1b6342e75 [svn] - itemtag buffer was too short. e.g. 256 bytes could hold only 28 url encoded kanji characters.
yaz
parents: 1239
diff changeset
21 #define SCROBBLER_SB_MAXLEN 1024
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
22 #define CACHE_SIZE 1024
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
23
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
24 /* Scrobblerbackend for xmms plugin, first draft */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
25
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
26 static int sc_hs_status,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
27 sc_hs_timeout,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
28 sc_hs_errors,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
29 sc_sb_errors,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
30 sc_bad_users,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
31 sc_submit_interval,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
32 sc_submit_timeout,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
33 sc_srv_res_size,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
34 sc_giveup,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
35 sc_major_error_present;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
36
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
37 static char *sc_submit_url,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
38 *sc_username,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
39 *sc_password,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
40 *sc_challenge_hash,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
41 sc_response_hash[33],
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
42 *sc_srv_res,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
43 sc_curl_errbuf[CURL_ERROR_SIZE],
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
44 *sc_major_error;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
45
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
46 static void dump_queue();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
47
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
48 /* Error functions */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
49
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
50 static void sc_throw_error(char *errortxt)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
51 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
52 sc_major_error_present = 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
53 if(sc_major_error == NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
54 sc_major_error = strdup(errortxt);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
55
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
56 return;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
57 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
58
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
59 int sc_catch_error(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
60 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
61 return sc_major_error_present;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
62 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
63
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
64 char *sc_fetch_error(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
65 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
66 return sc_major_error;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
67 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
68
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
69 void sc_clear_error(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
70 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
71 sc_major_error_present = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
72 if(sc_major_error != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
73 free(sc_major_error);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
74 sc_major_error = NULL;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
75
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
76 return;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
77 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
78
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
79 static size_t sc_store_res(void *ptr, size_t size,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
80 size_t nmemb,
700
99382cddf771 [svn] Fixes for all warnings (except 3 spurious GCC 4.0 ones, upgrade to 4.1 if you see them) and a performance increase. By external contributor Diego "FlameEyes" Petteno (Gentoo).
chainsaw
parents: 690
diff changeset
81 void *stream __attribute__((unused)))
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
82 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
83 int len = size * nmemb;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
84
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
85 sc_srv_res = realloc(sc_srv_res, sc_srv_res_size + len + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
86 memcpy(sc_srv_res + sc_srv_res_size,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
87 ptr, len);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
88 sc_srv_res_size += len;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
89 return len;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
90 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
91
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
92 static void sc_free_res(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
93 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
94 if(sc_srv_res != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
95 free(sc_srv_res);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
96 sc_srv_res = NULL;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
97 sc_srv_res_size = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
98 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
99
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
100 static int sc_parse_hs_res(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
101 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
102 char *interval;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
103
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
104 if (!sc_srv_res_size) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
105 pdebug("No reply from server", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
106 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
107 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
108 *(sc_srv_res + sc_srv_res_size) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
109
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
110 if (!strncmp(sc_srv_res, "FAILED ", 7)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
111 interval = strstr(sc_srv_res, "INTERVAL");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
112 if(!interval) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
113 pdebug("missing INTERVAL", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
114 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
115 else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
116 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
117 *(interval - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
118 sc_submit_interval = strtol(interval + 8, NULL, 10);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
119 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
120
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
121 /* Throwing a major error, just in case */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
122 /* sc_throw_error(fmt_vastr("%s", sc_srv_res));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
123 sc_hs_errors++; */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
124 pdebug(fmt_vastr("error: %s", sc_srv_res), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
125
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
126 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
127 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
128
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
129 if (!strncmp(sc_srv_res, "UPDATE ", 7)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
130 interval = strstr(sc_srv_res, "INTERVAL");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
131 if(!interval)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
132 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
133 pdebug("missing INTERVAL", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
134 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
135 else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
136 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
137 *(interval - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
138 sc_submit_interval = strtol(interval + 8, NULL, 10);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
139 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
140
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
141 sc_submit_url = strchr(strchr(sc_srv_res, '\n') + 1, '\n') + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
142 *(sc_submit_url - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
143 sc_submit_url = strdup(sc_submit_url);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
144 sc_challenge_hash = strchr(sc_srv_res, '\n') + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
145 *(sc_challenge_hash - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
146 sc_challenge_hash = strdup(sc_challenge_hash);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
147
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
148 /* Throwing major error. Need to alert client to update. */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
149 sc_throw_error(fmt_vastr("Please update Audacious.\n"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
150 "Update available at: http://audacious-media-player.org"));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
151 pdebug(fmt_vastr("update client: %s", sc_srv_res + 7), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
152
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
153 /*
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
154 * Russ isn't clear on whether we can submit with a not-updated
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
155 * client. Neither is RJ. I use what we did before.
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
156 */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
157 sc_giveup = -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
158 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
159 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
160 if (!strncmp(sc_srv_res, "UPTODATE\n", 9)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
161 sc_bad_users = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
162
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
163 interval = strstr(sc_srv_res, "INTERVAL");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
164 if (!interval) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
165 pdebug("missing INTERVAL", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
166 /*
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
167 * This is probably a bad thing, but Russ seems to
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
168 * think its OK to assume that an UPTODATE response
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
169 * may not have an INTERVAL... We return -1 anyway.
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
170 */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
171 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
172 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
173 else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
174 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
175 *(interval - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
176 sc_submit_interval = strtol(interval + 8, NULL, 10);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
177 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
178
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
179 sc_submit_url = strchr(strchr(sc_srv_res, '\n') + 1, '\n') + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
180 *(sc_submit_url - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
181 sc_submit_url = strdup(sc_submit_url);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
182 sc_challenge_hash = strchr(sc_srv_res, '\n') + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
183 *(sc_challenge_hash - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
184 sc_challenge_hash = strdup(sc_challenge_hash);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
185
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
186 return 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
187 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
188 if(!strncmp(sc_srv_res, "BADUSER", 7)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
189 /* Throwing major error. */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
190 sc_throw_error("Incorrect username/password.\n"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
191 "Please fix in configuration.");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
192 pdebug("incorrect username/password", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
193
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
194 interval = strstr(sc_srv_res, "INTERVAL");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
195 if(!interval)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
196 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
197 pdebug("missing INTERVAL", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
198 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
199 else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
200 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
201 *(interval - 1) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
202 sc_submit_interval = strtol(interval + 8, NULL, 10);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
203 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
204
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
205 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
206 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
207
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
208 pdebug(fmt_vastr("unknown server-reply '%s'", sc_srv_res), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
209 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
210 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
211
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
212 static void hexify(char *pass, int len)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
213 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
214 char *bp = sc_response_hash;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
215 char hexchars[] = "0123456789abcdef";
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
216 int i;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
217
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
218 memset(sc_response_hash, 0, sizeof(sc_response_hash));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
219
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
220 for(i = 0; i < len; i++) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
221 *(bp++) = hexchars[(pass[i] >> 4) & 0x0f];
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
222 *(bp++) = hexchars[pass[i] & 0x0f];
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
223 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
224 *bp = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
225
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
226 return;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
227 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
228
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
229 static int sc_handshake(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
230 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
231 int status;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
232 char buf[4096];
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
233 CURL *curl;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
234
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
235 snprintf(buf, sizeof(buf), "%s/?hs=true&p=%s&c=%s&v=%s&u=%s",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
236 SCROBBLER_HS_URL, SCROBBLER_VERSION,
690
85fa136c9edf [svn] - Change client id from XMMS-Scrobbler to Audacious (we now have our own ID)
nenolod
parents: 688
diff changeset
237 SCROBBLER_CLI_ID, SCROBBLER_IMPLEMENTATION, sc_username);
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
238
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
239 curl = curl_easy_init();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
240 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
241 curl_easy_setopt(curl, CURLOPT_URL, buf);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
242 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
243 sc_store_res);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
244 memset(sc_curl_errbuf, 0, sizeof(sc_curl_errbuf));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
245 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, sc_curl_errbuf);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
246 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
247 status = curl_easy_perform(curl);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
248 curl_easy_cleanup(curl);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
249
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
250 sc_hs_timeout = time(NULL) + SCROBBLER_HS_WAIT;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
251
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
252 if (status) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
253 pdebug(sc_curl_errbuf, DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
254 sc_hs_errors++;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
255 sc_free_res();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
256 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
257 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
258
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
259 if (sc_parse_hs_res()) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
260 sc_hs_errors++;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
261 sc_free_res();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
262 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
263 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
264
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
265 if (sc_challenge_hash != NULL) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
266 md5_state_t md5state;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
267 unsigned char md5pword[16];
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
268
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
269 md5_init(&md5state);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
270 /*pdebug(fmt_vastr("Pass Hash: %s", sc_password), DEBUG);*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
271 md5_append(&md5state, (unsigned const char *)sc_password,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
272 strlen(sc_password));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
273 /*pdebug(fmt_vastr("Challenge Hash: %s", sc_challenge_hash), DEBUG);*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
274 md5_append(&md5state, (unsigned const char *)sc_challenge_hash,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
275 strlen(sc_challenge_hash));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
276 md5_finish(&md5state, md5pword);
700
99382cddf771 [svn] Fixes for all warnings (except 3 spurious GCC 4.0 ones, upgrade to 4.1 if you see them) and a performance increase. By external contributor Diego "FlameEyes" Petteno (Gentoo).
chainsaw
parents: 690
diff changeset
277 hexify((char*)md5pword, sizeof(md5pword));
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
278 /*pdebug(fmt_vastr("Response Hash: %s", sc_response_hash), DEBUG);*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
279 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
280
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
281 sc_hs_errors = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
282 sc_hs_status = 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
283
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
284 sc_free_res();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
285
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
286 pdebug(fmt_vastr("submiturl: %s - interval: %d",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
287 sc_submit_url, sc_submit_interval), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
288
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
289 return 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
290 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
291
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
292 static int sc_parse_sb_res(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
293 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
294 char *ch, *ch2;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
295
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
296 if (!sc_srv_res_size) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
297 pdebug("No response from server", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
298 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
299 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
300 *(sc_srv_res + sc_srv_res_size) = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
301
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
302 if (!strncmp(sc_srv_res, "OK", 2)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
303 if ((ch = strstr(sc_srv_res, "INTERVAL"))) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
304 sc_submit_interval = strtol(ch + 8, NULL, 10);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
305 pdebug(fmt_vastr("got new interval: %d",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
306 sc_submit_interval), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
307 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
308
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
309 pdebug(fmt_vastr("submission ok: %s", sc_srv_res), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
310
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
311 return 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
312 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
313
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
314 if (!strncmp(sc_srv_res, "BADAUTH", 7)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
315 if ((ch = strstr(sc_srv_res, "INTERVAL"))) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
316 sc_submit_interval = strtol(ch + 8, NULL, 10);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
317 pdebug(fmt_vastr("got new interval: %d",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
318 sc_submit_interval), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
319 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
320
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
321 pdebug("incorrect username/password", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
322
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
323 sc_giveup = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
324
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
325 /*
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
326 * We obviously aren't authenticated. The server might have
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
327 * lost our handshake status though, so let's try
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
328 * re-handshaking... This might not be proper.
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
329 * (we don't give up)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
330 */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
331 sc_hs_status = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
332
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
333 if(sc_challenge_hash != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
334 free(sc_challenge_hash);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
335 if(sc_submit_url != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
336 free(sc_submit_url);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
337
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
338 sc_challenge_hash = sc_submit_url = NULL;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
339 sc_bad_users++;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
340
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
341 if(sc_bad_users > 2)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
342 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
343 pdebug("3 BADAUTH returns on submission. Halting "
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
344 "submissions until login fixed.", DEBUG)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
345 sc_throw_error("Incorrect username/password.\n"
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
346 "Please fix in configuration.");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
347 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
348
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
349 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
350 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
351
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
352 if (!strncmp(sc_srv_res, "FAILED", 6)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
353 if ((ch = strstr(sc_srv_res, "INTERVAL"))) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
354 sc_submit_interval = strtol(ch + 8, NULL, 10);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
355 pdebug(fmt_vastr("got new interval: %d",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
356 sc_submit_interval), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
357 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
358
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
359 /* This could be important. (Such as FAILED - Get new plugin) */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
360 /*sc_throw_error(fmt_vastr("%s", sc_srv_res));*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
361
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
362 pdebug(sc_srv_res, DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
363
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
364 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
365 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
366
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
367 if (!strncmp(sc_srv_res, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">", 50)) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
368 ch = strstr(sc_srv_res, "<TITLE>") + 7;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
369 ch2 = strstr(sc_srv_res, "</TITLE>");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
370 *ch2 = '\0';
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
371
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
372 pdebug(fmt_vastr("HTTP Error (%d): '%s'",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
373 atoi(ch), ch + 4), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
374 *ch2 = '<';
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
375
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
376 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
377 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
378
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
379 pdebug(fmt_vastr("unknown server-reply %s", sc_srv_res), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
380
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
381 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
382 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
383
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
384 static gchar *sc_itemtag(char c, int n, char *str)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
385 {
1340
eaa1b6342e75 [svn] - itemtag buffer was too short. e.g. 256 bytes could hold only 28 url encoded kanji characters.
yaz
parents: 1239
diff changeset
386 static char buf[SCROBBLER_SB_MAXLEN];
eaa1b6342e75 [svn] - itemtag buffer was too short. e.g. 256 bytes could hold only 28 url encoded kanji characters.
yaz
parents: 1239
diff changeset
387 snprintf(buf, SCROBBLER_SB_MAXLEN, "&%c[%d]=%s", c, n, str);
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
388 return buf;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
389 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
390
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
391 #define cfa(f, l, n, v) \
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
392 curl_formadd(f, l, CURLFORM_COPYNAME, n, \
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
393 CURLFORM_PTRCONTENTS, v, CURLFORM_END)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
394
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
395 static int sc_generateentry(GString *submission)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
396 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
397 int i;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
398 item_t *item;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
399
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
400 i = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
401 #ifdef ALLOW_MULTIPLE
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
402 q_peekall(1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
403 while ((item = q_peekall(0)) && i < 10) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
404 #else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
405 item = q_peek();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
406 #endif
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
407 if (!item)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
408 return i;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
409
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
410 g_string_append(submission,sc_itemtag('a',i,I_ARTIST(item)));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
411 g_string_append(submission,sc_itemtag('t',i,I_TITLE(item)));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
412 g_string_append(submission,sc_itemtag('l',i,I_LEN(item)));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
413 g_string_append(submission,sc_itemtag('i',i,I_TIME(item)));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
414 g_string_append(submission,sc_itemtag('m',i,I_MB(item)));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
415 g_string_append(submission,sc_itemtag('b',i,I_ALBUM(item)));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
416
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
417 pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%s i[%d]=%s m[%d]=%s b[%d]=%s",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
418 i, I_ARTIST(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
419 i, I_TITLE(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
420 i, I_LEN(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
421 i, I_TIME(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
422 i, I_MB(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
423 i, I_ALBUM(item)), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
424 #ifdef ALLOW_MULTIPLE
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
425 i++;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
426 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
427 #endif
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
428
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
429 return i;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
430 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
431
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
432 static int sc_submitentry(gchar *entry)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
433 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
434 CURL *curl;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
435 /* struct HttpPost *post = NULL , *last = NULL; */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
436 int status;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
437 GString *submission;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
438
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
439 curl = curl_easy_init();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
440 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
441 curl_easy_setopt(curl, CURLOPT_URL, sc_submit_url);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
442 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
443 sc_store_res);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
444 curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
445 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
446 /*cfa(&post, &last, "debug", "failed");*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
447
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
448 /*pdebug(fmt_vastr("Username: %s", sc_username), DEBUG);*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
449 submission = g_string_new("u=");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
450 g_string_append(submission,(gchar *)sc_username);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
451
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
452 /*pdebug(fmt_vastr("Response Hash: %s", sc_response_hash), DEBUG);*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
453 g_string_append(submission,"&s=");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
454 g_string_append(submission,(gchar *)sc_response_hash);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
455
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
456 g_string_append(submission, entry);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
457
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
458 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (char *)submission->str);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
459 memset(sc_curl_errbuf, 0, sizeof(sc_curl_errbuf));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
460 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, sc_curl_errbuf);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
461
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
462 /*
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
463 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
464 curl_easy_setopt(curl, CURLOPT_TIMEOUT, SCROBBLER_SB_WAIT);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
465 */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
466
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
467 status = curl_easy_perform(curl);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
468
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
469 curl_easy_cleanup(curl);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
470
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
471 g_string_free(submission,TRUE);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
472
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
473 if (status) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
474 pdebug(sc_curl_errbuf, DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
475 sc_sb_errors++;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
476 sc_free_res();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
477 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
478 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
479
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
480 if (sc_parse_sb_res()) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
481 sc_sb_errors++;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
482 sc_free_res();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
483 pdebug(fmt_vastr("Retrying in %d secs, %d elements in queue",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
484 sc_submit_interval, q_len()), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
485 return -1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
486 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
487 sc_free_res();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
488 return 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
489 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
490
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
491 static void sc_handlequeue(GMutex *mutex)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
492 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
493 GString *submitentry;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
494 int nsubmit;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
495 int wait;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
496
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
497 if(sc_submit_timeout < time(NULL) && sc_bad_users < 3)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
498 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
499 submitentry = g_string_new("");
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
500
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
501 g_mutex_lock(mutex);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
502
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
503 nsubmit = sc_generateentry(submitentry);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
504
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
505 g_mutex_unlock(mutex);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
506
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
507 if (nsubmit > 0)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
508 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
509 pdebug(fmt_vastr("Number submitting: %d", nsubmit), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
510 pdebug(fmt_vastr("Submission: %s", submitentry->str), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
511
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
512 if(!sc_submitentry(submitentry->str))
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
513 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
514 g_mutex_lock(mutex);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
515
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
516 #ifdef ALLOW_MULTIPLE
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
517 q_free();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
518 #else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
519 q_get();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
520 #endif
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
521 /*
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
522 * This should make sure that the queue doesn't
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
523 * get submitted multiple times on a nasty
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
524 * segfault...
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
525 */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
526 dump_queue();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
527
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
528 g_mutex_unlock(mutex);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
529
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
530 sc_sb_errors = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
531 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
532 if(sc_sb_errors)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
533 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
534 if(sc_sb_errors < 5)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
535 /* Retry after 1 min */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
536 wait = 60;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
537 else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
538 wait = /* sc_submit_interval + */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
539 ( ((sc_sb_errors - 5) < 7) ?
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
540 (60 << (sc_sb_errors-5)) :
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
541 7200 );
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
542
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
543 sc_submit_timeout = time(NULL) + wait;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
544
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
545 pdebug(fmt_vastr("Error while submitting. "
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
546 "Retrying after %d seconds.", wait),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
547 DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
548 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
549 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
550
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
551 g_string_free(submitentry, TRUE);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
552 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
553 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
554
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
555 static void read_cache(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
556 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
557 FILE *fd;
1028
872e802cb1c0 [svn] - Use g_get_home_dir() instead of getenv ugliness here.
nenolod
parents: 813
diff changeset
558 char buf[PATH_MAX], *cache = NULL, *ptr1, *ptr2;
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
559 int cachesize, written, i = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
560 item_t *item;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
561
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
562 cachesize = written = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
563
1028
872e802cb1c0 [svn] - Use g_get_home_dir() instead of getenv ugliness here.
nenolod
parents: 813
diff changeset
564 snprintf(buf, sizeof(buf), "%s/.audacious/scrobblerqueue.txt", g_get_home_dir());
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
565
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
566 if (!(fd = fopen(buf, "r")))
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
567 return;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
568 pdebug(fmt_vastr("Opening %s", buf), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
569 while(!feof(fd))
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
570 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
571 cachesize += CACHE_SIZE;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
572 cache = realloc(cache, cachesize + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
573 written += fread(cache + written, 1, CACHE_SIZE, fd);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
574 cache[written] = '\0';
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
575 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
576 fclose(fd);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
577 ptr1 = cache;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
578 while(ptr1 < cache + written - 1)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
579 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
580 char *artist, *title, *len, *time, *album, *mb;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
581
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
582 pdebug("Pushed:", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
583 ptr2 = strchr(ptr1, ' ');
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
584 artist = calloc(1, ptr2 - ptr1 + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
585 strncpy(artist, ptr1, ptr2 - ptr1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
586 ptr1 = ptr2 + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
587 ptr2 = strchr(ptr1, ' ');
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
588 title = calloc(1, ptr2 - ptr1 + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
589 strncpy(title, ptr1, ptr2 - ptr1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
590 ptr1 = ptr2 + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
591 ptr2 = strchr(ptr1, ' ');
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
592 len = calloc(1, ptr2 - ptr1 + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
593 strncpy(len, ptr1, ptr2 - ptr1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
594 ptr1 = ptr2 + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
595 ptr2 = strchr(ptr1, ' ');
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
596 time = calloc(1, ptr2 - ptr1 + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
597 strncpy(time, ptr1, ptr2 - ptr1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
598 ptr1 = ptr2 + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
599 ptr2 = strchr(ptr1, ' ');
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
600 album = calloc(1, ptr2 - ptr1 + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
601 strncpy(album, ptr1, ptr2 - ptr1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
602 ptr1 = ptr2 + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
603 ptr2 = strchr(ptr1, '\n');
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
604 if(ptr2 != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
605 *ptr2 = '\0';
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
606 mb = calloc(1, strlen(ptr1) + 1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
607 strncpy(mb, ptr1, strlen(ptr1));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
608 if(ptr2 != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
609 *ptr2 = '\n';
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
610 /* Why is our save printing out CR/LF? */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
611 ptr1 = ptr2 + 1;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
612
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
613 item = q_put2(artist, title, len, time, album, mb);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
614 pdebug(fmt_vastr("a[%d]=%s t[%d]=%s l[%d]=%s i[%d]=%s m[%d]=%s b[%d]=%s",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
615 i, I_ARTIST(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
616 i, I_TITLE(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
617 i, I_LEN(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
618 i, I_TIME(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
619 i, I_MB(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
620 i, I_ALBUM(item)), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
621 free(artist);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
622 free(title);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
623 free(len);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
624 free(time);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
625 free(album);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
626 free(mb);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
627
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
628 i++;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
629 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
630 pdebug("Done loading cache.", DEBUG);
813
c8cf439179b8 [svn] - Fix a ton and a half of memory leaks, via the wonderful Leonardo Boshell <leonardop -at- gentoo.org>.
nenolod
parents: 700
diff changeset
631 free(cache);
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
632 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
633
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
634 static void dump_queue(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
635 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
636 FILE *fd;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
637 item_t *item;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
638 char *home, buf[PATH_MAX];
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
639
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
640 /*pdebug("Entering dump_queue();", DEBUG);*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
641
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
642 if (!(home = getenv("HOME")))
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
643 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
644 pdebug("No HOME directory found. Cannot dump queue.", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
645 return;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
646 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
647
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
648 snprintf(buf, sizeof(buf), "%s/.audacious/scrobblerqueue.txt", home);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
649
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
650 if (!(fd = fopen(buf, "w")))
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
651 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
652 pdebug(fmt_vastr("Failure opening %s", buf), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
653 return;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
654 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
655
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
656 pdebug(fmt_vastr("Opening %s", buf), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
657
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
658 q_peekall(1);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
659
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
660 while ((item = q_peekall(0))) {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
661 fprintf(fd, "%s %s %s %s %s %s\n",
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
662 I_ARTIST(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
663 I_TITLE(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
664 I_LEN(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
665 I_TIME(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
666 I_ALBUM(item),
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
667 I_MB(item));
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
668 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
669
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
670 fclose(fd);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
671 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
672
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
673 /* This was made public */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
674
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
675 void sc_cleaner(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
676 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
677 if(sc_submit_url != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
678 free(sc_submit_url);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
679 if(sc_username != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
680 free(sc_username);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
681 if(sc_password != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
682 free(sc_password);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
683 if(sc_challenge_hash != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
684 free(sc_challenge_hash);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
685 if(sc_srv_res != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
686 free(sc_srv_res);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
687 if(sc_major_error != NULL)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
688 free(sc_major_error);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
689 dump_queue();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
690 q_free();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
691 pdebug("scrobbler shutting down", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
692 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
693
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
694 static void sc_checkhandshake(void)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
695 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
696 int wait;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
697
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
698 if (sc_hs_status)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
699 return;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
700 if (sc_hs_timeout < time(NULL))
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
701 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
702 sc_handshake();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
703
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
704 if(sc_hs_errors)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
705 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
706 if(sc_hs_errors < 5)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
707 /* Retry after 60 seconds */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
708 wait = 60;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
709 else
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
710 wait = /* sc_submit_interval + */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
711 ( ((sc_hs_errors - 5) < 7) ?
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
712 (60 << (sc_hs_errors-5)) :
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
713 7200 );
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
714 sc_hs_timeout = time(NULL) + wait;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
715 pdebug(fmt_vastr("Error while handshaking. Retrying "
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
716 "after %d seconds.", wait), DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
717 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
718 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
719 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
720
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
721 /**** Public *****/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
722
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
723 /* Called at session startup*/
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
724
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
725 void sc_init(char *uname, char *pwd)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
726 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
727 sc_hs_status = sc_hs_timeout = sc_hs_errors = sc_submit_timeout =
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
728 sc_srv_res_size = sc_giveup = sc_major_error_present =
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
729 sc_bad_users = sc_sb_errors = 0;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
730 sc_submit_interval = 100;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
731
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
732 sc_submit_url = sc_username = sc_password = sc_srv_res =
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
733 sc_challenge_hash = sc_major_error = NULL;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
734 sc_username = strdup(uname);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
735 sc_password = strdup(pwd);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
736 read_cache();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
737 pdebug("scrobbler starting up", DEBUG);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
738 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
739
1236
fc478bfe6fe6 [svn] - disconnect the scrobbler client from metatag logically
nenolod
parents: 1028
diff changeset
740 void sc_addentry(GMutex *mutex, TitleInput *tuple, int len)
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
741 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
742 g_mutex_lock(mutex);
1236
fc478bfe6fe6 [svn] - disconnect the scrobbler client from metatag logically
nenolod
parents: 1028
diff changeset
743 q_put(tuple, len);
688
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
744 /*
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
745 * This will help make sure the queue will be saved on a nasty
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
746 * segfault...
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
747 */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
748 dump_queue();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
749 g_mutex_unlock(mutex);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
750 }
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
751
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
752 /* Call periodically from the plugin */
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
753 int sc_idle(GMutex *mutex)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
754 {
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
755 sc_checkhandshake();
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
756 if (sc_hs_status)
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
757 sc_handlequeue(mutex);
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
758
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
759 return sc_giveup;
cc1969408403 [svn] - add scrobbler support
nenolod
parents:
diff changeset
760 }