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