]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customprefix.cpp
Clean up the configuration reading in m_customprefix.
[user/henk/code/inspircd.git] / src / modules / m_customprefix.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21
22 class CustomPrefixMode : public PrefixMode
23 {
24  public:
25         reference<ConfigTag> tag;
26
27         CustomPrefixMode(Module* parent, const std::string& Name, char Letter, char Prefix, ConfigTag* Tag)
28                 : PrefixMode(parent, Name, Letter, 0, Prefix)
29                 , tag(Tag)
30         {
31                 prefixrank = tag->getInt("rank", 0, 0, UINT_MAX);
32                 ranktoset = tag->getInt("ranktoset", prefixrank, prefixrank, UINT_MAX);
33                 ranktounset = tag->getInt("ranktounset", ranktoset, ranktoset, UINT_MAX);
34                 selfremove = tag->getBool("depriv", true);
35         }
36 };
37
38 class ModuleCustomPrefix : public Module
39 {
40         std::vector<CustomPrefixMode*> modes;
41  public:
42         void init() CXX11_OVERRIDE
43         {
44                 ConfigTagList tags = ServerInstance->Config->ConfTags("customprefix");
45                 for (ConfigIter iter = tags.first; iter != tags.second; ++iter)
46                 {
47                         ConfigTag* tag = iter->second;
48
49                         const std::string name = tag->getString("name");
50                         if (name.empty())
51                                 throw ModuleException("<customprefix:name> must be specified at " + tag->getTagLocation());
52
53                         const std::string letter = tag->getString("letter");
54                         if (letter.length() != 1)
55                                 throw ModuleException("<customprefix:letter> must be set to a mode character at " + tag->getTagLocation());
56
57                         const std::string prefix = tag->getString("prefix");
58                         if (prefix.length() != 1)
59                                 throw ModuleException("<customprefix:prefix> must be set to a mode prefix at " + tag->getTagLocation());
60
61                         try
62                         {
63                                 CustomPrefixMode* mh = new CustomPrefixMode(this, name, letter[0], prefix[0], tag);
64                                 ServerInstance->Modules->AddService(*mh);
65                         }
66                         catch (ModuleException& e)
67                         {
68                                 throw ModuleException(e.GetReason() + " (while creating mode from " + tag->getTagLocation() + ")");
69                         }
70                 }
71         }
72
73         ~ModuleCustomPrefix()
74         {
75                 stdalgo::delete_all(modes);
76         }
77
78         Version GetVersion() CXX11_OVERRIDE
79         {
80                 return Version("Provides custom prefix channel modes", VF_VENDOR);
81         }
82 };
83
84 MODULE_INIT(ModuleCustomPrefix)