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