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