]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services.cpp
Gah, im forgetting to SetMode!
[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 <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include <string>
24 #include "helperfuncs.h"
25 #include "hashcomp.h"
26
27 /* $ModDesc: Povides support for services +r user/chan modes and more */
28
29 class ModuleServices : public Module
30 {
31         Server *Srv; 
32         bool kludgeme;
33
34  public:
35         ModuleServices(Server* Me)
36                 : Module::Module(Me)
37         {
38                 Srv = Me;
39
40                 Srv->AddExtendedMode('r',MT_CHANNEL,false,0,0);
41                 Srv->AddExtendedMode('r',MT_CLIENT,false,0,0);
42                 Srv->AddExtendedMode('R',MT_CHANNEL,false,0,0);
43                 Srv->AddExtendedMode('R',MT_CLIENT,false,0,0);
44                 Srv->AddExtendedMode('M',MT_CHANNEL,false,0,0);
45
46                 kludgeme = false;
47         }
48
49         virtual void On005Numeric(std::string &output)
50         {
51                 InsertMode(output, "rRM", 4);
52         }
53
54         /* <- :stitch.chatspike.net 307 w00t w00t :is a registered nick */
55         virtual void OnWhois(userrec* source, userrec* dest)
56         {
57                 if (dest->modes['r'-65])
58                 {
59                         /* user is registered */
60                         WriteServ(source->fd, "307 %s %s :is a registered nick", source->nick, dest->nick);
61                 }
62         }
63
64         void Implements(char* List)
65         {
66                 List[I_OnWhois] = List[I_OnUserPostNick] = List[I_OnUserPreMessage] = List[I_OnExtendedMode] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
67         }
68
69         virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
70         {
71                 /* On nickchange, if they have +r, remove it */
72                 if (user->modes['r'-65])
73                 {
74                         char* modechange[2];
75                         modechange[0] = user->nick;
76                         modechange[1] = "-r";
77                         kludgeme = true;
78                         Srv->SendMode(modechange,2,user);
79                         kludgeme = false;
80                 }
81         }
82         
83         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
84         {
85                 
86                 if (modechar == 'r')
87                 {
88                         if (type == MT_CHANNEL)
89                         {
90                                 // only a u-lined server may add or remove the +r mode.
91                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"") || (strchr(user->nick,'.'))))
92                                 {
93                                         log(DEBUG,"Allowing umode +r, server and nick are: '%s','%s'",user->nick,user->server);
94                                         return 1;
95                                 }
96                                 else
97                                 {
98                                         log(DEBUG,"Only a server can set chanmode +r, server and nick are: '%s','%s'",user->nick,user->server);
99                                         Srv->SendServ(user->fd,"500 "+std::string(user->nick)+" :Only a server may modify the +r channel mode");
100                                 }
101                         }
102                         else
103                         {
104                                 if ((kludgeme) || (Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"") || (strchr(user->nick,'.'))))
105                                 {
106                                         log(DEBUG,"Allowing umode +r, server and nick are: '%s','%s'",user->nick,user->server);
107                                         return 1;
108                                 }
109                                 else
110                                 {
111                                         log(DEBUG,"Only a server can set umode +r, server and nick are: '%s','%s'",user->nick,user->server);
112                                         Srv->SendServ(user->fd,"500 "+std::string(user->nick)+" :Only a server may modify the +r user mode");
113                                 }
114                         }
115                 }
116                 else if (modechar == 'R')
117                 {
118                         return 1;
119                 }
120                 else if (modechar == 'M')
121                 {
122                         if (type == MT_CHANNEL)
123                         {
124                                 return 1;
125                         }
126                 }
127
128                 return 0;
129         }
130
131         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
132         {
133                 if (target_type == TYPE_CHANNEL)
134                 {
135                         chanrec* c = (chanrec*)dest;
136                         if ((c->IsModeSet('M')) && (!user->modes['r'-65]))
137                         {
138                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
139                                 {
140                                         // user is ulined, can speak regardless
141                                         return 0;
142                                 }
143                                 // user messaging a +M channel and is not registered
144                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need a registered nickname to speak on this channel");
145                                 return 1;
146                         }
147                 }
148                 if (target_type == TYPE_USER)
149                 {
150                         userrec* u = (userrec*)dest;
151                         if ((u->modes['R'-65]) && (user->modes['r'-65]))
152                         {
153                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
154                                 {
155                                         // user is ulined, can speak regardless
156                                         return 0;
157                                 }
158                                 // user messaging a +R user and is not registered
159                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need a registered nickname to message this user");
160                                 return 1;
161                         }
162                 }
163                 return 0;
164         }
165         
166         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
167         {
168                 return OnUserPreMessage(user,dest,target_type,text,status);
169         }
170         
171         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
172         {
173                 if (chan)
174                 {
175                         if (chan->IsModeSet('R'))
176                         {
177                                 if (user->modes['r'-65])
178                                 {
179                                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
180                                         {
181                                                 // user is ulined, won't be stopped from joining
182                                                 return 0;
183                                         }
184                                         // joining a +R channel and not identified
185                                         Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need a registered nickname to join this channel");
186                                         return 1;
187                                 }
188                         }
189                 }
190                 return 0;
191         }
192
193         virtual ~ModuleServices()
194         {
195         }
196         
197         virtual Version GetVersion()
198         {
199                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
200         }
201 };
202
203
204 class ModuleServicesFactory : public ModuleFactory
205 {
206  public:
207         ModuleServicesFactory()
208         {
209         }
210         
211         ~ModuleServicesFactory()
212         {
213         }
214         
215         virtual Module * CreateModule(Server* Me)
216         {
217                 return new ModuleServices(Me);
218         }
219         
220 };
221
222
223 extern "C" void * init_module( void )
224 {
225         return new ModuleServicesFactory;
226 }
227