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
, string(getenv("HOME") ? getenv("HOME") : "") + "/.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_URL
, handshake_url
.c_str());
166 curl_easy_setopt(hs
, CURLOPT_WRITEFUNCTION
, write_data
);
167 curl_easy_setopt(hs
, CURLOPT_WRITEDATA
, &result
);
168 curl_easy_setopt(hs
, CURLOPT_CONNECTTIMEOUT
, curl_timeout
);
169 code
= curl_easy_perform(hs
);
170 curl_easy_cleanup(hs
);
171 pthread_mutex_unlock(&curl_lock
);
173 if (code
!= CURLE_OK
)
175 Log(llInfo
, "Error while sending handshake: %s", curl_easy_strerror(code
));
179 size_t i
= result
.find("\n");
180 handshake
.status
= result
.substr(0, i
);
181 if (handshake
.status
!= "OK")
183 result
= result
.substr(i
+1);
184 i
= result
.find("\n");
185 handshake
.session_id
= result
.substr(0, i
);
186 result
= result
.substr(i
+1);
187 i
= result
.find("\n");
188 handshake
.nowplaying_url
= result
.substr(0, i
);
189 result
= result
.substr(i
+1);
190 ignore_newlines(result
);
191 handshake
.submission_url
= result
;
195 void *mpdconnection_handler(void *data
)
197 MPD::Connection
*Mpd
= static_cast<MPD::Connection
*>(data
);
198 while (!scrobby_exit
)
201 while (!Mpd
->Connected())
204 Log(llVerbose
, "Connecting to MPD...");
208 Log(llInfo
, "Connected to MPD at %s !", config
.mpd_host
.c_str());
214 Log(llInfo
, "Cannot connect to MPD, retrieving in %d seconds...", 10*x
);
223 void *handshake_handler(void *)
226 while (!scrobby_exit
)
228 if (handshake
.status
!= "OK")
230 pthread_mutex_lock(&handshake_lock
);
232 if (send_handshake() && !handshake
.status
.empty())
234 Log(llInfo
, "Handshake returned %s", handshake
.status
.c_str());
236 if (handshake
.status
== "OK")
238 Log(llInfo
, "Connected to Audioscrobbler!");
239 if (!SongsQueue
.empty())
241 Log(llInfo
, "Queue's not empty, submitting songs...");
243 string result
, postdata
;
246 pthread_mutex_lock(&curl_lock
);
247 CURL
*submission
= curl_easy_init();
250 postdata
+= handshake
.session_id
;
252 for (std::vector
<string
>::const_iterator it
= SongsQueue
.begin(); it
!= SongsQueue
.end(); it
++)
255 Log(llVerbose
, "URL: %s", handshake
.submission_url
.c_str());
256 Log(llVerbose
, "Post data: %s", postdata
.c_str());
258 curl_easy_setopt(submission
, CURLOPT_URL
, handshake
.submission_url
.c_str());
259 curl_easy_setopt(submission
, CURLOPT_POST
, 1);
260 curl_easy_setopt(submission
, CURLOPT_POSTFIELDS
, postdata
.c_str());
261 curl_easy_setopt(submission
, CURLOPT_WRITEFUNCTION
, write_data
);
262 curl_easy_setopt(submission
, CURLOPT_WRITEDATA
, &result
);
263 curl_easy_setopt(submission
, CURLOPT_CONNECTTIMEOUT
, curl_timeout
);
264 code
= curl_easy_perform(submission
);
265 curl_easy_cleanup(submission
);
266 pthread_mutex_unlock(&curl_lock
);
268 ignore_newlines(result
);
272 Log(llInfo
, "Number of submitted songs: %d", SongsQueue
.size());
281 Log(llInfo
, "Error while submitting songs: %s", curl_easy_strerror(code
));
285 Log(llInfo
, "Audioscrobbler returned status %s", result
.c_str());
289 notify_about_now_playing
= !s
.isStream();
294 Log(llInfo
, "Connection to Audioscrobbler refused, retrieving in %d seconds...", 10*x
);
296 pthread_mutex_unlock(&handshake_lock
);
298 sleep(!x
? 1 : 10*x
);