]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customprefix.cpp
Replace m_halfvoice with m_customprefix
[user/henk/code/inspircd.git] / src / modules / m_customprefix.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Allows custom prefix modes to be created. */
17
18 class CustomPrefixMode : public ModeHandler
19 {
20  public:
21         reference<ConfigTag> tag;
22         int rank;
23         bool depriv;
24         CustomPrefixMode(Module* parent, ConfigTag* Tag)
25                 : ModeHandler(parent, Tag->getString("name"), 0, PARAM_ALWAYS, MODETYPE_CHANNEL), tag(Tag)
26         {
27                 list = true;
28                 m_paramtype = TR_NICK;
29                 std::string v = tag->getString("prefix");
30                 prefix = v.c_str()[0];
31                 v = tag->getString("letter");
32                 mode = v.c_str()[0];
33                 rank = tag->getInt("rank");
34                 levelrequired = tag->getInt("ranktoset", rank);
35                 depriv = tag->getBool("depriv", true);
36         }
37
38         unsigned int GetPrefixRank()
39         {
40                 return rank;
41         }
42
43         ModResult AccessCheck(User* src, Channel*, std::string& value, bool adding)
44         {
45                 if (!adding && src->nick == value && depriv)
46                         return MOD_RES_ALLOW;
47                 return MOD_RES_PASSTHRU;
48         }
49
50         void RemoveMode(Channel* channel, irc::modestacker* stack)
51         {
52                 const UserMembList* cl = channel->GetUsers();
53                 std::vector<std::string> mode_junk;
54                 mode_junk.push_back(channel->name);
55                 irc::modestacker modestack(false);
56                 std::deque<std::string> stackresult;
57
58                 for (UserMembCIter i = cl->begin(); i != cl->end(); i++)
59                 {
60                         if (i->second->hasMode(mode))
61                         {
62                                 if (stack)
63                                         stack->Push(this->GetModeChar(), i->first->nick);
64                                 else
65                                         modestack.Push(this->GetModeChar(), i->first->nick);
66                         }
67                 }
68
69                 if (stack)
70                         return;
71
72                 while (modestack.GetStackedLine(stackresult))
73                 {
74                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
75                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
76                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
77                 }
78         }
79
80         void RemoveMode(User* user, irc::modestacker* stack)
81         {
82         }
83
84         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
85         {
86                 return MODEACTION_ALLOW;
87         }
88 };
89
90 class ModuleCustomPrefix : public Module
91 {
92         std::vector<CustomPrefixMode*> modes;
93  public:
94         ModuleCustomPrefix()
95         {
96         }
97
98         void init()
99         {
100                 ConfigTagList tags = ServerInstance->Config->ConfTags("customprefix");
101                 while (tags.first != tags.second)
102                 {
103                         ConfigTag* tag = tags.first->second;
104                         CustomPrefixMode* mh = new CustomPrefixMode(this, tag);
105                         modes.push_back(mh);
106                         if (mh->rank <= 0)
107                                 throw ModuleException("Rank must be specified for prefix at " + tag->getTagLocation());
108                         if (!isalpha(mh->GetModeChar()))
109                                 throw ModuleException("Mode must be a letter for prefix at " + tag->getTagLocation());
110                         try
111                         {
112                                 ServerInstance->Modules->AddService(*mh);
113                         }
114                         catch (ModuleException& e)
115                         {
116                                 throw ModuleException(e.err + " (while creating mode from " + tag->getTagLocation() + ")");
117                         }
118                 }
119         }
120
121         ~ModuleCustomPrefix()
122         {
123                 for (std::vector<CustomPrefixMode*>::iterator i = modes.begin(); i != modes.end(); i++)
124                         delete *i;
125         }
126
127         Version GetVersion()
128         {
129                 return Version("Provides custom prefix channel modes", VF_VENDOR);
130         }
131 };
132
133 MODULE_INIT(ModuleCustomPrefix)