]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_exemptchanops.cpp
e92350eeba52c1a9b74c8f940b856590b643f50b
[user/henk/code/inspircd.git] / src / modules / m_exemptchanops.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 #include "u_listmode.h"
16
17 /* $ModDesc: Provides the ability to allow channel operators to be exempt from certain modes. */
18
19 /** Handles channel mode +X
20  */
21 class ExemptChanOps : public ListModeBase
22 {
23  public:
24         ExemptChanOps(Module* Creator) : ListModeBase(Creator, "exemptchanops", 'X', "End of channel exemptchanops list", 954, 953, false, "exemptchanops") { }
25
26         bool ValidateParam(User* user, Channel* chan, std::string &word)
27         {
28                 // TODO actually make sure there's a prop for this
29                 if ((word.length() > 35) || (word.empty()))
30                 {
31                         user->WriteNumeric(955, "%s %s %s :word is too %s for exemptchanops list",user->nick.c_str(), chan->name.c_str(), word.c_str(), (word.empty() ? "short" : "long"));
32                         return false;
33                 }
34
35                 return true;
36         }
37
38         bool TellListTooLong(User* user, Channel* chan, std::string &word)
39         {
40                 user->WriteNumeric(959, "%s %s %s :Channel exemptchanops list is full", user->nick.c_str(), chan->name.c_str(), word.c_str());
41                 return true;
42         }
43
44         void TellAlreadyOnList(User* user, Channel* chan, std::string &word)
45         {
46                 user->WriteNumeric(957, "%s %s :The word %s is already on the exemptchanops list",user->nick.c_str(), chan->name.c_str(), word.c_str());
47         }
48
49         void TellNotSet(User* user, Channel* chan, std::string &word)
50         {
51                 user->WriteNumeric(958, "%s %s :No such exemptchanops word is set",user->nick.c_str(), chan->name.c_str());
52         }
53 };
54
55 class ExemptHandler : public HandlerBase3<ModResult, User*, Channel*, const std::string&>
56 {
57  public:
58         ExemptChanOps ec;
59         ExemptHandler(Module* me) : ec(me) {}
60         
61         ModeHandler* FindMode(const std::string& mid)
62         {
63                 if (mid.length() == 1)
64                         return ServerInstance->Modes->FindMode(mid[0], MODETYPE_CHANNEL);
65                 for(char c='A'; c < 'z'; c++)
66                 {
67                         ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
68                         if (mh && mh->name == mid)
69                                 return mh;
70                 }
71                 return NULL;
72         }
73
74         ModResult Call(User* user, Channel* chan, const std::string& restriction)
75         {
76                 unsigned int mypfx = chan->GetPrefixValue(user);
77                 std::string minmode;
78
79                 modelist* list = ec.extItem.get(chan);
80
81                 if (list)
82                 {
83                         for (modelist::iterator i = list->begin(); i != list->end(); ++i)
84                         {
85                                 std::string::size_type pos = (*i).mask.find(':');
86                                 if (pos == std::string::npos)
87                                         continue;
88                                 if ((*i).mask.substr(0,pos) == restriction)
89                                         minmode = (*i).mask.substr(pos + 1);
90                         }
91                 }
92
93                 ModeHandler* mh = FindMode(minmode);
94                 if (mh && mypfx >= mh->GetPrefixRank())
95                         return MOD_RES_ALLOW;
96                 if (mh || minmode == "*")
97                         return MOD_RES_DENY;
98
99                 return ServerInstance->HandleOnCheckExemption.Call(user, chan, restriction);
100         }
101 };
102
103 class ModuleExemptChanOps : public Module
104 {
105         std::string defaults;
106         ExemptHandler eh;
107
108  public:
109
110         ModuleExemptChanOps() : eh(this)
111         {
112         }
113
114         void init()
115         {
116                 ServerInstance->Modules->AddService(eh.ec);
117                 Implementation eventlist[] = { I_OnRehash, I_OnSyncChannel };
118                 ServerInstance->Modules->Attach(eventlist, this, 2);
119                 ServerInstance->OnCheckExemption = &eh;
120
121                 OnRehash(NULL);
122         }
123
124         ~ModuleExemptChanOps()
125         {
126                 ServerInstance->OnCheckExemption = &ServerInstance->HandleOnCheckExemption;
127         }
128
129         Version GetVersion()
130         {
131                 return Version("Provides the ability to allow channel operators to be exempt from certain modes.",VF_VENDOR);
132         }
133
134         void OnRehash(User* user)
135         {
136                 eh.ec.DoRehash();
137         }
138
139         void OnSyncChannel(Channel* chan, Module* proto, void* opaque)
140         {
141                 eh.ec.DoSyncChannel(chan, proto, opaque);
142         }
143 };
144
145 MODULE_INIT(ModuleExemptChanOps)