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