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