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(const ListModeBase::ModeList* list)
75 CheckContext::List modelist(*this, "modelist");
76 for (ListModeBase::ModeList::const_iterator i = list->begin(); i != list->end(); ++i)
77 modelist.Add(i->mask);
82 void DumpExt(Extensible* ext)
84 CheckContext::List extlist(*this, "metadata");
85 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); ++i)
87 ExtensionItem* item = i->first;
88 std::string value = item->serialize(FORMAT_USER, ext, i->second);
90 Write("meta:" + item->name, value);
91 else if (!item->name.empty())
92 extlist.Add(item->name);
98 class List : public Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>
101 List(CheckContext& context, const char* checktype)
102 : 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)
104 GetNumeric().push(checktype).push(std::string());
111 class CommandCheck : public Command
113 UserModeReference snomaskmode;
115 std::string GetSnomasks(User* user)
119 ret = snomaskmode->GetUserParameter(user);
126 static std::string GetAllowedOperOnlyModes(LocalUser* user, ModeType modetype)
129 const ModeParser::ModeHandlerMap& modes = ServerInstance->Modes.GetModes(modetype);
130 for (ModeParser::ModeHandlerMap::const_iterator i = modes.begin(); i != modes.end(); ++i)
132 const ModeHandler* const mh = i->second;
133 if ((mh->NeedsOper()) && (user->HasModePermission(mh)))
134 ret.push_back(mh->GetModeChar());
140 CommandCheck(Module* parent)
141 : Command(parent,"CHECK", 1)
142 , snomaskmode(parent, "snomask")
144 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
147 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
149 if (parameters.size() > 1 && !irc::equals(parameters[1], ServerInstance->Config->ServerName))
154 std::string chliststr;
156 targuser = ServerInstance->FindNick(parameters[0]);
157 targchan = ServerInstance->FindChan(parameters[0]);
160 * Syntax of a /check reply:
161 * :server.name 802 target START <target>
162 * :server.name 802 target <field> :<value>
163 * :server.name 802 target END <target>
166 // Constructor sends START, destructor sends END
167 CheckContext context(user, parameters[0]);
171 LocalUser* loctarg = IS_LOCAL(targuser);
172 /* /check on a user */
173 context.Write("nuh", targuser->GetFullHost());
174 context.Write("realnuh", targuser->GetFullRealHost());
175 context.Write("realname", targuser->GetRealName());
176 context.Write("modes", targuser->GetModeLetters());
177 context.Write("snomasks", GetSnomasks(targuser));
178 context.Write("server", targuser->server->GetName());
179 context.Write("uid", targuser->uuid);
180 context.Write("signon", targuser->signon);
181 context.Write("nickts", targuser->age);
183 context.Write("lastmsg", loctarg->idle_lastmsg);
185 if (targuser->IsAway())
188 context.Write("awaytime", targuser->awaytime);
189 context.Write("awaymsg", targuser->awaymsg);
192 if (targuser->IsOper())
194 OperInfo* oper = targuser->oper;
195 /* user is an oper of type ____ */
196 context.Write("opertype", oper->name);
199 std::string umodes = GetAllowedOperOnlyModes(loctarg, MODETYPE_USER);
200 std::string cmodes = GetAllowedOperOnlyModes(loctarg, MODETYPE_CHANNEL);
201 context.Write("modeperms", "user=" + umodes + " channel=" + cmodes);
203 CheckContext::List opcmdlist(context, "commandperms");
204 opcmdlist.Add(oper->AllowedOperCommands.ToString());
206 CheckContext::List privlist(context, "permissions");
207 privlist.Add(oper->AllowedPrivs.ToString());
214 context.Write("clientaddr", loctarg->client_sa.str());
215 context.Write("serveraddr", loctarg->server_sa.str());
217 std::string classname = loctarg->GetClass()->name;
218 if (!classname.empty())
219 context.Write("connectclass", classname);
222 context.Write("onip", targuser->GetIPString());
224 CheckContext::List chanlist(context, "onchans");
225 for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
227 Membership* memb = *i;
228 Channel* c = memb->chan;
229 char prefix = memb->GetPrefixChar();
231 chliststr.push_back(prefix);
232 chliststr.append(c->name);
233 chanlist.Add(chliststr);
239 context.DumpExt(targuser);
243 /* /check on a channel */
244 context.Write("timestamp", targchan->age);
246 if (!targchan->topic.empty())
248 /* there is a topic, assume topic related information exists */
249 context.Write("topic", targchan->topic);
250 context.Write("topic_setby", targchan->setby);
251 context.Write("topic_setat", targchan->topicset);
254 context.Write("modes", targchan->ChanModes(true));
255 context.Write("membercount", ConvToStr(targchan->GetUserCounter()));
257 /* now the ugly bit, spool current members of a channel. :| */
259 const Channel::MemberMap& ulist = targchan->GetUsers();
261 /* note that unlike /names, we do NOT check +i vs in the channel */
262 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
265 * Unlike Asuka, I define a clone as coming from the same host. --w00t
267 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
268 context.Write("member", InspIRCd::Format("%-3u %s%s (%s@%s) %s ", clonecount.global,
269 i->second->GetAllPrefixChars().c_str(), i->first->nick.c_str(),
270 i->first->ident.c_str(), i->first->GetDisplayedHost().c_str(), i->first->GetRealName().c_str()));
273 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
274 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
275 context.DumpListMode((*i)->GetList(targchan));
277 context.DumpExt(targchan);
281 /* /check on an IP address, or something that doesn't exist */
284 /* hostname or other */
285 const user_hash& users = ServerInstance->Users->GetUsers();
286 for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
288 if (InspIRCd::Match(a->second->GetRealHost(), parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->GetDisplayedHost(), parameters[0], ascii_case_insensitive_map))
290 /* host or vhost matches mask */
291 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
294 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
297 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
301 context.Write("matches", ConvToStr(x));
304 // END is sent by the CheckContext destructor
308 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
310 if ((parameters.size() > 1) && (parameters[1].find('.') != std::string::npos))
311 return ROUTE_OPT_UCAST(parameters[1]);
312 return ROUTE_LOCALONLY;
316 class ModuleCheck : public Module
318 CommandCheck mycommand;
320 ModuleCheck() : mycommand(this)
324 Version GetVersion() CXX11_OVERRIDE
326 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
330 MODULE_INIT(ModuleCheck)