comparison irccdctl/cli.cpp @ 528:9daccaeedcce

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