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