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 (%s)", mytime);
77 return std::string(timebuf);
80 void dumpExt(User* user, const std::string& checkstr, Extensible* ext)
82 std::stringstream dumpkeys;
83 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
85 ExtensionItem* item = i->first;
86 std::string value = item->serialize(FORMAT_USER, ext, i->second);
88 user->SendText(checkstr + " meta:" + item->name + " " + value);
89 else if (!item->name.empty())
90 dumpkeys << " " << item->name;
92 if (!dumpkeys.str().empty())
93 user->SendText(checkstr + " metadata", dumpkeys);
96 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
98 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName.c_str())
103 std::string checkstr;
104 std::string chliststr;
106 checkstr = ":" + ServerInstance->Config->ServerName + " 304 " + user->nick + " :CHECK";
108 targuser = ServerInstance->FindNick(parameters[0]);
109 targchan = ServerInstance->FindChan(parameters[0]);
112 * Syntax of a /check reply:
113 * :server.name 304 target :CHECK START <target>
114 * :server.name 304 target :CHECK <field> <value>
115 * :server.name 304 target :CHECK END
118 user->SendText(checkstr + " START " + parameters[0]);
122 LocalUser* loctarg = IS_LOCAL(targuser);
123 /* /check on a user */
124 user->SendText(checkstr + " nuh " + targuser->GetFullHost());
125 user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost());
126 user->SendText(checkstr + " realname " + targuser->fullname);
127 user->SendText(checkstr + " modes +" + targuser->FormatModes());
128 user->SendText(checkstr + " snomasks " + GetSnomasks(targuser));
129 user->SendText(checkstr + " server " + targuser->server->GetName());
130 user->SendText(checkstr + " uid " + targuser->uuid);
131 user->SendText(checkstr + " signon " + timestring(targuser->signon));
132 user->SendText(checkstr + " nickts " + timestring(targuser->age));
134 user->SendText(checkstr + " lastmsg " + timestring(loctarg->idle_lastmsg));
136 if (targuser->IsAway())
139 user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime));
140 user->SendText(checkstr + " awaymsg " + targuser->awaymsg);
143 if (targuser->IsOper())
145 OperInfo* oper = targuser->oper;
146 /* user is an oper of type ____ */
147 user->SendText(checkstr + " opertype " + oper->name);
152 for(char c='A'; c <= 'z'; c++)
154 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
155 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
157 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
158 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
161 user->SendText(checkstr + " modeperms user=" + umodes + " channel=" + cmodes);
163 for (OperInfo::PrivSet::const_iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); ++i)
165 opcmds.push_back(' ');
168 std::stringstream opcmddump(opcmds);
169 user->SendText(checkstr + " commandperms", opcmddump);
171 for (OperInfo::PrivSet::const_iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); ++i)
173 privs.push_back(' ');
176 std::stringstream privdump(privs);
177 user->SendText(checkstr + " permissions", privdump);
183 user->SendText(checkstr + " clientaddr " + loctarg->client_sa.str());
184 user->SendText(checkstr + " serveraddr " + loctarg->server_sa.str());
186 std::string classname = loctarg->GetClass()->name;
187 if (!classname.empty())
188 user->SendText(checkstr + " connectclass " + classname);
191 user->SendText(checkstr + " onip " + targuser->GetIPString());
193 for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
195 Membership* memb = *i;
196 Channel* c = memb->chan;
197 char prefix = memb->GetPrefixChar();
199 chliststr.push_back(prefix);
200 chliststr.append(c->name).push_back(' ');
203 std::stringstream dump(chliststr);
205 user->SendText(checkstr + " onchans", dump);
207 dumpExt(user, checkstr, targuser);
211 /* /check on a channel */
212 user->SendText(checkstr + " timestamp " + timestring(targchan->age));
214 if (targchan->topic[0] != 0)
216 /* there is a topic, assume topic related information exists */
217 user->SendText(checkstr + " topic " + targchan->topic);
218 user->SendText(checkstr + " topic_setby " + targchan->setby);
219 user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset));
222 user->SendText(checkstr + " modes " + targchan->ChanModes(true));
223 user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
225 /* now the ugly bit, spool current members of a channel. :| */
227 const Channel::MemberMap& ulist = targchan->GetUsers();
229 /* note that unlike /names, we do NOT check +i vs in the channel */
230 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
233 * Unlike Asuka, I define a clone as coming from the same host. --w00t
235 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
236 user->SendText("%s member %-3u %s%s (%s@%s) %s ",
237 checkstr.c_str(), clonecount.global,
238 i->second->GetAllPrefixChars(), i->first->nick.c_str(),
239 i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
242 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
243 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
244 dumpListMode(user, checkstr, (*i)->GetList(targchan));
246 dumpExt(user, checkstr, targchan);
250 /* /check on an IP address, or something that doesn't exist */
253 /* hostname or other */
254 const user_hash& users = ServerInstance->Users->GetUsers();
255 for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
257 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
259 /* host or vhost matches mask */
260 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
263 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
266 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
270 user->SendText(checkstr + " matches " + ConvToStr(x));
273 user->SendText(checkstr + " END " + parameters[0]);
278 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
280 if (parameters.size() > 1)
281 return ROUTE_OPT_UCAST(parameters[1]);
282 return ROUTE_LOCALONLY;
286 class ModuleCheck : public Module
288 CommandCheck mycommand;
290 ModuleCheck() : mycommand(this)
294 Version GetVersion() CXX11_OVERRIDE
296 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
300 MODULE_INIT(ModuleCheck)