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