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