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