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