comparison irccdctl/cli.cpp @ 453:acb2d4990249

Irccdctl: implement rule-edit
author David Demelier <markand@malikania.fr>
date Thu, 20 Jul 2017 22:55:02 +0200
parents 2170aa0e38aa
children 7e273b7f4f92
comparison
equal deleted inserted replaced
452:2170aa0e38aa 453:acb2d4990249
916 // And action. 916 // And action.
917 if (copy[0] != "accept" && copy[0] != "drop") 917 if (copy[0] != "accept" && copy[0] != "drop")
918 throw std::runtime_error("invalid action '"s + copy[0] + "'"); 918 throw std::runtime_error("invalid action '"s + copy[0] + "'");
919 919
920 json["action"] = copy[0]; 920 json["action"] = copy[0];
921
922 check(request(irccdctl, json));
923 }
924
925 /*
926 * RuleEditCli.
927 * ------------------------------------------------------------------
928 */
929
930 RuleEditCli::RuleEditCli()
931 : Cli("rule-edit",
932 "edit an existing rule",
933 "rule-edit [options] index",
934 "Edit an existing rule in irccd.\n\n"
935 "All options can be specified multiple times.\n\n"
936 "Available options:\n"
937 " -a, --action\t\t\tset action\n"
938 " -c, --add-channel\t\tmatch a channel\n"
939 " -C, --remove-channel\t\tremove a channel\n"
940 " -e, --add-event\t\tmatch an event\n"
941 " -E, --remove-event\t\tremove an event\n"
942 " -p, --add-plugin\t\tmatch a plugin\n"
943 " -P, --add-plugin\t\tremove a plugin\n"
944 " -s, --add-server\t\tmatch a server\n"
945 " -S, --remove-server\t\tremove a server\n\n"
946 "Example:\n"
947 "\tirccdctl rule-edit -p hangman 0\n"
948 "\tirccdctl rule-edit -S localhost -c #games -p hangman 1")
949 {
950 }
951
952 void RuleEditCli::exec(Irccdctl &irccdctl, const std::vector<std::string> &args)
953 {
954 static const option::Options options{
955 { "-a", true },
956 { "--action", true },
957 { "-c", true },
958 { "--add-channel", true },
959 { "-C", true },
960 { "--remove-channel", true },
961 { "-e", true },
962 { "--add-event", true },
963 { "-E", true },
964 { "--remove-event", true },
965 { "-p", true },
966 { "--add-plugin", true },
967 { "-P", true },
968 { "--remove-plugin", true },
969 { "-s", true },
970 { "--add-server", true },
971 { "-S", true },
972 { "--remove-server", true },
973 };
974
975 auto copy = args;
976 auto result = option::read(copy, options);
977
978 if (copy.size() < 1)
979 throw std::invalid_argument("rule-edit requires at least 1 argument");
980
981 auto json = nlohmann::json::object({
982 { "command", "rule-edit" },
983 { "channels", nlohmann::json::array() },
984 { "events", nlohmann::json::array() },
985 { "plugins", nlohmann::json::array() },
986 { "servers", nlohmann::json::array() }
987 });
988
989 for (const auto& pair : result) {
990 // Action.
991 if (pair.first == "-a" || pair.first == "--action")
992 json["action"] = pair.second;
993
994 // Additions.
995 if (pair.first == "-c" || pair.first == "--add-channel")
996 json["add-channels"].push_back(pair.second);
997 if (pair.first == "-e" || pair.first == "--add-event")
998 json["add-events"].push_back(pair.second);
999 if (pair.first == "-p" || pair.first == "--add-plugin")
1000 json["add-plugins"].push_back(pair.second);
1001 if (pair.first == "-s" || pair.first == "--add-server")
1002 json["add-servers"].push_back(pair.second);
1003
1004 // Removals.
1005 if (pair.first == "-C" || pair.first == "--remove-channel")
1006 json["remove-channels"].push_back(pair.second);
1007 if (pair.first == "-E" || pair.first == "--remove-event")
1008 json["remove-events"].push_back(pair.second);
1009 if (pair.first == "-P" || pair.first == "--remove-plugin")
1010 json["remove-plugins"].push_back(pair.second);
1011 if (pair.first == "-S" || pair.first == "--remove-server")
1012 json["remove-servers"].push_back(pair.second);
1013 }
1014
1015 // Index.
1016 json["index"] = util::toNumber<unsigned>(copy[0]);
921 1017
922 check(request(irccdctl, json)); 1018 check(request(irccdctl, json));
923 } 1019 }
924 1020
925 /* 1021 /*