Merge branch 'master' into cairocanvas
[ardour.git] / gtk2_ardour / pingback.cc
1 /*
2     Copyright (C) 2012 Paul Davis 
3     Inspired by code from Ben Loftis @ Harrison Consoles
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 Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24 #include <cstring>
25
26 #ifndef PLATFORM_WINDOWS
27 #include <sys/utsname.h>
28 #endif
29
30 #include <curl/curl.h>
31
32 #include <glibmm/miscutils.h>
33
34 #include "pbd/compose.h"
35 #include "pbd/pthread_utils.h"
36
37 #include "ardour/filesystem_paths.h"
38 #include "ardour/rc_configuration.h"
39
40 #include "pingback.h"
41
42 using std::string;
43 using namespace ARDOUR;
44
45 static size_t
46 curl_write_data (char *bufptr, size_t size, size_t nitems, void *ptr)
47 {
48         /* we know its a string */
49
50         string* sptr = (string*) ptr;
51
52         for (size_t i = 0; i < nitems; ++i) {
53                 for (size_t n = 0; n < size; ++n) {
54                         if (*bufptr == '\n') {
55                                 break;
56                         }
57
58                         (*sptr) += *bufptr++;
59                 }
60         }
61
62         return size * nitems;
63 }
64
65 struct ping_call {
66     std::string version;
67     std::string announce_path;
68
69     ping_call (const std::string& v, const std::string& a)
70             : version (v), announce_path (a) {}
71 };
72
73 static void*
74 _pingback (void *arg)
75 {
76 #ifndef PLATFORM_WINDOWS
77         ping_call* cm = static_cast<ping_call*> (arg);
78         CURL* c;
79         struct utsname utb;
80         string return_str;
81
82         if (uname (&utb)) {
83                 return 0;
84         }
85
86         //initialize curl
87
88         curl_global_init (CURL_GLOBAL_NOTHING);
89         c = curl_easy_init ();
90         
91         curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, curl_write_data); 
92         curl_easy_setopt (c, CURLOPT_WRITEDATA, &return_str); 
93         char errbuf[CURL_ERROR_SIZE];
94         curl_easy_setopt (c, CURLOPT_ERRORBUFFER, errbuf); 
95
96         string url;
97
98 #ifdef __APPLE__
99         url = Config->get_osx_pingback_url ();
100 #else
101         url = Config->get_linux_pingback_url ();
102 #endif
103
104         char* v = curl_easy_escape (c, cm->version.c_str(), cm->version.length());
105         url += v;
106         url += '?';
107         free (v);
108
109         string uts = string_compose ("%1 %2 %3 %4", utb.sysname, utb.release, utb.version, utb.machine);
110         string s;
111         char* query;
112
113         query = curl_easy_escape (c, utb.sysname, strlen (utb.sysname));
114         s = string_compose ("s=%1", query);
115         url += s;
116         url += '&';
117         free (query);
118
119         query = curl_easy_escape (c, utb.release, strlen (utb.release));
120         s = string_compose ("r=%1", query);
121         url += s;
122         url += '&';
123         free (query);
124
125         query = curl_easy_escape (c, utb.machine, strlen (utb.machine));
126         s = string_compose ("m=%1", query);
127         url += s;
128         free (query);
129
130         curl_easy_setopt (c, CURLOPT_URL, url.c_str());
131
132         return_str = "";
133
134         if (curl_easy_perform (c) == 0) {
135                 long http_status; 
136
137                 curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &http_status);
138
139                 if (http_status != 200) {
140                         std::cerr << "Bad HTTP status" << std::endl;
141                         return 0;
142                 }
143
144                 if ( return_str.length() > 140 ) { // like a tweet :)
145                         std::cerr << "Announcement string is too long (probably behind a proxy)." << std::endl;
146                 } else {
147                         std::cerr << "Announcement is: " << return_str << std::endl;
148                         
149                         //write announcements to local file, even if the
150                         //announcement is empty
151                                 
152                         std::ofstream annc_file (cm->announce_path.c_str());
153                         
154                         if (annc_file) {
155                                 annc_file << return_str;
156                         }
157                 }
158         } else {
159                 std::cerr << "curl failed: " << errbuf << std::endl;
160         }
161
162         curl_easy_cleanup (c);
163         delete cm;
164
165 #endif /* PLATFORM_WINDOWS */
166
167         return 0;
168 }
169
170 namespace ARDOUR {
171
172 void pingback (const string& version, const string& announce_path) 
173 {
174         ping_call* cm = new ping_call (version, announce_path);
175         pthread_t thread;
176
177         pthread_create_and_store ("pingback", &thread, _pingback, cm);
178 }
179
180 }