comparison libirccd-daemon/irccd/daemon/rule_service.cpp @ 809:8460b4a34191

misc: reorganize namespaces, closes #952 @4h
author David Demelier <markand@malikania.fr>
date Fri, 16 Nov 2018 12:25:00 +0100
parents libirccd/irccd/daemon/rule_service.cpp@292482f36454
children d0fc4f14678d
comparison
equal deleted inserted replaced
808:80bccab4a093 809:8460b4a34191
1 /*
2 * rule_service.cpp -- rule service
3 *
4 * Copyright (c) 2013-2018 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 <stdexcept>
20
21 #include <irccd/config.hpp>
22 #include <irccd/string_util.hpp>
23
24 #include "bot.hpp"
25 #include "logger.hpp"
26 #include "rule_service.hpp"
27 #include "rule_util.hpp"
28
29 namespace irccd::daemon {
30
31 rule_service::rule_service(bot& bot)
32 : bot_(bot)
33 {
34 }
35
36 auto rule_service::list() const noexcept -> const std::vector<rule>&
37 {
38 return rules_;
39 }
40
41 void rule_service::add(rule rule)
42 {
43 rules_.push_back(std::move(rule));
44 }
45
46 void rule_service::insert(rule rule, unsigned position)
47 {
48 assert(position <= rules_.size());
49
50 rules_.insert(rules_.begin() + position, std::move(rule));
51 }
52
53 void rule_service::remove(unsigned position)
54 {
55 assert(position < rules_.size());
56
57 rules_.erase(rules_.begin() + position);
58 }
59
60 auto rule_service::require(unsigned position) const -> const rule&
61 {
62 if (position >= rules_.size())
63 throw rule_error(rule_error::invalid_index);
64
65 return rules_[position];
66 }
67
68 auto rule_service::require(unsigned position) -> rule&
69 {
70 if (position >= rules_.size())
71 throw rule_error(rule_error::invalid_index);
72
73 return rules_[position];
74 }
75
76 auto rule_service::solve(std::string_view server,
77 std::string_view channel,
78 std::string_view origin,
79 std::string_view plugin,
80 std::string_view event) noexcept -> bool
81 {
82 bool result = true;
83
84 for (const auto& rule : rules_)
85 if (rule.match(server, channel, origin, plugin, event))
86 result = rule.action == rule::action_type::accept;
87
88 return result;
89 }
90
91 void rule_service::load(const config& cfg) noexcept
92 {
93 rules_.clear();
94
95 for (const auto& section : cfg) {
96 if (section.get_key() != "rule")
97 continue;
98
99 try {
100 rules_.push_back(rule_util::from_config(section));
101 } catch (const std::exception& ex) {
102 bot_.get_log().warning("rule", "") << ex.what() << std::endl;
103 }
104 }
105 }
106
107 namespace logger {
108
109 auto loggable_traits<rule>::get_category(const rule&) -> std::string_view
110 {
111 return "rule";
112 }
113
114 auto loggable_traits<rule>::get_component(const rule&) -> std::string_view
115 {
116 return "";
117 }
118
119 } // !logger
120
121 } // !irccd::daemon