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