2 Copyright (C) 2012 Paul Davis
3 Inspired by code from Ben Loftis @ Harrison Consoles
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 Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #ifdef PLATFORM_WINDOWS
28 #include <sys/utsname.h>
31 #include <curl/curl.h>
33 #include "pbd/gstdio_compat.h"
34 #include <glibmm/miscutils.h>
36 #include "pbd/compose.h"
37 #include "pbd/pthread_utils.h"
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/rc_configuration.h"
45 using namespace ARDOUR;
48 curl_write_data (char *bufptr, size_t size, size_t nitems, void *ptr)
50 /* we know its a string */
52 string* sptr = (string*) ptr;
54 for (size_t i = 0; i < nitems; ++i) {
55 for (size_t n = 0; n < size; ++n) {
56 if (*bufptr == '\n') {
69 std::string announce_path;
71 ping_call (const std::string& v, const std::string& a)
72 : version (v), announce_path (a) {}
75 #ifdef PLATFORM_WINDOWS
77 _query_registry (const char *regkey, const char *regval, std::string &rv) {
79 DWORD size = PATH_MAX;
82 if ( (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ, &key))
83 && (ERROR_SUCCESS == RegQueryValueExA (key, regval, 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
86 rv = Glib::locale_to_utf8 (tmp);
90 if ( (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ | KEY_WOW64_32KEY, &key))
91 && (ERROR_SUCCESS == RegQueryValueExA (key, regval, 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
94 rv = Glib::locale_to_utf8 (tmp);
104 _pingback (void *arg)
106 ping_call* cm = static_cast<ping_call*> (arg);
111 curl_global_init (CURL_GLOBAL_NOTHING);
112 c = curl_easy_init ();
114 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, curl_write_data);
115 curl_easy_setopt (c, CURLOPT_WRITEDATA, &return_str);
116 char errbuf[CURL_ERROR_SIZE];
117 curl_easy_setopt (c, CURLOPT_ERRORBUFFER, errbuf);
122 url = Config->get_osx_pingback_url ();
123 #elif defined PLATFORM_WINDOWS
124 url = Config->get_windows_pingback_url ();
126 url = Config->get_linux_pingback_url ();
129 if (url.compare (0, 4, "http") != 0) {
134 char* v = curl_easy_escape (c, cm->version.c_str(), cm->version.length());
139 #ifndef PLATFORM_WINDOWS
147 //string uts = string_compose ("%1 %2 %3 %4", utb.sysname, utb.release, utb.version, utb.machine);
151 query = curl_easy_escape (c, utb.sysname, strlen (utb.sysname));
152 s = string_compose ("s=%1", query);
157 query = curl_easy_escape (c, utb.release, strlen (utb.release));
158 s = string_compose ("r=%1", query);
163 query = curl_easy_escape (c, utb.machine, strlen (utb.machine));
164 s = string_compose ("m=%1", query);
169 if (_query_registry("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", val)) {
170 char* query = curl_easy_escape (c, val.c_str(), strlen (val.c_str()));
179 if (_query_registry("Hardware\\Description\\System\\CentralProcessor\\0", "Identifier", val)) {
180 // remove "Family X Model YY Stepping Z" tail
181 size_t cut = val.find (" Family ");
182 if (string::npos != cut) {
183 val = val.substr (0, cut);
185 char* query = curl_easy_escape (c, val.c_str(), strlen (val.c_str()));
194 # if ( defined(__x86_64__) || defined(_M_X64) )
195 url += "s=Windows64";
197 url += "s=Windows32";
200 #endif /* PLATFORM_WINDOWS */
202 curl_easy_setopt (c, CURLOPT_URL, url.c_str());
206 if (curl_easy_perform (c) == 0) {
209 curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &http_status);
211 if (http_status != 200) {
212 std::cerr << "Bad HTTP status" << std::endl;
216 if ( return_str.length() > 140 ) { // like a tweet :)
217 std::cerr << "Announcement string is too long (probably behind a proxy)." << std::endl;
219 std::cerr << "Announcement is: " << return_str << std::endl;
221 //write announcements to local file, even if the
222 //announcement is empty
224 FILE* fout = g_fopen (cm->announce_path.c_str(), "wb");
227 fwrite (return_str.c_str(), sizeof(char), return_str.length (), fout);
232 std::cerr << "curl failed: " << errbuf << std::endl;
235 curl_easy_cleanup (c);
242 void pingback (const string& version, const string& announce_path)
244 ping_call* cm = new ping_call (version, announce_path);
247 pthread_create_and_store ("pingback", &thread, _pingback, cm);