Mercurial > irccd
comparison irccdctl/cli.cpp @ 752:252506d9839b
Irccdctl: merge all cli in cli.cpp|hpp
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 05 Aug 2018 11:47:00 +0200 |
parents | ad1ee47165fa |
children | c216d148558d |
comparison
equal
deleted
inserted
replaced
751:8876412ba633 | 752:252506d9839b |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 #include <iostream> | |
20 | |
19 #include <irccd/json_util.hpp> | 21 #include <irccd/json_util.hpp> |
20 #include <irccd/options.hpp> | 22 #include <irccd/options.hpp> |
21 #include <irccd/string_util.hpp> | 23 #include <irccd/string_util.hpp> |
22 | 24 |
23 #include <irccd/ctl/controller.hpp> | 25 #include <irccd/ctl/controller.hpp> |
24 | 26 |
27 #include <irccd/daemon/service/rule_service.hpp> | |
28 | |
25 #include "cli.hpp" | 29 #include "cli.hpp" |
26 | 30 |
27 namespace irccd { | 31 namespace irccd::ctl { |
28 | 32 |
29 namespace ctl { | 33 // {{{ helpers |
34 | |
35 namespace { | |
36 | |
37 auto format(std::vector<std::string> args) -> std::string | |
38 { | |
39 auto result = option::read(args, { | |
40 { "-f", true }, | |
41 { "--format", true } | |
42 }); | |
43 | |
44 if (result.count("-f") > 0) | |
45 return result.find("-f")->second; | |
46 if (result.count("--format") > 0) | |
47 return result.find("--format")->second; | |
48 | |
49 return "native"; | |
50 } | |
51 | |
52 void onConnect(const nlohmann::json &v) | |
53 { | |
54 std::cout << "event: onConnect\n"; | |
55 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
56 } | |
57 | |
58 void onInvite(const nlohmann::json &v) | |
59 { | |
60 std::cout << "event: onInvite\n"; | |
61 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
62 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
63 std::cout << "channel: " << json_util::pretty(v.value("channel", "(unknown)")) << "\n"; | |
64 } | |
65 | |
66 void onJoin(const nlohmann::json &v) | |
67 { | |
68 std::cout << "event: onJoin\n"; | |
69 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
70 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
71 std::cout << "channel: " << json_util::pretty(v.value("channel", "(unknown)")) << "\n"; | |
72 } | |
73 | |
74 void onKick(const nlohmann::json &v) | |
75 { | |
76 std::cout << "event: onKick\n"; | |
77 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
78 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
79 std::cout << "channel: " << json_util::pretty(v.value("channel", "(unknown)")) << "\n"; | |
80 std::cout << "target: " << json_util::pretty(v.value("target", "(unknown)")) << "\n"; | |
81 std::cout << "reason: " << json_util::pretty(v.value("reason", "(unknown)")) << "\n"; | |
82 } | |
83 | |
84 void onMessage(const nlohmann::json &v) | |
85 { | |
86 std::cout << "event: onMessage\n"; | |
87 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
88 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
89 std::cout << "channel: " << json_util::pretty(v.value("channel", "(unknown)")) << "\n"; | |
90 std::cout << "message: " << json_util::pretty(v.value("message", "(unknown)")) << "\n"; | |
91 } | |
92 | |
93 void onMe(const nlohmann::json &v) | |
94 { | |
95 std::cout << "event: onMe\n"; | |
96 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
97 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
98 std::cout << "target: " << json_util::pretty(v.value("target", "(unknown)")) << "\n"; | |
99 std::cout << "message: " << json_util::pretty(v.value("message", "(unknown)")) << "\n"; | |
100 } | |
101 | |
102 void onMode(const nlohmann::json &v) | |
103 { | |
104 std::cout << "event: onMode\n"; | |
105 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
106 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
107 std::cout << "mode: " << json_util::pretty(v.value("mode", "(unknown)")) << "\n"; | |
108 } | |
109 | |
110 void onNames(const nlohmann::json &v) | |
111 { | |
112 std::cout << "event: onNames\n"; | |
113 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
114 std::cout << "channel: " << json_util::pretty(v.value("channel", "(unknown)")) << "\n"; | |
115 std::cout << "names: " << json_util::pretty(v.value("names", "(unknown)")) << "\n"; | |
116 } | |
117 | |
118 void onNick(const nlohmann::json &v) | |
119 { | |
120 std::cout << "event: onNick\n"; | |
121 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
122 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
123 std::cout << "nickname: " << json_util::pretty(v.value("nickname", "(unknown)")) << "\n"; | |
124 } | |
125 | |
126 void onNotice(const nlohmann::json &v) | |
127 { | |
128 std::cout << "event: onNotice\n"; | |
129 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
130 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
131 std::cout << "message: " << json_util::pretty(v.value("message", "(unknown)")) << "\n"; | |
132 } | |
133 | |
134 void onPart(const nlohmann::json &v) | |
135 { | |
136 std::cout << "event: onPart\n"; | |
137 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
138 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
139 std::cout << "channel: " << json_util::pretty(v.value("channel", "(unknown)")) << "\n"; | |
140 std::cout << "reason: " << json_util::pretty(v.value("reason", "(unknown)")) << "\n"; | |
141 } | |
142 | |
143 void onTopic(const nlohmann::json &v) | |
144 { | |
145 std::cout << "event: onTopic\n"; | |
146 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
147 std::cout << "origin: " << json_util::pretty(v.value("origin", "(unknown)")) << "\n"; | |
148 std::cout << "channel: " << json_util::pretty(v.value("channel", "(unknown)")) << "\n"; | |
149 std::cout << "topic: " << json_util::pretty(v.value("topic", "(unknown)")) << "\n"; | |
150 } | |
151 | |
152 void onWhois(const nlohmann::json &v) | |
153 { | |
154 std::cout << "event: onWhois\n"; | |
155 std::cout << "server: " << json_util::pretty(v.value("server", "(unknown)")) << "\n"; | |
156 std::cout << "nickname: " << json_util::pretty(v.value("nickname", "(unknown)")) << "\n"; | |
157 std::cout << "username: " << json_util::pretty(v.value("username", "(unknown)")) << "\n"; | |
158 std::cout << "host: " << json_util::pretty(v.value("host", "(unknown)")) << "\n"; | |
159 std::cout << "realname: " << json_util::pretty(v.value("realname", "(unknown)")) << "\n"; | |
160 } | |
161 | |
162 const std::unordered_map<std::string_view, std::function<void (const nlohmann::json&)>> events{ | |
163 { "onConnect", onConnect }, | |
164 { "onInvite", onInvite }, | |
165 { "onJoin", onJoin }, | |
166 { "onKick", onKick }, | |
167 { "onMessage", onMessage }, | |
168 { "onMe", onMe }, | |
169 { "onMode", onMode }, | |
170 { "onNames", onNames }, | |
171 { "onNick", onNick }, | |
172 { "onNotice", onNotice }, | |
173 { "onPart", onPart }, | |
174 { "onTopic", onTopic }, | |
175 { "onWhois", onWhois } | |
176 }; | |
177 | |
178 void get_event(ctl::controller& ctl, std::string fmt) | |
179 { | |
180 ctl.read([&ctl, fmt] (auto code, auto message) { | |
181 if (code) | |
182 throw std::system_error(code); | |
183 | |
184 const auto event = json_util::document(message).get<std::string>("event"); | |
185 const auto it = events.find(event ? *event : ""); | |
186 | |
187 if (it != events.end()) { | |
188 if (fmt == "json") | |
189 std::cout << message.dump(4) << std::endl; | |
190 else { | |
191 it->second(message); | |
192 std::cout << std::endl; | |
193 } | |
194 } | |
195 | |
196 get_event(ctl, std::move(fmt)); | |
197 }); | |
198 } | |
199 | |
200 auto parse(std::vector<std::string> &args) -> option::result | |
201 { | |
202 option::options options{ | |
203 { "-c", true }, | |
204 { "--command", true }, | |
205 { "-n", true }, | |
206 { "--nickname", true }, | |
207 { "-r", true }, | |
208 { "--realname", true }, | |
209 { "-S", false }, | |
210 { "--ssl-verify", false }, | |
211 { "-s", false }, | |
212 { "--ssl", false }, | |
213 { "-u", true }, | |
214 { "--username", true } | |
215 }; | |
216 | |
217 return option::read(args, options); | |
218 } | |
219 | |
220 } // !namespace | |
221 | |
222 // }}} | |
223 | |
224 // {{{ cli | |
30 | 225 |
31 void cli::recv_response(ctl::controller& ctl, nlohmann::json req, handler_t handler) | 226 void cli::recv_response(ctl::controller& ctl, nlohmann::json req, handler_t handler) |
32 { | 227 { |
33 ctl.read([&ctl, req, handler, this] (auto code, auto message) { | 228 ctl.read([&ctl, req, handler, this] (auto code, auto message) { |
34 if (code) | 229 if (code) |
54 | 249 |
55 recv_response(ctl, std::move(req), std::move(handler)); | 250 recv_response(ctl, std::move(req), std::move(handler)); |
56 }); | 251 }); |
57 } | 252 } |
58 | 253 |
59 } // !cli | 254 // }}} |
60 | 255 |
61 } // !irccd | 256 // {{{ plugin_config_cli |
257 | |
258 void plugin_config_cli::set(ctl::controller& ctl, const std::vector<std::string>&args) | |
259 { | |
260 request(ctl, { | |
261 { "command", "plugin-config" }, | |
262 { "plugin", args[0] }, | |
263 { "variable", args[1] }, | |
264 { "value", args[2] } | |
265 }); | |
266 } | |
267 | |
268 void plugin_config_cli::get(ctl::controller& ctl, const std::vector<std::string>& args) | |
269 { | |
270 auto json = nlohmann::json::object({ | |
271 { "command", "plugin-config" }, | |
272 { "plugin", args[0] }, | |
273 { "variable", args[1] } | |
274 }); | |
275 | |
276 request(ctl, std::move(json), [args] (auto result) { | |
277 if (result["variables"].is_object()) | |
278 std::cout << json_util::pretty(result["variables"][args[1]]) << std::endl; | |
279 }); | |
280 } | |
281 | |
282 void plugin_config_cli::getall(ctl::controller& ctl, const std::vector<std::string> &args) | |
283 { | |
284 const auto json = nlohmann::json::object({ | |
285 { "command", "plugin-config" }, | |
286 { "plugin", args[0] } | |
287 }); | |
288 | |
289 request(ctl, json, [] (auto result) { | |
290 const auto variables = result["variables"]; | |
291 | |
292 for (auto v = variables.begin(); v != variables.end(); ++v) | |
293 std::cout << std::setw(16) << std::left << v.key() << " : " << json_util::pretty(v.value()) << std::endl; | |
294 }); | |
295 } | |
296 | |
297 auto plugin_config_cli::get_name() const noexcept -> std::string_view | |
298 { | |
299 return "plugin-config"; | |
300 } | |
301 | |
302 void plugin_config_cli::exec(ctl::controller& ctl, const std::vector<std::string> &args) | |
303 { | |
304 switch (args.size()) { | |
305 case 3: | |
306 set(ctl, args); | |
307 break; | |
308 case 2: | |
309 get(ctl, args); | |
310 break; | |
311 case 1: | |
312 getall(ctl, args); | |
313 break; | |
314 default: | |
315 throw std::invalid_argument("plugin-config requires at least 1 argument"); | |
316 } | |
317 } | |
318 | |
319 // }}} | |
320 | |
321 // {{{ plugin_info_cli | |
322 | |
323 auto plugin_info_cli::get_name() const noexcept -> std::string_view | |
324 { | |
325 return "plugin-info"; | |
326 } | |
327 | |
328 void plugin_info_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
329 { | |
330 if (args.size() < 1) | |
331 throw std::invalid_argument("plugin-info requires 1 argument"); | |
332 | |
333 const auto json = nlohmann::json::object({ | |
334 { "command", "plugin-info" }, | |
335 { "plugin", args[0] } | |
336 }); | |
337 | |
338 request(ctl, json, [] (auto result) { | |
339 const json_util::document doc(result); | |
340 | |
341 std::cout << std::boolalpha; | |
342 std::cout << "Author : " << | |
343 doc.get<std::string>("author").value_or("(unknown)") << std::endl; | |
344 std::cout << "License : " << | |
345 doc.get<std::string>("license").value_or("(unknown)") << std::endl; | |
346 std::cout << "Summary : " << | |
347 doc.get<std::string>("summary").value_or("(unknown)") << std::endl; | |
348 std::cout << "Version : " << | |
349 doc.get<std::string>("version").value_or("(unknown)") << std::endl; | |
350 }); | |
351 } | |
352 | |
353 // }}} | |
354 | |
355 // {{{ plugin_list_cli | |
356 | |
357 auto plugin_list_cli::get_name() const noexcept -> std::string_view | |
358 { | |
359 return "plugin-list"; | |
360 } | |
361 | |
362 void plugin_list_cli::exec(ctl::controller& ctl, const std::vector<std::string>&) | |
363 { | |
364 request(ctl, {{ "command", "plugin-list" }}, [] (auto result) { | |
365 for (const auto& value : result["list"]) | |
366 if (value.is_string()) | |
367 std::cout << value.template get<std::string>() << std::endl; | |
368 }); | |
369 } | |
370 | |
371 // }}} | |
372 | |
373 // {{{ plugin_load_cli | |
374 | |
375 auto plugin_load_cli::get_name() const noexcept -> std::string_view | |
376 { | |
377 return "plugin-load"; | |
378 } | |
379 | |
380 void plugin_load_cli::exec(ctl::controller& ctl, const std::vector<std::string> &args) | |
381 { | |
382 if (args.size() < 1) | |
383 throw std::invalid_argument("plugin-load requires 1 argument"); | |
384 | |
385 request(ctl, { | |
386 { "command", "plugin-load" }, | |
387 { "plugin", args[0] } | |
388 }); | |
389 } | |
390 | |
391 // }}} | |
392 | |
393 // {{{ plugin_reload_cli | |
394 | |
395 auto plugin_reload_cli::get_name() const noexcept -> std::string_view | |
396 { | |
397 return "plugin-reload"; | |
398 } | |
399 | |
400 void plugin_reload_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
401 { | |
402 if (args.size() < 1) | |
403 throw std::invalid_argument("plugin-reload requires 1 argument"); | |
404 | |
405 request(ctl, { | |
406 { "command", "plugin-reload" }, | |
407 { "plugin", args[0] } | |
408 }); | |
409 } | |
410 | |
411 // }}} | |
412 | |
413 // {{{ plugin_unload_cli | |
414 | |
415 auto plugin_unload_cli::get_name() const noexcept -> std::string_view | |
416 { | |
417 return "plugin-unload"; | |
418 } | |
419 | |
420 void plugin_unload_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
421 { | |
422 if (args.size() < 1) | |
423 throw std::invalid_argument("plugin-unload requires 1 argument"); | |
424 | |
425 request(ctl, { | |
426 { "command", "plugin-unload" }, | |
427 { "plugin", args[0] } | |
428 }); | |
429 } | |
430 | |
431 // }}} | |
432 | |
433 // {{{ rule_add_cli | |
434 | |
435 auto rule_add_cli::get_name() const noexcept -> std::string_view | |
436 { | |
437 return "rule-add"; | |
438 } | |
439 | |
440 void rule_add_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
441 { | |
442 static const option::options options{ | |
443 { "-c", true }, | |
444 { "--add-channel", true }, | |
445 { "-e", true }, | |
446 { "--add-event", true }, | |
447 { "-i", true }, | |
448 { "--index", true }, | |
449 { "-p", true }, | |
450 { "--add-plugin", true }, | |
451 { "-s", true }, | |
452 { "--add-server", true } | |
453 }; | |
454 | |
455 auto copy = args; | |
456 auto result = option::read(copy, options); | |
457 | |
458 if (copy.size() < 1) | |
459 throw std::invalid_argument("rule-add requires at least 1 argument"); | |
460 | |
461 auto json = nlohmann::json::object({ | |
462 { "command", "rule-add" }, | |
463 { "channels", nlohmann::json::array() }, | |
464 { "events", nlohmann::json::array() }, | |
465 { "plugins", nlohmann::json::array() }, | |
466 { "servers", nlohmann::json::array() } | |
467 }); | |
468 | |
469 // All sets. | |
470 for (const auto& pair : result) { | |
471 if (pair.first == "-c" || pair.first == "--add-channel") | |
472 json["channels"].push_back(pair.second); | |
473 if (pair.first == "-e" || pair.first == "--add-event") | |
474 json["events"].push_back(pair.second); | |
475 if (pair.first == "-p" || pair.first == "--add-plugin") | |
476 json["plugins"].push_back(pair.second); | |
477 if (pair.first == "-s" || pair.first == "--add-server") | |
478 json["servers"].push_back(pair.second); | |
479 } | |
480 | |
481 // Index. | |
482 std::optional<unsigned> index; | |
483 | |
484 if (result.count("-i") > 0 && !(index = string_util::to_uint(result.find("-i")->second))) | |
485 throw std::invalid_argument("invalid index argument"); | |
486 if (result.count("--index") > 0 && !(index = string_util::to_uint(result.find("--index")->second))) | |
487 throw std::invalid_argument("invalid index argument"); | |
488 | |
489 if (index) | |
490 json["index"] = *index; | |
491 | |
492 json["action"] = copy[0]; | |
493 | |
494 request(ctl, json); | |
495 } | |
496 | |
497 // }}} | |
498 | |
499 // {{{ rule_edit_cli | |
500 | |
501 auto rule_edit_cli::get_name() const noexcept -> std::string_view | |
502 { | |
503 return "rule-edit"; | |
504 } | |
505 | |
506 void rule_edit_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
507 { | |
508 static const option::options options{ | |
509 { "-a", true }, | |
510 { "--action", true }, | |
511 { "-c", true }, | |
512 { "--add-channel", true }, | |
513 { "-C", true }, | |
514 { "--remove-channel", true }, | |
515 { "-e", true }, | |
516 { "--add-event", true }, | |
517 { "-E", true }, | |
518 { "--remove-event", true }, | |
519 { "-p", true }, | |
520 { "--add-plugin", true }, | |
521 { "-P", true }, | |
522 { "--remove-plugin", true }, | |
523 { "-s", true }, | |
524 { "--add-server", true }, | |
525 { "-S", true }, | |
526 { "--remove-server", true }, | |
527 }; | |
528 | |
529 auto copy = args; | |
530 auto result = option::read(copy, options); | |
531 | |
532 if (copy.size() < 1) | |
533 throw std::invalid_argument("rule-edit requires at least 1 argument"); | |
534 | |
535 auto json = nlohmann::json::object({ | |
536 { "command", "rule-edit" }, | |
537 { "channels", nlohmann::json::array() }, | |
538 { "events", nlohmann::json::array() }, | |
539 { "plugins", nlohmann::json::array() }, | |
540 { "servers", nlohmann::json::array() } | |
541 }); | |
542 | |
543 for (const auto& pair : result) { | |
544 // Action. | |
545 if (pair.first == "-a" || pair.first == "--action") | |
546 json["action"] = pair.second; | |
547 | |
548 // Additions. | |
549 if (pair.first == "-c" || pair.first == "--add-channel") | |
550 json["add-channels"].push_back(pair.second); | |
551 if (pair.first == "-e" || pair.first == "--add-event") | |
552 json["add-events"].push_back(pair.second); | |
553 if (pair.first == "-p" || pair.first == "--add-plugin") | |
554 json["add-plugins"].push_back(pair.second); | |
555 if (pair.first == "-s" || pair.first == "--add-server") | |
556 json["add-servers"].push_back(pair.second); | |
557 | |
558 // Removals. | |
559 if (pair.first == "-C" || pair.first == "--remove-channel") | |
560 json["remove-channels"].push_back(pair.second); | |
561 if (pair.first == "-E" || pair.first == "--remove-event") | |
562 json["remove-events"].push_back(pair.second); | |
563 if (pair.first == "-P" || pair.first == "--remove-plugin") | |
564 json["remove-plugins"].push_back(pair.second); | |
565 if (pair.first == "-S" || pair.first == "--remove-server") | |
566 json["remove-servers"].push_back(pair.second); | |
567 } | |
568 | |
569 // Index. | |
570 const auto index = string_util::to_uint(copy[0]); | |
571 | |
572 if (!index) | |
573 throw rule_error(rule_error::invalid_index); | |
574 | |
575 json["index"] = *index; | |
576 | |
577 request(ctl, json); | |
578 } | |
579 | |
580 // }}} | |
581 | |
582 // {{{ rule_info_cli | |
583 | |
584 void rule_info_cli::print(const nlohmann::json& json, int index) | |
585 { | |
586 assert(json.is_object()); | |
587 | |
588 const auto unjoin = [] (auto array) { | |
589 std::ostringstream oss; | |
590 | |
591 for (auto it = array.begin(); it != array.end(); ++it) { | |
592 if (!it->is_string()) | |
593 continue; | |
594 | |
595 oss << it->template get<std::string>() << " "; | |
596 } | |
597 | |
598 return oss.str(); | |
599 }; | |
600 const auto unstr = [] (auto action) { | |
601 if (action.is_string() && action == "accept") | |
602 return "accept"; | |
603 else | |
604 return "drop"; | |
605 }; | |
606 | |
607 std::cout << "rule: " << index << std::endl; | |
608 std::cout << "servers: " << unjoin(json["servers"]) << std::endl; | |
609 std::cout << "channels: " << unjoin(json["channels"]) << std::endl; | |
610 std::cout << "plugins: " << unjoin(json["plugins"]) << std::endl; | |
611 std::cout << "events: " << unjoin(json["events"]) << std::endl; | |
612 std::cout << "action: " << unstr(json["action"]) << std::endl; | |
613 std::cout << std::endl; | |
614 } | |
615 | |
616 auto rule_info_cli::get_name() const noexcept -> std::string_view | |
617 { | |
618 return "rule-info"; | |
619 } | |
620 | |
621 void rule_info_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
622 { | |
623 if (args.size() < 1) | |
624 throw std::invalid_argument("rule-info requires 1 argument"); | |
625 | |
626 const auto index = string_util::to_int(args[0]); | |
627 | |
628 if (!index) | |
629 throw rule_error(rule_error::invalid_index); | |
630 | |
631 const auto json = nlohmann::json::object({ | |
632 { "command", "rule-info" }, | |
633 { "index", *index } | |
634 }); | |
635 | |
636 request(ctl, json, [index] (auto result) { | |
637 print(result, *index); | |
638 }); | |
639 } | |
640 | |
641 // }}} | |
642 | |
643 // {{{ rule_list_cli | |
644 | |
645 auto rule_list_cli::get_name() const noexcept -> std::string_view | |
646 { | |
647 return "rule-list"; | |
648 } | |
649 | |
650 void rule_list_cli::exec(ctl::controller& ctl, const std::vector<std::string>&) | |
651 { | |
652 request(ctl, {{ "command", "rule-list" }}, [] (auto result) { | |
653 auto pos = 0; | |
654 | |
655 for (const auto& obj : result["list"]) { | |
656 if (obj.is_object()) | |
657 rule_info_cli::print(obj, pos++); | |
658 } | |
659 }); | |
660 } | |
661 | |
662 // }}} | |
663 | |
664 // {{{ rule_move_cli | |
665 | |
666 auto rule_move_cli::get_name() const noexcept -> std::string_view | |
667 { | |
668 return "rule-move"; | |
669 } | |
670 | |
671 void rule_move_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
672 { | |
673 if (args.size() < 2) | |
674 throw std::invalid_argument("rule-move requires 2 arguments"); | |
675 | |
676 const auto from = string_util::to_int<int>(args[0]); | |
677 const auto to = string_util::to_int<int>(args[1]); | |
678 | |
679 if (!from) | |
680 throw rule_error(rule_error::invalid_index); | |
681 if (!to) | |
682 throw rule_error(rule_error::invalid_index); | |
683 | |
684 request(ctl, { | |
685 { "command", "rule-move" }, | |
686 { "from", *from }, | |
687 { "to", *to } | |
688 }); | |
689 } | |
690 | |
691 // }}} | |
692 | |
693 // {{{ rule_remove_cli | |
694 | |
695 auto rule_remove_cli::get_name() const noexcept -> std::string_view | |
696 { | |
697 return "rule-remove"; | |
698 } | |
699 | |
700 void rule_remove_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
701 { | |
702 if (args.size() < 1) | |
703 throw std::invalid_argument("rule-remove requires 1 argument"); | |
704 | |
705 const auto index = string_util::to_int(args[0]); | |
706 | |
707 if (!index) | |
708 throw rule_error(rule_error::invalid_index); | |
709 | |
710 request(ctl, { | |
711 { "command", "rule-remove" }, | |
712 { "index", *index } | |
713 }); | |
714 } | |
715 | |
716 // }}} | |
717 | |
718 // {{{ server_connect_cli | |
719 | |
720 auto server_connect_cli::get_name() const noexcept -> std::string_view | |
721 { | |
722 return "server-connect"; | |
723 } | |
724 | |
725 void server_connect_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
726 { | |
727 std::vector<std::string> copy(args); | |
728 | |
729 option::result result = parse(copy); | |
730 option::result::const_iterator it; | |
731 | |
732 if (copy.size() < 2) | |
733 throw std::invalid_argument("server-connect requires at least 2 arguments"); | |
734 | |
735 auto object = nlohmann::json::object({ | |
736 { "command", "server-connect" }, | |
737 { "name", copy[0] }, | |
738 { "host", copy[1] } | |
739 }); | |
740 | |
741 if (copy.size() == 3) { | |
742 const auto port = string_util::to_int(copy[2]); | |
743 | |
744 if (!port) | |
745 throw std::invalid_argument("invalid port given"); | |
746 | |
747 object["port"] = *port; | |
748 } | |
749 | |
750 if (result.count("-S") > 0 || result.count("--ssl-verify") > 0) | |
751 object["sslVerify"] = true; | |
752 if (result.count("-s") > 0 || result.count("--ssl") > 0) | |
753 object["ssl"] = true; | |
754 if ((it = result.find("-n")) != result.end() || (it = result.find("--nickname")) != result.end()) | |
755 object["nickname"] = it->second; | |
756 if ((it = result.find("-r")) != result.end() || (it = result.find("--realname")) != result.end()) | |
757 object["realname"] = it->second; | |
758 if ((it = result.find("-u")) != result.end() || (it = result.find("--username")) != result.end()) | |
759 object["username"] = it->second; | |
760 | |
761 request(ctl, object); | |
762 } | |
763 | |
764 // }}} | |
765 | |
766 // {{{ server_disconnect_cli | |
767 | |
768 auto server_disconnect_cli::get_name() const noexcept -> std::string_view | |
769 { | |
770 return "server-disconnect"; | |
771 } | |
772 | |
773 void server_disconnect_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
774 { | |
775 auto object = nlohmann::json::object({ | |
776 { "command", "server-disconnect" } | |
777 }); | |
778 | |
779 if (args.size() > 0) | |
780 object["server"] = args[0]; | |
781 | |
782 request(ctl, object); | |
783 } | |
784 | |
785 // }}} | |
786 | |
787 // {{{ server_info_cli | |
788 | |
789 auto server_info_cli::get_name() const noexcept -> std::string_view | |
790 { | |
791 return "server-info"; | |
792 } | |
793 | |
794 void server_info_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
795 { | |
796 if (args.size() < 1) | |
797 throw std::invalid_argument("server-info requires 1 argument"); | |
798 | |
799 auto json = nlohmann::json::object({ | |
800 { "command", "server-info" }, | |
801 { "server", args[0] } | |
802 }); | |
803 | |
804 request(ctl, std::move(json), [] (auto result) { | |
805 std::cout << std::boolalpha; | |
806 std::cout << "Name : " << json_util::pretty(result["name"]) << std::endl; | |
807 std::cout << "Host : " << json_util::pretty(result["host"]) << std::endl; | |
808 std::cout << "Port : " << json_util::pretty(result["port"]) << std::endl; | |
809 std::cout << "Ipv6 : " << json_util::pretty(result["ipv6"]) << std::endl; | |
810 std::cout << "SSL : " << json_util::pretty(result["ssl"]) << std::endl; | |
811 std::cout << "SSL verified : " << json_util::pretty(result["sslVerify"]) << std::endl; | |
812 std::cout << "Channels : "; | |
813 | |
814 for (const auto& v : result["channels"]) | |
815 if (v.is_string()) | |
816 std::cout << v.template get<std::string>() << " "; | |
817 | |
818 std::cout << std::endl; | |
819 | |
820 std::cout << "Nickname : " << json_util::pretty(result["nickname"]) << std::endl; | |
821 std::cout << "User name : " << json_util::pretty(result["username"]) << std::endl; | |
822 std::cout << "Real name : " << json_util::pretty(result["realname"]) << std::endl; | |
823 }); | |
824 } | |
825 | |
826 // }}} | |
827 | |
828 // {{{ server_invite_cli | |
829 | |
830 auto server_invite_cli::get_name() const noexcept -> std::string_view | |
831 { | |
832 return "server-invite"; | |
833 } | |
834 | |
835 void server_invite_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
836 { | |
837 if (args.size() < 3) | |
838 throw std::invalid_argument("server-invite requires 3 arguments"); | |
839 | |
840 request(ctl, { | |
841 { "command", "server-invite" }, | |
842 { "server", args[0] }, | |
843 { "target", args[1] }, | |
844 { "channel", args[2] } | |
845 }); | |
846 } | |
847 | |
848 // }}} | |
849 | |
850 // {{{ server_join_cli | |
851 | |
852 auto server_join_cli::get_name() const noexcept -> std::string_view | |
853 { | |
854 return "server-join"; | |
855 } | |
856 | |
857 void server_join_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
858 { | |
859 if (args.size() < 2) | |
860 throw std::invalid_argument("server-join requires at least 2 arguments"); | |
861 | |
862 auto object = nlohmann::json::object({ | |
863 { "command", "server-join" }, | |
864 { "server", args[0] }, | |
865 { "channel", args[1] } | |
866 }); | |
867 | |
868 if (args.size() == 3) | |
869 object["password"] = args[2]; | |
870 | |
871 request(ctl, object); | |
872 } | |
873 | |
874 // }}} | |
875 | |
876 // {{{ server_kick_cli | |
877 | |
878 auto server_kick_cli::get_name() const noexcept -> std::string_view | |
879 { | |
880 return "server-kick"; | |
881 } | |
882 | |
883 void server_kick_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
884 { | |
885 if (args.size() < 3) | |
886 throw std::invalid_argument("server-kick requires at least 3 arguments "); | |
887 | |
888 auto object = nlohmann::json::object({ | |
889 { "command", "server-kick" }, | |
890 { "server", args[0] }, | |
891 { "target", args[1] }, | |
892 { "channel", args[2] } | |
893 }); | |
894 | |
895 if (args.size() == 4) | |
896 object["reason"] = args[3]; | |
897 | |
898 request(ctl, object); | |
899 } | |
900 | |
901 // }}} | |
902 | |
903 // {{{ server_list_cli | |
904 | |
905 auto server_list_cli::get_name() const noexcept -> std::string_view | |
906 { | |
907 return "server-list"; | |
908 } | |
909 | |
910 void server_list_cli::exec(ctl::controller& ctl, const std::vector<std::string>&) | |
911 { | |
912 request(ctl, {{ "command", "server-list" }}, [] (auto result) { | |
913 for (const auto& n : result["list"]) | |
914 if (n.is_string()) | |
915 std::cout << n.template get<std::string>() << std::endl; | |
916 }); | |
917 } | |
918 | |
919 // }}} | |
920 | |
921 // {{{ server_me_cli | |
922 | |
923 auto server_me_cli::get_name() const noexcept -> std::string_view | |
924 { | |
925 return "server-me"; | |
926 } | |
927 | |
928 void server_me_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
929 { | |
930 if (args.size() < 3) | |
931 throw std::runtime_error("server-me requires 3 arguments"); | |
932 | |
933 request(ctl, { | |
934 { "command", "server-me" }, | |
935 { "server", args[0] }, | |
936 { "target", args[1] }, | |
937 { "message", args[2] } | |
938 }); | |
939 } | |
940 | |
941 // }}} | |
942 | |
943 // {{{ server_message_cli | |
944 | |
945 auto server_message_cli::get_name() const noexcept -> std::string_view | |
946 { | |
947 return "server-message"; | |
948 } | |
949 | |
950 void server_message_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
951 { | |
952 if (args.size() < 3) | |
953 throw std::invalid_argument("server-message requires 3 arguments"); | |
954 | |
955 request(ctl, { | |
956 { "command", "server-message" }, | |
957 { "server", args[0] }, | |
958 { "target", args[1] }, | |
959 { "message", args[2] } | |
960 }); | |
961 } | |
962 | |
963 // }}} | |
964 | |
965 // {{{ server_mode_cli | |
966 | |
967 auto server_mode_cli::get_name() const noexcept -> std::string_view | |
968 { | |
969 return "server-mode"; | |
970 } | |
971 | |
972 void server_mode_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
973 { | |
974 if (args.size() < 2) | |
975 throw std::invalid_argument("server-mode requires at least 3 arguments"); | |
976 | |
977 auto json = nlohmann::json({ | |
978 { "command", "server-mode" }, | |
979 { "server", args[0] }, | |
980 { "channel", args[1] }, | |
981 { "mode", args[2] } | |
982 }); | |
983 | |
984 if (args.size() >= 4) | |
985 json["limit"] = args[3]; | |
986 if (args.size() >= 5) | |
987 json["user"] = args[4]; | |
988 if (args.size() >= 6) | |
989 json["mask"] = args[5]; | |
990 | |
991 request(ctl, std::move(json)); | |
992 } | |
993 | |
994 // }}} | |
995 | |
996 // {{{ server_nick_cli | |
997 | |
998 auto server_nick_cli::get_name() const noexcept -> std::string_view | |
999 { | |
1000 return "server-nick"; | |
1001 } | |
1002 | |
1003 void server_nick_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
1004 { | |
1005 if (args.size() < 2) | |
1006 throw std::invalid_argument("server-nick requires 2 arguments"); | |
1007 | |
1008 request(ctl, { | |
1009 { "command", "server-nick" }, | |
1010 { "server", args[0] }, | |
1011 { "nickname", args[1] } | |
1012 }); | |
1013 } | |
1014 | |
1015 // }}} | |
1016 | |
1017 // {{{ server_notice_cli | |
1018 | |
1019 auto server_notice_cli::get_name() const noexcept -> std::string_view | |
1020 { | |
1021 return "server-notice"; | |
1022 } | |
1023 | |
1024 void server_notice_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
1025 { | |
1026 if (args.size() < 3) | |
1027 throw std::invalid_argument("server-notice requires 3 arguments"); | |
1028 | |
1029 request(ctl, { | |
1030 { "command", "server-notice" }, | |
1031 { "server", args[0] }, | |
1032 { "target", args[1] }, | |
1033 { "message", args[2] } | |
1034 }); | |
1035 } | |
1036 | |
1037 // }}} | |
1038 | |
1039 // {{{ server_part_cli | |
1040 | |
1041 auto server_part_cli::get_name() const noexcept -> std::string_view | |
1042 { | |
1043 return "server-part"; | |
1044 } | |
1045 | |
1046 void server_part_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
1047 { | |
1048 if (args.size() < 2) | |
1049 throw std::invalid_argument("server-part requires at least 2 arguments"); | |
1050 | |
1051 auto object = nlohmann::json::object({ | |
1052 { "command", "server-part" }, | |
1053 { "server", args[0] }, | |
1054 { "channel", args[1] } | |
1055 }); | |
1056 | |
1057 if (args.size() >= 3) | |
1058 object["reason"] = args[2]; | |
1059 | |
1060 request(ctl, object); | |
1061 } | |
1062 | |
1063 // }}} | |
1064 | |
1065 // {{{ server_reconnect_cli | |
1066 | |
1067 auto server_reconnect_cli::get_name() const noexcept -> std::string_view | |
1068 { | |
1069 return "server-reconnect"; | |
1070 } | |
1071 | |
1072 void server_reconnect_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
1073 { | |
1074 auto object = nlohmann::json::object({ | |
1075 { "command", "server-reconnect" } | |
1076 }); | |
1077 | |
1078 if (args.size() >= 1) | |
1079 object["server"] = args[0]; | |
1080 | |
1081 request(ctl, object); | |
1082 } | |
1083 | |
1084 // }}} | |
1085 | |
1086 // {{{ server_topic_cli | |
1087 | |
1088 auto server_topic_cli::get_name() const noexcept -> std::string_view | |
1089 { | |
1090 return "server-topic"; | |
1091 } | |
1092 | |
1093 void server_topic_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
1094 { | |
1095 if (args.size() < 3) | |
1096 throw std::invalid_argument("server-topic requires 3 arguments"); | |
1097 | |
1098 request(ctl, { | |
1099 { "command", "server-topic" }, | |
1100 { "server", args[0] }, | |
1101 { "channel", args[1] }, | |
1102 { "topic", args[2] } | |
1103 }); | |
1104 } | |
1105 | |
1106 // }}} | |
1107 | |
1108 // {{{ watch_cli | |
1109 | |
1110 auto watch_cli::get_name() const noexcept -> std::string_view | |
1111 { | |
1112 return "watch"; | |
1113 } | |
1114 | |
1115 void watch_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args) | |
1116 { | |
1117 auto fmt = format(args); | |
1118 | |
1119 if (fmt != "native" && fmt != "json") | |
1120 throw std::invalid_argument("invalid format given: " + fmt); | |
1121 | |
1122 get_event(ctl, fmt); | |
1123 } | |
1124 | |
1125 // }}} | |
1126 | |
1127 } // !irccd::ctl |