summaryrefslogtreecommitdiff
path: root/libs/ardour/callback.cc
blob: e0c02e64d4633d1858dda4d10b2d1ddfaf83970a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/utsname.h>
#include <curl/curl.h>

#include <glibmm/fileutils.h>
#include <glibmm/miscutils.h>

#include "pbd/compose.h"
#include "pbd/strsplit.h"
#include "pbd/convert.h"

#include "ardour/callback.h"
#include "ardour/filesystem_paths.h"

using namespace std;

#define PING_URL "http://ardour.org/pingback/versioncheck"

static size_t
curl_write_data (char *bufptr, size_t size, size_t nitems, void *ptr)
{
        /* we know its a string */

        string* sptr = (string*) ptr;

        for (size_t i = 0; i < nitems; ++i) {
                for (size_t n = 0; n < size; ++n) {
                        if (*bufptr == '\n') {
                                break;
                        }

                        (*sptr) += *bufptr++;
                }
        }

        return size * nitems;
}

static string
watermark ()
{
        return "";
}

void
call_the_mothership (const string& version)
{
        /* check if the user says never to do this 
         */

        string hangup = Glib::build_filename (ARDOUR::user_config_directory().to_string(), ".offthehook");

        if (Glib::file_test (hangup, Glib::FILE_TEST_EXISTS)) {
                return;
        }

        CURL* c;
        struct utsname utb;
        std::string versionstr;

        if (uname (&utb)) {
                return;
        }
        
        curl_global_init (CURL_GLOBAL_NOTHING);

        c = curl_easy_init ();

        string data;
        string wm;

        data = string_compose ("version=%1&platform=%2 %3 %4", version, utb.sysname, utb.release, utb.machine);

        wm = watermark();
        if (!wm.empty()) {
                data += string_compose ("&watermark=%1", wm);
        }
        
        curl_easy_setopt(c, CURLOPT_POSTFIELDS, data.c_str());
        curl_easy_setopt(c, CURLOPT_URL, PING_URL);
        curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, curl_write_data); 
        curl_easy_setopt(c, CURLOPT_WRITEDATA, &versionstr); 
        
        std::cerr << "Callback to ardour.org ...\n";

        char errbuf[CURL_ERROR_SIZE];
        curl_easy_setopt(c, CURLOPT_ERRORBUFFER, errbuf); 

        if (curl_easy_perform (c) == 0) {
                cerr << "Current release is " << versionstr << endl;

                vector<string> ours;
                vector<string> current;
                
                split (version, ours, '.');
                split (versionstr, current, '.');
                
                if (ours.size() == 3 && current.size() == 3) {

                        int ours_n[3];
                        int current_n[3];

                        using namespace PBD;

                        ours_n[0] = atoi (ours[0]);
                        ours_n[1] = atoi (ours[1]);
                        ours_n[2] = atoi (ours[2]);

                        current_n[0] = atoi (current[0]);
                        current_n[1] = atoi (current[1]);
                        current_n[2] = atoi (current[2]);

                        if (ours_n[0] < current_n[0] ||
                            ((ours_n[0] == current_n[0]) && (ours_n[1] < current_n[1])) || 
                            ((ours_n[0] == current_n[0]) && (ours_n[1] == current_n[1]) && (ours_n[2] < current_n[2]))) {
                                cerr << "TOO OLD\n";
                        } else {
                                cerr << "CURRENT\n";
                        }
                } else {
                        cerr << "Unusual local version: " << version << endl;
                }
        }
        
        curl_easy_cleanup (c);
}