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