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