-
Notifications
You must be signed in to change notification settings - Fork 1
/
pushbulletcontroller.cpp
228 lines (204 loc) · 9.67 KB
/
pushbulletcontroller.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "pushbulletcontroller.h"
#include <iostream> // TEMP!
#include <algorithm>
#define BOOL_STR(b) ((b)?"true":"false")
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static inline void replaceAll(std::string &str, const std::string& from, const std::string& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
}
PushBulletController::PushBulletController( string in_account, string in_password )
{
account = in_account;
password = in_password;
curl_global_init( CURL_GLOBAL_ALL );
curlHandler = curl_easy_init();
}
PushBulletController::PushBulletController()
{
curl_global_init( CURL_GLOBAL_ALL );
curlHandler = curl_easy_init();
}
void PushBulletController::setAccount( string input )
{
this->account = input;
}
void PushBulletController::setPassword( string input )
{
this->password = input;
}
void PushBulletController::setAccessToken( string in_accessToken )
{
this->access_token = in_accessToken;
}
vector<Device> *PushBulletController::listDevices()
{
this->setupCommonHeader( lastReturnedBuffer );
this->devices_list = new vector<Device>();
//string devices_url( "https://api.pushbullet.com/v2/devices" );
curl_easy_setopt( curlHandler, CURLOPT_URL, device_url.c_str() );
CURLcode res = curl_easy_perform( curlHandler );
if ( res == CURLE_OK &&
jsonReader->parse( lastReturnedBuffer.c_str(), lastReturnedBuffer.c_str() + lastReturnedBuffer.length(), &jsonRoot, nullptr ) ) {
// jsonRoot should be now ready parsed tree of json.
Json::Value devices = jsonRoot["devices"];
for ( unsigned int i = 0; i < devices.size(); i++ ) {
Json::Value cur = devices[i];
// Device::{iden|nickname|type}
Device *newDevice = new Device();
if ( !cur["nickname"].asString().empty() ) {
newDevice->iden = cur["iden"].asString();
newDevice->nickname = cur["nickname"].asString();
newDevice->type = cur["type"].asString();
this->devices_list->push_back( *newDevice );
}
}
// Add "ALL" device
Device *allDevice = new Device();
allDevice->iden = string( "ALL" );
allDevice->nickname = string( "ALL" );
allDevice->type = string( "" );
this->devices_list->push_back( *allDevice );
}
lastReturnedBuffer = "";
return this->devices_list;
}
// Presumption: Device is set in this->deviceSelected.
CURLcode PushBulletController::push( string type, string title, string body, string device_iden )
{
pthread_mutex_lock(&mutex);
std::cout << "PushBulletController::push" << std::endl;
lastReplyBuffer = "";
this->setupCommonHeader( lastReplyBuffer );
// lastReturnedBuffer = "";
curl_easy_setopt( curlHandler, CURLOPT_URL, push_url.c_str() );
// curl_easy_setopt( curlHandler, CURLOPT_WRITEDATA, &lastReplyBuffer );
// params should look like:
//char *jsonObj = "{ \"body\" : \"test body\", \"title\" : \"test title\", \"type\" : \"note\" }";
// Preprocessing: Escape '\n' for body:
replaceAll( body, "\n", "\\n" );
string jsonString;
if ( device_iden.empty() || device_iden == "ALL" ) {
jsonString = "{ \"body\" : \"" + body +
"\", \"title\" : \"" + title +
"\", \"type\" : \"" + type +
"\" }";
}
else {
jsonString = "{ \"body\" : \"" + body +
"\", \"title\" : \"" + title +
"\", \"type\" : \"" + type +
"\", \"device_iden\" : \"" + device_iden +
"\" }";
}
curl_easy_setopt( curlHandler, CURLOPT_POSTFIELDS, jsonString.c_str() );
lastResult = curl_easy_perform( curlHandler );
if ( lastResult == CURLE_OK ) {
// std::cout << lastReturnedBuffer << std::endl;
std::cout << "PUSH REPLY:" << lastReplyBuffer << std::endl;
}
pthread_mutex_unlock(&mutex);
return lastResult;
// return CURLE_OK;
}
CURLcode PushBulletController::getPushes( bool avoid_deleted, int since )
{
// GET from: https://api.pushbullet.com/v2/pushes == push_url
pthread_mutex_lock(&mutex);
lastReturnedBuffer = "";
this->setupCommonHeader( lastReturnedBuffer );
// string since_str = string( "\"" + since + "\"" );
string url = string( push_url + "?" + params_string( {
{ "active", BOOL_STR( avoid_deleted ) },
{ "modified_after", std::to_string( since ) },
} ) );
//const string url_copy = string( url );
//char *output = curl_escape( url_copy.c_str(), url_copy.length );
// URL is correct, if test by CURL command !!
std::cout << "NEWURL:" << url << std::endl;
curl_easy_setopt( curlHandler, CURLOPT_URL, url.c_str() );
curl_easy_setopt( curlHandler, CURLOPT_HTTPGET, 1 );
lastResult = curl_easy_perform( curlHandler );
if ( lastResult == CURLE_OK ) {
std::cout << "GetPush:" << lastReturnedBuffer << std::endl;
// jsonReader.parse( std::string( lastReturnedBuffer ), jsonRoot, false );
jsonReader->parse( lastReturnedBuffer.c_str(), lastReturnedBuffer.c_str() + lastReturnedBuffer.length(), &jsonRoot, nullptr );
// Now jsonRoot is ready to use for others.
}
pthread_mutex_unlock(&mutex);
return lastResult;
}
// stub: typeIcon
string PushBulletController::registerDevice( string nickname, string manufacturer, string model, string typeIcon ) {
pthread_mutex_lock(&mutex);
lastReturnedBuffer = "";
this->setupCommonHeader( lastReturnedBuffer );
curl_easy_setopt( curlHandler, CURLOPT_URL, device_url.c_str() );
curl_easy_setopt( curlHandler, CURLOPT_WRITEDATA, &lastReplyBuffer );
// params should look like:
//char *jsonObj = "{ \"body\" : \"test body\", \"title\" : \"test title\", \"type\" : \"note\" }";
// Preprocessing: Escape '\n' for body:
//replaceAll( body, "\n", "\\n" );
string jsonString;
jsonString = "{ \"nickname\" : \"" + nickname +
"\", \"model\" : \"" + model +
"\", \"manufacturer\" : \"" + manufacturer +
"\", \"push_token\" : \"" + "" +
"\", \"app_version\" : \"" + "8623" +
"\", \"icon\" : \"" + "laptop" +
"\", \"has_sms\" : " + "false" +
" }";
std::cout << "jsonString: " << jsonString << std::endl;
curl_easy_setopt( curlHandler, CURLOPT_POSTFIELDS, jsonString.c_str() );
lastResult = curl_easy_perform( curlHandler );
if ( lastResult == CURLE_OK ) { // Register SUCCESS
// std::cout << lastReturnedBuffer << std::endl;
std::cout << "Create-Device REPLY:" << lastReplyBuffer << std::endl;
// Parse the JSON response for "iden" value:
if ( jsonReader->parse( lastReplyBuffer.c_str(), lastReplyBuffer.c_str() + lastReplyBuffer.length(), &jsonRoot, nullptr ) ) {
// Json::Value devices = jsonRoot["devices"];
// for ( unsigned int i = 0; i < devices.size(); i++ ) {
// Json::Value cur = devices[i];
// // Device::{iden|nickname|type}
// Device *newDevice = new Device();
// if ( !cur["nickname"].asString().empty() ) {
// newDevice->iden = cur["iden"].asString();
// newDevice->nickname = cur["nickname"].asString();
// newDevice->type = cur["type"].asString();
// this->devices_list->push_back( *newDevice );
// }
// }
// // Add "ALL" device
// Device *allDevice = new Device();
// allDevice->iden = string( "ALL" );
// allDevice->nickname = string( "ALL" );
// allDevice->type = string( "" );
// this->devices_list->push_back( *allDevice );
}
}
pthread_mutex_unlock(&mutex);
return "lastResult";
}
// ======================== PRIVATE UTILITIES ========================
void PushBulletController::setupCommonHeader( string &filling_buffer )
{
struct curl_slist *headers = nullptr;
string header_str( "Accept-Token: " );
header_str += this->access_token;
headers = curl_slist_append( headers, header_str.c_str() );
if ( headers == nullptr ) {
std::cout << "HEADER FXXKED" << std::endl;
}
header_str = "Content-Type: application/json";
headers = curl_slist_append( headers, header_str.c_str() );
curl_easy_setopt( curlHandler, CURLOPT_HTTPHEADER, headers );
curl_easy_setopt( curlHandler, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC );
curl_easy_setopt( curlHandler, CURLOPT_USERNAME, this->access_token.c_str() );
curl_easy_setopt( curlHandler, CURLOPT_PASSWORD, "any_password" );
curl_easy_setopt( curlHandler, CURLOPT_WRITEFUNCTION, WriteCallback );
curl_easy_setopt( curlHandler, CURLOPT_WRITEDATA, &filling_buffer );
}