]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services.cpp
22d3242b7718fda657ec11208a3370fc57178ec1
[user/henk/code/inspircd.git] / src / modules / m_services.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
65         {
66                 
67                 if (modechar == 'r')
68                 {
69                         if (type == MT_CHANNEL)
70                         {
71                                 // only a u-lined server may add or remove the +r mode.
72                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"") || (strchr(user->nick,'.'))))
73                                 {
74                                         log(DEBUG,"Allowing umode +r, server and nick are: '%s','%s'",user->nick,user->server);
75                                         return 1;
76                                 }
77                                 else
78                                 {
79                                         log(DEBUG,"Only a server can set chanmode +r, server and nick are: '%s','%s'",user->nick,user->server);
80                                         Srv->SendServ(user->fd,"500 "+std::string(user->nick)+" :Only a server may modify the +r channel mode");
81                                 }
82                         }
83                         else
84                         {
85                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"") || (strchr(user->nick,'.'))))
86                                 {
87                                         log(DEBUG,"Allowing umode +r, server and nick are: '%s','%s'",user->nick,user->server);
88                                         return 1;
89                                 }
90                                 else
91                                 {
92                                         log(DEBUG,"Only a server can set umode +r, server and nick are: '%s','%s'",user->nick,user->server);
93                                         Srv->SendServ(user->fd,"500 "+std::string(user->nick)+" :Only a server may modify the +r user mode");
94                                 }
95                         }
96                 }
97                 else if (modechar == 'R')
98                 {
99                         if (type == MT_CHANNEL)
100                         {
101                                 return 1;
102                         }
103                 }
104                 else if (modechar == 'M')
105                 {
106                         if (type == MT_CHANNEL)
107                         {
108                                 return 1;
109                         }
110                 }
111
112                 return 0;
113         }
114
115         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
116         {
117                 if (target_type == TYPE_CHANNEL)
118                 {
119                         chanrec* c = (chanrec*)dest;
120                         if ((c->IsCustomModeSet('M')) && (!strchr(user->modes,'r')))
121                         {
122                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
123                                 {
124                                         // user is ulined, can speak regardless
125                                         return 0;
126                                 }
127                                 // user messaging a +M channel and is not registered
128                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need a registered nickname to speak on this channel");
129                                 return 1;
130                         }
131                 }
132                 if (target_type == TYPE_USER)
133                 {
134                         userrec* u = (userrec*)dest;
135                         if ((strchr(u->modes,'R')) && (!strchr(user->modes,'r')))
136                         {
137                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
138                                 {
139                                         // user is ulined, can speak regardless
140                                         return 0;
141                                 }
142                                 // user messaging a +R user and is not registered
143                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need a registered nickname to message this user");
144                                 return 1;
145                         }
146                 }
147                 return 0;
148         }
149         
150         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
151         {
152                 if (target_type == TYPE_CHANNEL)
153                 {
154                         chanrec* c = (chanrec*)dest;
155                         if ((c->IsCustomModeSet('M')) && (!strchr(user->modes,'r')))
156                         {
157                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
158                                 {
159                                         // user is ulined, can speak regardless
160                                         return 0;
161                                 }
162                                 // user noticing a +M channel and is not registered
163                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need a registered nickname to speak on this channel");
164                                 return 1;
165                         }
166                 }
167                 if (target_type == TYPE_USER)
168                 {
169                         userrec* u = (userrec*)dest;
170                         if ((strchr(u->modes,'R')) && (!strchr(user->modes,'r')))
171                         {
172                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
173                                 {
174                                         // user is ulined, can speak regardless
175                                         return 0;
176                                 }
177                                 // user noticing a +R user and is not registered
178                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need a registered nickname to message this user");
179                                 return 1;
180                         }
181                 }
182                 return 0;
183         }
184         
185         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
186         {
187                 if (chan)
188                 {
189                         if (chan->IsCustomModeSet('R'))
190                         {
191                                 if (!strchr(user->modes,'r'))
192                                 {
193                                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
194                                         {
195                                                 // user is ulined, won't be stopped from joining
196                                                 return 0;
197                                         }
198                                         // joining a +R channel and not identified
199                                         Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need a registered nickname to join this channel");
200                                         return 1;
201                                 }
202                         }
203                 }
204                 return 0;
205         }
206
207         virtual ~ModuleServices()
208         {
209         }
210         
211         virtual Version GetVersion()
212         {
213                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
214         }
215         
216         virtual void OnUserConnect(userrec* user)
217         {
218         }
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