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