]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
Merge pull request #179 from attilamolnar/insp20+desyncfix
[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, 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 = std::string(":") + ServerInstance->Config->ServerName + " 304 " + std::string(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                         std::string letter_b("b");
203                         for(BanList::iterator b = targchan->bans.begin(); b != targchan->bans.end(); ++b)
204                         {
205                                 modestack.Push('b', b->data);
206                         }
207                         std::vector<std::string> stackresult;
208                         std::vector<TranslateType> dummy;
209                         while (modestack.GetStackedLine(stackresult))
210                         {
211                                 creator->ProtoSendMode(user, TYPE_CHANNEL, targchan, stackresult, dummy);
212                                 stackresult.clear();
213                         }
214                         FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(targchan,creator,user));
215                         dumpExt(user, checkstr, targchan);
216                 }
217                 else
218                 {
219                         /*  /check on an IP address, or something that doesn't exist */
220                         long x = 0;
221
222                         /* hostname or other */
223                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
224                         {
225                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
226                                 {
227                                         /* host or vhost matches mask */
228                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
229                                 }
230                                 /* IP address */
231                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
232                                 {
233                                         /* same IP. */
234                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
235                                 }
236                         }
237
238                         user->SendText(checkstr + " matches " + ConvToStr(x));
239                 }
240
241                 user->SendText(checkstr + " END " + parameters[0]);
242
243                 return CMD_SUCCESS;
244         }
245
246         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
247         {
248                 if (parameters.size() > 1)
249                         return ROUTE_OPT_UCAST(parameters[1]);
250                 return ROUTE_LOCALONLY;
251         }
252 };
253
254
255 class ModuleCheck : public Module
256 {
257  private:
258         CommandCheck mycommand;
259  public:
260         ModuleCheck() : mycommand(this)
261         {
262         }
263
264         void init()
265         {
266                 ServerInstance->Modules->AddService(mycommand);
267         }
268
269         ~ModuleCheck()
270         {
271         }
272
273         void ProtoSendMode(void* uv, TargetTypeFlags, void*, const std::vector<std::string>& result, const std::vector<TranslateType>&)
274         {
275                 User* user = (User*)uv;
276                 std::string checkstr(":");
277                 checkstr.append(ServerInstance->Config->ServerName);
278                 checkstr.append(" 304 ");
279                 checkstr.append(user->nick);
280                 checkstr.append(" :CHECK modelist");
281                 for(unsigned int i=0; i < result.size(); i++)
282                 {
283                         checkstr.append(" ");
284                         checkstr.append(result[i]);
285                 }
286                 user->SendText(checkstr);
287         }
288
289         Version GetVersion()
290         {
291                 return Version("CHECK command, view user, channel, IP address or hostname information", VF_VENDOR|VF_OPTCOMMON);
292         }
293 };
294
295 MODULE_INIT(ModuleCheck)