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 context.Write("chanmodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_CHANNEL));
200 context.Write("usermodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_USER));
201 context.Write("commandperms", oper->AllowedOperCommands.ToString());
202 context.Write("permissions", oper->AllowedPrivs.ToString());
208 context.Write("clientaddr", loctarg->client_sa.str());
209 context.Write("serveraddr", loctarg->server_sa.str());
211 std::string classname = loctarg->GetClass()->name;
212 if (!classname.empty())
213 context.Write("connectclass", classname);
216 context.Write("onip", targuser->GetIPString());
218 CheckContext::List chanlist(context, "onchans");
219 for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
221 Membership* memb = *i;
222 Channel* c = memb->chan;
223 char prefix = memb->GetPrefixChar();
225 chliststr.push_back(prefix);
226 chliststr.append(c->name);
227 chanlist.Add(chliststr);
233 context.DumpExt(targuser);
237 /* /check on a channel */
238 context.Write("timestamp", targchan->age);
240 if (!targchan->topic.empty())
242 /* there is a topic, assume topic related information exists */
243 context.Write("topic", targchan->topic);
244 context.Write("topic_setby", targchan->setby);
245 context.Write("topic_setat", targchan->topicset);
248 context.Write("modes", targchan->ChanModes(true));
249 context.Write("membercount", ConvToStr(targchan->GetUserCounter()));
251 /* now the ugly bit, spool current members of a channel. :| */
253 const Channel::MemberMap& ulist = targchan->GetUsers();
255 /* note that unlike /names, we do NOT check +i vs in the channel */
256 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
259 * Unlike Asuka, I define a clone as coming from the same host. --w00t
261 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
262 context.Write("member", InspIRCd::Format("%-3u %s%s (%s@%s) %s ", clonecount.global,
263 i->second->GetAllPrefixChars().c_str(), i->first->nick.c_str(),
264 i->first->ident.c_str(), i->first->GetDisplayedHost().c_str(), i->first->GetRealName().c_str()));
267 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
268 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
269 context.DumpListMode((*i)->GetList(targchan));
271 context.DumpExt(targchan);
275 /* /check on an IP address, or something that doesn't exist */
278 /* hostname or other */
279 const user_hash& users = ServerInstance->Users->GetUsers();
280 for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
282 if (InspIRCd::Match(a->second->GetRealHost(), parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->GetDisplayedHost(), parameters[0], ascii_case_insensitive_map))
284 /* host or vhost matches mask */
285 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
288 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
291 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
295 context.Write("matches", ConvToStr(x));
298 // END is sent by the CheckContext destructor
302 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
304 if ((parameters.size() > 1) && (parameters[1].find('.') != std::string::npos))
305 return ROUTE_OPT_UCAST(parameters[1]);
306 return ROUTE_LOCALONLY;
310 class ModuleCheck : public Module
312 CommandCheck mycommand;
314 ModuleCheck() : mycommand(this)
318 Version GetVersion() CXX11_OVERRIDE
320 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
324 MODULE_INIT(ModuleCheck)