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/>.
23 /* $ModDesc: Provides the /CHECK command to retrieve information on a user, channel, hostname or IP address */
29 class CommandCheck : public Command
32 CommandCheck(Module* parent) : Command(parent,"CHECK", 1)
34 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
37 std::string timestring(time_t time)
40 struct tm *mytime = gmtime(&time);
41 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
42 return std::string(timebuf);
45 void dumpExt(User* user, std::string checkstr, Extensible* ext)
47 std::stringstream dumpkeys;
48 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
50 ExtensionItem* item = i->first;
51 std::string value = item->serialize(FORMAT_USER, ext, i->second);
53 user->SendText(checkstr + " meta:" + item->name + " " + value);
54 else if (!item->name.empty())
55 dumpkeys << " " << item->name;
57 if (!dumpkeys.str().empty())
58 user->SendText(checkstr + " metadata", dumpkeys);
61 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
63 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName.c_str())
69 std::string chliststr;
71 checkstr = std::string(":") + ServerInstance->Config->ServerName + " 304 " + std::string(user->nick) + " :CHECK";
73 targuser = ServerInstance->FindNick(parameters[0]);
74 targchan = ServerInstance->FindChan(parameters[0]);
77 * Syntax of a /check reply:
78 * :server.name 304 target :CHECK START <target>
79 * :server.name 304 target :CHECK <field> <value>
80 * :server.name 304 target :CHECK END
83 user->SendText(checkstr + " START " + parameters[0]);
87 LocalUser* loctarg = IS_LOCAL(targuser);
88 /* /check on a user */
89 user->SendText(checkstr + " nuh " + targuser->GetFullHost());
90 user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost());
91 user->SendText(checkstr + " realname " + targuser->fullname);
92 user->SendText(checkstr + " modes +" + targuser->FormatModes());
93 user->SendText(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
94 user->SendText(checkstr + " server " + targuser->server);
95 user->SendText(checkstr + " uid " + targuser->uuid);
96 user->SendText(checkstr + " signon " + timestring(targuser->signon));
97 user->SendText(checkstr + " nickts " + timestring(targuser->age));
99 user->SendText(checkstr + " lastmsg " + timestring(targuser->idle_lastmsg));
101 if (IS_AWAY(targuser))
104 user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime));
105 user->SendText(checkstr + " awaymsg " + targuser->awaymsg);
108 if (IS_OPER(targuser))
110 OperInfo* oper = targuser->oper;
111 /* user is an oper of type ____ */
112 user->SendText(checkstr + " opertype " + oper->NameStr());
117 for(char c='A'; c < 'z'; c++)
119 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
120 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
122 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
123 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
126 user->SendText(checkstr + " modeperms user=" + umodes + " channel=" + cmodes);
128 for(std::set<std::string>::iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); i++)
130 opcmds.push_back(' ');
133 std::stringstream opcmddump(opcmds);
134 user->SendText(checkstr + " commandperms", opcmddump);
136 for(std::set<std::string>::iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); i++)
138 privs.push_back(' ');
141 std::stringstream privdump(privs);
142 user->SendText(checkstr + " permissions", privdump);
148 user->SendText(checkstr + " clientaddr " + irc::sockets::satouser(loctarg->client_sa));
149 user->SendText(checkstr + " serveraddr " + irc::sockets::satouser(loctarg->server_sa));
151 std::string classname = loctarg->GetClass()->name;
152 if (!classname.empty())
153 user->SendText(checkstr + " connectclass " + classname);
156 user->SendText(checkstr + " onip " + targuser->GetIPString());
158 for (UCListIter i = targuser->chans.begin(); i != targuser->chans.end(); i++)
161 chliststr.append(c->GetPrefixChar(targuser)).append(c->name).append(" ");
164 std::stringstream dump(chliststr);
166 user->SendText(checkstr + " onchans", dump);
168 dumpExt(user, checkstr, targuser);
172 /* /check on a channel */
173 user->SendText(checkstr + " timestamp " + timestring(targchan->age));
175 if (targchan->topic[0] != 0)
177 /* there is a topic, assume topic related information exists */
178 user->SendText(checkstr + " topic " + targchan->topic);
179 user->SendText(checkstr + " topic_setby " + targchan->setby);
180 user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset));
183 user->SendText(checkstr + " modes " + targchan->ChanModes(true));
184 user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
186 /* now the ugly bit, spool current members of a channel. :| */
188 const UserMembList *ulist= targchan->GetUsers();
190 /* note that unlike /names, we do NOT check +i vs in the channel */
191 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
195 * Unlike Asuka, I define a clone as coming from the same host. --w00t
197 snprintf(tmpbuf, MAXBUF, "%-3lu %s%s (%s@%s) %s ", ServerInstance->Users->GlobalCloneCount(i->first), targchan->GetAllPrefixChars(i->first), i->first->nick.c_str(), i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
198 user->SendText(checkstr + " member " + tmpbuf);
201 irc::modestacker modestack(true);
202 std::string letter_b("b");
203 for(BanList::iterator b = targchan->bans.begin(); b != targchan->bans.end(); ++b)
205 modestack.Push('b', b->data);
207 std::vector<std::string> stackresult;
208 std::vector<TranslateType> dummy;
209 while (modestack.GetStackedLine(stackresult))
211 creator->ProtoSendMode(user, TYPE_CHANNEL, targchan, stackresult, dummy);
214 FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(targchan,creator,user));
215 dumpExt(user, checkstr, targchan);
219 /* /check on an IP address, or something that doesn't exist */
222 /* hostname or other */
223 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
225 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
227 /* host or vhost matches mask */
228 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
231 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
234 user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
238 user->SendText(checkstr + " matches " + ConvToStr(x));
241 user->SendText(checkstr + " END " + parameters[0]);
246 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
248 if (parameters.size() > 1)
249 return ROUTE_OPT_UCAST(parameters[1]);
250 return ROUTE_LOCALONLY;
255 class ModuleCheck : public Module
258 CommandCheck mycommand;
260 ModuleCheck() : mycommand(this)
266 ServerInstance->Modules->AddService(mycommand);
273 void ProtoSendMode(void* uv, TargetTypeFlags, void*, const std::vector<std::string>& result, const std::vector<TranslateType>&)
275 User* user = (User*)uv;
276 std::string checkstr(":");
277 checkstr.append(ServerInstance->Config->ServerName);
278 checkstr.append(" 304 ");
279 checkstr.append(user->nick);
280 checkstr.append(" :CHECK modelist");
281 for(unsigned int i=0; i < result.size(); i++)
283 checkstr.append(" ");
284 checkstr.append(result[i]);
286 user->SendText(checkstr);
291 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
295 MODULE_INIT(ModuleCheck)