comparison irccdctl/rule_add_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
children 7cd7b2cdf923
comparison
equal deleted inserted replaced
527:a88796ed040a 528:9daccaeedcce
1 /*
2 * rule_add_cli.cpp -- implementation of irccdctl rule-add
3 *
4 * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
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
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <irccd/options.hpp>
20 #include <irccd/string_util.hpp>
21
22 #include "rule_add_cli.hpp"
23
24 namespace irccd {
25
26 namespace ctl {
27
28 std::string rule_add_cli::name() const
29 {
30 return "rule-add";
31 }
32
33 void rule_add_cli::exec(ctl::controller& ctl, const std::vector<std::string>& args)
34 {
35 static const option::options options{
36 { "-c", true },
37 { "--add-channel", true },
38 { "-e", true },
39 { "--add-event", true },
40 { "-i", true },
41 { "--index", true },
42 { "-p", true },
43 { "--add-plugin", true },
44 { "-s", true },
45 { "--add-server", true }
46 };
47
48 auto copy = args;
49 auto result = option::read(copy, options);
50
51 if (copy.size() < 1)
52 throw std::invalid_argument("rule-add requires at least 1 argument");
53
54 auto json = nlohmann::json::object({
55 { "command", "rule-add" },
56 { "channels", nlohmann::json::array() },
57 { "events", nlohmann::json::array() },
58 { "plugins", nlohmann::json::array() },
59 { "servers", nlohmann::json::array() }
60 });
61
62 // All sets.
63 for (const auto& pair : result) {
64 if (pair.first == "-c" || pair.first == "--add-channel")
65 json["channels"].push_back(pair.second);
66 if (pair.first == "-e" || pair.first == "--add-event")
67 json["events"].push_back(pair.second);
68 if (pair.first == "-p" || pair.first == "--add-plugin")
69 json["plugins"].push_back(pair.second);
70 if (pair.first == "-s" || pair.first == "--add-server")
71 json["servers"].push_back(pair.second);
72 }
73
74 // Index.
75 if (result.count("-i") > 0)
76 json["index"] = string_util::to_number<unsigned>(result.find("-i")->second);
77 if (result.count("--index") > 0)
78 json["index"] = string_util::to_number<unsigned>(result.find("--index")->second);
79
80 // And action.
81 if (copy[0] != "accept" && copy[0] != "drop")
82 throw std::runtime_error(string_util::sprintf("invalid action '%s'", copy[0]));
83
84 json["action"] = copy[0];
85
86 request(ctl, json);
87 }
88
89 } // !ctl
90
91 } // !irccd