comparison irccdctl/cli.cpp @ 448:9be4f8a5cf1a

Irccdctl: implement rule-list
author David Demelier <markand@malikania.fr>
date Wed, 05 Jul 2017 18:20:26 +0200
parents 35c40ac0dc26
children 9968eac538e6
comparison
equal deleted inserted replaced
446:f3c27790d0d1 448:9be4f8a5cf1a
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 <cassert>
19 #include <iostream> 20 #include <iostream>
21 #include <sstream>
20 22
21 #include <json.hpp> 23 #include <json.hpp>
22 24
23 #include "cli.hpp" 25 #include "cli.hpp"
24 #include "elapsed-timer.hpp" 26 #include "elapsed-timer.hpp"
835 check(request(irccdctl, { 837 check(request(irccdctl, {
836 { "server", args[0] }, 838 { "server", args[0] },
837 { "channel", args[1] }, 839 { "channel", args[1] },
838 { "topic", args[2] } 840 { "topic", args[2] }
839 })); 841 }));
842 }
843
844 /*
845 * RuleListCli.
846 * ------------------------------------------------------------------
847 */
848
849 namespace {
850
851 void showRule(const nlohmann::json& json, int index)
852 {
853 assert(json.is_object());
854
855 auto unjoin = [] (auto array) {
856 std::ostringstream oss;
857
858 for (auto it = array.begin(); it != array.end(); ++it) {
859 if (!it->is_string())
860 continue;
861
862 oss << it->template get<std::string>() << " ";
863 }
864
865 return oss.str();
866 };
867 auto unstr = [] (auto action) {
868 if (action.is_string() && action == "accept")
869 return "accept";
870 else
871 return "drop";
872 };
873
874 std::cout << "rule: " << index << std::endl;
875 std::cout << "servers: " << unjoin(json["servers"]) << std::endl;
876 std::cout << "channels: " << unjoin(json["channels"]) << std::endl;
877 std::cout << "plugins: " << unjoin(json["plugins"]) << std::endl;
878 std::cout << "events: " << unjoin(json["events"]) << std::endl;
879 std::cout << "action: " << unstr(json["action"]) << std::endl;
880 std::cout << std::endl;
881 }
882
883 } // !namespace
884
885 RuleListCli::RuleListCli()
886 : Cli("rule-list",
887 "list all rules",
888 "rule-list",
889 "List all rules.\n\n"
890 "Example:\n"
891 "\tirccdctl rule-list")
892 {
893 }
894
895 void RuleListCli::exec(Irccdctl &irccdctl, const std::vector<std::string> &)
896 {
897 auto response = request(irccdctl);
898 auto pos = 0;
899
900 check(response);
901
902 for (const auto &obj : response["list"]) {
903 if (!obj.is_object())
904 continue;
905
906 showRule(obj, pos++);
907 }
840 } 908 }
841 909
842 /* 910 /*
843 * WatchCli. 911 * WatchCli.
844 * ------------------------------------------------------------------ 912 * ------------------------------------------------------------------