add various options for curl connections / fix write_data() prototype
[scrobby.git] / src / callback.cpp
blob482a2e0b1bf113f211815d482c1e3b2d021fc5be
1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <curl/curl.h>
22 #include <cstring>
24 #include "callback.h"
25 #include "misc.h"
26 #include "scrobby.h"
27 #include "song.h"
29 using std::string;
31 extern Handshake myHandshake;
32 extern MPD::Song s;
34 void ScrobbyErrorCallback(MPD::Connection *, int, string errormessage, void *)
36 IgnoreNewlines(errormessage);
37 Log(llVerbose, "MPD: %s", errormessage.c_str());
40 void ScrobbyStatusChanged(MPD::Connection *Mpd, MPD::StatusChanges changed, void *)
42 static MPD::State old_state = MPD::psUnknown;
43 static MPD::State current_state = MPD::psUnknown;
44 static bool NowPlayingNotify = 0;
46 if (changed.State)
48 old_state = current_state;
49 current_state = Mpd->GetState();
50 if (old_state == MPD::psStop && current_state == MPD::psPlay)
51 changed.SongID = 1;
53 if (changed.ElapsedTime)
55 static int crossfade;
56 if (Mpd->GetElapsedTime() == ((crossfade = Mpd->GetCrossfade()) ? crossfade : 0 ))
57 changed.SongID = 1;
58 s.Playback++;
60 if (changed.SongID || (old_state == MPD::psPlay && current_state == MPD::psStop))
62 s.Submit();
64 // in this case allow entering only once
65 if (old_state == MPD::psPlay && current_state == MPD::psStop)
66 old_state = MPD::psUnknown;
68 if (Mpd->GetElapsedTime() < Mpd->GetCrossfade()+10)
69 time(&s.StartTime);
71 if (current_state == MPD::psPlay || current_state == MPD::psPause)
73 s.SetData(Mpd->CurrentSong());
74 NowPlayingNotify = s.Data && !s.isStream();
77 if (!NowPlayingNotify || !s.Data || myHandshake.Status != "OK" || myHandshake.NowPlayingURL.empty())
78 return;
80 NowPlayingNotify = 0;
82 if (!s.Data->artist || !s.Data->title)
84 Log(llInfo, "Playing song with missing tags detected.");
86 else if (s.Data->time <= 0)
88 Log(llInfo, "Playing song with unknown length detected.");
90 else if (s.Data->artist && s.Data->title)
92 Log(llVerbose, "Playing song detected: %s - %s", s.Data->artist, s.Data->title);
93 Log(llInfo, "Sending now playing notification...");
95 std::ostringstream postdata;
96 string result, postdata_str;
97 CURLcode code;
99 char *c_artist = curl_easy_escape(0, s.Data->artist, 0);
100 char *c_title = curl_easy_escape(0, s.Data->title, 0);
101 char *c_album = s.Data->album ? curl_easy_escape(0, s.Data->album, 0) : NULL;
102 char *c_track = s.Data->track ? curl_easy_escape(0, s.Data->track, 0) : NULL;
103 char *c_mb_trackid = s.Data->musicbrainz_trackid ? curl_easy_escape(0, s.Data->musicbrainz_trackid, 0) : NULL;
105 postdata
106 << "s=" << myHandshake.SessionID
107 << "&a=" << c_artist
108 << "&t=" << c_title
109 << "&b=";
110 if (c_album)
111 postdata << c_album;
112 postdata << "&l=" << s.Data->time
113 << "&n=";
114 if (c_track)
115 postdata << c_track;
116 postdata << "&m=";
117 if (c_mb_trackid)
118 postdata << c_mb_trackid;
120 curl_free(c_artist);
121 curl_free(c_title);
122 curl_free(c_album);
123 curl_free(c_track);
124 curl_free(c_mb_trackid);
126 postdata_str = postdata.str();
128 Log(llVerbose, "URL: %s", myHandshake.NowPlayingURL.c_str());
129 Log(llVerbose, "Post data: %s", postdata_str.c_str());
131 CURL *np_notification = curl_easy_init();
132 curl_easy_setopt(np_notification, CURLOPT_URL, myHandshake.NowPlayingURL.c_str());
133 curl_easy_setopt(np_notification, CURLOPT_POST, 1);
134 curl_easy_setopt(np_notification, CURLOPT_POSTFIELDS, postdata_str.c_str());
135 curl_easy_setopt(np_notification, CURLOPT_WRITEFUNCTION, write_data);
136 curl_easy_setopt(np_notification, CURLOPT_WRITEDATA, &result);
137 curl_easy_setopt(np_notification, CURLOPT_CONNECTTIMEOUT, curl_connecttimeout);
138 curl_easy_setopt(np_notification, CURLOPT_TIMEOUT, curl_timeout);
139 curl_easy_setopt(np_notification, CURLOPT_DNS_CACHE_TIMEOUT, 0);
140 curl_easy_setopt(np_notification, CURLOPT_NOPROGRESS, 1);
141 curl_easy_setopt(np_notification, CURLOPT_NOSIGNAL, 1);
142 code = curl_easy_perform(np_notification);
143 curl_easy_cleanup(np_notification);
145 IgnoreNewlines(result);
147 if (result == "OK")
149 Log(llInfo, "Notification about currently playing song sent.");
151 else
153 if (result.empty())
155 Log(llInfo, "Error while sending notification: %s", curl_easy_strerror(code));
157 else
159 Log(llInfo, "Audioscrobbler returned status %s", result.c_str());
160 myHandshake.Clear(); // handshake probably failed if we are here, so reset it
161 Log(llVerbose, "Handshake reset");
162 NowPlayingNotify = 1;