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