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