]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
5b92c8a2666d82d28ac660fbf4b699113500dca7
[user/henk/code/inspircd.git] / src / modules / m_services_account.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2012 Shawn Smith <shawn@inspircd.org>
5  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8  *   Copyright (C) 2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 /* $ModDesc: Provides support for ircu-style services accounts, including chmode +R, etc. */
26
27 #include "inspircd.h"
28 #include "account.h"
29
30 /** Channel mode +r - mark a channel as identified
31  */
32 class Channel_r : public ModeHandler
33 {
34  public:
35         Channel_r(Module* Creator) : ModeHandler(Creator, "c_registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
36
37         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
38         {
39                 // only a u-lined server may add or remove the +r mode.
40                 if (!IS_LOCAL(source) || ServerInstance->ULine(source->server))
41                 {
42                         // Only change the mode if it's not redundant
43                         if ((adding != channel->IsModeSet('r')))
44                         {
45                                 channel->SetMode('r',adding);
46                                 return MODEACTION_ALLOW;
47                         }
48                 }
49                 else
50                 {
51                         source->WriteNumeric(500, "%s :Only a server may modify the +r channel mode", source->nick.c_str());
52                 }
53                 return MODEACTION_DENY;
54         }
55 };
56
57 /** User mode +r - mark a user as identified
58  */
59 class User_r : public ModeHandler
60 {
61
62  public:
63         User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
64
65         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
66         {
67                 if (!IS_LOCAL(source) || ServerInstance->ULine(source->server))
68                 {
69                         if ((adding != dest->IsModeSet('r')))
70                         {
71                                 dest->SetMode('r',adding);
72                                 return MODEACTION_ALLOW;
73                         }
74                 }
75                 else
76                 {
77                         source->WriteNumeric(500, "%s :Only a server may modify the +r user mode", source->nick.c_str());
78                 }
79                 return MODEACTION_DENY;
80         }
81 };
82
83 /** Channel mode +R - unidentified users cannot join
84  */
85 class AChannel_R : public SimpleChannelModeHandler
86 {
87  public:
88         AChannel_R(Module* Creator) : SimpleChannelModeHandler(Creator, "reginvite", 'R') { }
89 };
90
91 /** User mode +R - unidentified users cannot message
92  */
93 class AUser_R : public SimpleUserModeHandler
94 {
95  public:
96         AUser_R(Module* Creator) : SimpleUserModeHandler(Creator, "regdeaf", 'R') { }
97 };
98
99 /** Channel mode +M - unidentified users cannot message channel
100  */
101 class AChannel_M : public SimpleChannelModeHandler
102 {
103  public:
104         AChannel_M(Module* Creator) : SimpleChannelModeHandler(Creator, "regmoderated", 'M') { }
105 };
106
107 class ModuleServicesAccount : public Module
108 {
109         AChannel_R m1;
110         AChannel_M m2;
111         AUser_R m3;
112         Channel_r m4;
113         User_r m5;
114         AccountExtItem accountname;
115  public:
116         ModuleServicesAccount() : m1(this), m2(this), m3(this), m4(this), m5(this),
117                 accountname("accountname", this)
118         {
119         }
120
121         void init()
122         {
123                 ServerInstance->Modules->AddService(m1);
124                 ServerInstance->Modules->AddService(m2);
125                 ServerInstance->Modules->AddService(m3);
126                 ServerInstance->Modules->AddService(m4);
127                 ServerInstance->Modules->AddService(m5);
128                 ServerInstance->Modules->AddService(accountname);
129                 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin, I_OnCheckBan,
130                         I_OnDecodeMetaData, I_On005Numeric, I_OnUserPostNick, I_OnSetConnectClass };
131
132                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
133         }
134
135         void On005Numeric(std::string &t)
136         {
137                 ServerInstance->AddExtBanChar('R');
138                 ServerInstance->AddExtBanChar('U');
139         }
140
141         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
142         void OnWhois(User* source, User* dest)
143         {
144                 std::string *account = accountname.get(dest);
145
146                 if (account)
147                 {
148                         ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
149                 }
150
151                 if (dest->IsModeSet('r'))
152                 {
153                         /* user is registered */
154                         ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick.c_str(), dest->nick.c_str());
155                 }
156         }
157
158         void OnUserPostNick(User* user, const std::string &oldnick)
159         {
160                 /* On nickchange, if they have +r, remove it */
161                 if (user->IsModeSet('r') && assign(user->nick) != oldnick)
162                 {
163                         std::vector<std::string> modechange;
164                         modechange.push_back(user->nick);
165                         modechange.push_back("-r");
166                         ServerInstance->SendMode(modechange, ServerInstance->FakeClient);
167                 }
168         }
169
170         ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
171         {
172                 if (!IS_LOCAL(user))
173                         return MOD_RES_PASSTHRU;
174
175                 std::string *account = accountname.get(user);
176                 bool is_registered = account && !account->empty();
177
178                 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
179                 {
180                         // user is ulined, can speak regardless
181                         return MOD_RES_PASSTHRU;
182                 }
183
184                 if (target_type == TYPE_CHANNEL)
185                 {
186                         Channel* c = (Channel*)dest;
187                         ModResult res = ServerInstance->OnCheckExemption(user,c,"regmoderated");
188
189                         if (c->IsModeSet('M') && !is_registered && res != MOD_RES_ALLOW)
190                         {
191                                 // user messaging a +M channel and is not registered
192                                 user->WriteNumeric(477, user->nick+" "+c->name+" :You need to be identified to a registered account to message this channel");
193                                 return MOD_RES_DENY;
194                         }
195                 }
196                 else if (target_type == TYPE_USER)
197                 {
198                         User* u = (User*)dest;
199
200                         if (u->IsModeSet('R') && !is_registered)
201                         {
202                                 // user messaging a +R user and is not registered
203                                 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
204                                 return MOD_RES_DENY;
205                         }
206                 }
207                 return MOD_RES_PASSTHRU;
208         }
209
210         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
211         {
212                 static bool checking = false;
213                 if (checking)
214                         return MOD_RES_PASSTHRU;
215
216                 if ((mask.length() > 2) && (mask[1] == ':'))
217                 {
218                         if (mask[0] == 'R')
219                         {
220                                 std::string *account = accountname.get(user);
221                                 if (account && InspIRCd::Match(*account, mask.substr(2)))
222                                         return MOD_RES_DENY;
223                         }
224                         else if (mask[0] == 'U')
225                         {
226                                 std::string *account = accountname.get(user);
227                                 /* If the user is registered we don't care. */
228                                 if (account)
229                                         return MOD_RES_PASSTHRU;
230
231                                 /* If we made it this far we know the user isn't registered
232                                         so just deny if it matches */
233                                 checking = true;
234                                 bool result = chan->CheckBan(user, mask.substr(2));
235                                 checking = false;
236
237                                 if (result)
238                                         return MOD_RES_DENY;
239                         }
240                 }
241
242                 /* If we made it this far then the ban wasn't an ExtBan
243                         or the user we were checking for didn't match either ExtBan */
244                 return MOD_RES_PASSTHRU;
245         }
246
247         ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
248         {
249                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
250         }
251
252         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
253         {
254                 if (!IS_LOCAL(user))
255                         return MOD_RES_PASSTHRU;
256
257                 std::string *account = accountname.get(user);
258                 bool is_registered = account && !account->empty();
259
260                 if (chan)
261                 {
262                         if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
263                         {
264                                 // user is ulined, won't be stopped from joining
265                                 return MOD_RES_PASSTHRU;
266                         }
267
268                         if (chan->IsModeSet('R'))
269                         {
270                                 if (!is_registered)
271                                 {
272                                         // joining a +R channel and not identified
273                                         user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
274                                         return MOD_RES_DENY;
275                                 }
276                         }
277                 }
278                 return MOD_RES_PASSTHRU;
279         }
280
281         // Whenever the linking module receives metadata from another server and doesnt know what
282         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
283         // module in turn to figure out if this metadata key belongs to them, and what they want
284         // to do with it.
285         // In our case we're only sending a single string around, so we just construct a std::string.
286         // Some modules will probably get much more complex and format more detailed structs and classes
287         // in a textual way for sending over the link.
288         void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
289         {
290                 User* dest = dynamic_cast<User*>(target);
291                 // check if its our metadata key, and its associated with a user
292                 if (dest && (extname == "accountname"))
293                 {
294                         std::string *account = accountname.get(dest);
295                         if (account && !account->empty())
296                         {
297                                 trim(*account);
298
299                                 if (IS_LOCAL(dest))
300                                         dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
301                                                 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
302
303                                 AccountEvent(this, dest, *account).Send();
304                         }
305                         else
306                         {
307                                 AccountEvent(this, dest, "").Send();
308                         }
309                 }
310         }
311
312         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
313         {
314                 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
315                         return MOD_RES_DENY;
316                 return MOD_RES_PASSTHRU;
317         }
318
319         Version GetVersion()
320         {
321                 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
322         }
323 };
324
325 MODULE_INIT(ModuleServicesAccount)