]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
m_check Refactor, move to the new RPL_CHECK numeric
[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         User* const user;
34         const std::string& target;
35
36  public:
37         CheckContext(User* u, const std::string& targetstr)
38                 : user(u)
39                 , target(targetstr)
40         {
41                 Write("START", target);
42         }
43
44         ~CheckContext()
45         {
46                 Write("END", target);
47         }
48
49         void Write(const std::string& type, const std::string& text)
50         {
51                 user->WriteRemoteNumeric(RPL_CHECK, type, text);
52         }
53
54         User* GetUser() const { return user; }
55
56         class List : public Numeric::GenericBuilder<' ', false, Numeric::WriteRemoteNumericSink>
57         {
58          public:
59                 List(CheckContext& context, const char* checktype)
60                         : 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)
61                 {
62                         GetNumeric().push(checktype).push(std::string());
63                 }
64         };
65 };
66
67 /** Handle /CHECK
68  */
69 class CommandCheck : public Command
70 {
71         UserModeReference snomaskmode;
72
73         std::string GetSnomasks(User* user)
74         {
75                 std::string ret;
76                 if (snomaskmode)
77                         ret = snomaskmode->GetUserParameter(user);
78
79                 if (ret.empty())
80                         ret = "+";
81                 return ret;
82         }
83
84         static void dumpListMode(CheckContext& context, const ListModeBase::ModeList* list)
85         {
86                 if (!list)
87                         return;
88
89                 CheckContext::List modelist(context, "modelist");
90                 for (ListModeBase::ModeList::const_iterator i = list->begin(); i != list->end(); ++i)
91                         modelist.Add(i->mask);
92
93                 modelist.Flush();
94         }
95
96  public:
97         CommandCheck(Module* parent)
98                 : Command(parent,"CHECK", 1)
99                 , snomaskmode(parent, "snomask")
100         {
101                 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
102         }
103
104         std::string timestring(time_t time)
105         {
106                 char timebuf[60];
107                 struct tm *mytime = gmtime(&time);
108                 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (", mytime);
109                 std::string ret(timebuf);
110                 ret.append(ConvToStr(time)).push_back(')');
111                 return ret;
112         }
113
114         void dumpExt(CheckContext& context, Extensible* ext)
115         {
116                 CheckContext::List extlist(context, "metadata");
117                 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
118                 {
119                         ExtensionItem* item = i->first;
120                         std::string value = item->serialize(FORMAT_USER, ext, i->second);
121                         if (!value.empty())
122                                 context.Write("meta:" + item->name, value);
123                         else if (!item->name.empty())
124                                 extlist.Add(item->name);
125                 }
126
127                 extlist.Flush();
128         }
129
130         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
131         {
132                 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName)
133                         return CMD_SUCCESS;
134
135                 User *targuser;
136                 Channel *targchan;
137                 std::string chliststr;
138
139                 targuser = ServerInstance->FindNick(parameters[0]);
140                 targchan = ServerInstance->FindChan(parameters[0]);
141
142                 /*
143                  * Syntax of a /check reply:
144                  *  :server.name 802 target START <target>
145                  *  :server.name 802 target <field> :<value>
146                  *  :server.name 802 target END <target>
147                  */
148
149                 // Constructor sends START, destructor sends END
150                 CheckContext context(user, parameters[0]);
151
152                 if (targuser)
153                 {
154                         LocalUser* loctarg = IS_LOCAL(targuser);
155                         /* /check on a user */
156                         context.Write("nuh", targuser->GetFullHost());
157                         context.Write("realnuh", targuser->GetFullRealHost());
158                         context.Write("realname", targuser->fullname);
159                         context.Write("modes", std::string("+") + targuser->FormatModes());
160                         context.Write("snomasks", GetSnomasks(targuser));
161                         context.Write("server", targuser->server->GetName());
162                         context.Write("uid", targuser->uuid);
163                         context.Write("signon", timestring(targuser->signon));
164                         context.Write("nickts", timestring(targuser->age));
165                         if (loctarg)
166                                 context.Write("lastmsg", timestring(loctarg->idle_lastmsg));
167
168                         if (targuser->IsAway())
169                         {
170                                 /* user is away */
171                                 context.Write("awaytime", timestring(targuser->awaytime));
172                                 context.Write("awaymsg", targuser->awaymsg);
173                         }
174
175                         if (targuser->IsOper())
176                         {
177                                 OperInfo* oper = targuser->oper;
178                                 /* user is an oper of type ____ */
179                                 context.Write("opertype", oper->name);
180                                 if (loctarg)
181                                 {
182                                         std::string umodes;
183                                         std::string cmodes;
184                                         for(char c='A'; c <= 'z'; c++)
185                                         {
186                                                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
187                                                 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
188                                                         umodes.push_back(c);
189                                                 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
190                                                 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
191                                                         cmodes.push_back(c);
192                                         }
193                                         context.Write("modeperms", "user=" + umodes + " channel=" + cmodes);
194
195                                         CheckContext::List opcmdlist(context, "commandperms");
196                                         for (OperInfo::PrivSet::const_iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); ++i)
197                                                 opcmdlist.Add(*i);
198                                         opcmdlist.Flush();
199                                         CheckContext::List privlist(context, "permissions");
200                                         for (OperInfo::PrivSet::const_iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); ++i)
201                                                 privlist.Add(*i);
202                                         privlist.Flush();
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                         dumpExt(context, targuser);
234                 }
235                 else if (targchan)
236                 {
237                         /* /check on a channel */
238                         context.Write("timestamp", timestring(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", timestring(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("%-3u %s%s (%s@%s) %s ", clonecount.global,
263                                         i->second->GetAllPrefixChars(), i->first->nick.c_str(),
264                                         i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.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                                 dumpListMode(context, (*i)->GetList(targchan));
270
271                         dumpExt(context, 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->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, 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->fullname);
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->fullname);
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 std::vector<std::string>& parameters)
303         {
304                 if (parameters.size() > 1)
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)