]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
65cccec5b8d1a8f5f7405f0069836ef92b84c9c0
[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 enum
27 {
28         RPL_CHECK = 802
29 };
30
31 class CheckContext
32 {
33  private:
34         User* const user;
35         const std::string& target;
36
37         std::string FormatTime(time_t ts)
38         {
39                 std::string timestr(InspIRCd::TimeString(ts, "%Y-%m-%d %H:%M:%S UTC (", true));
40                 timestr.append(ConvToStr(ts));
41                 timestr.push_back(')');
42                 return timestr;
43         }
44
45  public:
46         CheckContext(User* u, const std::string& targetstr)
47                 : user(u)
48                 , target(targetstr)
49         {
50                 Write("START", target);
51         }
52
53         ~CheckContext()
54         {
55                 Write("END", target);
56         }
57
58         void Write(const std::string& type, const std::string& text)
59         {
60                 user->WriteRemoteNumeric(RPL_CHECK, type, text);
61         }
62
63         void Write(const std::string& type, time_t ts)
64         {
65                 user->WriteRemoteNumeric(RPL_CHECK, type, FormatTime(ts));
66         }
67
68         User* GetUser() const { return user; }
69
70         void DumpListMode(const ListModeBase::ModeList* list)
71         {
72                 if (!list)
73                         return;
74
75                 CheckContext::List modelist(*this, "modelist");
76                 for (ListModeBase::ModeList::const_iterator i = list->begin(); i != list->end(); ++i)
77                         modelist.Add(i->mask);
78
79                 modelist.Flush();
80         }
81
82         void DumpExt(Extensible* ext)
83         {
84                 CheckContext::List extlist(*this, "metadata");
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                                 Write("meta:" + item->name, value);
91                         else if (!item->name.empty())
92                                 extlist.Add(item->name);
93                 }
94
95                 extlist.Flush();
96         }
97
98         class List : public Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>
99         {
100          public:
101                 List(CheckContext& context, const char* checktype)
102                         : 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)
103                 {
104                         GetNumeric().push(checktype).push(std::string());
105                 }
106         };
107 };
108
109 /** Handle /CHECK
110  */
111 class CommandCheck : public Command
112 {
113         UserModeReference snomaskmode;
114
115         std::string GetSnomasks(User* user)
116         {
117                 std::string ret;
118                 if (snomaskmode)
119                         ret = snomaskmode->GetUserParameter(user);
120
121                 if (ret.empty())
122                         ret = "+";
123                 return ret;
124         }
125
126         static std::string GetAllowedOperOnlyModes(LocalUser* user, ModeType modetype)
127         {
128                 std::string ret;
129                 const ModeParser::ModeHandlerMap& modes = ServerInstance->Modes.GetModes(modetype);
130                 for (ModeParser::ModeHandlerMap::const_iterator i = modes.begin(); i != modes.end(); ++i)
131                 {
132                         const ModeHandler* const mh = i->second;
133                         if ((mh->NeedsOper()) && (user->HasModePermission(mh)))
134                                 ret.push_back(mh->GetModeChar());
135                 }
136                 return ret;
137         }
138
139  public:
140         CommandCheck(Module* parent)
141                 : Command(parent,"CHECK", 1)
142                 , snomaskmode(parent, "snomask")
143         {
144                 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
145         }
146
147         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
148         {
149                 if (parameters.size() > 1 && !irc::equals(parameters[1], ServerInstance->Config->ServerName))
150                         return CMD_SUCCESS;
151
152                 User *targuser;
153                 Channel *targchan;
154                 std::string chliststr;
155
156                 targuser = ServerInstance->FindNick(parameters[0]);
157                 targchan = ServerInstance->FindChan(parameters[0]);
158
159                 /*
160                  * Syntax of a /check reply:
161                  *  :server.name 802 target START <target>
162                  *  :server.name 802 target <field> :<value>
163                  *  :server.name 802 target END <target>
164                  */
165
166                 // Constructor sends START, destructor sends END
167                 CheckContext context(user, parameters[0]);
168
169                 if (targuser)
170                 {
171                         LocalUser* loctarg = IS_LOCAL(targuser);
172                         /* /check on a user */
173                         context.Write("nuh", targuser->GetFullHost());
174                         context.Write("realnuh", targuser->GetFullRealHost());
175                         context.Write("realname", targuser->GetRealName());
176                         context.Write("modes", targuser->GetModeLetters());
177                         context.Write("snomasks", GetSnomasks(targuser));
178                         context.Write("server", targuser->server->GetName());
179                         context.Write("uid", targuser->uuid);
180                         context.Write("signon", targuser->signon);
181                         context.Write("nickts", targuser->age);
182                         if (loctarg)
183                                 context.Write("lastmsg", loctarg->idle_lastmsg);
184
185                         if (targuser->IsAway())
186                         {
187                                 /* user is away */
188                                 context.Write("awaytime", targuser->awaytime);
189                                 context.Write("awaymsg", targuser->awaymsg);
190                         }
191
192                         if (targuser->IsOper())
193                         {
194                                 OperInfo* oper = targuser->oper;
195                                 /* user is an oper of type ____ */
196                                 context.Write("opertype", oper->name);
197                                 if (loctarg)
198                                 {
199                                         context.Write("chanmodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_CHANNEL));
200                                         context.Write("usermodeperms", GetAllowedOperOnlyModes(loctarg, MODETYPE_USER));
201                                         context.Write("commandperms", oper->AllowedOperCommands.ToString());
202                                         context.Write("permissions", oper->AllowedPrivs.ToString());
203                                 }
204                         }
205
206                         if (loctarg)
207                         {
208                                 context.Write("clientaddr", loctarg->client_sa.str());
209                                 context.Write("serveraddr", loctarg->server_sa.str());
210
211                                 std::string classname = loctarg->GetClass()->name;
212                                 if (!classname.empty())
213                                         context.Write("connectclass", classname);
214                         }
215                         else
216                                 context.Write("onip", targuser->GetIPString());
217
218                         CheckContext::List chanlist(context, "onchans");
219                         for (User::ChanList::iterator i = targuser->chans.begin(); i != targuser->chans.end(); i++)
220                         {
221                                 Membership* memb = *i;
222                                 Channel* c = memb->chan;
223                                 char prefix = memb->GetPrefixChar();
224                                 if (prefix)
225                                         chliststr.push_back(prefix);
226                                 chliststr.append(c->name);
227                                 chanlist.Add(chliststr);
228                                 chliststr.clear();
229                         }
230
231                         chanlist.Flush();
232
233                         context.DumpExt(targuser);
234                 }
235                 else if (targchan)
236                 {
237                         /* /check on a channel */
238                         context.Write("createdat", targchan->age);
239
240                         if (!targchan->topic.empty())
241                         {
242                                 /* there is a topic, assume topic related information exists */
243                                 context.Write("topic", targchan->topic);
244                                 context.Write("topic_setby", targchan->setby);
245                                 context.Write("topic_setat", targchan->topicset);
246                         }
247
248                         context.Write("modes", targchan->ChanModes(true));
249                         context.Write("membercount", ConvToStr(targchan->GetUserCounter()));
250
251                         /* now the ugly bit, spool current members of a channel. :| */
252
253                         const Channel::MemberMap& ulist = targchan->GetUsers();
254
255                         /* note that unlike /names, we do NOT check +i vs in the channel */
256                         for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
257                         {
258                                 /*
259                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
260                                  */
261                                 const UserManager::CloneCounts& clonecount = ServerInstance->Users->GetCloneCounts(i->first);
262                                 context.Write("member", InspIRCd::Format("%u %s%s (%s)", clonecount.global,
263                                         i->second->GetAllPrefixChars().c_str(), i->first->GetFullHost().c_str(),
264                                         i->first->GetRealName().c_str()));
265                         }
266
267                         const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
268                         for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
269                                 context.DumpListMode((*i)->GetList(targchan));
270
271                         context.DumpExt(targchan);
272                 }
273                 else
274                 {
275                         /*  /check on an IP address, or something that doesn't exist */
276                         long x = 0;
277
278                         /* hostname or other */
279                         const user_hash& users = ServerInstance->Users->GetUsers();
280                         for (user_hash::const_iterator a = users.begin(); a != users.end(); ++a)
281                         {
282                                 if (InspIRCd::Match(a->second->GetRealHost(), parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->GetDisplayedHost(), parameters[0], ascii_case_insensitive_map))
283                                 {
284                                         /* host or vhost matches mask */
285                                         context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
286                                 }
287                                 /* IP address */
288                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
289                                 {
290                                         /* same IP. */
291                                         context.Write("match", ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->GetRealName());
292                                 }
293                         }
294
295                         context.Write("matches", ConvToStr(x));
296                 }
297
298                 // END is sent by the CheckContext destructor
299                 return CMD_SUCCESS;
300         }
301
302         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
303         {
304                 if ((parameters.size() > 1) && (parameters[1].find('.') != std::string::npos))
305                         return ROUTE_OPT_UCAST(parameters[1]);
306                 return ROUTE_LOCALONLY;
307         }
308 };
309
310 class ModuleCheck : public Module
311 {
312         CommandCheck mycommand;
313  public:
314         ModuleCheck() : mycommand(this)
315         {
316         }
317
318         Version GetVersion() CXX11_OVERRIDE
319         {
320                 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
321         }
322 };
323
324 MODULE_INIT(ModuleCheck)