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/>.
34 const std::string& target;
37 CheckContext(User* u, const std::string& targetstr)
41 Write("START", target);
49 void Write(const std::string& type, const std::string& text)
51 user->WriteRemoteNumeric(RPL_CHECK, type, text);
54 User* GetUser() const { return user; }
56 void DumpListMode(const ListModeBase::ModeList* list)
61 CheckContext::List modelist(*this, "modelist");
62 for (ListModeBase::ModeList::const_iterator i = list->begin(); i != list->end(); ++i)
63 modelist.Add(i->mask);
68 void DumpExt(Extensible* ext)
70 CheckContext::List extlist(*this, "metadata");
71 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); ++i)
73 ExtensionItem* item = i->first;
74 std::string value = item->serialize(FORMAT_USER, ext, i->second);
76 Write("meta:" + item->name, value);
77 else if (!item->name.empty())
78 extlist.Add(item->name);
84 class List : public Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>
87 List(CheckContext& context, const char* checktype)
88 : 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)
90 GetNumeric().push(checktype).push(std::string());
97 class CommandCheck : public Command
99 UserModeReference snomaskmode;
101 std::string GetSnomasks(User* user)
105 ret = snomaskmode->GetUserParameter(user);
112 static std::string GetAllowedOperOnlyModes(LocalUser* user, ModeType modetype)
115 const ModeParser::ModeHandlerMap& modes = ServerInstance->Modes.GetModes(modetype);
116 for (ModeParser::ModeHandlerMap::const_iterator i = modes.begin(); i != modes.end(); ++i)
118 const ModeHandler* const mh = i->second;
119 if ((mh->NeedsOper()) && (user->HasModePermission(mh)))
120 ret.push_back(mh->GetModeChar());
126 CommandCheck(Module* parent)
127 : Command(parent,"CHECK", 1)
128 , snomaskmode(parent, "snomask")
130 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
133 std::string timestring(time_t time)
136 struct tm *mytime = gmtime(&time);
137 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (", mytime);
138 std::string ret(timebuf);
139 ret.append(ConvToStr(time)).push_back(')');
143 CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
145 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName)
150 std::string chliststr;
152 targuser = ServerInstance->FindNick(parameters[0]);
153 targchan = ServerInstance->FindChan(parameters[0]);
156 * Syntax of a /check reply:
157 * :server.name 802 target START <target>
158 * :server.name 802 target <field> :<value>
159 * :server.name 802 target END <target>
162 // Constructor sends START, destructor sends END
163 CheckContext context(user, parameters[0]);
167 LocalUser* loctarg = IS_LOCAL(targuser);
168 /* /check on a user */
169 context.Write("nuh", targuser->GetFullHost());
170 context.Write("realnuh", targuser->GetFullRealHost());
171 context.Write("realname", targuser->fullname);
172 context.Write("modes", targuser->GetModeLetters());
173 context.Write("snomasks", GetSnomasks(targuser));
174 context.Write("server", targuser->server->GetName());
175 context.Write("uid", targuser->uuid);
176 context.Write("signon", timestring(targuser->signon));
177 context.Write("nickts", timestring(targuser->age));
179 context.Write("lastmsg", timestring(loctarg->idle_lastmsg));
181 if (targuser->IsAway())
184 context.Write("awaytime", timestring(targuser->awaytime));
185 context.Write("awaymsg", targuser->awaymsg);
188 if (targuser->IsOper())
190 OperInfo* oper = targuser->oper;
191 /* user is an oper of type ____ */
192 context.Write("opertype", oper->name);
195 std::string umodes = GetAllowedOperOnlyModes(loctarg, MODETYPE_USER);
196 std::string cmodes = GetAllowedOperOnlyModes(loctarg, MODETYPE_CHANNEL);
197 context.Write("modeperms", "user=" + umodes + " channel=" + cmodes);
199 CheckContext::List opcmdlist(context, "commandperms");
200 opcmdlist.Add(oper->AllowedOperCommands.ToString());
202 CheckContext::List privlist(context, "permissions");
203 opcmdlist.Add(oper->AllowedPrivs.ToString());
210 context.Write("clientaddr", loctarg->client_sa.str());
211 context.Write("serveraddr", loctarg->server_sa.str());
213 std::string classname = loctarg->GetClass()->name;
214 if (!classname.empty())
215 context.Write("connectclass", classname);
218 context.Write("onip", targuser->GetIPString());
220 CheckContext::List chanlist(context, "onchans");
221 for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
223 Membership* memb = *i;
224 Channel* c = memb->chan;
225 char prefix = memb->GetPrefixChar();
227 chliststr.push_back(prefix);
228 chliststr.append(c->name);
229 chanlist.Add(chliststr);
235 context.DumpExt(targuser);
239 /* /check on a channel */
240 context.Write("timestamp", timestring(targchan->age));
242 if (!targchan->topic.empty())
244 /* there is a topic, assume topic related information exists */
245 context.Write("topic", targchan->topic);
246 context.Write("topic_setby", targchan->setby);
247 context.Write("topic_setat", timestring(targchan->topicset));
250 context.Write("modes", targchan->ChanModes(true));
251 context.Write("membercount", ConvToStr(targchan->GetUserCounter()));
253 /* now the ugly bit, spool current members of a channel. :| */
255 const Channel::MemberMap& ulist = targchan->GetUsers();
257 /* note that unlike /names, we do NOT check +i vs in the channel */
258 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
261 * Unlike Asuka, I define a clone as coming from the same host. --w00t
263 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
264 context.Write("member", InspIRCd::Format("%-3u %s%s (%s@%s) %s ", clonecount.global,
265 i->second->GetAllPrefixChars().c_str(), i->first->nick.c_str(),
266 i->first->ident.c_str(), i->first->GetDisplayedHost().c_str(), i->first->fullname.c_str()));
269 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
270 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
271 context.DumpListMode((*i)->GetList(targchan));
273 context.DumpExt(targchan);
277 /* /check on an IP address, or something that doesn't exist */
280 /* hostname or other */
281 const user_hash& users = ServerInstance->Users->GetUsers();
282 for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
284 if (InspIRCd::Match(a->second->GetRealHost(), parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->GetDisplayedHost(), parameters[0], ascii_case_insensitive_map))
286 /* host or vhost matches mask */
287 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
290 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
293 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
297 context.Write("matches", ConvToStr(x));
300 // END is sent by the CheckContext destructor
304 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) CXX11_OVERRIDE
306 if ((parameters.size() > 1) && (parameters[1].find('.') != std::string::npos))
307 return ROUTE_OPT_UCAST(parameters[1]);
308 return ROUTE_LOCALONLY;
312 class ModuleCheck : public Module
314 CommandCheck mycommand;
316 ModuleCheck() : mycommand(this)
320 Version GetVersion() CXX11_OVERRIDE
322 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
326 MODULE_INIT(ModuleCheck)