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);
113 CommandCheck(Module* parent)
114 : Command(parent,"CHECK", 1)
115 , snomaskmode(parent, "snomask")
117 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
120 std::string timestring(time_t time)
123 struct tm *mytime = gmtime(&time);
124 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (", mytime);
125 std::string ret(timebuf);
126 ret.append(ConvToStr(time)).push_back(')');
130 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
132 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName)
137 std::string chliststr;
139 targuser = ServerInstance->FindNick(parameters[0]);
140 targchan = ServerInstance->FindChan(parameters[0]);
143 * Syntax of a /check reply:
144 * :server.name 802 target START <target>
145 * :server.name 802 target <field> :<value>
146 * :server.name 802 target END <target>
149 // Constructor sends START, destructor sends END
150 CheckContext context(user, parameters[0]);
154 LocalUser* loctarg = IS_LOCAL(targuser);
155 /* /check on a user */
156 context.Write("nuh", targuser->GetFullHost());
157 context.Write("realnuh", targuser->GetFullRealHost());
158 context.Write("realname", targuser->fullname);
159 context.Write("modes", std::string("+") + targuser->FormatModes());
160 context.Write("snomasks", GetSnomasks(targuser));
161 context.Write("server", targuser->server->GetName());
162 context.Write("uid", targuser->uuid);
163 context.Write("signon", timestring(targuser->signon));
164 context.Write("nickts", timestring(targuser->age));
166 context.Write("lastmsg", timestring(loctarg->idle_lastmsg));
168 if (targuser->IsAway())
171 context.Write("awaytime", timestring(targuser->awaytime));
172 context.Write("awaymsg", targuser->awaymsg);
175 if (targuser->IsOper())
177 OperInfo* oper = targuser->oper;
178 /* user is an oper of type ____ */
179 context.Write("opertype", oper->name);
184 for(char c='A'; c <= 'z'; c++)
186 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
187 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
189 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
190 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
193 context.Write("modeperms", "user=" + umodes + " channel=" + cmodes);
195 CheckContext::List opcmdlist(context, "commandperms");
196 for (OperInfo::PrivSet::const_iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); ++i)
199 CheckContext::List privlist(context, "permissions");
200 for (OperInfo::PrivSet::const_iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); ++i)
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", timestring(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", timestring(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(), i->first->nick.c_str(),
264 i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.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->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, 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->fullname);
288 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
291 context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
295 context.Write("matches", ConvToStr(x));
298 // END is sent by the CheckContext destructor
302 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
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)