]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customprefix.cpp
Merge pull request #1157 from SaberUK/insp20+fix-cron-restart
[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         int rank;
29         bool depriv;
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                 rank = tag->getInt("rank");
40                 levelrequired = tag->getInt("ranktoset", rank);
41                 depriv = tag->getBool("depriv", true);
42         }
43
44         unsigned int GetPrefixRank()
45         {
46                 return rank;
47         }
48
49         ModResult AccessCheck(User* src, Channel*, std::string& value, bool adding)
50         {
51                 if (!adding && src->nick == value && depriv)
52                         return MOD_RES_ALLOW;
53                 return MOD_RES_PASSTHRU;
54         }
55
56         void RemoveMode(Channel* channel, irc::modestacker* stack)
57         {
58                 const UserMembList* cl = channel->GetUsers();
59                 std::vector<std::string> mode_junk;
60                 mode_junk.push_back(channel->name);
61                 irc::modestacker modestack(false);
62                 std::deque<std::string> stackresult;
63
64                 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
65                 {
66                         if (i->second->hasMode(mode))
67                         {
68                                 if (stack)
69                                         stack->Push(this->GetModeChar(), i->first->nick);
70                                 else
71                                         modestack.Push(this->GetModeChar(), i->first->nick);
72                         }
73                 }
74
75                 if (stack)
76                         return;
77
78                 while (modestack.GetStackedLine(stackresult))
79                 {
80                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
81                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
82                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
83                 }
84         }
85
86         void RemoveMode(User* user, irc::modestacker* stack)
87         {
88         }
89
90         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
91         {
92                 return MODEACTION_ALLOW;
93         }
94 };
95
96 class ModuleCustomPrefix : public Module
97 {
98         std::vector<CustomPrefixMode*> modes;
99  public:
100         ModuleCustomPrefix()
101         {
102         }
103
104         void init()
105         {
106                 ConfigTagList tags = ServerInstance->Config->ConfTags("customprefix");
107                 while (tags.first != tags.second)
108                 {
109                         ConfigTag* tag = tags.first->second;
110                         tags.first++;
111                         CustomPrefixMode* mh = new CustomPrefixMode(this, tag);
112                         modes.push_back(mh);
113                         if (mh->rank <= 0)
114                                 throw ModuleException("Rank must be specified for prefix at " + tag->getTagLocation());
115                         if (!isalpha(mh->GetModeChar()))
116                                 throw ModuleException("Mode must be a letter for prefix at " + tag->getTagLocation());
117                         try
118                         {
119                                 ServerInstance->Modules->AddService(*mh);
120                         }
121                         catch (ModuleException& e)
122                         {
123                                 throw ModuleException(e.err + " (while creating mode from " + tag->getTagLocation() + ")");
124                         }
125                 }
126         }
127
128         ~ModuleCustomPrefix()
129         {
130                 for (std::vector<CustomPrefixMode*>::iterator i = modes.begin(); i != modes.end(); i++)
131                         delete *i;
132         }
133
134         Version GetVersion()
135         {
136                 return Version("Provides custom prefix channel modes", VF_VENDOR);
137         }
138 };
139
140 MODULE_INIT(ModuleCustomPrefix)