]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
Update the module descriptions using mkversion.
[user/henk/code/inspircd.git] / src / modules / m_check.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "listmode.h"
29
30 enum
31 {
32         RPL_CHECK = 802
33 };
34
35 class CheckContext
36 {
37  private:
38         User* const user;
39         const std::string& target;
40
41         std::string FormatTime(time_t ts)
42         {
43                 std::string timestr(InspIRCd::TimeString(ts, "%Y-%m-%d %H:%M:%S UTC (", true));
44                 timestr.append(ConvToStr(ts));
45                 timestr.push_back(')');
46                 return timestr;
47         }
48
49  public:
50         CheckContext(User* u, const std::string& targetstr)
51                 : user(u)
52                 , target(targetstr)
53         {
54                 Write("START", target);
55         }
56
57         ~CheckContext()
58         {
59                 Write("END", target);
60         }
61
62         void Write(const std::string& type, const std::string& text)
63         {
64                 user->WriteRemoteNumeric(RPL_CHECK, type, text);
65         }
66
67         void Write(const std::string& type, time_t ts)
68         {
69                 user->WriteRemoteNumeric(RPL_CHECK, type, FormatTime(ts));
70         }
71
72         User* GetUser() const { return user; }
73
74         void DumpListMode(ListModeBase* mode, Channel* chan)
75         {
76                 const ListModeBase::ModeList* list = mode->GetList(chan);
77                 if (!list)
78                         return;
79
80                 for (ListModeBase::ModeList::const_iterator i = list->begin(); i != list->end(); ++i)
81                 {
82                         CheckContext::List listmode(*this, "listmode");
83                         listmode.Add(ConvToStr(mode->GetModeChar()));
84                         listmode.Add(i->mask);
85                         listmode.Add(i->setter);
86                         listmode.Add(FormatTime(i->time));
87                         listmode.Flush();
88                 }
89         }
90
91         void DumpExt(Extensible* ext)
92         {
93                 CheckContext::List extlist(*this, "metadata");
94                 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); ++i)
95                 {
96                         ExtensionItem* item = i->first;
97                         std::string value = item->ToHuman(ext, i->second);
98                         if (!value.empty())
99                                 Write("meta:" + item->name, value);
100                         else if (!item->name.empty())
101                                 extlist.Add(item->name);
102                 }
103
104                 extlist.Flush();
105         }
106
107         class List : public Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>
108         {
109          public:
110                 List(CheckContext& context, const char* checktype)
111                         : Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>(Numeric::WriteRemoteNumericSink(context.GetUser()), RPL_CHECK, false, (IS_LOCAL(context.GetUser()) ? context.GetUser()->nick.length() : ServerInstance->Config->Limits.NickMax) + strlen(checktype) + 1)
112                 {
113                         GetNumeric().push(checktype).push(std::string());
114                 }
115         };
116 };
117
118 /** Handle /CHECK
119  */
120 class CommandCheck : public Command
121 {
122         UserModeReference snomaskmode;
123
124         std::string GetSnomasks(User* user)
125         {
126                 std::string ret;
127                 if (snomaskmode)
128                         ret = snomaskmode->GetUserParameter(user);
129
130                 if (ret.empty())
131                         ret = "+";
132                 return ret;
133         }
134
135         static std::string GetAllowedOperOnlyModes(LocalUser* user, ModeType modetype)
136         {
137                 std::string ret;
138                 const ModeParser::ModeHandlerMap& modes = ServerInstance->Modes.GetModes(modetype);
139                 for (ModeParser::ModeHandlerMap::const_iterator i = modes.begin(); i != modes.end(); ++i)
140                 {
141                         const ModeHandler* const mh = i->second;
142                         if ((mh->NeedsOper()) && (user->HasModePermission(mh)))
143                                 ret.push_back(mh->GetModeChar());
144                 }
145                 return ret;
146         }
147
148  public:
149         CommandCheck(Module* parent)
150                 : Command(parent,"CHECK", 1)
151                 , snomaskmode(parent, "snomask")
152         {
153                 flags_needed = 'o';
154                 syntax = "<nick>|<ipmask>|<hostmask>|<channel> [<servername>]";
155         }
156
157         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
158         {
159                 if (parameters.size() > 1 && !irc::equals(parameters[1], ServerInstance->Config->ServerName))
160                         return CMD_SUCCESS;
161
162                 User *targuser;
163                 Channel *targchan;
164                 std::string chliststr;
165
166                 targuser = ServerInstance->FindNick(parameters[0]);
167                 targchan = ServerInstance->FindChan(parameters[0]);
168
169                 /*
170                  * Syntax of a /check reply:
171                  *  :server.name 802 target START <target>
172                  *  :server.name 802 target <field> :<value>
173                  *  :server.name 802 target END <target>
174                  */
175
176                 // Constructor sends START, destructor sends END
177                 CheckContext context(user, parameters[0]);
178
179                 if (targuser)
180                 {
181                         LocalUser* loctarg = IS_LOCAL(targuser);
182                         /* /check on a user */
183                         context.Write("nuh", targuser->GetFullHost());
184                         context.Write("realnuh", targuser->GetFullRealHost());
185                         context.Write("realname", targuser->GetRealName());
186                         context.Write("modes", targuser->GetModeLetters());
187                         context.Write("snomasks", GetSnomasks(targuser));
188                         context.Write("server", targuser->server->GetName());
189                         context.Write("uid", targuser->uuid);
190                         context.Write("signon", targuser->signon);
191                         context.Write("nickts", targuser->age);
192                         if (loctarg)
193                                 context.Write("lastmsg", loctarg->idle_lastmsg);
194
195                         if (targuser->IsAway())
196                         {
197                                 /* user is away */
198                                 context.Write("awaytime", targuser->awaytime);
199                                 context.Write("awaymsg", targuser->awaymsg);
200                         }
201
202                         if (targuser->IsOper())
203                         {
204                                 OperInfo* oper = targuser->oper;
205                                 /* user is an oper of type ____ */
206                                 context.Write("opertype", oper->name);
207                                 if (loctarg)
208                                 {
209                                         context.Write("chanmodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_CHANNEL));
210                                         context.Write("usermodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_USER));
211                                         context.Write("commandperms", oper->AllowedOperCommands.ToString());
212                                         context.Write("permissions", oper->AllowedPrivs.ToString());
213                                 }
214                         }
215
216                         if (loctarg)
217                         {
218                                 context.Write("clientaddr", loctarg->client_sa.str());
219                                 context.Write("serveraddr", loctarg->server_sa.str());
220
221                                 std::string classname = loctarg->GetClass()->name;
222                                 if (!classname.empty())
223                                         context.Write("connectclass", classname);
224                         }
225                         else
226                                 context.Write("onip", targuser->GetIPString());
227
228                         CheckContext::List chanlist(context, "onchans");
229                         for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
230                         {
231                                 Membership* memb = *i;
232                                 Channel* c = memb->chan;
233                                 char prefix = memb->GetPrefixChar();
234                                 if (prefix)
235                                         chliststr.push_back(prefix);
236                                 chliststr.append(c->name);
237                                 chanlist.Add(chliststr);
238                                 chliststr.clear();
239                         }
240
241                         chanlist.Flush();
242
243                         context.DumpExt(targuser);
244                 }
245                 else if (targchan)
246                 {
247                         /* /check on a channel */
248                         context.Write("createdat", targchan->age);
249
250                         if (!targchan->topic.empty())
251                         {
252                                 /* there is a topic, assume topic related information exists */
253                                 context.Write("topic", targchan->topic);
254                                 context.Write("topic_setby", targchan->setby);
255                                 context.Write("topic_setat", targchan->topicset);
256                         }
257
258                         context.Write("modes", targchan->ChanModes(true));
259                         context.Write("membercount", ConvToStr(targchan->GetUserCounter()));
260
261                         /* now the ugly bit, spool current members of a channel. :| */
262
263                         const Channel::MemberMap& ulist = targchan->GetUsers();
264
265                         /* note that unlike /names, we do NOT check +i vs in the channel */
266                         for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
267                         {
268                                 /*
269                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
270                                  */
271                                 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
272                                 context.Write("member", InspIRCd::Format("%u %s%s (%s)", clonecount.global,
273                                         i->second->GetAllPrefixChars().c_str(), i->first->GetFullHost().c_str(),
274                                         i->first->GetRealName().c_str()));
275                         }
276
277                         const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
278                         for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
279                                 context.DumpListMode(*i, targchan);
280
281                         context.DumpExt(targchan);
282                 }
283                 else
284                 {
285                         /*  /check on an IP address, or something that doesn't exist */
286                         long x = 0;
287
288                         /* hostname or other */
289                         const user_hash& users = ServerInstance->Users->GetUsers();
290                         for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
291                         {
292                                 if (InspIRCd::Match(a->second->GetRealHost(), parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->GetDisplayedHost(), parameters[0], ascii_case_insensitive_map))
293                                 {
294                                         /* host or vhost matches mask */
295                                         context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
296                                 }
297                                 /* IP address */
298                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
299                                 {
300                                         /* same IP. */
301                                         context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
302                                 }
303                         }
304
305                         context.Write("matches", ConvToStr(x));
306                 }
307
308                 // END is sent by the CheckContext destructor
309                 return CMD_SUCCESS;
310         }
311
312         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
313         {
314                 if ((parameters.size() > 1) && (parameters[1].find('.') != std::string::npos))
315                         return ROUTE_OPT_UCAST(parameters[1]);
316                 return ROUTE_LOCALONLY;
317         }
318 };
319
320 class ModuleCheck : public Module
321 {
322         CommandCheck mycommand;
323  public:
324         ModuleCheck() : mycommand(this)
325         {
326         }
327
328         Version GetVersion() CXX11_OVERRIDE
329         {
330                 return Version("Adds the /CHECK command which allows server operators to look up details about a channel, user, IP address, or hostname.", VF_VENDOR|VF_OPTCOMMON);
331         }
332 };
333
334 MODULE_INIT(ModuleCheck)