]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banexception.cpp
c26e111555f8de83e8181a6428f370bab33c1899
[user/henk/code/inspircd.git] / src / modules / m_banexception.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 #include "u_listmode.h"
16
17 /* $ModDesc: Provides support for the +e channel mode */
18 /* $ModDep: ../../include/u_listmode.h */
19
20 /* Written by Om<om@inspircd.org>, April 2005. */
21 /* Rewritten to use the listmode utility by Om, December 2005 */
22 /* Adapted from m_exception, which was originally based on m_chanprotect and m_silence */
23
24 // The +e channel mode takes a nick!ident@host, glob patterns allowed,
25 // and if a user matches an entry on the +e list then they can join the channel, overriding any (+b) bans set on them
26 // Now supports CIDR and IP addresses -- Brain
27
28
29 /** Handles +e channel mode
30  */
31 class BanException : public ListModeBase
32 {
33  public:
34         BanException(InspIRCd* Instance) : ListModeBase(Instance, 'e', "End of Channel Exception List", 348, 349, true) { }
35 };
36
37
38 class ModuleBanException : public Module
39 {
40         BanException* be;
41
42
43 public:
44         ModuleBanException(InspIRCd* Me) : Module(Me)
45         {
46                 be = new BanException(ServerInstance);
47                 if (!ServerInstance->Modes->AddMode(be))
48                         throw ModuleException("Could not add new modes!");
49                 ServerInstance->Modules->PublishInterface("ChannelBanList", this);
50
51                 be->DoImplements(this);
52                 Implementation list[] = { I_OnRehash, I_OnRequest, I_On005Numeric, I_OnCheckBan, I_OnCheckExtBan, I_OnCheckStringExtBan };
53                 Me->Modules->Attach(list, this, 6);
54
55         }
56
57         virtual void On005Numeric(std::string &output)
58         {
59                 output.append(" EXCEPTS=e");
60         }
61
62         virtual int OnCheckExtBan(User *user, Channel *chan, char type)
63         {
64                 if (chan != NULL)
65                 {
66                         modelist *list;
67                         chan->GetExt(be->GetInfoKey(), list);
68
69                         if (!list)
70                                 return 0;
71
72                         std::string mask = std::string(user->nick) + "!" + user->ident + "@" + user->GetIPString();
73                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
74                         {
75                                 if (it->mask[0] != type || it->mask[1] != ':')
76                                         continue;
77
78                                 std::string maskptr = it->mask.substr(2);
79
80                                 if (InspIRCd::Match(user->GetFullRealHost(), maskptr) || InspIRCd::Match(user->GetFullHost(), maskptr) || (InspIRCd::MatchCIDR(mask, maskptr)))
81                                 {
82                                         // They match an entry on the list, so let them pass this.
83                                         return 1;
84                                 }
85                         }
86                 }
87
88                 return 0;
89         }
90
91         virtual int OnCheckStringExtBan(const std::string &str, Channel *chan, char type)
92         {
93                 if (chan != NULL)
94                 {
95                         modelist *list;
96                         chan->GetExt(be->GetInfoKey(), list);
97
98                         if (!list)
99                                 return 0;
100                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
101                         {
102                                 if (it->mask[0] != type || it->mask[1] != ':')
103                                         continue;
104
105                                 std::string maskptr = it->mask.substr(2);
106                                 if (InspIRCd::Match(str, maskptr))
107                                         // They match an entry on the list, so let them in.
108                                         return 1;
109                         }
110                 }
111
112                 return 0;
113         }
114
115         virtual int OnCheckBan(User* user, Channel* chan)
116         {
117                 if (chan != NULL)
118                 {
119                         modelist* list;
120                         chan->GetExt(be->GetInfoKey(), list);
121
122                         if (!list)
123                         {
124                                 // No list, proceed normally
125                                 return 0;
126                         }
127
128                         std::string mask = std::string(user->nick) + "!" + user->ident + "@" + user->GetIPString();
129                         for (modelist::iterator it = list->begin(); it != list->end(); it++)
130                         {
131                                 if (InspIRCd::Match(user->GetFullRealHost(), it->mask) || InspIRCd::Match(user->GetFullHost(), it->mask) || (InspIRCd::MatchCIDR(mask, it->mask)))
132                                 {
133                                         // They match an entry on the list, so let them in.
134                                         return 1;
135                                 }
136                         }
137                 }
138                 return 0;
139         }
140
141         virtual void OnCleanup(int target_type, void* item)
142         {
143                 be->DoCleanup(target_type, item);
144         }
145
146         virtual void OnSyncChannel(Channel* chan, Module* proto, void* opaque)
147         {
148                 be->DoSyncChannel(chan, proto, opaque);
149         }
150
151         virtual void OnChannelDelete(Channel* chan)
152         {
153                 be->DoChannelDelete(chan);
154         }
155
156         virtual void OnRehash(User* user, const std::string &param)
157         {
158                 be->DoRehash();
159         }
160
161         virtual const char* OnRequest(Request* request)
162         {
163                 return be->DoOnRequest(request);
164         }
165
166         virtual Version GetVersion()
167         {
168                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
169         }
170
171         virtual ~ModuleBanException()
172         {
173                 ServerInstance->Modes->DelMode(be);
174                 delete be;
175                 ServerInstance->Modules->UnpublishInterface("ChannelBanList", this);
176         }
177 };
178
179 MODULE_INIT(ModuleBanException)