]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
96b357f20cc1d3716818447df7cbebd0ff105137
[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 /* $ModDesc: Provides the /CHECK command to retrieve information on a user, channel, hostname or IP address */
24
25 #include "inspircd.h"
26
27 /** Handle /CHECK
28  */
29 class CommandCheck : public Command
30 {
31  public:
32         CommandCheck(Module* parent) : Command(parent,"CHECK", 1)
33         {
34                 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
35         }
36
37         std::string timestring(time_t time)
38         {
39                 char timebuf[60];
40                 struct tm *mytime = gmtime(&time);
41                 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
42                 return std::string(timebuf);
43         }
44
45         void dumpExt(User* user, const std::string& checkstr, Extensible* ext)
46         {
47                 std::stringstream dumpkeys;
48                 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
49                 {
50                         ExtensionItem* item = i->first;
51                         std::string value = item->serialize(FORMAT_USER, ext, i->second);
52                         if (!value.empty())
53                                 user->SendText(checkstr + " meta:" + item->name + " " + value);
54                         else if (!item->name.empty())
55                                 dumpkeys << " " << item->name;
56                 }
57                 if (!dumpkeys.str().empty())
58                         user->SendText(checkstr + " metadata", dumpkeys);
59         }
60
61         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
62         {
63                 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName.c_str())
64                         return CMD_SUCCESS;
65
66                 User *targuser;
67                 Channel *targchan;
68                 std::string checkstr;
69                 std::string chliststr;
70
71                 checkstr = ":" + ServerInstance->Config->ServerName + " 304 " + user->nick + " :CHECK";
72
73                 targuser = ServerInstance->FindNick(parameters[0]);
74                 targchan = ServerInstance->FindChan(parameters[0]);
75
76                 /*
77                  * Syntax of a /check reply:
78                  *  :server.name 304 target :CHECK START <target>
79                  *  :server.name 304 target :CHECK <field> <value>
80                  *  :server.name 304 target :CHECK END
81                  */
82
83                 user->SendText(checkstr + " START " + parameters[0]);
84
85                 if (targuser)
86                 {
87                         LocalUser* loctarg = IS_LOCAL(targuser);
88                         /* /check on a user */
89                         user->SendText(checkstr + " nuh " + targuser->GetFullHost());
90                         user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost());
91                         user->SendText(checkstr + " realname " + targuser->fullname);
92                         user->SendText(checkstr + " modes +" + targuser->FormatModes());
93                         user->SendText(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
94                         user->SendText(checkstr + " server " + targuser->server);
95                         user->SendText(checkstr + " uid " + targuser->uuid);
96                         user->SendText(checkstr + " signon " + timestring(targuser->signon));
97                         user->SendText(checkstr + " nickts " + timestring(targuser->age));
98                         if (loctarg)
99                                 user->SendText(checkstr + " lastmsg " + timestring(targuser->idle_lastmsg));
100
101                         if (IS_AWAY(targuser))
102                         {
103                                 /* user is away */
104                                 user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime));
105                                 user->SendText(checkstr + " awaymsg " + targuser->awaymsg);
106                         }
107
108                         if (IS_OPER(targuser))
109                         {
110                                 OperInfo* oper = targuser->oper;
111                                 /* user is an oper of type ____ */
112                                 user->SendText(checkstr + " opertype " + oper->NameStr());
113                                 if (loctarg)
114                                 {
115                                         std::string umodes;
116                                         std::string cmodes;
117                                         for(char c='A'; c <= 'z'; c++)
118                                         {
119                                                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_USER);
120                                                 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_USER))
121                                                         umodes.push_back(c);
122                                                 mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
123                                                 if (mh && mh->NeedsOper() && loctarg->HasModePermission(c, MODETYPE_CHANNEL))
124                                                         cmodes.push_back(c);
125                                         }
126                                         user->SendText(checkstr + " modeperms user=" + umodes + " channel=" + cmodes);
127                                         std::string opcmds;
128                                         for(std::set<std::string>::iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); i++)
129                                         {
130                                                 opcmds.push_back(' ');
131                                                 opcmds.append(*i);
132                                         }
133                                         std::stringstream opcmddump(opcmds);
134                                         user->SendText(checkstr + " commandperms", opcmddump);
135                                         std::string privs;
136                                         for(std::set<std::string>::iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); i++)
137                                         {
138                                                 privs.push_back(' ');
139                                                 privs.append(*i);
140                                         }
141                                         std::stringstream privdump(privs);
142                                         user->SendText(checkstr + " permissions", privdump);
143                                 }
144                         }
145
146                         if (loctarg)
147                         {
148                                 user->SendText(checkstr + " clientaddr " + irc::sockets::satouser(loctarg->client_sa));
149                                 user->SendText(checkstr + " serveraddr " + irc::sockets::satouser(loctarg->server_sa));
150
151                                 std::string classname = loctarg->GetClass()->name;
152                                 if (!classname.empty())
153                                         user->SendText(checkstr + " connectclass " + classname);
154                         }
155                         else
156                                 user->SendText(checkstr + " onip " + targuser->GetIPString());
157
158                         for (UCListIter i = targuser->chans.begin(); i != targuser->chans.end(); i++)
159                         {
160                                 Channel* c = *i;
161                                 chliststr.append(c->GetPrefixChar(targuser)).append(c->name).append(" ");
162                         }
163
164                         std::stringstream dump(chliststr);
165
166                         user->SendText(checkstr + " onchans", dump);
167
168                         dumpExt(user, checkstr, targuser);
169                 }
170                 else if (targchan)
171                 {
172                         /* /check on a channel */
173                         user->SendText(checkstr + " timestamp " + timestring(targchan->age));
174
175                         if (targchan->topic[0] != 0)
176                         {
177                                 /* there is a topic, assume topic related information exists */
178                                 user->SendText(checkstr + " topic " + targchan->topic);
179                                 user->SendText(checkstr + " topic_setby " + targchan->setby);
180                                 user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset));
181                         }
182
183                         user->SendText(checkstr + " modes " + targchan->ChanModes(true));
184                         user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
185
186                         /* now the ugly bit, spool current members of a channel. :| */
187
188                         const UserMembList *ulist= targchan->GetUsers();
189
190                         /* note that unlike /names, we do NOT check +i vs in the channel */
191                         for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
192                         {
193                                 char tmpbuf[MAXBUF];
194                                 /*
195                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
196                                  */
197                                 snprintf(tmpbuf, MAXBUF, "%-3lu %s%s (%s@%s) %s ", ServerInstance->Users->GlobalCloneCount(i->first), targchan->GetAllPrefixChars(i->first), i->first->nick.c_str(), i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
198                                 user->SendText(checkstr + " member " + tmpbuf);
199                         }
200
201                         irc::modestacker modestack(true);
202                         for(BanList::iterator b = targchan->bans.begin(); b != targchan->bans.end(); ++b)
203                         {
204                                 modestack.Push('b', b->data);
205                         }
206                         std::vector<std::string> stackresult;
207                         std::vector<TranslateType> dummy;
208                         while (modestack.GetStackedLine(stackresult))
209                         {
210                                 creator->ProtoSendMode(user, TYPE_CHANNEL, targchan, stackresult, dummy);
211                                 stackresult.clear();
212                         }
213                         FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(targchan,creator,user));
214                         dumpExt(user, checkstr, targchan);
215                 }
216                 else
217                 {
218                         /*  /check on an IP address, or something that doesn't exist */
219                         long x = 0;
220
221                         /* hostname or other */
222                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
223                         {
224                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
225                                 {
226                                         /* host or vhost matches mask */
227                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
228                                 }
229                                 /* IP address */
230                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
231                                 {
232                                         /* same IP. */
233                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost() + " " + a->second->GetIPString() + " " + a->second->fullname);
234                                 }
235                         }
236
237                         user->SendText(checkstr + " matches " + ConvToStr(x));
238                 }
239
240                 user->SendText(checkstr + " END " + parameters[0]);
241
242                 return CMD_SUCCESS;
243         }
244
245         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
246         {
247                 if (parameters.size() > 1)
248                         return ROUTE_OPT_UCAST(parameters[1]);
249                 return ROUTE_LOCALONLY;
250         }
251 };
252
253
254 class ModuleCheck : public Module
255 {
256  private:
257         CommandCheck mycommand;
258  public:
259         ModuleCheck() : mycommand(this)
260         {
261         }
262
263         void init()
264         {
265                 ServerInstance->Modules->AddService(mycommand);
266         }
267
268         ~ModuleCheck()
269         {
270         }
271
272         void ProtoSendMode(void* uv, TargetTypeFlags, void*, const std::vector<std::string>& result, const std::vector<TranslateType>&)
273         {
274                 User* user = (User*)uv;
275                 std::string checkstr(":");
276                 checkstr.append(ServerInstance->Config->ServerName);
277                 checkstr.append(" 304 ");
278                 checkstr.append(user->nick);
279                 checkstr.append(" :CHECK modelist");
280                 for(unsigned int i=0; i < result.size(); i++)
281                 {
282                         checkstr.append(" ");
283                         checkstr.append(result[i]);
284                 }
285                 user->SendText(checkstr);
286         }
287
288         Version GetVersion()
289         {
290                 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
291         }
292 };
293
294 MODULE_INIT(ModuleCheck)