1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Povides support for ircu-style services accounts, including chmode +R, etc. */
19 /** Channel mode +r - mark a channel as identified
21 class Channel_r : public ModeHandler
25 Channel_r(InspIRCd* Instance, Module* Creator) : ModeHandler(Instance, Creator, 'r', 0, 0, false, MODETYPE_CHANNEL, false) { }
27 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
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))
32 // Only change the mode if it's not redundant
33 if ((adding && !channel->IsModeSet('r')) || (!adding && channel->IsModeSet('r')))
35 channel->SetMode('r',adding);
36 return MODEACTION_ALLOW;
39 return MODEACTION_DENY;
43 source->WriteNumeric(500, "%s :Only a server may modify the +r channel mode", source->nick.c_str());
44 return MODEACTION_DENY;
49 /** User mode +r - mark a user as identified
51 class User_r : public ModeHandler
55 User_r(InspIRCd* Instance, Module* Creator) : ModeHandler(Instance, Creator, 'r', 0, 0, false, MODETYPE_USER, false) { }
57 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
59 if (IS_REMOTE(source) || ServerInstance->ULine(source->nick.c_str()) || ServerInstance->ULine(source->server))
61 if ((adding && !dest->IsModeSet('r')) || (!adding && dest->IsModeSet('r')))
63 dest->SetMode('r',adding);
64 return MODEACTION_ALLOW;
66 return MODEACTION_DENY;
70 source->WriteNumeric(500, "%s :Only a server may modify the +r user mode", source->nick.c_str());
71 return MODEACTION_DENY;
76 /** Channel mode +R - unidentified users cannot join
78 class AChannel_R : public SimpleChannelModeHandler
81 AChannel_R(InspIRCd* Instance, Module* Creator) : SimpleChannelModeHandler(Instance, Creator, 'R') { }
84 /** User mode +R - unidentified users cannot message
86 class AUser_R : public SimpleUserModeHandler
89 AUser_R(InspIRCd* Instance, Module* Creator) : SimpleUserModeHandler(Instance, Creator, 'R') { }
92 /** Channel mode +M - unidentified users cannot message channel
94 class AChannel_M : public SimpleChannelModeHandler
97 AChannel_M(InspIRCd* Instance, Module* Creator) : SimpleChannelModeHandler(Instance, Creator, 'M') { }
100 class ModuleServicesAccount : public Module
108 ModuleServicesAccount(InspIRCd* Me) : Module(Me),
109 m1(Me, this), m2(Me, this), m3(Me, this), m4(Me, this), m5(Me, this)
112 if (!ServerInstance->Modes->AddMode(&m1) || !ServerInstance->Modes->AddMode(&m2) ||
113 !ServerInstance->Modes->AddMode(&m3) || !ServerInstance->Modes->AddMode(&m4) ||
114 !ServerInstance->Modes->AddMode(&m5))
115 throw ModuleException("Some other module has claimed our modes!");
117 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin, I_OnCheckBan,
118 I_OnSyncUser, I_OnUserQuit, I_OnCleanup, I_OnDecodeMetaData, I_On005Numeric, I_OnUserPostNick };
120 ServerInstance->Modules->Attach(eventlist, this, 10);
123 virtual void On005Numeric(std::string &t)
125 ServerInstance->AddExtBanChar('R');
126 ServerInstance->AddExtBanChar('M');
129 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
130 virtual void OnWhois(User* source, User* dest)
132 std::string *account;
133 dest->GetExt("accountname", account);
137 ServerInstance->SendWhoisLine(source, dest, 330, "%s %s %s :is logged in as", source->nick.c_str(), dest->nick.c_str(), account->c_str());
140 if (dest->IsModeSet('r'))
142 /* user is registered */
143 ServerInstance->SendWhoisLine(source, dest, 307, "%s %s :is a registered nick", source->nick.c_str(), dest->nick.c_str());
147 virtual void OnUserPostNick(User* user, const std::string &oldnick)
149 /* On nickchange, if they have +r, remove it */
150 if (user->IsModeSet('r') && assign(user->nick) != oldnick)
152 std::vector<std::string> modechange;
153 modechange.push_back(user->nick);
154 modechange.push_back("-r");
155 ServerInstance->SendMode(modechange, user);
159 virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
162 return MOD_RES_PASSTHRU;
164 std::string *account;
165 bool is_registered = user->GetExt("accountname", account);
166 is_registered = is_registered && !account->empty();
168 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
170 // user is ulined, can speak regardless
171 return MOD_RES_PASSTHRU;
174 if (target_type == TYPE_CHANNEL)
176 Channel* c = (Channel*)dest;
178 if (c->IsModeSet('M') && !is_registered)
180 // user messaging a +M channel and is not registered
181 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
187 if (c->GetExtBanStatus(*account, 'M') == MOD_RES_DENY)
189 // may not speak (text is deliberately vague, so they don't know which restriction to evade)
190 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You may not speak in this channel");
195 else if (target_type == TYPE_USER)
197 User* u = (User*)dest;
199 if (u->IsModeSet('R') && !is_registered)
201 // user messaging a +R user and is not registered
202 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
206 return MOD_RES_PASSTHRU;
209 virtual ModResult OnCheckBan(User* user, Channel* chan)
211 std::string* account;
212 if (!user->GetExt("accountname", account))
213 return MOD_RES_PASSTHRU;
214 return chan->GetExtBanStatus(*account, 'R');
217 virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
219 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
222 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
225 return MOD_RES_PASSTHRU;
227 std::string *account;
228 bool is_registered = user->GetExt("accountname", account);
229 is_registered = is_registered && !account->empty();
233 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
235 // user is ulined, won't be stopped from joining
236 return MOD_RES_PASSTHRU;
239 if (chan->IsModeSet('R'))
243 // joining a +R channel and not identified
244 user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
249 return MOD_RES_PASSTHRU;
252 // Whenever the linking module wants to send out data, but doesnt know what the data
253 // represents (e.g. it is metadata, added to a User or Channel by a module) then
254 // this method is called. We should use the ProtoSendMetaData function after we've
255 // corrected decided how the data should look, to send the metadata on its way if
257 virtual void OnSyncUser(User* user, Module* proto, void* opaque)
259 // check if this user has an swhois field to send
260 std::string* account;
261 user->GetExt("accountname", account);
264 // remove any accidental leading/trailing spaces
267 // call this function in the linking module, let it format the data how it
268 // sees fit, and send it on its way. We dont need or want to know how.
269 proto->ProtoSendMetaData(opaque,user,"accountname",*account);
273 // when a user quits, tidy up their metadata
274 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
276 std::string* account;
277 user->GetExt("accountname", account);
280 user->Shrink("accountname");
285 // if the module is unloaded, tidy up all our dangling metadata
286 virtual void OnCleanup(int target_type, void* item)
288 if (target_type == TYPE_USER)
290 User* user = (User*)item;
291 std::string* account;
292 user->GetExt("accountname", account);
295 user->Shrink("accountname");
301 // Whenever the linking module receives metadata from another server and doesnt know what
302 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
303 // module in turn to figure out if this metadata key belongs to them, and what they want
305 // In our case we're only sending a single string around, so we just construct a std::string.
306 // Some modules will probably get much more complex and format more detailed structs and classes
307 // in a textual way for sending over the link.
308 virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
310 User* dest = dynamic_cast<User*>(target);
311 // check if its our metadata key, and its associated with a user
312 if (dest && (extname == "accountname"))
314 std::string* account;
315 if (dest->GetExt("accountname", account)) {
316 // remove old account so that we can set new (or leave unset)
317 dest->Shrink("accountname");
321 if (!extdata.empty())
323 account = new std::string(extdata);
324 // remove any accidental leading/trailing spaces
326 dest->Extend("accountname", account);
329 dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
330 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
334 ac.account = *account;
335 Event n((char*)&ac, this, "account_login");
336 n.Send(ServerInstance);
341 virtual ~ModuleServicesAccount()
343 ServerInstance->Modes->DelMode(&m1);
344 ServerInstance->Modes->DelMode(&m2);
345 ServerInstance->Modes->DelMode(&m3);
346 ServerInstance->Modes->DelMode(&m4);
347 ServerInstance->Modes->DelMode(&m5);
350 virtual Version GetVersion()
352 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
356 MODULE_INIT(ModuleServicesAccount)