]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
fda211cf80eb03061972ab36471cd3a51817439a
[user/henk/code/inspircd.git] / src / modules / m_check.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 /* $ModDesc: Provides the /CHECK command to retrieve information on a user, channel, hostname or IP address */
24
25 #include "inspircd.h"
26 #include "listmode.h"
27
28 /** Handle /CHECK
29  */
30 class CommandCheck : public Command
31 {
32         ModeReference ban;
33  public:
34         CommandCheck(Module* parent)
35                 : Command(parent,"CHECK", 1)
36                 , ban(parent, "ban")
37         {
38                 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
39         }
40
41         std::string timestring(time_t time)
42         {
43                 char timebuf[60];
44                 struct tm *mytime = gmtime(&time);
45                 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
46                 return std::string(timebuf);
47         }
48
49         void dumpExt(User* user, const std::string& checkstr, Extensible* ext)
50         {
51                 std::stringstream dumpkeys;
52                 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
53                 {
54                         ExtensionItem* item = i->first;
55                         std::string value = item->serialize(FORMAT_USER, ext, i->second);
56                         if (!value.empty())
57                                 user->SendText(checkstr + " meta:" + item->name + " " + value);
58                         else if (!item->name.empty())
59                                 dumpkeys << " " << item->name;
60                 }
61                 if (!dumpkeys.str().empty())
62                         user->SendText(checkstr + " metadata", dumpkeys);
63         }
64
65         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
66         {
67                 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName.c_str())
68                         return CMD_SUCCESS;
69
70                 User *targuser;
71                 Channel *targchan;
72                 std::string checkstr;
73                 std::string chliststr;
74
75                 checkstr = ":" + ServerInstance->Config->ServerName + " 304 " + user->nick + " :CHECK";
76
77                 targuser = ServerInstance->FindNick(parameters[0]);
78                 targchan = ServerInstance->FindChan(parameters[0]);
79
80                 /*
81                  * Syntax of a /check reply:
82                  *  :server.name 304 target :CHECK START <target>
83                  *  :server.name 304 target :CHECK <field> <value>
84                  *  :server.name 304 target :CHECK END
85                  */
86
87                 user->SendText(checkstr + " START " + parameters[0]);
88
89                 if (targuser)
90                 {
91                         LocalUser* loctarg = IS_LOCAL(targuser);
92                         /* /check on a user */
93                         user->SendText(checkstr + " nuh " + targuser->GetFullHost());
94                         user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost());
95                         user->SendText(checkstr + " realname " + targuser->fullname);
96                         user->SendText(checkstr + " modes +" + targuser->FormatModes());
97                         user->SendText(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
98                         user->SendText(checkstr + " server " + targuser->server);
99                         user->SendText(checkstr + " uid " + targuser->uuid);
100                         user->SendText(checkstr + " signon " + timestring(targuser->signon));
101                         user->SendText(checkstr + " nickts " + timestring(targuser->age));
102                         if (loctarg)
103                                 user->SendText(checkstr + " lastmsg " + timestring(loctarg->idle_lastmsg));
104
105                         if (targuser->IsAway())
106                         {
107                                 /* user is away */
108                                 user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime));
109                                 user->SendText(checkstr + " awaymsg " + targuser->awaymsg);
110                         }
111
112                         if (targuser->IsOper())
113                         {
114                                 OperInfo* oper = targuser->oper;
115                                 /* user is an oper of type ____ */
116                                 user->SendText(checkstr + " opertype " + oper->name);
117                                 if (loctarg)
118                                 {
119                                         std::string umodes;
120                                         std::string cmodes;
121                                         for(char c='A'; c < 'z'; c++)
122                                         {
123                                                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
124                                                 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
125                                                         umodes.push_back(c);
126                                                 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
127                                                 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
128                                                         cmodes.push_back(c);
129                                         }
130                                         user->SendText(checkstr + " modeperms user=" + umodes + " channel=" + cmodes);
131                                         std::string opcmds;
132                                         for(std::set<std::string>::iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); i++)
133                                         {
134                                                 opcmds.push_back(' ');
135                                                 opcmds.append(*i);
136                                         }
137                                         std::stringstream opcmddump(opcmds);
138                                         user->SendText(checkstr + " commandperms", opcmddump);
139                                         std::string privs;
140                                         for(std::set<std::string>::iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); i++)
141                                         {
142                                                 privs.push_back(' ');
143                                                 privs.append(*i);
144                                         }
145                                         std::stringstream privdump(privs);
146                                         user->SendText(checkstr + " permissions", privdump);
147                                 }
148                         }
149
150                         if (loctarg)
151                         {
152                                 user->SendText(checkstr + " clientaddr " + irc::sockets::satouser(loctarg->client_sa));
153                                 user->SendText(checkstr + " serveraddr " + irc::sockets::satouser(loctarg->server_sa));
154
155                                 std::string classname = loctarg->GetClass()->name;
156                                 if (!classname.empty())
157                                         user->SendText(checkstr + " connectclass " + classname);
158                         }
159                         else
160                                 user->SendText(checkstr + " onip " + targuser->GetIPString());
161
162                         for (UCListIter i = targuser->chans.begin(); i != targuser->chans.end(); i++)
163                         {
164                                 Channel* c = *i;
165                                 chliststr.append(c->GetPrefixChar(targuser)).append(c->name).append(" ");
166                         }
167
168                         std::stringstream dump(chliststr);
169
170                         user->SendText(checkstr + " onchans", dump);
171
172                         dumpExt(user, checkstr, targuser);
173                 }
174                 else if (targchan)
175                 {
176                         /* /check on a channel */
177                         user->SendText(checkstr + " timestamp " + timestring(targchan->age));
178
179                         if (targchan->topic[0] != 0)
180                         {
181                                 /* there is a topic, assume topic related information exists */
182                                 user->SendText(checkstr + " topic " + targchan->topic);
183                                 user->SendText(checkstr + " topic_setby " + targchan->setby);
184                                 user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset));
185                         }
186
187                         user->SendText(checkstr + " modes " + targchan->ChanModes(true));
188                         user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
189
190                         /* now the ugly bit, spool current members of a channel. :| */
191
192                         const UserMembList *ulist= targchan->GetUsers();
193
194                         /* note that unlike /names, we do NOT check +i vs in the channel */
195                         for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
196                         {
197                                 char tmpbuf[MAXBUF];
198                                 /*
199                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
200                                  */
201                                 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());
202                                 user->SendText(checkstr + " member " + tmpbuf);
203                         }
204
205                         // We know that the mode handler for bans is in the core and is derived from ListModeBase
206                         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
207                         banlm->DoSyncChannel(targchan, creator, user);
208
209                         // Show other listmodes as well
210                         FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(targchan,creator,user));
211                         dumpExt(user, checkstr, targchan);
212                 }
213                 else
214                 {
215                         /*  /check on an IP address, or something that doesn't exist */
216                         long x = 0;
217
218                         /* hostname or other */
219                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
220                         {
221                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
222                                 {
223                                         /* host or vhost matches mask */
224                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
225                                 }
226                                 /* IP address */
227                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
228                                 {
229                                         /* same IP. */
230                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
231                                 }
232                         }
233
234                         user->SendText(checkstr + " matches " + ConvToStr(x));
235                 }
236
237                 user->SendText(checkstr + " END " + parameters[0]);
238
239                 return CMD_SUCCESS;
240         }
241
242         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
243         {
244                 if (parameters.size() > 1)
245                         return ROUTE_OPT_UCAST(parameters[1]);
246                 return ROUTE_LOCALONLY;
247         }
248 };
249
250 class ModuleCheck : public Module
251 {
252         CommandCheck mycommand;
253  public:
254         ModuleCheck() : mycommand(this)
255         {
256         }
257
258         void init() CXX11_OVERRIDE
259         {
260                 ServerInstance->Modules->AddService(mycommand);
261         }
262
263         void ProtoSendMode(void* uv, TargetTypeFlags, void*, const std::vector<std::string>& result, const std::vector<TranslateType>&)
264         {
265                 User* user = (User*)uv;
266                 std::string checkstr(":");
267                 checkstr.append(ServerInstance->Config->ServerName);
268                 checkstr.append(" 304 ");
269                 checkstr.append(user->nick);
270                 checkstr.append(" :CHECK modelist");
271                 for(unsigned int i=0; i < result.size(); i++)
272                 {
273                         checkstr.append(" ");
274                         checkstr.append(result[i]);
275                 }
276                 user->SendText(checkstr);
277         }
278
279         Version GetVersion() CXX11_OVERRIDE
280         {
281                 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
282         }
283 };
284
285 MODULE_INIT(ModuleCheck)