Mercurial > irccd
comparison irccdctl/cli.cpp @ 452:2170aa0e38aa
Irccdctl: implement rule-add
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 20 Jul 2017 22:56:34 +0200 |
parents | 1fdedd2977d2 |
children | acb2d4990249 |
comparison
equal
deleted
inserted
replaced
451:1fdedd2977d2 | 452:2170aa0e38aa |
---|---|
27 #include "irccdctl.hpp" | 27 #include "irccdctl.hpp" |
28 #include "logger.hpp" | 28 #include "logger.hpp" |
29 #include "options.hpp" | 29 #include "options.hpp" |
30 #include "util.hpp" | 30 #include "util.hpp" |
31 | 31 |
32 using namespace std::string_literals; | |
33 | |
32 namespace irccd { | 34 namespace irccd { |
33 | 35 |
34 /* | 36 /* |
35 * Cli. | 37 * Cli. |
36 * ------------------------------------------------------------------ | 38 * ------------------------------------------------------------------ |
837 check(request(irccdctl, { | 839 check(request(irccdctl, { |
838 { "server", args[0] }, | 840 { "server", args[0] }, |
839 { "channel", args[1] }, | 841 { "channel", args[1] }, |
840 { "topic", args[2] } | 842 { "topic", args[2] } |
841 })); | 843 })); |
844 } | |
845 | |
846 /* | |
847 * RuleAddCli. | |
848 * ------------------------------------------------------------------ | |
849 */ | |
850 | |
851 RuleAddCli::RuleAddCli() | |
852 : Cli("rule-add", | |
853 "add a new rule", | |
854 "rule-add [options] accept|drop", | |
855 "Add a new rule to irccd.\n\n" | |
856 "If no index is specified, the rule is added to the end.\n\n" | |
857 "Available options:\n" | |
858 " -c, --add-channel\t\tmatch a channel\n" | |
859 " -e, --add-event\t\tmatch an event\n" | |
860 " -i, --index\t\t\trule position\n" | |
861 " -p, --add-plugin\t\tmatch a plugin\n" | |
862 " -s, --add-server\t\tmatch a server\n\n" | |
863 "Example:\n" | |
864 "\tirccdctl rule-add -p hangman drop\n" | |
865 "\tirccdctl rule-add -s localhost -c #games -p hangman accept") | |
866 { | |
867 } | |
868 | |
869 void RuleAddCli::exec(Irccdctl &irccdctl, const std::vector<std::string> &args) | |
870 { | |
871 static const option::Options options{ | |
872 { "-c", true }, | |
873 { "--add-channel", true }, | |
874 { "-e", true }, | |
875 { "--add-event", true }, | |
876 { "-i", true }, | |
877 { "--index", true }, | |
878 { "-p", true }, | |
879 { "--add-plugin", true }, | |
880 { "-s", true }, | |
881 { "--add-server", true } | |
882 }; | |
883 | |
884 auto copy = args; | |
885 auto result = option::read(copy, options); | |
886 | |
887 if (copy.size() < 1) | |
888 throw std::invalid_argument("rule-add requires at least 1 argument"); | |
889 | |
890 auto json = nlohmann::json::object({ | |
891 { "command", "rule-add" }, | |
892 { "channels", nlohmann::json::array() }, | |
893 { "events", nlohmann::json::array() }, | |
894 { "plugins", nlohmann::json::array() }, | |
895 { "servers", nlohmann::json::array() } | |
896 }); | |
897 | |
898 // All sets. | |
899 for (const auto& pair : result) { | |
900 if (pair.first == "-c" || pair.first == "--add-channel") | |
901 json["channels"].push_back(pair.second); | |
902 if (pair.first == "-e" || pair.first == "--add-event") | |
903 json["events"].push_back(pair.second); | |
904 if (pair.first == "-p" || pair.first == "--add-plugin") | |
905 json["plugins"].push_back(pair.second); | |
906 if (pair.first == "-s" || pair.first == "--add-server") | |
907 json["servers"].push_back(pair.second); | |
908 } | |
909 | |
910 // Index. | |
911 if (result.count("-i") > 0) | |
912 json["index"] = util::toNumber<unsigned>(result.find("-i")->second); | |
913 if (result.count("--index") > 0) | |
914 json["index"] = util::toNumber<unsigned>(result.find("--index")->second); | |
915 | |
916 // And action. | |
917 if (copy[0] != "accept" && copy[0] != "drop") | |
918 throw std::runtime_error("invalid action '"s + copy[0] + "'"); | |
919 | |
920 json["action"] = copy[0]; | |
921 | |
922 check(request(irccdctl, json)); | |
842 } | 923 } |
843 | 924 |
844 /* | 925 /* |
845 * RuleListCli. | 926 * RuleListCli. |
846 * ------------------------------------------------------------------ | 927 * ------------------------------------------------------------------ |