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(Module* Creator) : ModeHandler(Creator, "registered", 'r', PARAM_NONE, MODETYPE_CHANNEL) { }
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(Module* Creator) : ModeHandler(Creator, "registered", 'r', PARAM_NONE, MODETYPE_USER) { }
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(Module* Creator) : SimpleChannelModeHandler(Creator, "reginvite", 'R') { }
84 /** User mode +R - unidentified users cannot message
86 class AUser_R : public SimpleUserModeHandler
89 AUser_R(Module* Creator) : SimpleUserModeHandler(Creator, "regdeaf", 'R') { }
92 /** Channel mode +M - unidentified users cannot message channel
94 class AChannel_M : public SimpleChannelModeHandler
97 AChannel_M(Module* Creator) : SimpleChannelModeHandler(Creator, "regmoderated", 'M') { }
100 class ModuleServicesAccount : public Module
107 AccountExtItem accountname;
109 ModuleServicesAccount() : m1(this), m2(this), m3(this), m4(this), m5(this),
110 accountname("accountname", this)
113 if (!ServerInstance->Modes->AddMode(&m1) || !ServerInstance->Modes->AddMode(&m2) ||
114 !ServerInstance->Modes->AddMode(&m3) || !ServerInstance->Modes->AddMode(&m4) ||
115 !ServerInstance->Modes->AddMode(&m5))
116 throw ModuleException("Some other module has claimed our modes!");
118 ServerInstance->Extensions.Register(&accountname);
119 Implementation eventlist[] = { I_OnWhois, I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserPreJoin, I_OnCheckBan,
120 I_OnSyncUser, I_OnUserQuit, I_OnDecodeMetaData, I_On005Numeric, I_OnUserPostNick };
122 ServerInstance->Modules->Attach(eventlist, this, 10);
125 void On005Numeric(std::string &t)
127 ServerInstance->AddExtBanChar('R');
130 /* <- :twisted.oscnet.org 330 w00t2 w00t2 w00t :is logged in as */
131 void OnWhois(User* source, User* dest)
133 std::string *account = accountname.get(dest);
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 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 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 = accountname.get(user);
165 bool is_registered = account && !account->empty();
167 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
169 // user is ulined, can speak regardless
170 return MOD_RES_PASSTHRU;
173 if (target_type == TYPE_CHANNEL)
175 Channel* c = (Channel*)dest;
177 FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,c,"regmoderated"));
179 if (c->IsModeSet('M') && !is_registered && res != MOD_RES_ALLOW)
181 // user messaging a +M channel and is not registered
182 user->WriteNumeric(477, ""+std::string(user->nick)+" "+std::string(c->name)+" :You need to be identified to a registered account to message this channel");
186 else if (target_type == TYPE_USER)
188 User* u = (User*)dest;
190 if (u->IsModeSet('R') && !is_registered)
192 // user messaging a +R user and is not registered
193 user->WriteNumeric(477, ""+ user->nick +" "+ u->nick +" :You need to be identified to a registered account to message this user");
197 return MOD_RES_PASSTHRU;
200 ModResult OnCheckBan(User* user, Channel* chan, const std::string& mask)
202 if (mask[0] == 'R' && mask[1] == ':')
204 std::string *account = accountname.get(user);
205 if (account && InspIRCd::Match(*account, mask.substr(2)))
208 return MOD_RES_PASSTHRU;
211 ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
213 return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
216 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
219 return MOD_RES_PASSTHRU;
221 std::string *account = accountname.get(user);
222 bool is_registered = account && !account->empty();
226 if ((ServerInstance->ULine(user->nick.c_str())) || (ServerInstance->ULine(user->server)))
228 // user is ulined, won't be stopped from joining
229 return MOD_RES_PASSTHRU;
232 if (chan->IsModeSet('R'))
236 // joining a +R channel and not identified
237 user->WriteNumeric(477, user->nick + " " + chan->name + " :You need to be identified to a registered account to join this channel");
242 return MOD_RES_PASSTHRU;
245 // Whenever the linking module receives metadata from another server and doesnt know what
246 // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
247 // module in turn to figure out if this metadata key belongs to them, and what they want
249 // In our case we're only sending a single string around, so we just construct a std::string.
250 // Some modules will probably get much more complex and format more detailed structs and classes
251 // in a textual way for sending over the link.
252 void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
254 User* dest = dynamic_cast<User*>(target);
255 // check if its our metadata key, and its associated with a user
256 if (dest && (extname == "accountname"))
258 std::string *account = accountname.get(dest);
259 if (account && !account->empty())
264 dest->WriteNumeric(900, "%s %s %s :You are now logged in as %s",
265 dest->nick.c_str(), dest->GetFullHost().c_str(), account->c_str(), account->c_str());
267 AccountEvent(this, dest, *account).Send();
272 ~ModuleServicesAccount()
278 return Version("Povides support for ircu-style services accounts, including chmode +R, etc.",VF_COMMON|VF_VENDOR);
282 MODULE_INIT(ModuleServicesAccount)