1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include <curl/curl.h>
29 #include "configuration.h"
42 pthread_t mpdconnection_th
;
43 pthread_t handshake_th
;
45 pthread_mutex_t curl_lock
= PTHREAD_MUTEX_INITIALIZER
;
46 pthread_mutex_t handshake_lock
= PTHREAD_MUTEX_INITIALIZER
;
48 std::vector
<string
> SongsQueue
;
50 bool scrobby_exit
= 0;
51 bool notify_about_now_playing
= 0;
54 void signal_handler(int);
55 bool send_handshake();
57 void *mpdconnection_handler(void *data
);
58 void *handshake_handler(void *);
61 int main(int argc
, char **argv
)
63 DefaultConfiguration(config
);
67 ParseArgv(config
, argc
, argv
);
69 if (!config
.file_config
.empty())
71 if (!ReadConfiguration(config
, config
.file_config
))
73 std::cerr
<< "cannot read configuration file: " << config
.file_config
<< std::endl
;
77 else if (!ReadConfiguration(config
, config
.user_home_folder
+ "/.scrobbyconf"))
79 if (!ReadConfiguration(config
, "/etc/scrobby.conf"))
81 std::cerr
<< "default configuration files not found!\n";
85 if (config
.log_level
== llUndefined
)
87 config
.log_level
= llInfo
;
89 if (config
.lastfm_user
.empty() || (config
.lastfm_md5_password
.empty() && config
.lastfm_password
.empty()))
91 std::cerr
<< "last.fm user/password is not set.\n";
95 if (!CheckFiles(config
))
102 std::cerr
<< "couldn't daemonize!\n";
105 GetCachedSongs(SongsQueue
);
107 MPD::Connection
*Mpd
= new MPD::Connection
;
109 if (config
.mpd_host
!= "localhost")
110 Mpd
->SetHostname(config
.mpd_host
);
111 if (config
.mpd_port
!= 6600)
112 Mpd
->SetPort(config
.mpd_port
);
114 Mpd
->SetTimeout(config
.mpd_timeout
);
115 Mpd
->SetStatusUpdater(ScrobbyStatusChanged
, NULL
);
116 Mpd
->SetErrorHandler(ScrobbyErrorCallback
, NULL
);
118 signal(SIGHUP
, signal_handler
);
119 signal(SIGINT
, signal_handler
);
120 signal(SIGTERM
, signal_handler
);
121 signal(SIGPIPE
, SIG_IGN
);
123 pthread_create(&mpdconnection_th
, NULL
, mpdconnection_handler
, Mpd
);
124 pthread_create(&handshake_th
, NULL
, handshake_handler
, NULL
);
125 pthread_detach(mpdconnection_th
);
126 pthread_detach(handshake_th
);
128 while (!scrobby_exit
&& !usleep(500000))
130 if (Mpd
->Connected())
135 Log(llInfo
, "Shutting down...");
136 if (remove(config
.file_pid
.c_str()) != 0)
137 Log(llInfo
, "Couldn't remove pid file!");
144 void signal_handler(int)
149 bool send_handshake()
152 string handshake_url
;
154 string timestamp
= IntoStr(time(NULL
));
156 handshake_url
= "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=mpc&v="VERSION
"&u=";
157 handshake_url
+= config
.lastfm_user
;
158 handshake_url
+= "&t=";
159 handshake_url
+= timestamp
;
160 handshake_url
+= "&a=";
161 handshake_url
+= md5sum((config
.lastfm_md5_password
.empty() ? md5sum(config
.lastfm_password
) : config
.lastfm_md5_password
) + timestamp
);
163 pthread_mutex_lock(&curl_lock
);
164 CURL
*hs
= curl_easy_init();
165 curl_easy_setopt(hs
, CURLOPT_DNS_CACHE_TIMEOUT
, 0);
166 curl_easy_setopt(hs
, CURLOPT_NOSIGNAL
, 1);
167 curl_easy_setopt(hs
, CURLOPT_URL
, handshake_url
.c_str());
168 curl_easy_setopt(hs
, CURLOPT_WRITEFUNCTION
, write_data
);
169 curl_easy_setopt(hs
, CURLOPT_WRITEDATA
, &result
);
170 curl_easy_setopt(hs
, CURLOPT_CONNECTTIMEOUT
, curl_timeout
);
171 code
= curl_easy_perform(hs
);
172 curl_easy_cleanup(hs
);
173 pthread_mutex_unlock(&curl_lock
);
175 if (code
!= CURLE_OK
)
177 Log(llInfo
, "Error while sending handshake: %s", curl_easy_strerror(code
));
181 size_t i
= result
.find("\n");
182 handshake
.status
= result
.substr(0, i
);
183 if (handshake
.status
!= "OK")
185 result
= result
.substr(i
+1);
186 i
= result
.find("\n");
187 handshake
.session_id
= result
.substr(0, i
);
188 result
= result
.substr(i
+1);
189 i
= result
.find("\n");
190 handshake
.nowplaying_url
= result
.substr(0, i
);
191 result
= result
.substr(i
+1);
192 ignore_newlines(result
);
193 handshake
.submission_url
= result
;
197 void *mpdconnection_handler(void *data
)
199 MPD::Connection
*Mpd
= static_cast<MPD::Connection
*>(data
);
201 while (!scrobby_exit
)
203 while (!Mpd
->Connected())
206 Log(llVerbose
, "Connecting to MPD...");
210 Log(llInfo
, "Connected to MPD at %s !", config
.mpd_host
.c_str());
216 Log(llInfo
, "Cannot connect to MPD, retrieving in %d seconds...", 10*x
);
225 void *handshake_handler(void *)
228 while (!scrobby_exit
)
230 if (handshake
.status
!= "OK")
232 pthread_mutex_lock(&handshake_lock
);
234 if (send_handshake() && !handshake
.status
.empty())
236 Log(llInfo
, "Handshake returned %s", handshake
.status
.c_str());
238 if (handshake
.status
== "OK")
240 Log(llInfo
, "Connected to Audioscrobbler!");
241 if (!SongsQueue
.empty())
243 Log(llInfo
, "Queue's not empty, submitting songs...");
245 string result
, postdata
;
248 pthread_mutex_lock(&curl_lock
);
249 CURL
*submission
= curl_easy_init();
252 postdata
+= handshake
.session_id
;
254 for (std::vector
<string
>::const_iterator it
= SongsQueue
.begin(); it
!= SongsQueue
.end(); it
++)
257 Log(llVerbose
, "URL: %s", handshake
.submission_url
.c_str());
258 Log(llVerbose
, "Post data: %s", postdata
.c_str());
260 curl_easy_setopt(submission
, CURLOPT_DNS_CACHE_TIMEOUT
, 0);
261 curl_easy_setopt(submission
, CURLOPT_NOSIGNAL
, 1);
262 curl_easy_setopt(submission
, CURLOPT_URL
, handshake
.submission_url
.c_str());
263 curl_easy_setopt(submission
, CURLOPT_POST
, 1);
264 curl_easy_setopt(submission
, CURLOPT_POSTFIELDS
, postdata
.c_str());
265 curl_easy_setopt(submission
, CURLOPT_WRITEFUNCTION
, write_data
);
266 curl_easy_setopt(submission
, CURLOPT_WRITEDATA
, &result
);
267 curl_easy_setopt(submission
, CURLOPT_CONNECTTIMEOUT
, curl_timeout
);
268 code
= curl_easy_perform(submission
);
269 curl_easy_cleanup(submission
);
270 pthread_mutex_unlock(&curl_lock
);
272 ignore_newlines(result
);
276 Log(llInfo
, "Number of submitted songs: %d", SongsQueue
.size());
285 Log(llInfo
, "Error while submitting songs: %s", curl_easy_strerror(code
));
289 Log(llInfo
, "Audioscrobbler returned status %s", result
.c_str());
293 notify_about_now_playing
= !s
.isStream();
298 Log(llInfo
, "Connection to Audioscrobbler refused, retrieving in %d seconds...", 10*x
);
300 pthread_mutex_unlock(&handshake_lock
);
302 sleep(!x
? 1 : 10*x
);