]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
cf07e364461b6347cb5b2fffdaa839e21fffee27
[user/henk/code/inspircd.git] / src / modules / m_check.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides the /check command to retrieve information on a user, channel, or IP address */
17
18 /** Handle /CHECK
19  */
20 class CommandCheck : public Command
21 {
22  public:
23         CommandCheck(Module* parent) : Command(parent,"CHECK", 1)
24         {
25                 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
26         }
27
28         std::string timestring(time_t time)
29         {
30                 char timebuf[60];
31                 struct tm *mytime = gmtime(&time);
32                 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
33                 return std::string(timebuf);
34         }
35
36         void dumpExt(User* user, std::string checkstr, Extensible* ext)
37         {
38                 std::stringstream dumpkeys;
39                 for(ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
40                 {
41                         ExtensionItem* item = Extensible::GetItem(i->first);
42                         std::string value;
43                         if (item)
44                                 value = item->serialize(FORMAT_USER, ext, i->second);
45                         if (value.empty())
46                                 dumpkeys << " " << i->first;
47                         else
48                                 ServerInstance->DumpText(user, checkstr + " meta:" + i->first + " " + value);
49                 }
50                 if (!dumpkeys.str().empty())
51                         ServerInstance->DumpText(user,checkstr + " metadata", dumpkeys);
52         }
53
54         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
55         {
56                 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName)
57                         return CMD_SUCCESS;
58
59                 User *targuser;
60                 Channel *targchan;
61                 std::string checkstr;
62                 std::string chliststr;
63
64                 checkstr = std::string(":") + ServerInstance->Config->ServerName + " 304 " + std::string(user->nick) + " :CHECK";
65
66                 targuser = ServerInstance->FindNick(parameters[0]);
67                 targchan = ServerInstance->FindChan(parameters[0]);
68
69                 /*
70                  * Syntax of a /check reply:
71                  *  :server.name 304 target :CHECK START <target>
72                  *  :server.name 304 target :CHECK <field> <value>
73                  *  :server.name 304 target :CHECK END
74                  */
75
76                 ServerInstance->DumpText(user, checkstr + " START " + parameters[0]);
77
78                 if (targuser)
79                 {
80                         /* /check on a user */
81                         ServerInstance->DumpText(user, checkstr + " nuh " + targuser->GetFullHost());
82                         ServerInstance->DumpText(user, checkstr + " realnuh " + targuser->GetFullRealHost());
83                         ServerInstance->DumpText(user, checkstr + " realname " + targuser->fullname);
84                         ServerInstance->DumpText(user, checkstr + " modes +" + targuser->FormatModes());
85                         ServerInstance->DumpText(user, checkstr + " snomasks +" + targuser->FormatNoticeMasks());
86                         ServerInstance->DumpText(user, checkstr + " server " + targuser->server);
87                         ServerInstance->DumpText(user, checkstr + " uid " + targuser->uuid);
88                         ServerInstance->DumpText(user, checkstr + " signon " + timestring(targuser->signon));
89                         ServerInstance->DumpText(user, checkstr + " nickts " + timestring(targuser->age));
90                         if (IS_LOCAL(targuser))
91                                 ServerInstance->DumpText(user, checkstr + " lastmsg " + timestring(targuser->idle_lastmsg));
92
93                         if (IS_AWAY(targuser))
94                         {
95                                 /* user is away */
96                                 ServerInstance->DumpText(user, checkstr + " awaytime " + timestring(targuser->awaytime));
97                                 ServerInstance->DumpText(user, checkstr + " awaymsg " + targuser->awaymsg);
98                         }
99
100                         if (IS_OPER(targuser))
101                         {
102                                 /* user is an oper of type ____ */
103                                 ServerInstance->DumpText(user, checkstr + " opertype " + irc::Spacify(targuser->oper.c_str()));
104                         }
105
106                         if (IS_LOCAL(targuser))
107                         {
108                                 ServerInstance->DumpText(user, checkstr + " clientaddr " + irc::sockets::satouser(&targuser->client_sa));
109                                 ServerInstance->DumpText(user, checkstr + " serveraddr " + irc::sockets::satouser(&targuser->server_sa));
110
111                                 std::string classname = targuser->GetClass()->name;
112                                 if (!classname.empty())
113                                         ServerInstance->DumpText(user, checkstr + " connectclass " + classname);
114                         }
115                         else
116                                 ServerInstance->DumpText(user, checkstr + " onip " + targuser->GetIPString());
117
118                         for (UCListIter i = targuser->chans.begin(); i != targuser->chans.end(); i++)
119                         {
120                                 Channel* c = *i;
121                                 chliststr.append(c->GetPrefixChar(targuser)).append(c->name).append(" ");
122                         }
123
124                         std::stringstream dump(chliststr);
125
126                         ServerInstance->DumpText(user,checkstr + " onchans", dump);
127
128                         dumpExt(user, checkstr, targuser);
129                 }
130                 else if (targchan)
131                 {
132                         /* /check on a channel */
133                         ServerInstance->DumpText(user, checkstr + " timestamp " + timestring(targchan->age));
134
135                         if (targchan->topic[0] != 0)
136                         {
137                                 /* there is a topic, assume topic related information exists */
138                                 ServerInstance->DumpText(user, checkstr + " topic " + targchan->topic);
139                                 ServerInstance->DumpText(user, checkstr + " topic_setby " + targchan->setby);
140                                 ServerInstance->DumpText(user, checkstr + " topic_setat " + timestring(targchan->topicset));
141                         }
142
143                         ServerInstance->DumpText(user, checkstr + " modes " + targchan->ChanModes(true));
144                         ServerInstance->DumpText(user, checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
145
146                         /* now the ugly bit, spool current members of a channel. :| */
147
148                         const UserMembList *ulist= targchan->GetUsers();
149
150                         /* note that unlike /names, we do NOT check +i vs in the channel */
151                         for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
152                         {
153                                 char tmpbuf[MAXBUF];
154                                 /*
155                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
156                                  */
157                                 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());
158                                 ServerInstance->DumpText(user, checkstr + " member " + tmpbuf);
159                         }
160
161                         dumpExt(user, checkstr, targchan);
162                 }
163                 else
164                 {
165                         /*  /check on an IP address, or something that doesn't exist */
166                         long x = 0;
167
168                         /* hostname or other */
169                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
170                         {
171                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
172                                 {
173                                         /* host or vhost matches mask */
174                                         ServerInstance->DumpText(user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
175                                 }
176                                 /* IP address */
177                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
178                                 {
179                                         /* same IP. */
180                                         ServerInstance->DumpText(user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
181                                 }
182                         }
183
184                         ServerInstance->DumpText(user, checkstr + " matches " + ConvToStr(x));
185                 }
186
187                 ServerInstance->DumpText(user, checkstr + " END " + parameters[0]);
188
189                 return CMD_SUCCESS;
190         }
191
192         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
193         {
194                 if (parameters.size() > 1)
195                         return ROUTE_OPT_UCAST(parameters[1]);
196                 return ROUTE_LOCALONLY;
197         }
198 };
199
200
201 class ModuleCheck : public Module
202 {
203  private:
204         CommandCheck mycommand;
205  public:
206         ModuleCheck(InspIRCd* Me) : Module(Me), mycommand(this)
207         {
208                 ServerInstance->AddCommand(&mycommand);
209         }
210
211         ~ModuleCheck()
212         {
213         }
214
215         Version GetVersion()
216         {
217                 return Version("CHECK command, view user/channel details", VF_VENDOR|VF_OPTCOMMON);
218         }
219 };
220
221 MODULE_INIT(ModuleCheck)