]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Updates, should be able to safely unload client modules with queries in progress...
[user/henk/code/inspircd.git] / src / modules / m_services_account.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 ircu-style services accounts, including chmode +R, etc. */
28
29 class AChannel_R : public ModeHandler
30 {
31  public:
32         AChannel_R() : ModeHandler('R', 0, 0, false, MODETYPE_CHANNEL, false) { }
33
34         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
35         {
36                 if (adding)
37                 {
38                         if (!channel->IsModeSet('R'))
39                         {
40                                 channel->SetMode('R',true);
41                                 return MODEACTION_ALLOW;
42                         }
43                 }
44                 else
45                 {
46                         if (channel->IsModeSet('R'))
47                         {
48                                 channel->SetMode('R',false);
49                                 return MODEACTION_ALLOW;
50                         }
51                 }
52
53                 return MODEACTION_DENY;
54         }
55 };
56
57 class AUser_R : public ModeHandler
58 {
59  public:
60         AUser_R() : ModeHandler('R', 0, 0, false, MODETYPE_USER, false) { }
61
62         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
63         {
64                 if (adding)
65                 {
66                         if (!dest->IsModeSet('R'))
67                         {
68                                 dest->SetMode('R',true);
69                                 return MODEACTION_ALLOW;
70                         }
71                 }
72                 else
73                 {
74                         if (dest->IsModeSet('R'))
75                         {
76                                 dest->SetMode('R',false);
77                                 return MODEACTION_ALLOW;
78                         }
79                 }
80
81                 return MODEACTION_DENY;
82         }
83 };
84
85 class AChannel_M : public ModeHandler
86 {
87  public:
88         AChannel_M() : ModeHandler('M', 0, 0, false, MODETYPE_CHANNEL, false) { }
89
90         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
91         {
92                 if (adding)
93                 {
94                         if (!channel->IsModeSet('M'))
95                         {
96                                 channel->SetMode('M',true);
97                                 return MODEACTION_ALLOW;
98                         }
99                 }
100                 else
101                 {
102                         if (channel->IsModeSet('M'))
103                         {
104                                 channel->SetMode('M',true);
105                                 return MODEACTION_ALLOW;
106                         }
107                 }
108
109                 return MODEACTION_DENY;
110         }
111 };
112
113 class ModuleServicesAccount : public Module
114 {
115         Server *Srv; 
116         AChannel_R* m1;
117         AChannel_M* m2;
118         AUser_R* m3;
119  public:
120         ModuleServicesAccount(Server* Me) : Module::Module(Me)
121         {
122                 Srv = Me;
123                 m1 = new AChannel_R();
124                 m2 = new AChannel_M();
125                 m3 = new AUser_R();
126                 Srv->AddMode(m1, 'R');
127                 Srv->AddMode(m2, 'M');
128                 Srv->AddMode(m3, 'R');
129         }
130
131         virtual void On005Numeric(std::string &output)
132         {
133                 InsertMode(output, "RM", 4);
134         }
135
136         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
137         virtual void OnWhois(userrec* source, userrec* dest)
138         {
139                 std::string *account;
140                 dest->GetExt("accountname", account);
141
142                 if (account)
143                 {
144                         WriteServ(source->fd, "330 %s %s %s :is logged in as", source->nick, dest->nick, account->c_str());
145                 }
146         }
147
148         void Implements(char* List)
149         {
150                 List[I_OnWhois] = List[I_OnUserPreMessage] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreJoin] = 1;
151                 List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = List[I_OnDecodeMetaData] = 1;
152         }
153
154         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
155         {
156                 std::string *account;
157                 user->GetExt("accountname", account);
158                 
159                 if (target_type == TYPE_CHANNEL)
160                 {
161                         chanrec* c = (chanrec*)dest;
162                         
163                         if ((c->IsModeSet('M')) && (!account))
164                         {
165                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
166                                 {
167                                         // user is ulined, can speak regardless
168                                         return 0;
169                                 }
170
171                                 // user messaging a +M channel and is not registered
172                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
173                                 return 1;
174                         }
175                 }
176                 if (target_type == TYPE_USER)
177                 {
178                         userrec* u = (userrec*)dest;
179                         
180                         if ((u->modes['R'-65]) && (!account))
181                         {
182                                 if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
183                                 {
184                                         // user is ulined, can speak regardless
185                                         return 0;
186                                 }
187
188                                 // user messaging a +R user and is not registered
189                                 Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(u->nick)+" :You need to be identified to a registered account to message this user");
190                                 return 1;
191                         }
192                 }
193                 return 0;
194         }
195          
196         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
197         {
198                 return OnUserPreMessage(user, dest, target_type, text, status);
199         }
200          
201         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
202         {
203                 std::string *account;
204                 user->GetExt("accountname", account);
205                 
206                 if (chan)
207                 {
208                         if (chan->IsModeSet('R'))
209                         {
210                                 if (!account)
211                                 {
212                                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)))
213                                         {
214                                                 // user is ulined, won't be stopped from joining
215                                                 return 0;
216                                         }
217                                         // joining a +R channel and not identified
218                                         Srv->SendServ(user->fd,"477 "+std::string(user->nick)+" "+std::string(chan->name)+" :You need to be identified to a registered account to join this channel");
219                                         return 1;
220                                 }
221                         }
222                 }
223                 return 0;
224         }
225         
226         // Whenever the linking module wants to send out data, but doesnt know what the data
227         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
228         // this method is called. We should use the ProtoSendMetaData function after we've
229         // corrected decided how the data should look, to send the metadata on its way if
230         // it is ours.
231         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
232         {
233                 // check if the linking module wants to know about OUR metadata
234                 if (extname == "accountname")
235                 {
236                         // check if this user has an swhois field to send
237                         std::string* account;
238                         user->GetExt("accountname", account);
239                         if (account)
240                         {
241                                 // call this function in the linking module, let it format the data how it
242                                 // sees fit, and send it on its way. We dont need or want to know how.
243                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*account);
244                         }
245                 }
246         }
247
248         // when a user quits, tidy up their metadata
249         virtual void OnUserQuit(userrec* user, const std::string &message)
250         {
251                 std::string* account;
252                 user->GetExt("accountname", account);
253                 if (account)
254                 {
255                         user->Shrink("accountname");
256                         delete account;
257                 }
258         }
259
260         // if the module is unloaded, tidy up all our dangling metadata
261         virtual void OnCleanup(int target_type, void* item)
262         {
263                 if (target_type == TYPE_USER)
264                 {
265                         userrec* user = (userrec*)item;
266                         std::string* account;
267                         user->GetExt("accountname", account);
268                         if (account)
269                         {
270                                 user->Shrink("accountname");
271                                 delete account;
272                         }
273                 }
274         }
275
276         // Whenever the linking module receives metadata from another server and doesnt know what
277         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
278         // module in turn to figure out if this metadata key belongs to them, and what they want
279         // to do with it.
280         // In our case we're only sending a single string around, so we just construct a std::string.
281         // Some modules will probably get much more complex and format more detailed structs and classes
282         // in a textual way for sending over the link.
283         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
284         {
285                 // check if its our metadata key, and its associated with a user
286                 if ((target_type == TYPE_USER) && (extname == "accountname"))
287                 {       
288                         userrec* dest = (userrec*)target;
289                         /* logging them out? */
290                         if (extdata == "")
291                         {
292                                 std::string* account;
293                                 dest->GetExt("accountname", account);
294                                 if (account)
295                                 {
296                                         dest->Shrink("accountname");
297                                         delete account;
298                                 }
299                         }
300                         else
301                         {
302                                 // if they dont already have an accountname field, accept the remote server's
303                                 std::string* text;
304                                 if (!dest->GetExt("accountname", text))
305                                 {
306                                         text = new std::string(extdata);
307                                         dest->Extend("accountname", text);
308                                 }
309                         }
310                 }
311         }
312
313         virtual ~ModuleServicesAccount()
314         {
315                 DELETE(m1);
316                 DELETE(m2);
317                 DELETE(m3);
318         }
319         
320         virtual Version GetVersion()
321         {
322                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
323         }
324 };
325
326
327 class ModuleServicesAccountFactory : public ModuleFactory
328 {
329  public:
330         ModuleServicesAccountFactory()
331         {
332         }
333         
334         ~ModuleServicesAccountFactory()
335         {
336         }
337         
338         virtual Module * CreateModule(Server* Me)
339         {
340                 return new ModuleServicesAccount(Me);
341         }
342         
343 };
344
345
346 extern "C" void * init_module( void )
347 {
348         return new ModuleServicesAccountFactory;
349 }