comparison irccdctl/cli.cpp @ 521:e03521cf207b

Common: split util.hpp into more appropriate files, closes #721
author David Demelier <markand@malikania.fr>
date Fri, 27 Oct 2017 21:45:32 +0200
parents fbd80bfcf58d
children a88796ed040a
comparison
equal deleted inserted replaced
520:defacef00c82 521:e03521cf207b
24 24
25 #include <json.hpp> 25 #include <json.hpp>
26 26
27 #include "cli.hpp" 27 #include "cli.hpp"
28 #include "irccdctl.hpp" 28 #include "irccdctl.hpp"
29 #include "json_util.hpp"
29 #include "logger.hpp" 30 #include "logger.hpp"
30 #include "options.hpp" 31 #include "options.hpp"
31 #include "util.hpp" 32 #include "net_util.hpp"
33 #include "string_util.hpp"
32 34
33 using namespace std::string_literals; 35 using namespace std::string_literals;
34 36
35 namespace irccd { 37 namespace irccd {
36 38
39 * ------------------------------------------------------------------ 41 * ------------------------------------------------------------------
40 */ 42 */
41 43
42 void Cli::check(const nlohmann::json &response) 44 void Cli::check(const nlohmann::json &response)
43 { 45 {
44 if (!util::json::get_bool(response, "status", false)) { 46 if (!json_util::get_bool(response, "status", false)) {
45 auto error = util::json::get_string(response, "error"); 47 auto error = json_util::get_string(response, "error");
46 48
47 if (error.empty()) 49 if (error.empty())
48 throw std::runtime_error("command failed with an unknown error"); 50 throw std::runtime_error("command failed with an unknown error");
49 51
50 throw std::runtime_error(error); 52 throw std::runtime_error(error);
67 69
68 try { 70 try {
69 boost::timer::cpu_timer timer; 71 boost::timer::cpu_timer timer;
70 72
71 while (irccdctl.client().isConnected() && !msg.is_object() && timer.elapsed().wall / 1000000LL < 3000) 73 while (irccdctl.client().isConnected() && !msg.is_object() && timer.elapsed().wall / 1000000LL < 3000)
72 util::poller::poll(3000 - timer.elapsed().wall / 1000000LL, irccdctl); 74 net_util::poll(3000 - timer.elapsed().wall / 1000000LL, irccdctl);
73 } catch (const std::exception &) { 75 } catch (const std::exception &) {
74 irccdctl.client().onMessage.disconnect(id); 76 irccdctl.client().onMessage.disconnect(id);
75 throw; 77 throw;
76 } 78 }
77 79
78 irccdctl.client().onMessage.disconnect(id); 80 irccdctl.client().onMessage.disconnect(id);
79 81
80 if (!msg.is_object()) 82 if (!msg.is_object())
81 throw std::runtime_error("no response received"); 83 throw std::runtime_error("no response received");
82 if (util::json::get_string(msg, "command") != m_name) 84 if (json_util::get_string(msg, "command") != m_name)
83 throw std::runtime_error("unexpected command result received"); 85 throw std::runtime_error("unexpected command result received");
84 86
85 check(msg); 87 check(msg);
86 88
87 return msg; 89 return msg;
116 })); 118 }));
117 119
118 check(result); 120 check(result);
119 121
120 if (result["variables"].is_object()) 122 if (result["variables"].is_object())
121 std::cout << util::json::pretty(result["variables"][args[1]]) << std::endl; 123 std::cout << json_util::pretty(result["variables"][args[1]]) << std::endl;
122 } 124 }
123 125
124 void PluginConfigCli::getall(Irccdctl &irccdctl, const std::vector<std::string> &args) 126 void PluginConfigCli::getall(Irccdctl &irccdctl, const std::vector<std::string> &args)
125 { 127 {
126 auto result = request(irccdctl, nlohmann::json::object({{ "plugin", args[0] }})); 128 auto result = request(irccdctl, nlohmann::json::object({{ "plugin", args[0] }}));
128 check(result); 130 check(result);
129 131
130 auto variables = result["variables"]; 132 auto variables = result["variables"];
131 133
132 for (auto v = variables.begin(); v != variables.end(); ++v) 134 for (auto v = variables.begin(); v != variables.end(); ++v)
133 std::cout << std::setw(16) << std::left << v.key() << " : " << util::json::pretty(v.value()) << std::endl; 135 std::cout << std::setw(16) << std::left << v.key() << " : " << json_util::pretty(v.value()) << std::endl;
134 } 136 }
135 137
136 PluginConfigCli::PluginConfigCli() 138 PluginConfigCli::PluginConfigCli()
137 : Cli("plugin-config", 139 : Cli("plugin-config",
138 "configure a plugin", 140 "configure a plugin",
186 throw std::invalid_argument("plugin-info requires 1 argument"); 188 throw std::invalid_argument("plugin-info requires 1 argument");
187 189
188 auto result = request(irccdctl, {{ "plugin", args[0] }}); 190 auto result = request(irccdctl, {{ "plugin", args[0] }});
189 191
190 std::cout << std::boolalpha; 192 std::cout << std::boolalpha;
191 std::cout << "Author : " << util::json::get_string(result, "author") << std::endl; 193 std::cout << "Author : " << json_util::get_string(result, "author") << std::endl;
192 std::cout << "License : " << util::json::get_string(result, "license") << std::endl; 194 std::cout << "License : " << json_util::get_string(result, "license") << std::endl;
193 std::cout << "Summary : " << util::json::get_string(result, "summary") << std::endl; 195 std::cout << "Summary : " << json_util::get_string(result, "summary") << std::endl;
194 std::cout << "Version : " << util::json::get_string(result, "version") << std::endl; 196 std::cout << "Version : " << json_util::get_string(result, "version") << std::endl;
195 } 197 }
196 198
197 /* 199 /*
198 * PluginListCli. 200 * PluginListCli.
199 * ------------------------------------------------------------------ 201 * ------------------------------------------------------------------
406 { "name", copy[0] }, 408 { "name", copy[0] },
407 { "host", copy[1] } 409 { "host", copy[1] }
408 }); 410 });
409 411
410 if (copy.size() == 3) { 412 if (copy.size() == 3) {
411 if (!util::is_int(copy[2])) 413 if (!string_util::is_int(copy[2]))
412 throw std::invalid_argument("invalid port number"); 414 throw std::invalid_argument("invalid port number");
413 415
414 object["port"] = std::stoi(copy[2]); 416 object["port"] = std::stoi(copy[2]);
415 } 417 }
416 418
482 }); 484 });
483 485
484 check(result); 486 check(result);
485 487
486 std::cout << std::boolalpha; 488 std::cout << std::boolalpha;
487 std::cout << "Name : " << util::json::pretty(result["name"]) << std::endl; 489 std::cout << "Name : " << json_util::pretty(result["name"]) << std::endl;
488 std::cout << "Host : " << util::json::pretty(result["host"]) << std::endl; 490 std::cout << "Host : " << json_util::pretty(result["host"]) << std::endl;
489 std::cout << "Port : " << util::json::pretty(result["port"]) << std::endl; 491 std::cout << "Port : " << json_util::pretty(result["port"]) << std::endl;
490 std::cout << "Ipv6 : " << util::json::pretty(result["ipv6"]) << std::endl; 492 std::cout << "Ipv6 : " << json_util::pretty(result["ipv6"]) << std::endl;
491 std::cout << "SSL : " << util::json::pretty(result["ssl"]) << std::endl; 493 std::cout << "SSL : " << json_util::pretty(result["ssl"]) << std::endl;
492 std::cout << "SSL verified : " << util::json::pretty(result["sslVerify"]) << std::endl; 494 std::cout << "SSL verified : " << json_util::pretty(result["sslVerify"]) << std::endl;
493 std::cout << "Channels : "; 495 std::cout << "Channels : ";
494 496
495 for (const auto &v : result["channels"]) 497 for (const auto &v : result["channels"])
496 if (v.is_string()) 498 if (v.is_string())
497 std::cout << v.get<std::string>() << " "; 499 std::cout << v.get<std::string>() << " ";
498 500
499 std::cout << std::endl; 501 std::cout << std::endl;
500 502
501 std::cout << "Nickname : " << util::json::pretty(result["nickname"]) << std::endl; 503 std::cout << "Nickname : " << json_util::pretty(result["nickname"]) << std::endl;
502 std::cout << "User name : " << util::json::pretty(result["username"]) << std::endl; 504 std::cout << "User name : " << json_util::pretty(result["username"]) << std::endl;
503 std::cout << "Real name : " << util::json::pretty(result["realname"]) << std::endl; 505 std::cout << "Real name : " << json_util::pretty(result["realname"]) << std::endl;
504 } 506 }
505 507
506 /* 508 /*
507 * ServerInviteCli. 509 * ServerInviteCli.
508 * ------------------------------------------------------------------ 510 * ------------------------------------------------------------------
908 json["servers"].push_back(pair.second); 910 json["servers"].push_back(pair.second);
909 } 911 }
910 912
911 // Index. 913 // Index.
912 if (result.count("-i") > 0) 914 if (result.count("-i") > 0)
913 json["index"] = util::to_number<unsigned>(result.find("-i")->second); 915 json["index"] = string_util::to_number<unsigned>(result.find("-i")->second);
914 if (result.count("--index") > 0) 916 if (result.count("--index") > 0)
915 json["index"] = util::to_number<unsigned>(result.find("--index")->second); 917 json["index"] = string_util::to_number<unsigned>(result.find("--index")->second);
916 918
917 // And action. 919 // And action.
918 if (copy[0] != "accept" && copy[0] != "drop") 920 if (copy[0] != "accept" && copy[0] != "drop")
919 throw std::runtime_error("invalid action '"s + copy[0] + "'"); 921 throw std::runtime_error("invalid action '"s + copy[0] + "'");
920 922
1012 if (pair.first == "-S" || pair.first == "--remove-server") 1014 if (pair.first == "-S" || pair.first == "--remove-server")
1013 json["remove-servers"].push_back(pair.second); 1015 json["remove-servers"].push_back(pair.second);
1014 } 1016 }
1015 1017
1016 // Index. 1018 // Index.
1017 json["index"] = util::to_number<unsigned>(copy[0]); 1019 json["index"] = string_util::to_number<unsigned>(copy[0]);
1018 1020
1019 check(request(irccdctl, json)); 1021 check(request(irccdctl, json));
1020 } 1022 }
1021 1023
1022 /* 1024 /*
1224 } 1226 }
1225 1227
1226 void onChannelMode(const nlohmann::json &v) 1228 void onChannelMode(const nlohmann::json &v)
1227 { 1229 {
1228 std::cout << "event: onChannelMode\n"; 1230 std::cout << "event: onChannelMode\n";
1229 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1231 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1230 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1232 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1231 std::cout << "mode: " << util::json::pretty(v, "mode") << "\n"; 1233 std::cout << "mode: " << json_util::pretty(v, "mode") << "\n";
1232 std::cout << "argument: " << util::json::pretty(v, "argument") << "\n"; 1234 std::cout << "argument: " << json_util::pretty(v, "argument") << "\n";
1233 } 1235 }
1234 1236
1235 void onChannelNotice(const nlohmann::json &v) 1237 void onChannelNotice(const nlohmann::json &v)
1236 { 1238 {
1237 std::cout << "event: onChannelNotice\n"; 1239 std::cout << "event: onChannelNotice\n";
1238 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1240 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1239 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1241 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1240 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1242 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1241 std::cout << "message: " << util::json::pretty(v, "message") << "\n"; 1243 std::cout << "message: " << json_util::pretty(v, "message") << "\n";
1242 } 1244 }
1243 1245
1244 void onConnect(const nlohmann::json &v) 1246 void onConnect(const nlohmann::json &v)
1245 { 1247 {
1246 std::cout << "event: onConnect\n"; 1248 std::cout << "event: onConnect\n";
1247 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1249 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1248 } 1250 }
1249 1251
1250 void onInvite(const nlohmann::json &v) 1252 void onInvite(const nlohmann::json &v)
1251 { 1253 {
1252 std::cout << "event: onInvite\n"; 1254 std::cout << "event: onInvite\n";
1253 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1255 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1254 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1256 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1255 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1257 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1256 } 1258 }
1257 1259
1258 void onJoin(const nlohmann::json &v) 1260 void onJoin(const nlohmann::json &v)
1259 { 1261 {
1260 std::cout << "event: onJoin\n"; 1262 std::cout << "event: onJoin\n";
1261 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1263 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1262 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1264 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1263 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1265 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1264 } 1266 }
1265 1267
1266 void onKick(const nlohmann::json &v) 1268 void onKick(const nlohmann::json &v)
1267 { 1269 {
1268 std::cout << "event: onKick\n"; 1270 std::cout << "event: onKick\n";
1269 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1271 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1270 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1272 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1271 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1273 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1272 std::cout << "target: " << util::json::pretty(v, "target") << "\n"; 1274 std::cout << "target: " << json_util::pretty(v, "target") << "\n";
1273 std::cout << "reason: " << util::json::pretty(v, "reason") << "\n"; 1275 std::cout << "reason: " << json_util::pretty(v, "reason") << "\n";
1274 } 1276 }
1275 1277
1276 void onMessage(const nlohmann::json &v) 1278 void onMessage(const nlohmann::json &v)
1277 { 1279 {
1278 std::cout << "event: onMessage\n"; 1280 std::cout << "event: onMessage\n";
1279 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1281 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1280 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1282 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1281 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1283 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1282 std::cout << "message: " << util::json::pretty(v, "message") << "\n"; 1284 std::cout << "message: " << json_util::pretty(v, "message") << "\n";
1283 } 1285 }
1284 1286
1285 void onMe(const nlohmann::json &v) 1287 void onMe(const nlohmann::json &v)
1286 { 1288 {
1287 std::cout << "event: onMe\n"; 1289 std::cout << "event: onMe\n";
1288 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1290 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1289 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1291 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1290 std::cout << "target: " << util::json::pretty(v, "target") << "\n"; 1292 std::cout << "target: " << json_util::pretty(v, "target") << "\n";
1291 std::cout << "message: " << util::json::pretty(v, "message") << "\n"; 1293 std::cout << "message: " << json_util::pretty(v, "message") << "\n";
1292 } 1294 }
1293 1295
1294 void onMode(const nlohmann::json &v) 1296 void onMode(const nlohmann::json &v)
1295 { 1297 {
1296 std::cout << "event: onMode\n"; 1298 std::cout << "event: onMode\n";
1297 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1299 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1298 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1300 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1299 std::cout << "mode: " << util::json::pretty(v, "mode") << "\n"; 1301 std::cout << "mode: " << json_util::pretty(v, "mode") << "\n";
1300 } 1302 }
1301 1303
1302 void onNames(const nlohmann::json &v) 1304 void onNames(const nlohmann::json &v)
1303 { 1305 {
1304 std::cout << "event: onNames\n"; 1306 std::cout << "event: onNames\n";
1305 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1307 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1306 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1308 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1307 std::cout << "names: " << util::json::pretty(v, "names") << "\n"; 1309 std::cout << "names: " << json_util::pretty(v, "names") << "\n";
1308 } 1310 }
1309 1311
1310 void onNick(const nlohmann::json &v) 1312 void onNick(const nlohmann::json &v)
1311 { 1313 {
1312 std::cout << "event: onNick\n"; 1314 std::cout << "event: onNick\n";
1313 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1315 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1314 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1316 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1315 std::cout << "nickname: " << util::json::pretty(v, "nickname") << "\n"; 1317 std::cout << "nickname: " << json_util::pretty(v, "nickname") << "\n";
1316 } 1318 }
1317 1319
1318 void onNotice(const nlohmann::json &v) 1320 void onNotice(const nlohmann::json &v)
1319 { 1321 {
1320 std::cout << "event: onNotice\n"; 1322 std::cout << "event: onNotice\n";
1321 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1323 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1322 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1324 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1323 std::cout << "message: " << util::json::pretty(v, "message") << "\n"; 1325 std::cout << "message: " << json_util::pretty(v, "message") << "\n";
1324 } 1326 }
1325 1327
1326 void onPart(const nlohmann::json &v) 1328 void onPart(const nlohmann::json &v)
1327 { 1329 {
1328 std::cout << "event: onPart\n"; 1330 std::cout << "event: onPart\n";
1329 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1331 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1330 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1332 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1331 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1333 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1332 std::cout << "reason: " << util::json::pretty(v, "reason") << "\n"; 1334 std::cout << "reason: " << json_util::pretty(v, "reason") << "\n";
1333 } 1335 }
1334 1336
1335 void onQuery(const nlohmann::json &v) 1337 void onQuery(const nlohmann::json &v)
1336 { 1338 {
1337 std::cout << "event: onQuery\n"; 1339 std::cout << "event: onQuery\n";
1338 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1340 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1339 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1341 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1340 std::cout << "message: " << util::json::pretty(v, "message") << "\n"; 1342 std::cout << "message: " << json_util::pretty(v, "message") << "\n";
1341 } 1343 }
1342 1344
1343 void onTopic(const nlohmann::json &v) 1345 void onTopic(const nlohmann::json &v)
1344 { 1346 {
1345 std::cout << "event: onTopic\n"; 1347 std::cout << "event: onTopic\n";
1346 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1348 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1347 std::cout << "origin: " << util::json::pretty(v, "origin") << "\n"; 1349 std::cout << "origin: " << json_util::pretty(v, "origin") << "\n";
1348 std::cout << "channel: " << util::json::pretty(v, "channel") << "\n"; 1350 std::cout << "channel: " << json_util::pretty(v, "channel") << "\n";
1349 std::cout << "topic: " << util::json::pretty(v, "topic") << "\n"; 1351 std::cout << "topic: " << json_util::pretty(v, "topic") << "\n";
1350 } 1352 }
1351 1353
1352 void onWhois(const nlohmann::json &v) 1354 void onWhois(const nlohmann::json &v)
1353 { 1355 {
1354 std::cout << "event: onWhois\n"; 1356 std::cout << "event: onWhois\n";
1355 std::cout << "server: " << util::json::pretty(v, "server") << "\n"; 1357 std::cout << "server: " << json_util::pretty(v, "server") << "\n";
1356 std::cout << "nickname: " << util::json::pretty(v, "nickname") << "\n"; 1358 std::cout << "nickname: " << json_util::pretty(v, "nickname") << "\n";
1357 std::cout << "username: " << util::json::pretty(v, "username") << "\n"; 1359 std::cout << "username: " << json_util::pretty(v, "username") << "\n";
1358 std::cout << "host: " << util::json::pretty(v, "host") << "\n"; 1360 std::cout << "host: " << json_util::pretty(v, "host") << "\n";
1359 std::cout << "realname: " << util::json::pretty(v, "realname") << "\n"; 1361 std::cout << "realname: " << json_util::pretty(v, "realname") << "\n";
1360 } 1362 }
1361 1363
1362 const std::unordered_map<std::string, std::function<void (const nlohmann::json &)>> events{ 1364 const std::unordered_map<std::string, std::function<void (const nlohmann::json &)>> events{
1363 { "onChannelMode", onChannelMode }, 1365 { "onChannelMode", onChannelMode },
1364 { "onChannelNotice", onChannelNotice }, 1366 { "onChannelNotice", onChannelNotice },
1423 } 1425 }
1424 }); 1426 });
1425 1427
1426 try { 1428 try {
1427 while (client.client().isConnected()) { 1429 while (client.client().isConnected()) {
1428 util::poller::poll(500, client); 1430 net_util::poll(500, client);
1429 } 1431 }
1430 } catch (const std::exception &ex) { 1432 } catch (const std::exception &ex) {
1431 log::warning() << ex.what() << std::endl; 1433 log::warning() << ex.what() << std::endl;
1432 } 1434 }
1433 1435