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