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