1 /***************************************************************************
2 * *
3 * Copyright 2011 Eugene Petrov <dhamp@ya.ru> *
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 ***************************************************************************/
11
12 #include "stdafx.h"
13 #include "jsonrpcmethods.h"
14 #include "ServerManager.h"
15 #include "utility.h"
16 #include "ServerThread.h"
17 #include "VersionGlobal.h"
18 #include "dcpp/format.h"
19 #include "json/jsonrpc-cpp/jsonrpc_common.h"
20
21 using namespace std;
22
23 // ./cli-jsonrpc-curl.pl '{"jsonrpc": "2.0", "id": "1", "method": "show.version"}'
24 // ./cli-jsonrpc-curl.pl '{"jsonrpc": "2.0", "id": "sv0t7t2r", "method": "queue.getsources", "params":{"target": "/home/egik/Видео/Shakugan no Shana III - 16 - To Battle, Once More [Zero-Raws].mp4"}}'
25 // ./cli-jsonrpc-curl.pl '{"jsonrpc": "2.0", "id": "sv0t7t2r", "method": "hub.add", "params":{"huburl": "adc://localhost:1511"}}'
26 // ./cli-jsonrpc-curl.pl '{"jsonrpc": "2.0", "id": "1", "method": "hub.pm", "params":{"huburl": "adc://localhost:1511", "nick" : "test", "message" : "test"}}'
27
FailedValidateRequest(Json::Value & error)28 void JsonRpcMethods::FailedValidateRequest(Json::Value& error) {
29 Json::Value err;
30 error["id"] = Json::Value::null;
31 error["jsonrpc"] = "2.0";
32
33 err["code"] = Json::Rpc::INVALID_PARAMS;
34 err["message"] = "Invalid params in JSON-RPC request.";
35 error["error"] = err;
36 }
37
StopDaemon(const Json::Value & root,Json::Value & response)38 bool JsonRpcMethods::StopDaemon(const Json::Value& root, Json::Value& response)
39 {
40 if (isDebug) std::cout << "StopDaemon (root): " << root << std::endl;
41 response["jsonrpc"] = "2.0";
42 response["id"] = root["id"];
43 response["result"] = 0;
44 bServerTerminated = true;
45 if (isDebug) std::cout << "StopDaemon (response): " << response << std::endl;
46 return true;
47 }
48
MagnetAdd(const Json::Value & root,Json::Value & response)49 bool JsonRpcMethods::MagnetAdd(const Json::Value& root, Json::Value& response)
50 {
51 if (isDebug) std::cout << "MagnetAdd (root): " << root << std::endl;
52 response["jsonrpc"] = "2.0";
53 response["id"] = root["id"];
54 std::string name,tth;int64_t size;
55
56 if (root["params"].isMember("magnet") && !root["params"]["magnet"].isString()
57 && !root["params"]["magnet"].isConvertibleTo(Json::stringValue)) {
58 FailedValidateRequest(response);
59 return false;
60 }
61
62 bool ok = splitMagnet(root["params"]["magnet"].asString(), name, size, tth);
63 if (isDebug) std::cout << "splitMagnet: \n tth: " << tth << "\n size: " << size << "\n name: " << name << std::endl;
64 if (ok && ServerThread::getInstance()->addInQueue(root["params"]["directory"].asString(), name, size, tth))
65 response["result"] = 0;
66 else
67 response["result"] = 1;
68 if (isDebug) std::cout << "MagnetAdd (response): " << response << std::endl;
69 return true;
70 }
71
HubAdd(const Json::Value & root,Json::Value & response)72 bool JsonRpcMethods::HubAdd(const Json::Value& root, Json::Value& response)
73 {
74 if (isDebug) std::cout << "HubAdd (root): " << root << std::endl;
75 response["jsonrpc"] = "2.0";
76 response["id"] = root["id"];
77
78 if ((root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
79 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue))
80 || (root["params"].isMember("enc") && !root["params"]["enc"].isString()
81 && !root["params"]["enc"].isConvertibleTo(Json::stringValue))) {
82 FailedValidateRequest(response);
83 return false;
84 }
85
86 ServerThread::getInstance()->connectClient(root["params"]["huburl"].asString(), root["params"]["enc"].asString());
87 response["result"] = "Connecting to " + root["params"]["huburl"].asString();
88 if (isDebug) std::cout << "HubAdd (response): " << response << std::endl;
89 return true;
90 }
HubDel(const Json::Value & root,Json::Value & response)91 bool JsonRpcMethods::HubDel(const Json::Value& root, Json::Value& response)
92 {
93 if (isDebug) std::cout << "HubDel (root): " << root << std::endl;
94 response["jsonrpc"] = "2.0";
95 response["id"] = root["id"];
96
97 if (root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
98 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue)) {
99 FailedValidateRequest(response);
100 return false;
101 }
102
103 ServerThread::getInstance()->disconnectClient(root["params"]["huburl"].asString());
104 response["result"] = 0;
105 if (isDebug) std::cout << "HubDel (response): " << response << std::endl;
106 return true;
107 }
108
HubSay(const Json::Value & root,Json::Value & response)109 bool JsonRpcMethods::HubSay(const Json::Value& root, Json::Value& response)
110 {
111 if (isDebug) std::cout << "HubSay (root): " << root << std::endl;
112 response["jsonrpc"] = "2.0";
113 response["id"] = root["id"];
114
115 if ((root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
116 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue))
117 || (root["params"].isMember("message") && !root["params"]["message"].isString()
118 && !root["params"]["message"].isConvertibleTo(Json::stringValue))) {
119 FailedValidateRequest(response);
120 return false;
121 }
122
123 if (ServerThread::getInstance()->findHubInConnectedClients(root["params"]["huburl"].asString())) {
124 ServerThread::getInstance()->sendMessage(root["params"]["huburl"].asString(),root["params"]["message"].asString());
125 response["result"] = 0;
126 } else
127 response["result"] = 1;
128 if (isDebug) std::cout << "HubSay (response): " << response << std::endl;
129 return true;
130 }
131
HubSayPM(const Json::Value & root,Json::Value & response)132 bool JsonRpcMethods::HubSayPM(const Json::Value& root, Json::Value& response)
133 {
134 if (isDebug) std::cout << "HubSayPM (root): " << root << std::endl;
135 response["jsonrpc"] = "2.0";
136 response["id"] = root["id"];
137
138 if ((root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
139 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue))
140 || (root["params"].isMember("nick") && !root["params"]["nick"].isString()
141 && !root["params"]["nick"].isConvertibleTo(Json::stringValue))
142 || (root["params"].isMember("message") && !root["params"]["message"].isString()
143 && !root["params"]["message"].isConvertibleTo(Json::stringValue))) {
144 FailedValidateRequest(response);
145 return false;
146 }
147
148 if (ServerThread::getInstance()->sendPrivateMessage(root["params"]["huburl"].asString(), root["params"]["nick"].asString(), root["params"]["message"].asString()))
149 response["result"] = 0;
150 else
151 response["result"] = 1;
152 if (isDebug) std::cout << "HubSayPM (response): " << response << std::endl;
153 return true;
154 }
155
ListHubs(const Json::Value & root,Json::Value & response)156 bool JsonRpcMethods::ListHubs(const Json::Value& root, Json::Value& response)
157 {
158 if (isDebug) std::cout << "ListHubs (root): " << root << std::endl;
159 response["jsonrpc"] = "2.0";
160 response["id"] = root["id"];
161
162 if (root["params"].isMember("separator") && !root["params"]["separator"].isString()
163 && !root["params"]["separator"].isConvertibleTo(Json::stringValue)) {
164 FailedValidateRequest(response);
165 return false;
166 }
167
168 string listhubs;
169 ServerThread::getInstance()->listConnectedClients(listhubs, root["params"]["separator"].asString());
170 response["result"] = listhubs;
171 if (isDebug) std::cout << "ListHubs (response): " << response << std::endl;
172 return true;
173 }
174
AddDirInShare(const Json::Value & root,Json::Value & response)175 bool JsonRpcMethods::AddDirInShare(const Json::Value& root, Json::Value& response)
176 {
177 if (isDebug) std::cout << "AddDirInShare (root): " << root << std::endl;
178 response["jsonrpc"] = "2.0";
179 response["id"] = root["id"];
180
181 if ((root["params"].isMember("directory") && !root["params"]["directory"].isString()
182 && !root["params"]["directory"].isConvertibleTo(Json::stringValue))
183 || (root["params"].isMember("virtname") && !root["params"]["virtname"].isString()
184 && !root["params"]["virtname"].isConvertibleTo(Json::stringValue))) {
185 FailedValidateRequest(response);
186 return false;
187 }
188
189 try {
190 if (ServerThread::getInstance()->addDirInShare(root["params"]["directory"].asString(), root["params"]["virtname"].asString()))
191 response["result"] = 0;
192 else
193 response["result"] = 1;
194 } catch (const ShareException& e) {
195 response["result"] = e.getError();
196 }
197 if (isDebug) std::cout << "AddDirInShare (response): " << response << std::endl;
198 return true;
199 }
200
RenameDirInShare(const Json::Value & root,Json::Value & response)201 bool JsonRpcMethods::RenameDirInShare(const Json::Value& root, Json::Value& response)
202 {
203 if (isDebug) std::cout << "RenameDirInShare (root): " << root << std::endl;
204 response["jsonrpc"] = "2.0";
205 response["id"] = root["id"];
206
207 if ((root["params"].isMember("directory") && !root["params"]["directory"].isString()
208 && !root["params"]["directory"].isConvertibleTo(Json::stringValue))
209 || (root["params"].isMember("virtname") && !root["params"]["virtname"].isString()
210 && !root["params"]["virtname"].isConvertibleTo(Json::stringValue))) {
211 FailedValidateRequest(response);
212 return false;
213 }
214
215 try {
216 if (ServerThread::getInstance()->renameDirInShare(root["params"]["directory"].asString(), root["params"]["virtname"].asString()))
217 response["result"] = 0;
218 else
219 response["result"] = 1;
220 } catch (const ShareException& e) {
221 response["result"] = e.getError();
222 }
223 if (isDebug) std::cout << "RenameDirInShare (response): " << response << std::endl;
224 return true;
225 }
226
DelDirFromShare(const Json::Value & root,Json::Value & response)227 bool JsonRpcMethods::DelDirFromShare(const Json::Value& root, Json::Value& response)
228 {
229 if (isDebug) std::cout << "DelDirFromShare (root): " << root << std::endl;
230 response["jsonrpc"] = "2.0";
231 response["id"] = root["id"];
232
233 if (root["params"].isMember("directory") && !root["params"]["directory"].isString()
234 && !root["params"]["directory"].isConvertibleTo(Json::stringValue)) {
235 FailedValidateRequest(response);
236 return false;
237 }
238
239 if (ServerThread::getInstance()->delDirFromShare(root["params"]["directory"].asString()))
240 response["result"] = 0;
241 else
242 response["result"] = 1;
243 if (isDebug) std::cout << "DelDirFromShare (response): " << response << std::endl;
244 return true;
245 }
246
ListShare(const Json::Value & root,Json::Value & response)247 bool JsonRpcMethods::ListShare(const Json::Value& root, Json::Value& response)
248 {
249 if (isDebug) std::cout << "ListShare (root): " << root << std::endl;
250 response["jsonrpc"] = "2.0";
251 response["id"] = root["id"];
252
253 if (root["params"].isMember("separator") && !root["params"]["separator"].isString()
254 && !root["params"]["separator"].isConvertibleTo(Json::stringValue)) {
255 FailedValidateRequest(response);
256 return false;
257 }
258
259 string listshare;
260 ServerThread::getInstance()->listShare(listshare, root["params"]["separator"].asString());
261 response["result"] = listshare;
262 if (isDebug) std::cout << "ListShare (response): " << response << std::endl;
263 return true;
264 }
265
RefreshShare(const Json::Value & root,Json::Value & response)266 bool JsonRpcMethods::RefreshShare(const Json::Value& root, Json::Value& response)
267 {
268 if (isDebug) std::cout << "RefreshShare (root): " << root << std::endl;
269 response["jsonrpc"] = "2.0";
270 response["id"] = root["id"];
271 ShareManager::getInstance()->setDirty();
272 ShareManager::getInstance()->refresh(true);
273 response["result"] = 0;
274 if (isDebug) std::cout << "RefreshShare (response): " << response << std::endl;
275 return true;
276 }
277
GetFileList(const Json::Value & root,Json::Value & response)278 bool JsonRpcMethods::GetFileList(const Json::Value& root, Json::Value& response)
279 {
280 if (isDebug) std::cout << "GetFileList (root): " << root << std::endl;
281 response["jsonrpc"] = "2.0";
282 response["id"] = root["id"];
283
284 if ((root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
285 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue))
286 || (root["params"].isMember("nick") && !root["params"]["nick"].isString()
287 && !root["params"]["nick"].isConvertibleTo(Json::stringValue))) {
288 FailedValidateRequest(response);
289 return false;
290 }
291
292 if (ServerThread::getInstance()->getFileList(root["params"]["huburl"].asString(), root["params"]["nick"].asString(), false))
293 response["result"] = 0;
294 else
295 response["result"] = 1;
296 if (isDebug) std::cout << "GetFileList (response): " << response << std::endl;
297 return true;
298 }
299
GetChatPub(const Json::Value & root,Json::Value & response)300 bool JsonRpcMethods::GetChatPub(const Json::Value& root, Json::Value& response)
301 {
302 if (isDebug) std::cout << "GetChatPub (root): " << root << std::endl;
303 response["jsonrpc"] = "2.0";
304 response["id"] = root["id"];
305
306 if ((root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
307 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue))
308 || (root["params"].isMember("separator") && !root["params"]["separator"].isString()
309 && !root["params"]["separator"].isConvertibleTo(Json::stringValue))) {
310 FailedValidateRequest(response);
311 return false;
312 }
313
314 string retchat;
315 ServerThread::getInstance()->getChatPubFromClient(retchat, root["params"]["huburl"].asString(), root["params"]["separator"].asString());
316 response["result"] = retchat;
317 if (isDebug) std::cout << "GetChatPub (response): " << response << std::endl;
318 return true;
319 }
320
SendSearch(const Json::Value & root,Json::Value & response)321 bool JsonRpcMethods::SendSearch(const Json::Value& root, Json::Value& response)
322 {
323 if (isDebug) std::cout << "SendSearch (root): " << root << std::endl;
324 response["jsonrpc"] = "2.0";
325 response["id"] = root["id"];
326
327 if ((root["params"].isMember("searchstring") && !root["params"]["searchstring"].isString()
328 && !root["params"]["searchstring"].isConvertibleTo(Json::stringValue))
329 || (root["params"].isMember("searchtype") && !root["params"]["searchtype"].isInt()
330 && !root["params"]["searchtype"].isConvertibleTo(Json::intValue))
331 || (root["params"].isMember("sizemode") && !root["params"]["sizemode"].isInt()
332 && !root["params"]["sizemode"].isConvertibleTo(Json::intValue))
333 || (root["params"].isMember("sizetype") && !root["params"]["sizetype"].isInt()
334 && !root["params"]["sizetype"].isConvertibleTo(Json::intValue))
335 || (root["params"].isMember("size") && !root["params"]["size"].isDouble()
336 && !root["params"]["size"].isConvertibleTo(Json::realValue))
337 || (root["params"].isMember("huburls") && !root["params"]["huburls"].isString()
338 && !root["params"]["huburls"].isConvertibleTo(Json::stringValue))
339 ) {
340 FailedValidateRequest(response);
341 return false;
342 }
343
344 if (ServerThread::getInstance()->sendSearchonHubs(root["params"]["searchstring"].asString(), root["params"]["searchtype"].asInt(), root["params"]["sizemode"].asInt(), root["params"]["sizetype"].asInt(), root["params"]["size"].asDouble(), root["params"]["huburls"].asString()))
345 response["result"] = 0;
346 else
347 response["result"] = 1;
348 if (isDebug) std::cout << "SendSearch (response): " << response << std::endl;
349 return true;
350 }
351
ReturnSearchResults(const Json::Value & root,Json::Value & response)352 bool JsonRpcMethods::ReturnSearchResults(const Json::Value& root, Json::Value& response)
353 {
354 if (isDebug) std::cout << "ReturnSearchResults (root): " << root << std::endl;
355 response["jsonrpc"] = "2.0";
356 response["id"] = root["id"];
357
358 if (root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
359 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue)) {
360 FailedValidateRequest(response);
361 return false;
362 }
363
364 vector<StringMap> hublist;
365 Json::Value parameters;
366 ServerThread::getInstance()->returnSearchResults(hublist, root["params"]["huburl"].asString());
367 int k = 0;
368 for (const auto& hub : hublist) {
369 for (const auto& rearchresult : hub) {
370 parameters[k][rearchresult.first] = rearchresult.second;
371 }
372 ++k;
373 }
374 response["result"] = parameters;
375 if (isDebug) std::cout << "ReturnSearchResults (response): " << response << std::endl;
376 return true;
377 }
378
ShowVersion(const Json::Value & root,Json::Value & response)379 bool JsonRpcMethods::ShowVersion(const Json::Value& root, Json::Value& response)
380 {
381 if (isDebug) std::cout << "ShowVersion (root): " << root << std::endl;
382 response["jsonrpc"] = "2.0";
383 response["id"] = root["id"];
384 string version(EISKALTDCPP_VERSION);
385 version.append(" (");
386 version.append(EISKALTDCPP_VERSION_SFX);
387 version.append(")");
388 response["result"] = version;
389 if (isDebug) std::cout << "ShowVersion (response): " << response << std::endl;
390 return true;
391 }
392
ShowRatio(const Json::Value & root,Json::Value & response)393 bool JsonRpcMethods::ShowRatio(const Json::Value& root, Json::Value& response)
394 {
395 if (isDebug) std::cout << "ShowRatio (root): " << root << std::endl;
396 response["jsonrpc"] = "2.0";
397 response["id"] = root["id"];
398
399 auto up = SETTING(TOTAL_UPLOAD);
400 auto down = SETTING(TOTAL_DOWNLOAD);
401 auto ratio = (down > 0) ? up / down : 0;
402
403 string upload = Util::formatBytes(up);
404 string download = Util::formatBytes(down);
405 response["result"]["ratio"] = Util::toString(ratio);
406 response["result"]["up"] = upload;
407 response["result"]["down"] = download;
408 response["result"]["up_bytes"] = Util::toString(up);
409 response["result"]["down_bytes"] = Util::toString(down);
410 if (isDebug) std::cout << "ShowRatio (response): " << response << std::endl;
411 return true;
412 }
413
SetPriorityQueueItem(const Json::Value & root,Json::Value & response)414 bool JsonRpcMethods::SetPriorityQueueItem(const Json::Value& root, Json::Value& response) {
415 if (isDebug) std::cout << "SetPriorityQueueItem (root): " << root << std::endl;
416 response["jsonrpc"] = "2.0";
417 response["id"] = root["id"];
418
419 if ((root["params"].isMember("target") && !root["params"]["target"].isString()
420 && !root["params"]["target"].isConvertibleTo(Json::stringValue))
421 || (root["params"].isMember("priority") && !root["params"]["priority"].isInt()
422 && !root["params"]["priority"].isConvertibleTo(Json::intValue))) {
423 FailedValidateRequest(response);
424 return false;
425 }
426
427 if (ServerThread::getInstance()->setPriorityQueueItem(root["params"]["target"].asString(), root["params"]["priority"].asInt()))
428 response["result"] = 0;
429 else
430 response["result"] = 1;
431 if (isDebug) std::cout << "SetPriorityQueueItem (response): " << response << std::endl;
432 return true;
433 }
434
MoveQueueItem(const Json::Value & root,Json::Value & response)435 bool JsonRpcMethods::MoveQueueItem(const Json::Value& root, Json::Value& response) {
436 if (isDebug) std::cout << "MoveQueueItem (root): " << root << std::endl;
437 response["jsonrpc"] = "2.0";
438 response["id"] = root["id"];
439
440 if ((root["params"].isMember("source") && !root["params"]["source"].isString()
441 && !root["params"]["source"].isConvertibleTo(Json::stringValue))
442 || (root["params"].isMember("target") && !root["params"]["target"].isString()
443 && !root["params"]["target"].isConvertibleTo(Json::stringValue))) {
444 FailedValidateRequest(response);
445 return false;
446 }
447
448 if (ServerThread::getInstance()->moveQueueItem(root["params"]["source"].asString(), root["params"]["target"].asString()))
449 response["result"] = 0;
450 else
451 response["result"] = 1;
452 if (isDebug) std::cout << "MoveQueueItem (response): " << response << std::endl;
453 return true;
454 }
455
RemoveQueueItem(const Json::Value & root,Json::Value & response)456 bool JsonRpcMethods::RemoveQueueItem(const Json::Value& root, Json::Value& response) {
457 if (isDebug) std::cout << "removeQueueItem (root): " << root << std::endl;
458 response["jsonrpc"] = "2.0";
459 response["id"] = root["id"];
460
461 if (root["params"].isMember("target") && !root["params"]["target"].isString()
462 && !root["params"]["target"].isConvertibleTo(Json::stringValue)) {
463 FailedValidateRequest(response);
464 return false;
465 }
466
467 if (ServerThread::getInstance()->removeQueueItem(root["params"]["target"].asString()))
468 response["result"] = 0;
469 else
470 response["result"] = 1;
471 if (isDebug) std::cout << "removeQueueItem (response): " << response << std::endl;
472 return true;
473 }
474
ListQueueTargets(const Json::Value & root,Json::Value & response)475 bool JsonRpcMethods::ListQueueTargets(const Json::Value& root, Json::Value& response) {
476 if (isDebug) std::cout << "ListQueueTargets (root): " << root << std::endl;
477 response["jsonrpc"] = "2.0";
478 response["id"] = root["id"];
479
480 if (root["params"].isMember("separator") && !root["params"]["separator"].isString()
481 && !root["params"]["separator"].isConvertibleTo(Json::stringValue)) {
482 FailedValidateRequest(response);
483 return false;
484 }
485
486 string tmp;
487 ServerThread::getInstance()->listQueueTargets(tmp, root["params"]["separator"].asString());
488 response["result"] = tmp;
489 if (isDebug) std::cout << "ListQueueTargets (response): " << response << std::endl;
490 return true;
491 }
492
ListQueue(const Json::Value & root,Json::Value & response)493 bool JsonRpcMethods::ListQueue(const Json::Value& root, Json::Value& response) {
494 if (isDebug) std::cout << "ListQueue (root): " << root << std::endl;
495 response["jsonrpc"] = "2.0";
496 response["id"] = root["id"];
497 Json::Value parameters;
498 unordered_map<string,StringMap> listqueue;
499 ServerThread::getInstance()->listQueue(listqueue);
500 for (const auto& item : listqueue) {
501 for (const auto& parameter : item.second) {
502 parameters[item.first][parameter.first] = parameter.second;
503 }
504 }
505 response["result"] = parameters;
506 if (isDebug) std::cout << "ListQueue (response): " << response << std::endl;
507 return true;
508 }
509
ClearSearchResults(const Json::Value & root,Json::Value & response)510 bool JsonRpcMethods::ClearSearchResults(const Json::Value& root, Json::Value& response) {
511 if (isDebug) std::cout << "ClearSearchResults (root): " << root << std::endl;
512 response["jsonrpc"] = "2.0";
513 response["id"] = root["id"];
514
515 if (root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
516 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue)) {
517 FailedValidateRequest(response);
518 return false;
519 }
520
521 if (ServerThread::getInstance()->clearSearchResults(root["params"]["huburl"].asString()))
522 response["result"] = 0;
523 else
524 response["result"] = 1;
525 if (isDebug) std::cout << "ClearSearchResults (response): " << response << std::endl;
526 return true;
527 }
528
AddQueueItem(const Json::Value & root,Json::Value & response)529 bool JsonRpcMethods::AddQueueItem(const Json::Value& root, Json::Value& response) {
530 if (isDebug) std::cout << "AddQueueItem (root): " << root << std::endl;
531 response["jsonrpc"] = "2.0";
532 response["id"] = root["id"];
533
534 if ((root["params"].isMember("directory") && !root["params"]["directory"].isString()
535 && !root["params"]["directory"].isConvertibleTo(Json::stringValue))
536 || (root["params"].isMember("tth") && !root["params"]["tth"].isString()
537 && !root["params"]["tth"].isConvertibleTo(Json::stringValue))
538 || (root["params"].isMember("filename") && !root["params"]["filename"].isString()
539 && !root["params"]["filename"].isConvertibleTo(Json::stringValue))
540 || (root["params"].isMember("size") && !root["params"]["size"].isInt64()
541 && !root["params"]["size"].isConvertibleTo(Json::intValue))
542 ) {
543 FailedValidateRequest(response);
544 return false;
545 }
546
547 std::string directory = root["params"]["directory"].asString();
548 std::string tth = root["params"]["tth"].asString();
549 std::string name = root["params"]["filename"].asString();
550 int64_t size = root["params"]["size"].asInt64();
551
552 if (ServerThread::getInstance()->addInQueue(directory, name, size, tth))
553 response["result"] = 0;
554 else
555 response["result"] = 1;
556
557 if (isDebug) std::cout << "AddQueueItem (response): " << response << std::endl;
558 return true;
559 }
560
GetSourcesItem(const Json::Value & root,Json::Value & response)561 bool JsonRpcMethods::GetSourcesItem(const Json::Value& root, Json::Value& response) {
562 if (isDebug) std::cout << "GetSourcesItem (root): " << root << std::endl;
563 response["jsonrpc"] = "2.0";
564 response["id"] = root["id"];
565
566 if ((root["params"].isMember("target") && !root["params"]["target"].isString()
567 && !root["params"]["target"].isConvertibleTo(Json::stringValue))
568 || (root["params"].isMember("separator") && !root["params"]["separator"].isString()
569 && !root["params"]["separator"].isConvertibleTo(Json::stringValue))) {
570 FailedValidateRequest(response);
571 return false;
572 }
573
574 string sources;
575 unsigned int online = 0;
576 ServerThread::getInstance()->getItemSourcesbyTarget(root["params"]["target"].asString(), root["params"]["separator"].asString(), sources, online);
577 response["result"]["sources"] = sources;
578 response["result"]["online"] = online;
579 if (isDebug) std::cout << "GetSourcesItem (response): " << response << std::endl;
580 return true;
581 }
582
GetHashStatus(const Json::Value & root,Json::Value & response)583 bool JsonRpcMethods::GetHashStatus(const Json::Value& root, Json::Value& response) {
584 if (isDebug) std::cout << "GetHashStatus (root): " << root << std::endl;
585 response["jsonrpc"] = "2.0";
586 response["id"] = root["id"];
587 string tmp = " ",status = " "; int64_t bytes = 0; size_t files = 0;
588 ServerThread::getInstance()->getHashStatus(tmp, bytes, files, status);
589 response["result"]["currentfile"]=tmp;
590 response["result"]["status"]=status;
591 response["result"]["bytesleft"]=Json::Value::Int64(bytes);
592 response["result"]["filesleft"]=Json::Value::UInt(files);
593 if (isDebug) std::cout << "GetHashStatus (response): " << response << std::endl;
594 return true;
595 }
596
PauseHash(const Json::Value & root,Json::Value & response)597 bool JsonRpcMethods::PauseHash(const Json::Value& root, Json::Value& response) {
598 if (isDebug) std::cout << "PauseHash (root): " << root << std::endl;
599 response["jsonrpc"] = "2.0";
600 response["id"] = root["id"];
601 if (ServerThread::getInstance()->pauseHash())
602 response["result"] = 0;
603 else
604 response["result"] = 1;
605 if (isDebug) std::cout << "PauseHash (response): " << response << std::endl;
606 return true;
607 }
608
MatchAllLists(const Json::Value & root,Json::Value & response)609 bool JsonRpcMethods::MatchAllLists(const Json::Value& root, Json::Value& response) {
610 if (isDebug) std::cout << "MatchAllLists (root): " << root << std::endl;
611 response["jsonrpc"] = "2.0";
612 response["id"] = root["id"];
613 ServerThread::getInstance()->matchAllList();
614 response["result"] = 0;
615 if (isDebug) std::cout << "MatchAllLists (response): " << response << std::endl;
616 return true;
617 }
618
ListHubsFullDesc(const Json::Value & root,Json::Value & response)619 bool JsonRpcMethods::ListHubsFullDesc(const Json::Value& root, Json::Value& response)
620 {
621 if (isDebug) std::cout << "ListHubsFullDesc (root): " << root << std::endl;
622 response["jsonrpc"] = "2.0";
623 response["id"] = root["id"];
624 Json::Value parameters;
625 unordered_map<string,StringMap> listhubs;
626 ServerThread::getInstance()->listHubsFullDesc(listhubs);
627 for (const auto& hub : listhubs) {
628 for (const auto& parameter : hub.second) {
629 parameters[hub.first][parameter.first] = parameter.second;
630 }
631 }
632 response["result"] = parameters;
633 if (isDebug) std::cout << "ListHubsFullDesc (response): " << response << std::endl;
634 return true;
635 }
636
GetHubUserList(const Json::Value & root,Json::Value & response)637 bool JsonRpcMethods::GetHubUserList(const Json::Value& root, Json::Value& response) {
638 if (isDebug) std::cout << "GetHubUserList (root): " << root << std::endl;
639 response["jsonrpc"] = "2.0";
640 response["id"] = root["id"];
641
642 if ((root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
643 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue))
644 || (root["params"].isMember("separator") && !root["params"]["separator"].isString()
645 && !root["params"]["separator"].isConvertibleTo(Json::stringValue))
646 ) {
647 FailedValidateRequest(response);
648 return false;
649 }
650
651 string tmp;
652 ServerThread::getInstance()->getHubUserList(tmp, root["params"]["huburl"].asString(), root["params"]["separator"].asString());
653 response["result"] = tmp;
654 if (isDebug) std::cout << "GetHubUserList (response): " << response << std::endl;
655 return true;
656 }
657
GetUserInfo(const Json::Value & root,Json::Value & response)658 bool JsonRpcMethods::GetUserInfo(const Json::Value& root, Json::Value& response) {
659 if (isDebug) std::cout << "GetUserInfo (root): " << root << std::endl;
660 response["jsonrpc"] = "2.0";
661 response["id"] = root["id"];
662
663 if ((root["params"].isMember("nick") && !root["params"]["nick"].isString()
664 && !root["params"]["nick"].isConvertibleTo(Json::stringValue))
665 || (root["params"].isMember("huburl") && !root["params"]["huburl"].isString()
666 && !root["params"]["huburl"].isConvertibleTo(Json::stringValue))
667 ) {
668 FailedValidateRequest(response);
669 return false;
670 }
671
672 StringMap params; Json::Value parameters;
673 if (ServerThread::getInstance()->getUserInfo(params, root["params"]["nick"].asString(),root["params"]["huburl"].asString())) {
674 for (const auto& parameter : params) {
675 parameters[parameter.first] = parameter.second;
676 }
677 }
678 response["result"] = parameters;
679 if (isDebug) std::cout << "GetUserInfo (response): " << response << std::endl;
680 return true;
681 }
682
ShowLocalLists(const Json::Value & root,Json::Value & response)683 bool JsonRpcMethods::ShowLocalLists(const Json::Value& root, Json::Value& response) {
684 if (isDebug) std::cout << "ShowLocalLists (root): " << root << std::endl;
685 response["jsonrpc"] = "2.0";
686 response["id"] = root["id"];
687
688 if (root["params"].isMember("separator") && !root["params"]["separator"].isString()
689 && !root["params"]["separator"].isConvertibleTo(Json::stringValue)) {
690 FailedValidateRequest(response);
691 return false;
692 }
693
694 string tmp;
695 ServerThread::getInstance()->showLocalLists(tmp, root["params"]["separator"].asString());
696 response["result"] = tmp;
697 if (isDebug) std::cout << "ShowLocalLists (response): " << response << std::endl;
698 return true;
699 }
700
GetClientFileList(const Json::Value & root,Json::Value & response)701 bool JsonRpcMethods::GetClientFileList(const Json::Value& root, Json::Value& response) {
702 if (isDebug) std::cout << "GetClientFileList (root): " << root << std::endl;
703 response["jsonrpc"] = "2.0";
704 response["id"] = root["id"];
705
706 if (root["params"].isMember("filelist") && !root["params"]["filelist"].isString()
707 && !root["params"]["filelist"].isConvertibleTo(Json::stringValue)) {
708 FailedValidateRequest(response);
709 return false;
710 }
711
712 string ret;
713 if (ServerThread::getInstance()->getClientFileList(root["params"]["filelist"].asString(), ret))
714 response["result"] = ret;
715 else
716 response["result"] = 1;
717 if (isDebug) std::cout << "GetClientFileList (response): " << response << std::endl;
718 return true;
719 }
OpenFileList(const Json::Value & root,Json::Value & response)720 bool JsonRpcMethods::OpenFileList(const Json::Value& root, Json::Value& response) {
721 if (isDebug) std::cout << "OpenFileList (root): " << root << std::endl;
722 response["jsonrpc"] = "2.0";
723 response["id"] = root["id"];
724
725 if (root["params"].isMember("filelist") && !root["params"]["filelist"].isString()
726 && !root["params"]["filelist"].isConvertibleTo(Json::stringValue)) {
727 FailedValidateRequest(response);
728 return false;
729 }
730
731 if (ServerThread::getInstance()->openFileList(root["params"]["filelist"].asString()))
732 response["result"] = 0;
733 else
734 response["result"] = 1;
735 if (isDebug) std::cout << "OpenFileList (response): " << response << std::endl;
736 return true;
737 }
738
CloseFileList(const Json::Value & root,Json::Value & response)739 bool JsonRpcMethods::CloseFileList(const Json::Value& root, Json::Value& response) {
740 if (isDebug) std::cout << "CloseFileList (root): " << root << std::endl;
741 response["jsonrpc"] = "2.0";
742 response["id"] = root["id"];
743
744 if (root["params"].isMember("filelist") && !root["params"]["filelist"].isString()
745 && !root["params"]["filelist"].isConvertibleTo(Json::stringValue)) {
746 FailedValidateRequest(response);
747 return false;
748 }
749
750 if (ServerThread::getInstance()->closeFileList(root["params"]["filelist"].asString()))
751 response["result"] = 0;
752 else
753 response["result"] = 1;
754 if (isDebug) std::cout << "CloseFileList (response): " << response << std::endl;
755 return true;
756 }
757
CloseAllFileLists(const Json::Value & root,Json::Value & response)758 bool JsonRpcMethods::CloseAllFileLists(const Json::Value& root, Json::Value& response) {
759 if (isDebug) std::cout << "CloseAllFileList (root): " << root << std::endl;
760 response["jsonrpc"] = "2.0";
761 response["id"] = root["id"];
762 ServerThread::getInstance()->closeAllFileLists();
763 response["result"] = 0;
764 if (isDebug) std::cout << "CloseAllFileList (response): " << response << std::endl;
765 return true;
766 }
767
ShowOpenedLists(const Json::Value & root,Json::Value & response)768 bool JsonRpcMethods::ShowOpenedLists(const Json::Value& root, Json::Value& response) {
769 if (isDebug) std::cout << "ShowOpenedLists (root): " << root << std::endl;
770 response["jsonrpc"] = "2.0";
771 response["id"] = root["id"];
772
773 if (root["params"].isMember("separator") && !root["params"]["separator"].isString()
774 && !root["params"]["separator"].isConvertibleTo(Json::stringValue)) {
775 FailedValidateRequest(response);
776 return false;
777 }
778
779 string tmp;
780 ServerThread::getInstance()->showOpenedLists(tmp, root["params"]["separator"].asString());
781 response["result"] = tmp;
782 if (isDebug) std::cout << "ShowOpenedLists (response): " << response << std::endl;
783 return true;
784 }
785
LsDirInList(const Json::Value & root,Json::Value & response)786 bool JsonRpcMethods::LsDirInList(const Json::Value& root, Json::Value& response) {
787 if (isDebug) std::cout << "LsDirInList (root): " << root << std::endl;
788 response["jsonrpc"] = "2.0";
789 response["id"] = root["id"];
790
791 if ((root["params"].isMember("directory") && !root["params"]["directory"].isString()
792 && !root["params"]["directory"].isConvertibleTo(Json::stringValue))
793 || (root["params"].isMember("filelist") && !root["params"]["filelist"].isString()
794 && !root["params"]["filelist"].isConvertibleTo(Json::stringValue))
795 ) {
796 FailedValidateRequest(response);
797 return false;
798 }
799
800 Json::Value parameters;
801 unordered_map<string,StringMap> map;
802 ServerThread::getInstance()->lsDirInList(root["params"]["directory"].asString(), root["params"]["filelist"].asString(), map);
803 for (const auto& item : map) {
804 for (const auto& parameter : item.second) {
805 parameters[item.first][parameter.first] = parameter.second;
806 }
807 }
808 response["result"] = parameters;
809 if (isDebug) std::cout << "LsDirInList (response): " << response << std::endl;
810 return true;
811 }
812
DownloadDirFromList(const Json::Value & root,Json::Value & response)813 bool JsonRpcMethods::DownloadDirFromList(const Json::Value& root, Json::Value& response) {
814 if (isDebug) std::cout << "DownloadDirFromList (root): " << root << std::endl;
815 response["jsonrpc"] = "2.0";
816 response["id"] = root["id"];
817
818 if ((root["params"].isMember("target") && !root["params"]["target"].isString()
819 && !root["params"]["target"].isConvertibleTo(Json::stringValue))
820 || (root["params"].isMember("downloadto") && !root["params"]["downloadto"].isString()
821 && !root["params"]["downloadto"].isConvertibleTo(Json::stringValue))
822 ) {
823 FailedValidateRequest(response);
824 return false;
825 }
826
827 if (ServerThread::getInstance()->downloadDirFromList(root["params"]["target"].asString(), root["params"]["downloadto"].asString(), root["params"]["filelist"].asString()))
828 response["result"] = 0;
829 else
830 response["result"] = 1;
831 if (isDebug) std::cout << "DownloadDirFromList (response): " << response << std::endl;
832 return true;
833 }
834
DownloadFileFromList(const Json::Value & root,Json::Value & response)835 bool JsonRpcMethods::DownloadFileFromList(const Json::Value& root, Json::Value& response) {
836 if (isDebug) std::cout << "DownloadFileFromList (root): " << root << std::endl;
837 response["jsonrpc"] = "2.0";
838 response["id"] = root["id"];
839
840 if ((root["params"].isMember("target") && !root["params"]["target"].isString()
841 && !root["params"]["target"].isConvertibleTo(Json::stringValue))
842 || (root["params"].isMember("downloadto") && !root["params"]["downloadto"].isString()
843 && !root["params"]["downloadto"].isConvertibleTo(Json::stringValue))
844 ) {
845 FailedValidateRequest(response);
846 return false;
847 }
848
849 if (ServerThread::getInstance()->downloadFileFromList(root["params"]["target"].asString(), root["params"]["downloadto"].asString(), root["params"]["filelist"].asString()))
850 response["result"] = 0;
851 else
852 response["result"] = 1;
853 if (isDebug) std::cout << "DownloadFileFromList (response): " << response << std::endl;
854 return true;
855 }
856
GetItemDescbyTarget(const Json::Value & root,Json::Value & response)857 bool JsonRpcMethods::GetItemDescbyTarget(const Json::Value& root, Json::Value& response) {
858 if (isDebug) std::cout << "GetItemDescbyTarget (root): " << root << std::endl;
859 response["jsonrpc"] = "2.0";
860 response["id"] = root["id"];
861
862 if (root["params"].isMember("target") && !root["params"]["target"].isString()
863 && !root["params"]["target"].isConvertibleTo(Json::stringValue)) {
864 FailedValidateRequest(response);
865 return false;
866 }
867
868 Json::Value parameters; StringMap map;
869 ServerThread::getInstance()->getItemDescbyTarget(root["params"]["target"].asString(), map);
870 for (const auto& parameter : map) {
871 parameters[parameter.first] = parameter.second;
872 }
873 response["result"] = parameters;
874 if (isDebug) std::cout << "GetItemDescbyTarget (response): " << response << std::endl;
875 return true;
876 }
877
QueueClear(const Json::Value & root,Json::Value & response)878 bool JsonRpcMethods::QueueClear(const Json::Value& root, Json::Value& response) {
879 if (isDebug) std::cout << "QueueClear (root): " << root << std::endl;
880 response["jsonrpc"] = "2.0";
881 response["id"] = root["id"];
882 ServerThread::getInstance()->queueClear();
883 response["result"] = 0;
884 if (isDebug) std::cout << "QueueClear (response): " << response << std::endl;
885 return true;
886 }
887
SettingsGetSet(const Json::Value & root,Json::Value & response)888 bool JsonRpcMethods::SettingsGetSet(const Json::Value& root, Json::Value& response) {
889 if (isDebug) std::cout << "SettingsGetSet (root): " << root << std::endl;
890 response["jsonrpc"] = "2.0";
891 response["id"] = root["id"];
892
893 if ((root["params"].isMember("key") && !root["params"]["key"].isString()
894 && !root["params"]["key"].isConvertibleTo(Json::stringValue))
895 || (root["params"].isMember("value") && !root["params"]["value"].isString()
896 && !root["params"]["value"].isConvertibleTo(Json::stringValue))
897 ) {
898 FailedValidateRequest(response);
899 return false;
900 }
901
902 string out;
903 bool b = ServerThread::getInstance()->settingsGetSet(out, root["params"]["key"].asString(), root["params"]["value"].asString());
904 if (b) {
905 if (root["params"]["value"].asString().empty()) {
906 response["result"]["value"] = out;
907 } else {
908 response["result"] = 0;
909 }
910 } else {
911 response["result"] = 1;
912 }
913 if (isDebug) std::cout << "SettingsGetSet (response): " << response << std::endl;
914 return true;
915 }
916
IpFilterOnOff(const Json::Value & root,Json::Value & response)917 bool JsonRpcMethods::IpFilterOnOff(const Json::Value& root, Json::Value& response) {
918 if (isDebug) std::cout << "IpFilterOnOff (root): " << root << std::endl;
919 response["jsonrpc"] = "2.0";
920 response["id"] = root["id"];
921
922 if (root["params"].isMember("on") && !root["params"]["on"].isInt()
923 && !root["params"]["on"].isConvertibleTo(Json::intValue)) {
924 FailedValidateRequest(response);
925 return false;
926 }
927
928 ServerThread::getInstance()->ipfilterOnOff(root["params"]["on"].asInt());
929 response["result"] = 0;
930 if (isDebug) std::cout << "IpFilterOnOff (response): " << response << std::endl;
931 return true;
932 }
933
IpFilterList(const Json::Value & root,Json::Value & response)934 bool JsonRpcMethods::IpFilterList(const Json::Value& root, Json::Value& response) {
935 if (isDebug) std::cout << "IpFilterList (root): " << root << std::endl;
936 response["jsonrpc"] = "2.0";
937 response["id"] = root["id"];
938
939 if (root["params"].isMember("separator") && !root["params"]["separator"].isString()
940 && !root["params"]["separator"].isConvertibleTo(Json::stringValue)) {
941 FailedValidateRequest(response);
942 return false;
943 }
944
945 string out;
946 ServerThread::getInstance()->ipfilterList(out, root["params"]["separator"].asString());
947 response["result"] = out;
948 if (isDebug) std::cout << "IpFilterList (response): " << response << std::endl;
949 return true;
950 }
951
IpFilterAddRules(const Json::Value & root,Json::Value & response)952 bool JsonRpcMethods::IpFilterAddRules(const Json::Value& root, Json::Value& response) {
953 if (isDebug) std::cout << "IpFilterAddRules (root): " << root << std::endl;
954 response["jsonrpc"] = "2.0";
955 response["id"] = root["id"];
956
957 if (root["params"].isMember("rules") && !root["params"]["rules"].isString()
958 && !root["params"]["rules"].isConvertibleTo(Json::stringValue)) {
959 FailedValidateRequest(response);
960 return false;
961 }
962
963 ServerThread::getInstance()->ipfilterAddRules(root["params"]["rules"].asString());
964 response["result"] = 0;
965 if (isDebug) std::cout << "IpFilterAddRules (response): " << response << std::endl;
966 return true;
967 }
968
IpFilterPurgeRules(const Json::Value & root,Json::Value & response)969 bool JsonRpcMethods::IpFilterPurgeRules(const Json::Value& root, Json::Value& response) {
970 if (isDebug) std::cout << "IpFilterPurgeRules (root): " << root << std::endl;
971 response["jsonrpc"] = "2.0";
972 response["id"] = root["id"];
973
974 if (root["params"].isMember("separator") && !root["params"]["separator"].isString()
975 && !root["params"]["separator"].isConvertibleTo(Json::stringValue)) {
976 FailedValidateRequest(response);
977 return false;
978 }
979
980 ServerThread::getInstance()->ipfilterPurgeRules(root["params"]["rules"].asString());
981 response["result"] = 0;
982 if (isDebug) std::cout << "IpFilterPurgeRules (response): " << response << std::endl;
983 return true;
984 }
985
IpFilterUpDownRule(const Json::Value & root,Json::Value & response)986 bool JsonRpcMethods::IpFilterUpDownRule(const Json::Value& root, Json::Value& response) {
987 if (isDebug) std::cout << "IpFilterUpDownRule (root): " << root << std::endl;
988 response["jsonrpc"] = "2.0";
989 response["id"] = root["id"];
990
991 if ((root["params"].isMember("up") && !root["params"]["up"].isInt()
992 && !root["params"]["up"].isConvertibleTo(Json::intValue))
993 || (root["params"].isMember("rule") && !root["params"]["rule"].isString()
994 && !root["params"]["rule"].isConvertibleTo(Json::stringValue))) {
995 FailedValidateRequest(response);
996 return false;
997 }
998
999 ServerThread::getInstance()->ipfilterUpDownRule(root["params"]["up"].asInt(), root["params"]["rule"].asString());
1000 response["result"] = 0;
1001 if (isDebug) std::cout << "IpFilterUpDownRule (response): " << response << std::endl;
1002 return true;
1003 }
1004