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