2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2006-2007 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 const std::string& target;
37 std::string FormatTime(time_t ts)
39 std::string timestr(InspIRCd::TimeString(ts, "%Y-%m-%d %H:%M:%S UTC (", true));
40 timestr.append(ConvToStr(ts));
41 timestr.push_back(')');
46 CheckContext(User* u, const std::string& targetstr)
50 Write("START", target);
58 void Write(const std::string& type, const std::string& text)
60 user->WriteRemoteNumeric(RPL_CHECK, type, text);
63 void Write(const std::string& type, time_t ts)
65 user->WriteRemoteNumeric(RPL_CHECK, type, FormatTime(ts));
68 User* GetUser() const { return user; }
70 void DumpListMode(ListModeBase* mode, Channel* chan)
72 const ListModeBase::ModeList* list = mode->GetList(chan);
76 for (ListModeBase::ModeList::const_iterator i = list->begin(); i != list->end(); ++i)
78 CheckContext::List listmode(*this, "listmode");
79 listmode.Add(ConvToStr(mode->GetModeChar()));
80 listmode.Add(i->mask);
81 listmode.Add(i->setter);
82 listmode.Add(FormatTime(i->time));
87 void DumpExt(Extensible* ext)
89 CheckContext::List extlist(*this, "metadata");
90 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); ++i)
92 ExtensionItem* item = i->first;
93 std::string value = item->serialize(FORMAT_USER, ext, i->second);
95 Write("meta:" + item->name, value);
96 else if (!item->name.empty())
97 extlist.Add(item->name);
103 class List : public Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>
106 List(CheckContext& context, const char* checktype)
107 : Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>(Numeric::WriteRemoteNumericSink(context.GetUser()), RPL_CHECK, false, (IS_LOCAL(context.GetUser()) ? context.GetUser()->nick.length() : ServerInstance->Config->Limits.NickMax) + strlen(checktype) + 1)
109 GetNumeric().push(checktype).push(std::string());
116 class CommandCheck : public Command
118 UserModeReference snomaskmode;
120 std::string GetSnomasks(User* user)
124 ret = snomaskmode->GetUserParameter(user);
131 static std::string GetAllowedOperOnlyModes(LocalUser* user, ModeType modetype)
134 const ModeParser::ModeHandlerMap& modes = ServerInstance->Modes.GetModes(modetype);
135 for (ModeParser::ModeHandlerMap::const_iterator i = modes.begin(); i != modes.end(); ++i)
137 const ModeHandler* const mh = i->second;
138 if ((mh->NeedsOper()) && (user->HasModePermission(mh)))
139 ret.push_back(mh->GetModeChar());
145 CommandCheck(Module* parent)
146 : Command(parent,"CHECK", 1)
147 , snomaskmode(parent, "snomask")
149 flags_needed = 'o'; syntax = "<nick>|<ipmask>|<hostmask>|<channel> [<servername>]";
152 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
154 if (parameters.size() > 1 && !irc::equals(parameters[1], ServerInstance->Config->ServerName))
159 std::string chliststr;
161 targuser = ServerInstance->FindNick(parameters[0]);
162 targchan = ServerInstance->FindChan(parameters[0]);
165 * Syntax of a /check reply:
166 * :server.name 802 target START <target>
167 * :server.name 802 target <field> :<value>
168 * :server.name 802 target END <target>
171 // Constructor sends START, destructor sends END
172 CheckContext context(user, parameters[0]);
176 LocalUser* loctarg = IS_LOCAL(targuser);
177 /* /check on a user */
178 context.Write("nuh", targuser->GetFullHost());
179 context.Write("realnuh", targuser->GetFullRealHost());
180 context.Write("realname", targuser->GetRealName());
181 context.Write("modes", targuser->GetModeLetters());
182 context.Write("snomasks", GetSnomasks(targuser));
183 context.Write("server", targuser->server->GetName());
184 context.Write("uid", targuser->uuid);
185 context.Write("signon", targuser->signon);
186 context.Write("nickts", targuser->age);
188 context.Write("lastmsg", loctarg->idle_lastmsg);
190 if (targuser->IsAway())
193 context.Write("awaytime", targuser->awaytime);
194 context.Write("awaymsg", targuser->awaymsg);
197 if (targuser->IsOper())
199 OperInfo* oper = targuser->oper;
200 /* user is an oper of type ____ */
201 context.Write("opertype", oper->name);
204 context.Write("chanmodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_CHANNEL));
205 context.Write("usermodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_USER));
206 context.Write("commandperms", oper->AllowedOperCommands.ToString());
207 context.Write("permissions", oper->AllowedPrivs.ToString());
213 context.Write("clientaddr", loctarg->client_sa.str());
214 context.Write("serveraddr", loctarg->server_sa.str());
216 std::string classname = loctarg->GetClass()->name;
217 if (!classname.empty())
218 context.Write("connectclass", classname);
221 context.Write("onip", targuser->GetIPString());
223 CheckContext::List chanlist(context, "onchans");
224 for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
226 Membership* memb = *i;
227 Channel* c = memb->chan;
228 char prefix = memb->GetPrefixChar();
230 chliststr.push_back(prefix);
231 chliststr.append(c->name);
232 chanlist.Add(chliststr);
238 context.DumpExt(targuser);
242 /* /check on a channel */
243 context.Write("createdat", targchan->age);
245 if (!targchan->topic.empty())
247 /* there is a topic, assume topic related information exists */
248 context.Write("topic", targchan->topic);
249 context.Write("topic_setby", targchan->setby);
250 context.Write("topic_setat", targchan->topicset);
253 context.Write("modes", targchan->ChanModes(true));
254 context.Write("membercount", ConvToStr(targchan->GetUserCounter()));
256 /* now the ugly bit, spool current members of a channel. :| */
258 const Channel::MemberMap& ulist = targchan->GetUsers();
260 /* note that unlike /names, we do NOT check +i vs in the channel */
261 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
264 * Unlike Asuka, I define a clone as coming from the same host. --w00t
266 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
267 context.Write("member", InspIRCd::Format("%u %s%s (%s)", clonecount.global,
268 i->second->GetAllPrefixChars().c_str(), i->first->GetFullHost().c_str(),
269 i->first->GetRealName().c_str()));
272 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
273 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
274 context.DumpListMode(*i, targchan);
276 context.DumpExt(targchan);
280 /* /check on an IP address, or something that doesn't exist */
283 /* hostname or other */
284 const user_hash& users = ServerInstance->Users->GetUsers();
285 for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
287 if (InspIRCd::Match(a->second->GetRealHost(), parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->GetDisplayedHost(), parameters[0], ascii_case_insensitive_map))
289 /* host or vhost matches mask */
290 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
293 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
296 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
300 context.Write("matches", ConvToStr(x));
303 // END is sent by the CheckContext destructor
307 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
309 if ((parameters.size() > 1) && (parameters[1].find('.') != std::string::npos))
310 return ROUTE_OPT_UCAST(parameters[1]);
311 return ROUTE_LOCALONLY;
315 class ModuleCheck : public Module
317 CommandCheck mycommand;
319 ModuleCheck() : mycommand(this)
323 Version GetVersion() CXX11_OVERRIDE
325 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
329 MODULE_INIT(ModuleCheck)