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