]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services.cpp
Updated copyrights in headers etc using perl inplace edit
[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  public:
33         ModuleServices(Server* Me)
34                 : Module::Module(Me)
35         {
36                 Srv = Me;
37
38                 Srv->AddExtendedMode('r',MT_CHANNEL,false,0,0);
39                 Srv->AddExtendedMode('r',MT_CLIENT,false,0,0);
40                 Srv->AddExtendedMode('R',MT_CHANNEL,false,0,0);
41                 Srv->AddExtendedMode('R',MT_CLIENT,false,0,0);
42                 Srv->AddExtendedMode('M',MT_CHANNEL,false,0,0);
43         }
44
45         virtual void On005Numeric(std::string &output)
46         {
47                 std::stringstream line(output);
48                 std::string temp1, temp2;
49                 while (!line.eof())
50                 {
51                         line >> temp1;
52                         if (temp1.substr(0,10) == "CHANMODES=")
53                         {
54                                 // append the chanmode to the end
55                                 temp1 = temp1.substr(10,temp1.length());
56                                 temp1 = "CHANMODES=" + temp1 + "rRM";
57                         }
58                         temp2 = temp2 + temp1 + " ";
59                 }
60                 if (temp2.length())
61                         output = temp2.substr(0,temp2.length()-1);
62         }
63
64         void Implements(char* List)
65         {
66                 List[I_OnUserPreMessage] = List[I_OnExtendedMode] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
67         }
68         
69         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
70         {
71                 
72                 if (modechar == 'r')
73                 {
74                         if (type == MT_CHANNEL)
75                         {
76                                 // only a u-lined server may add or remove the +r mode.
77                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"") || (strchr(user->nick,'.'))))
78                                 {
79                                         log(DEBUG,"Allowing umode +r, server and nick are: '%s','%s'",user->nick,user->server);
80                                         return 1;
81                                 }
82                                 else
83                                 {
84                                         log(DEBUG,"Only a server can set chanmode +r, server and nick are: '%s','%s'",user->nick,user->server);
85                                         Srv->SendServ(user->fd,"500 "+std::string(user->nick)+" :Only a server may modify the +r channel mode");
86                                 }
87                         }
88                         else
89                         {
90                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"") || (strchr(user->nick,'.'))))
91                                 {
92                                         log(DEBUG,"Allowing umode +r, server and nick are: '%s','%s'",user->nick,user->server);
93                                         return 1;
94                                 }
95                                 else
96                                 {
97                                         log(DEBUG,"Only a server can set umode +r, server and nick are: '%s','%s'",user->nick,user->server);
98                                         Srv->SendServ(user->fd,"500 "+std::string(user->nick)+" :Only a server may modify the +r user mode");
99                                 }
100                         }
101                 }
102                 else if (modechar == 'R')
103                 {
104                         if (type == MT_CHANNEL)
105                         {
106                                 return 1;
107                         }
108                 }
109                 else if (modechar == 'M')
110                 {
111                         if (type == MT_CHANNEL)
112                         {
113                                 return 1;
114                         }
115                 }
116
117                 return 0;
118         }
119
120         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
121         {
122                 if (target_type == TYPE_CHANNEL)
123                 {
124                         chanrec* c = (chanrec*)dest;
125                         if ((c->IsCustomModeSet('M')) && (!strchr(user->modes,'r')))
126                         {
127                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
128                                 {
129                                         // user is ulined, can speak regardless
130                                         return 0;
131                                 }
132                                 // user messaging a +M channel and is not registered
133                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need a registered nickname to speak on this channel");
134                                 return 1;
135                         }
136                 }
137                 if (target_type == TYPE_USER)
138                 {
139                         userrec* u = (userrec*)dest;
140                         if ((strchr(u->modes,'R')) && (!strchr(user->modes,'r')))
141                         {
142                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
143                                 {
144                                         // user is ulined, can speak regardless
145                                         return 0;
146                                 }
147                                 // user messaging a +R user and is not registered
148                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need a registered nickname to message this user");
149                                 return 1;
150                         }
151                 }
152                 return 0;
153         }
154         
155         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
156         {
157                 if (target_type == TYPE_CHANNEL)
158                 {
159                         chanrec* c = (chanrec*)dest;
160                         if ((c->IsCustomModeSet('M')) && (!strchr(user->modes,'r')))
161                         {
162                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
163                                 {
164                                         // user is ulined, can speak regardless
165                                         return 0;
166                                 }
167                                 // user noticing a +M channel and is not registered
168                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need a registered nickname to speak on this channel");
169                                 return 1;
170                         }
171                 }
172                 if (target_type == TYPE_USER)
173                 {
174                         userrec* u = (userrec*)dest;
175                         if ((strchr(u->modes,'R')) && (!strchr(user->modes,'r')))
176                         {
177                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
178                                 {
179                                         // user is ulined, can speak regardless
180                                         return 0;
181                                 }
182                                 // user noticing a +R user and is not registered
183                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need a registered nickname to message this user");
184                                 return 1;
185                         }
186                 }
187                 return 0;
188         }
189         
190         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
191         {
192                 if (chan)
193                 {
194                         if (chan->IsCustomModeSet('R'))
195                         {
196                                 if (!strchr(user->modes,'r'))
197                                 {
198                                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
199                                         {
200                                                 // user is ulined, won't be stopped from joining
201                                                 return 0;
202                                         }
203                                         // joining a +R channel and not identified
204                                         Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need a registered nickname to join this channel");
205                                         return 1;
206                                 }
207                         }
208                 }
209                 return 0;
210         }
211
212         virtual ~ModuleServices()
213         {
214         }
215         
216         virtual Version GetVersion()
217         {
218                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
219         }
220 };
221
222
223 class ModuleServicesFactory : public ModuleFactory
224 {
225  public:
226         ModuleServicesFactory()
227         {
228         }
229         
230         ~ModuleServicesFactory()
231         {
232         }
233         
234         virtual Module * CreateModule(Server* Me)
235         {
236                 return new ModuleServices(Me);
237         }
238         
239 };
240
241
242 extern "C" void * init_module( void )
243 {
244         return new ModuleServicesFactory;
245 }
246