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
32 CommandCheck(Module* parent)
33 : Command(parent,"CHECK", 1)
36 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
39 std::string timestring(time_t time)
42 struct tm *mytime = gmtime(&time);
43 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
44 return std::string(timebuf);
47 void dumpExt(User* user, const std::string& checkstr, Extensible* ext)
49 std::stringstream dumpkeys;
50 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
52 ExtensionItem* item = i->first;
53 std::string value = item->serialize(FORMAT_USER, ext, i->second);
55 user->SendText(checkstr + " meta:" + item->name + " " + value);
56 else if (!item->name.empty())
57 dumpkeys << " " << item->name;
59 if (!dumpkeys.str().empty())
60 user->SendText(checkstr + " metadata", dumpkeys);
63 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
65 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName.c_str())
71 std::string chliststr;
73 checkstr = ":" + ServerInstance->Config->ServerName + " 304 " + user->nick + " :CHECK";
75 targuser = ServerInstance->FindNick(parameters[0]);
76 targchan = ServerInstance->FindChan(parameters[0]);
79 * Syntax of a /check reply:
80 * :server.name 304 target :CHECK START <target>
81 * :server.name 304 target :CHECK <field> <value>
82 * :server.name 304 target :CHECK END
85 user->SendText(checkstr + " START " + parameters[0]);
89 LocalUser* loctarg = IS_LOCAL(targuser);
90 /* /check on a user */
91 user->SendText(checkstr + " nuh " + targuser->GetFullHost());
92 user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost());
93 user->SendText(checkstr + " realname " + targuser->fullname);
94 user->SendText(checkstr + " modes +" + targuser->FormatModes());
95 user->SendText(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
96 user->SendText(checkstr + " server " + targuser->server);
97 user->SendText(checkstr + " uid " + targuser->uuid);
98 user->SendText(checkstr + " signon " + timestring(targuser->signon));
99 user->SendText(checkstr + " nickts " + timestring(targuser->age));
101 user->SendText(checkstr + " lastmsg " + timestring(loctarg->idle_lastmsg));
103 if (targuser->IsAway())
106 user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime));
107 user->SendText(checkstr + " awaymsg " + targuser->awaymsg);
110 if (targuser->IsOper())
112 OperInfo* oper = targuser->oper;
113 /* user is an oper of type ____ */
114 user->SendText(checkstr + " opertype " + oper->name);
119 for(char c='A'; c < 'z'; c++)
121 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
122 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
124 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
125 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
128 user->SendText(checkstr + " modeperms user=" + umodes + " channel=" + cmodes);
130 for(std::set<std::string>::iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); i++)
132 opcmds.push_back(' ');
135 std::stringstream opcmddump(opcmds);
136 user->SendText(checkstr + " commandperms", opcmddump);
138 for(std::set<std::string>::iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); i++)
140 privs.push_back(' ');
143 std::stringstream privdump(privs);
144 user->SendText(checkstr + " permissions", privdump);
150 user->SendText(checkstr + " clientaddr " + irc::sockets::satouser(loctarg->client_sa));
151 user->SendText(checkstr + " serveraddr " + irc::sockets::satouser(loctarg->server_sa));
153 std::string classname = loctarg->GetClass()->name;
154 if (!classname.empty())
155 user->SendText(checkstr + " connectclass " + classname);
158 user->SendText(checkstr + " onip " + targuser->GetIPString());
160 for (UCListIter i = targuser->chans.begin(); i != targuser->chans.end(); i++)
163 chliststr.append(c->GetPrefixChar(targuser)).append(c->name).append(" ");
166 std::stringstream dump(chliststr);
168 user->SendText(checkstr + " onchans", dump);
170 dumpExt(user, checkstr, targuser);
174 /* /check on a channel */
175 user->SendText(checkstr + " timestamp " + timestring(targchan->age));
177 if (targchan->topic[0] != 0)
179 /* there is a topic, assume topic related information exists */
180 user->SendText(checkstr + " topic " + targchan->topic);
181 user->SendText(checkstr + " topic_setby " + targchan->setby);
182 user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset));
185 user->SendText(checkstr + " modes " + targchan->ChanModes(true));
186 user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
188 /* now the ugly bit, spool current members of a channel. :| */
190 const UserMembList *ulist= targchan->GetUsers();
192 /* note that unlike /names, we do NOT check +i vs in the channel */
193 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
196 * Unlike Asuka, I define a clone as coming from the same host. --w00t
198 user->SendText("%s member %-3lu %s%s (%s@%s) %s ",
199 checkstr.c_str(), ServerInstance->Users->GlobalCloneCount(i->first),
200 targchan->GetAllPrefixChars(i->first), i->first->nick.c_str(),
201 i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
204 // We know that the mode handler for bans is in the core and is derived from ListModeBase
205 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
206 banlm->DoSyncChannel(targchan, creator, user);
208 // Show other listmodes as well
209 FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(targchan,creator,user));
210 dumpExt(user, checkstr, targchan);
214 /* /check on an IP address, or something that doesn't exist */
217 /* hostname or other */
218 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
220 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
222 /* host or vhost matches mask */
223 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
226 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
229 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
233 user->SendText(checkstr + " matches " + ConvToStr(x));
236 user->SendText(checkstr + " END " + parameters[0]);
241 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
243 if (parameters.size() > 1)
244 return ROUTE_OPT_UCAST(parameters[1]);
245 return ROUTE_LOCALONLY;
249 class ModuleCheck : public Module
251 CommandCheck mycommand;
253 ModuleCheck() : mycommand(this)
257 void init() CXX11_OVERRIDE
259 ServerInstance->Modules->AddService(mycommand);
262 void ProtoSendMode(void* uv, TargetTypeFlags, void*, const std::vector<std::string>& result, const std::vector<TranslateType>&)
264 User* user = (User*)uv;
265 std::string checkstr(":");
266 checkstr.append(ServerInstance->Config->ServerName);
267 checkstr.append(" 304 ");
268 checkstr.append(user->nick);
269 checkstr.append(" :CHECK modelist");
270 for(unsigned int i=0; i < result.size(); i++)
272 checkstr.append(" ");
273 checkstr.append(result[i]);
275 user->SendText(checkstr);
278 Version GetVersion() CXX11_OVERRIDE
280 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
284 MODULE_INIT(ModuleCheck)