]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services.cpp
Move whowas containers into whowas class to avoid all cpp files including cmd_whowas...
[user/henk/code/inspircd.git] / src / modules / m_services.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 "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18
19 static bool kludgeme = false;
20
21 /* $ModDesc: Povides support for services +r user/chan modes and more */
22
23 /** Channel mode +r - mark a channel as identified
24  */
25 class Channel_r : public ModeHandler
26 {
27         
28  public:
29         Channel_r(InspIRCd* Instance) : ModeHandler(Instance, 'r', 0, 0, false, MODETYPE_CHANNEL, false) { }
30
31         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
32         {
33                 // only a u-lined server may add or remove the +r mode.
34                 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server || (strchr(source->nick,'.'))))
35                 {
36                         ServerInstance->Log(DEBUG,"Allowing cmode +r, server and nick are: '%s','%s'",source->nick,source->server);
37                         channel->SetMode('r',adding);
38                         return MODEACTION_ALLOW;
39                 }
40                 else
41                 {
42                         ServerInstance->Log(DEBUG,"Only a server can set chanmode +r, server and nick are: '%s','%s'",source->nick,source->server);
43                         source->WriteServ("500 "+std::string(source->nick)+" :Only a server may modify the +r channel mode");
44                         return MODEACTION_DENY;
45                 }
46         }
47 };
48
49 /** User mode +r - mark a user as identified
50  */
51 class User_r : public ModeHandler
52 {
53         
54  public:
55         User_r(InspIRCd* Instance) : ModeHandler(Instance, 'r', 0, 0, false, MODETYPE_USER, false) { }
56
57         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
58         {
59                 if ((kludgeme) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server || (strchr(source->nick,'.'))))
60                 {
61                         if ((adding && !dest->IsModeSet('r')) || (!adding && dest->IsModeSet('r')))
62                         {
63                                 ServerInstance->Log(DEBUG,"Allowing umode +r, server and nick are: '%s','%s'",source->nick,source->server);
64                                 dest->SetMode('r',adding);
65                                 return MODEACTION_ALLOW;
66                         }
67                         return MODEACTION_DENY;
68                 }
69                 else
70                 {
71                         ServerInstance->Log(DEBUG,"Only a server can set umode +r, server and nick are: '%s','%s'",source->nick, source->server);
72                         source->WriteServ("500 "+std::string(source->nick)+" :Only a server may modify the +r user mode");
73                         return MODEACTION_DENY;
74                 }
75         }
76 };
77
78 /** Channel mode +R - registered users only
79  */
80 class Channel_R : public ModeHandler
81 {
82  public:
83         Channel_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_CHANNEL, false) { }
84
85         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
86         {
87                 if (adding)
88                 {
89                         if (!channel->IsModeSet('R'))
90                         {
91                                 channel->SetMode('R',true);
92                                 return MODEACTION_ALLOW;
93                         }
94                 }
95                 else
96                 {
97                         if (channel->IsModeSet('R'))
98                         {
99                                 channel->SetMode('R',false);
100                                 return MODEACTION_ALLOW;
101                         }
102                 }
103
104                 return MODEACTION_DENY;
105         }
106 };
107
108 /** User mode +R - only allow PRIVMSG and NOTICE from registered users
109  */
110 class User_R : public ModeHandler
111 {
112  public:
113         User_R(InspIRCd* Instance) : ModeHandler(Instance, 'R', 0, 0, false, MODETYPE_USER, false) { }
114
115         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
116         {
117                 if (adding)
118                 {
119                         if (!dest->IsModeSet('R'))
120                         {
121                                 dest->SetMode('R',true);
122                                 return MODEACTION_ALLOW;
123                         }
124                 }
125                 else
126                 {
127                         if (dest->IsModeSet('R'))
128                         {
129                                 dest->SetMode('R',false);
130                                 return MODEACTION_ALLOW;
131                         }
132                 }
133
134                 return MODEACTION_DENY;
135         }
136 };
137
138 /** Channel mode +M - only allow privmsg and notice to channel from registered users
139  */
140 class Channel_M : public ModeHandler
141 {
142  public:
143         Channel_M(InspIRCd* Instance) : ModeHandler(Instance, 'M', 0, 0, false, MODETYPE_CHANNEL, false) { }
144
145         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
146         {
147                 if (adding)
148                 {
149                         if (!channel->IsModeSet('M'))
150                         {
151                                 channel->SetMode('M',true);
152                                 return MODEACTION_ALLOW;
153                         }
154                 }
155                 else
156                 {
157                         if (channel->IsModeSet('M'))
158                         {
159                                 channel->SetMode('M',false);
160                                 return MODEACTION_ALLOW;
161                         }
162                 }
163
164                 return MODEACTION_DENY;
165         }
166 };
167
168 /** Dreamnforge-like services support
169  */
170 class ModuleServices : public Module
171 {
172         
173         Channel_r* m1;
174         Channel_R* m2;
175         Channel_M* m3;
176         User_r* m4;
177         User_R* m5;
178  public:
179         ModuleServices(InspIRCd* Me)
180                 : Module::Module(Me)
181         {
182                 
183                 m1 = new Channel_r(ServerInstance);
184                 m2 = new Channel_R(ServerInstance);
185                 m3 = new Channel_M(ServerInstance);
186                 m4 = new User_r(ServerInstance);
187                 m5 = new User_R(ServerInstance);
188
189                 if (!ServerInstance->AddMode(m1, 'r') || !ServerInstance->AddMode(m2, 'R') || !ServerInstance->AddMode(m3, 'M')
190                         || !ServerInstance->AddMode(m4, 'r') || !ServerInstance->AddMode(m5, 'R'))
191                 {
192                         throw ModuleException("Could not add user and channel modes!");
193                 }
194         
195                 kludgeme = false;
196         }
197
198         /* <- :stitch.chatspike.net 307 w00t w00t :is a registered nick */
199         virtual void OnWhois(userrec* source, userrec* dest)
200         {
201                 if (dest->IsModeSet('r'))
202                 {
203                         /* user is registered */
204                         ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick, dest->nick);
205                 }
206         }
207
208         void Implements(char* List)
209         {
210                 List[I_OnWhois] = List[I_OnUserPostNick] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
211         }
212
213         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
214         {
215                 /* On nickchange, if they have +r, remove it */
216                 if (user->IsModeSet('r'))
217                 {
218                         const char* modechange[2];
219                         modechange[0] = user->nick;
220                         modechange[1] = "-r";
221                         kludgeme = true;
222                         ServerInstance->SendMode(modechange,2,user);
223                         kludgeme = false;
224                 }
225         }
226         
227         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
228         {
229                 if (target_type == TYPE_CHANNEL)
230                 {
231                         chanrec* c = (chanrec*)dest;
232                         if ((c->IsModeSet('M')) && (!user->IsModeSet('r')))
233                         {
234                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)) || (!strcmp(user->server,"")))
235                                 {
236                                         // user is ulined, can speak regardless
237                                         return 0;
238                                 }
239                                 // user messaging a +M channel and is not registered
240                                 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need a registered nickname to speak on this channel");
241                                 return 1;
242                         }
243                 }
244                 if (target_type == TYPE_USER)
245                 {
246                         userrec* u = (userrec*)dest;
247                         if ((u->IsModeSet('R')) && (!user->IsModeSet('r')))
248                         {
249                                 if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
250                                 {
251                                         // user is ulined, can speak regardless
252                                         return 0;
253                                 }
254                                 // user messaging a +R user and is not registered
255                                 user->WriteServ("477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need a registered nickname to message this user");
256                                 return 1;
257                         }
258                 }
259                 return 0;
260         }
261         
262         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
263         {
264                 return OnUserPreMessage(user,dest,target_type,text,status, exempt_list);
265         }
266         
267         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
268         {
269                 if (chan)
270                 {
271                         if (chan->IsModeSet('R'))
272                         {
273                                 if (!user->IsModeSet('r'))
274                                 {
275                                         if ((ServerInstance->ULine(user->nick)) || (ServerInstance->ULine(user->server)))
276                                         {
277                                                 // user is ulined, won't be stopped from joining
278                                                 return 0;
279                                         }
280                                         // joining a +R channel and not identified
281                                         user->WriteServ("477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need a registered nickname to join this channel");
282                                         return 1;
283                                 }
284                         }
285                 }
286                 return 0;
287         }
288
289         virtual ~ModuleServices()
290         {
291                 kludgeme = true;
292                 ServerInstance->Modes->DelMode(m1);
293                 ServerInstance->Modes->DelMode(m2);
294                 ServerInstance->Modes->DelMode(m3);
295                 ServerInstance->Modes->DelMode(m4);
296                 ServerInstance->Modes->DelMode(m5);
297                 DELETE(m1);
298                 DELETE(m2);
299                 DELETE(m3);
300                 DELETE(m4);
301                 DELETE(m5);
302         }
303         
304         virtual Version GetVersion()
305         {
306                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
307         }
308 };
309
310
311 class ModuleServicesFactory : public ModuleFactory
312 {
313  public:
314         ModuleServicesFactory()
315         {
316         }
317         
318         ~ModuleServicesFactory()
319         {
320         }
321         
322         virtual Module * CreateModule(InspIRCd* Me)
323         {
324                 return new ModuleServices(Me);
325         }
326         
327 };
328
329
330 extern "C" void * init_module( void )
331 {
332         return new ModuleServicesFactory;
333 }
334