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