]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customprefix.cpp
f0b6d88e340cc4fc77246256768ee801a6f355be
[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         bool depriv;
27
28         CustomPrefixMode(Module* parent, ConfigTag* Tag)
29                 : PrefixMode(parent, Tag->getString("name"), 0)
30                 , tag(Tag)
31         {
32                 std::string v = tag->getString("prefix");
33                 prefix = v.c_str()[0];
34                 v = tag->getString("letter");
35                 mode = v.c_str()[0];
36                 prefixrank = tag->getInt("rank");
37                 levelrequired = tag->getInt("ranktoset", prefixrank);
38                 depriv = tag->getBool("depriv", true);
39         }
40
41         ModResult AccessCheck(User* src, Channel*, std::string& value, bool adding)
42         {
43                 if (!adding && src->nick == value && depriv)
44                         return MOD_RES_ALLOW;
45                 return MOD_RES_PASSTHRU;
46         }
47 };
48
49 class ModuleCustomPrefix : public Module
50 {
51         std::vector<CustomPrefixMode*> modes;
52  public:
53         void init() CXX11_OVERRIDE
54         {
55                 ConfigTagList tags = ServerInstance->Config->ConfTags("customprefix");
56                 while (tags.first != tags.second)
57                 {
58                         ConfigTag* tag = tags.first->second;
59                         tags.first++;
60                         CustomPrefixMode* mh = new CustomPrefixMode(this, tag);
61                         modes.push_back(mh);
62                         if (mh->GetPrefixRank() == 0)
63                                 throw ModuleException("Rank must be specified for prefix at " + tag->getTagLocation());
64                         if (!isalpha(mh->GetModeChar()))
65                                 throw ModuleException("Mode must be a letter for prefix at " + tag->getTagLocation());
66                         try
67                         {
68                                 ServerInstance->Modules->AddService(*mh);
69                         }
70                         catch (ModuleException& e)
71                         {
72                                 throw ModuleException(e.err + " (while creating mode from " + tag->getTagLocation() + ")");
73                         }
74                 }
75         }
76
77         ~ModuleCustomPrefix()
78         {
79                 for (std::vector<CustomPrefixMode*>::iterator i = modes.begin(); i != modes.end(); i++)
80                         delete *i;
81         }
82
83         Version GetVersion() CXX11_OVERRIDE
84         {
85                 return Version("Provides custom prefix channel modes", VF_VENDOR);
86         }
87 };
88
89 MODULE_INIT(ModuleCustomPrefix)