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