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