]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_services_account.cpp
Added ExtBan U to match only Unregistered users
[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->nick.c_str()) || ServerInstance->ULine(source->server))
41                 {
42                         // Only change the mode if it's not redundant
43                         if ((adding && !channel->IsModeSet('r')) || (!adding && channel->IsModeSet('r')))
44                         {
45                                 channel->SetMode('r',adding);
46                                 return MODEACTION_ALLOW;
47                         }
48
49                         return MODEACTION_DENY;
50                 }
51                 else
52                 {
53                         source->WriteNumeric(500, "%s :Only a server may modify the +r channel mode", source->nick.c_str());
54                         return MODEACTION_DENY;
55                 }
56         }
57 };
58
59 /** User mode +r - mark a user as identified
60  */
61 class User_r : public ModeHandler
62 {
63
64  public:
65         User_r(Module* Creator) : ModeHandler(Creator, "u_registered", 'r', PARAM_NONE, MODETYPE_USER) { }
66
67         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
68         {
69                 if (!IS_LOCAL(source) || ServerInstance->ULine(source->nick.c_str()) || ServerInstance->ULine(source->server))
70                 {
71                         if ((adding && !dest->IsModeSet('r')) || (!adding && dest->IsModeSet('r')))
72                         {
73                                 dest->SetMode('r',adding);
74                                 return MODEACTION_ALLOW;
75                         }
76                         return MODEACTION_DENY;
77                 }
78                 else
79                 {
80                         source->WriteNumeric(500, "%s :Only a server may modify the +r user mode", source->nick.c_str());
81                         return MODEACTION_DENY;
82                 }
83         }
84 };
85
86 /** Channel mode +R - unidentified users cannot join
87  */
88 class AChannel_R : public SimpleChannelModeHandler
89 {
90  public:
91         AChannel_R(Module* Creator) : SimpleChannelModeHandler(Creator, "reginvite", 'R') { }
92 };
93
94 /** User mode +R - unidentified users cannot message
95  */
96 class AUser_R : public SimpleUserModeHandler
97 {
98  public:
99         AUser_R(Module* Creator) : SimpleUserModeHandler(Creator, "regdeaf", 'R') { }
100 };
101
102 /** Channel mode +M - unidentified users cannot message channel
103  */
104 class AChannel_M : public SimpleChannelModeHandler
105 {
106  public:
107         AChannel_M(Module* Creator) : SimpleChannelModeHandler(Creator, "regmoderated", 'M') { }
108 };
109
110 class ModuleServicesAccount : public Module
111 {
112         AChannel_R m1;
113         AChannel_M m2;
114         AUser_R m3;
115         Channel_r m4;
116         User_r m5;
117         AccountExtItem accountname;
118  public:
119         ModuleServicesAccount() : m1(this), m2(this), m3(this), m4(this), m5(this),
120                 accountname("accountname", this)
121         {
122         }
123
124         void init()
125         {
126                 ServerInstance->Modules->AddService(m1);
127                 ServerInstance->Modules->AddService(m2);
128                 ServerInstance->Modules->AddService(m3);
129                 ServerInstance->Modules->AddService(m4);
130                 ServerInstance->Modules->AddService(m5);
131                 ServerInstance->Modules->AddService(accountname);
132                 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin, I_OnCheckBan,
133                         I_OnDecodeMetaData, I_On005Numeric, I_OnUserPostNick, I_OnSetConnectClass };
134
135                 ServerInstance->Modules->Attach(eventlist, this, 9);
136         }
137
138         void On005Numeric(std::string &t)
139         {
140                 ServerInstance->AddExtBanChar('R');
141                 ServerInstance->AddExtBanChar('U');
142         }
143
144         /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
145         void OnWhois(User* source, User* dest)
146         {
147                 std::string *account = accountname.get(dest);
148
149                 if (account)
150                 {
151                         ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
152                 }
153
154                 if (dest->IsModeSet('r'))
155                 {
156                         /* user is registered */
157                         ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick.c_str(), dest->nick.c_str());
158                 }
159         }
160
161         void OnUserPostNick(User* user, const std::string &oldnick)
162         {
163                 /* On nickchange, if they have +r, remove it */
164                 if (user->IsModeSet('r') && assign(user->nick) != oldnick)
165                 {
166                         std::vector<std::string> modechange;
167                         modechange.push_back(user->nick);
168                         modechange.push_back("-r");
169                         ServerInstance->SendMode(modechange, ServerInstance->FakeClient);
170                 }
171         }
172
173         ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
174         {
175                 if (!IS_LOCAL(user))
176                         return MOD_RES_PASSTHRU;
177
178                 std::string *account = accountname.get(user);
179                 bool is_registered = account && !account->empty();
180
181                 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
182                 {
183                         // user is ulined, can speak regardless
184                         return MOD_RES_PASSTHRU;
185                 }
186
187                 if (target_type == TYPE_CHANNEL)
188                 {
189                         Channel* c = (Channel*)dest;
190                         ModResult res = ServerInstance->OnCheckExemption(user,c,"regmoderated");
191
192                         if (c->IsModeSet('M') && !is_registered && res != MOD_RES_ALLOW)
193                         {
194                                 // user messaging a +M channel and is not registered
195                                 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
196                                 return MOD_RES_DENY;
197                         }
198                 }
199                 else if (target_type == TYPE_USER)
200                 {
201                         User* u = (User*)dest;
202
203                         if (u->IsModeSet('R') && !is_registered)
204                         {
205                                 // user messaging a +R user and is not registered
206                                 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
207                                 return MOD_RES_DENY;
208                         }
209                 }
210                 return MOD_RES_PASSTHRU;
211         }
212
213         ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
214         {
215                 if (mask[1] == ':')
216                 {
217                         if (mask[0] == 'R')
218                         {
219                                 std::string *account = accountname.get(user);
220                                 if (account && InspIRCd::Match(*account, mask.substr(2)))
221                                         return MOD_RES_DENY;
222                         }
223                         else if (mask[0] == 'U')
224                         {
225                                 std::string *account = accountname.get(user);
226                                 /* If the user is registered we don't care. */
227                                 if (account)
228                                         return MOD_RES_PASSTHRU;
229
230                                 /* If we made it this far we know the user isn't registered
231                                         so just deny if it matches */
232                                 if (chan->GetExtBanStatus(user, 'U') == MOD_RES_DENY)
233                                         return MOD_RES_DENY;
234                         }
235                 }
236
237                 /* If we made it this far then the ban wasn't an ExtBan
238                         or the user we were checking for didn't match either ExtBan */
239                 return MOD_RES_PASSTHRU;
240         }
241
242         ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
243         {
244                 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
245         }
246
247         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
248         {
249                 if (!IS_LOCAL(user))
250                         return MOD_RES_PASSTHRU;
251
252                 std::string *account = accountname.get(user);
253                 bool is_registered = account && !account->empty();
254
255                 if (chan)
256                 {
257                         if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
258                         {
259                                 // user is ulined, won't be stopped from joining
260                                 return MOD_RES_PASSTHRU;
261                         }
262
263                         if (chan->IsModeSet('R'))
264                         {
265                                 if (!is_registered)
266                                 {
267                                         // joining a +R channel and not identified
268                                         user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
269                                         return MOD_RES_DENY;
270                                 }
271                         }
272                 }
273                 return MOD_RES_PASSTHRU;
274         }
275
276         // Whenever the linking module receives metadata from another server and doesnt know what
277         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
278         // module in turn to figure out if this metadata key belongs to them, and what they want
279         // to do with it.
280         // In our case we're only sending a single string around, so we just construct a std::string.
281         // Some modules will probably get much more complex and format more detailed structs and classes
282         // in a textual way for sending over the link.
283         void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
284         {
285                 User* dest = dynamic_cast<User*>(target);
286                 // check if its our metadata key, and its associated with a user
287                 if (dest && (extname == "accountname"))
288                 {
289                         std::string *account = accountname.get(dest);
290                         if (account && !account->empty())
291                         {
292                                 trim(*account);
293
294                                 if (IS_LOCAL(dest))
295                                         dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
296                                                 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
297
298                                 AccountEvent(this, dest, *account).Send();
299                         }
300                         else
301                         {
302                                 AccountEvent(this, dest, "").Send();
303                         }
304                 }
305         }
306
307         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
308         {
309                 if (myclass->config->getBool("requireaccount") && !accountname.get(user))
310                         return MOD_RES_DENY;
311                 return MOD_RES_PASSTHRU;
312         }
313
314         Version GetVersion()
315         {
316                 return Version("Provides support for ircu-style services accounts, including chmode +R, etc.",VF_OPTCOMMON|VF_VENDOR);
317         }
318 };
319
320 MODULE_INIT(ModuleServicesAccount)