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/>.
28 class CommandCheck : public Command
30 UserModeReference snomaskmode;
32 std::string GetSnomasks(User* user)
36 ret = snomaskmode->GetUserParameter(user);
43 static void dumpListMode(User* user, const std::string& checkstr, const ListModeBase::ModeList* list)
48 std::string buf = checkstr + " modelist";
49 const std::string::size_type headlen = buf.length();
50 const size_t maxline = ServerInstance->Config->Limits.MaxLine;
51 for (ListModeBase::ModeList::const_iterator i = list->begin(); i != list->end(); ++i)
53 if (buf.size() + i->mask.size() + 1 > maxline)
58 buf.append(" ").append(i->mask);
60 if (buf.length() > headlen)
65 CommandCheck(Module* parent)
66 : Command(parent,"CHECK", 1)
67 , snomaskmode(parent, "snomask")
69 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
72 std::string timestring(time_t time)
75 struct tm *mytime = gmtime(&time);
76 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (", mytime);
77 std::string ret(timebuf);
78 ret.append(ConvToStr(time)).push_back(')');
82 void dumpExt(User* user, const std::string& checkstr, Extensible* ext)
84 std::stringstream dumpkeys;
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 user->SendText(checkstr + " meta:" + item->name + " " + value);
91 else if (!item->name.empty())
92 dumpkeys << " " << item->name;
94 if (!dumpkeys.str().empty())
95 user->SendText(checkstr + " metadata", dumpkeys);
98 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
100 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName.c_str())
105 std::string checkstr;
106 std::string chliststr;
108 checkstr = ":" + ServerInstance->Config->ServerName + " 304 " + user->nick + " :CHECK";
110 targuser = ServerInstance->FindNick(parameters[0]);
111 targchan = ServerInstance->FindChan(parameters[0]);
114 * Syntax of a /check reply:
115 * :server.name 304 target :CHECK START <target>
116 * :server.name 304 target :CHECK <field> <value>
117 * :server.name 304 target :CHECK END
120 user->SendText(checkstr + " START " + parameters[0]);
124 LocalUser* loctarg = IS_LOCAL(targuser);
125 /* /check on a user */
126 user->SendText(checkstr + " nuh " + targuser->GetFullHost());
127 user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost());
128 user->SendText(checkstr + " realname " + targuser->fullname);
129 user->SendText(checkstr + " modes +" + targuser->FormatModes());
130 user->SendText(checkstr + " snomasks " + GetSnomasks(targuser));
131 user->SendText(checkstr + " server " + targuser->server->GetName());
132 user->SendText(checkstr + " uid " + targuser->uuid);
133 user->SendText(checkstr + " signon " + timestring(targuser->signon));
134 user->SendText(checkstr + " nickts " + timestring(targuser->age));
136 user->SendText(checkstr + " lastmsg " + timestring(loctarg->idle_lastmsg));
138 if (targuser->IsAway())
141 user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime));
142 user->SendText(checkstr + " awaymsg " + targuser->awaymsg);
145 if (targuser->IsOper())
147 OperInfo* oper = targuser->oper;
148 /* user is an oper of type ____ */
149 user->SendText(checkstr + " opertype " + oper->name);
154 for(char c='A'; c <= 'z'; c++)
156 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
157 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
159 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
160 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
163 user->SendText(checkstr + " modeperms user=" + umodes + " channel=" + cmodes);
165 for (OperInfo::PrivSet::const_iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); ++i)
167 opcmds.push_back(' ');
170 std::stringstream opcmddump(opcmds);
171 user->SendText(checkstr + " commandperms", opcmddump);
173 for (OperInfo::PrivSet::const_iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); ++i)
175 privs.push_back(' ');
178 std::stringstream privdump(privs);
179 user->SendText(checkstr + " permissions", privdump);
185 user->SendText(checkstr + " clientaddr " + loctarg->client_sa.str());
186 user->SendText(checkstr + " serveraddr " + loctarg->server_sa.str());
188 std::string classname = loctarg->GetClass()->name;
189 if (!classname.empty())
190 user->SendText(checkstr + " connectclass " + classname);
193 user->SendText(checkstr + " onip " + targuser->GetIPString());
195 for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
197 Membership* memb = *i;
198 Channel* c = memb->chan;
199 char prefix = memb->GetPrefixChar();
201 chliststr.push_back(prefix);
202 chliststr.append(c->name).push_back(' ');
205 std::stringstream dump(chliststr);
207 user->SendText(checkstr + " onchans", dump);
209 dumpExt(user, checkstr, targuser);
213 /* /check on a channel */
214 user->SendText(checkstr + " timestamp " + timestring(targchan->age));
216 if (targchan->topic[0] != 0)
218 /* there is a topic, assume topic related information exists */
219 user->SendText(checkstr + " topic " + targchan->topic);
220 user->SendText(checkstr + " topic_setby " + targchan->setby);
221 user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset));
224 user->SendText(checkstr + " modes " + targchan->ChanModes(true));
225 user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
227 /* now the ugly bit, spool current members of a channel. :| */
229 const Channel::MemberMap& ulist = targchan->GetUsers();
231 /* note that unlike /names, we do NOT check +i vs in the channel */
232 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
235 * Unlike Asuka, I define a clone as coming from the same host. --w00t
237 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
238 user->SendText("%s member %-3u %s%s (%s@%s) %s ",
239 checkstr.c_str(), clonecount.global,
240 i->second->GetAllPrefixChars(), i->first->nick.c_str(),
241 i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
244 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
245 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
246 dumpListMode(user, checkstr, (*i)->GetList(targchan));
248 dumpExt(user, checkstr, targchan);
252 /* /check on an IP address, or something that doesn't exist */
255 /* hostname or other */
256 const user_hash& users = ServerInstance->Users->GetUsers();
257 for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
259 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
261 /* host or vhost matches mask */
262 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
265 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
268 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
272 user->SendText(checkstr + " matches " + ConvToStr(x));
275 user->SendText(checkstr + " END " + parameters[0]);
280 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
282 if (parameters.size() > 1)
283 return ROUTE_OPT_UCAST(parameters[1]);
284 return ROUTE_LOCALONLY;
288 class ModuleCheck : public Module
290 CommandCheck mycommand;
292 ModuleCheck() : mycommand(this)
296 Version GetVersion() CXX11_OVERRIDE
298 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
302 MODULE_INIT(ModuleCheck)