]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
f8e770337794b60065b2e8f5b52b1936c6ef0e5a
[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         std::set<std::string> meta_seen;
24         CommandCheck (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"CHECK", "o", 1)
25         {
26                 syntax = "<nickname>|<ip>|<hostmask>|<channel>";
27         }
28
29         std::string timestring(time_t time)
30         {
31                 char timebuf[60];
32                 struct tm *mytime = gmtime(&time);
33                 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
34                 return std::string(timebuf);
35         }
36
37         void dumpExtra(User* user, std::string checkstr, Extensible* ext)
38         {
39                 std::deque<std::string> extlist;
40                 ext->GetExtList(extlist);
41                 std::stringstream dumpkeys;
42                 for(std::deque<std::string>::iterator i = extlist.begin(); i != extlist.end(); i++)
43                 {
44                         if (meta_seen.find(*i) == meta_seen.end())
45                                 dumpkeys << " " << *i;
46                 }
47                 meta_seen.clear();
48                 if (!dumpkeys.str().empty())
49                         ServerInstance->DumpText(user,checkstr + " metadata ", dumpkeys);
50         }
51
52         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
53         {
54                 User *targuser;
55                 Channel *targchan;
56                 std::string checkstr;
57                 std::string chliststr;
58
59                 checkstr = "304 " + std::string(user->nick) + " :CHECK";
60
61                 targuser = ServerInstance->FindNick(parameters[0]);
62                 targchan = ServerInstance->FindChan(parameters[0]);
63
64                 /*
65                  * Syntax of a /check reply:
66                  *  :server.name 304 target :CHECK START <target>
67                  *  :server.name 304 target :CHECK <field> <value>
68                  *  :server.name 304 target :CHECK END
69                  */
70
71                 user->WriteServ(checkstr + " START " + parameters[0]);
72
73                 if (targuser)
74                 {
75                         /* /check on a user */
76                         user->WriteServ(checkstr + " nuh " + targuser->GetFullHost());
77                         user->WriteServ(checkstr + " realnuh " + targuser->GetFullRealHost());
78                         user->WriteServ(checkstr + " realname " + targuser->fullname);
79                         user->WriteServ(checkstr + " modes +" + targuser->FormatModes());
80                         user->WriteServ(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
81                         user->WriteServ(checkstr + " server " + targuser->server);
82                         user->WriteServ(checkstr + " uid " + targuser->uuid);
83                         user->WriteServ(checkstr + " signon " + timestring(targuser->signon));
84                         user->WriteServ(checkstr + " nickts " + timestring(targuser->age));
85                         if (IS_LOCAL(targuser))
86                                 user->WriteServ(checkstr + " lastmsg " + timestring(targuser->idle_lastmsg));
87
88                         if (IS_AWAY(targuser))
89                         {
90                                 /* user is away */
91                                 user->WriteServ(checkstr + " awaytime " + timestring(targuser->awaytime));
92                                 user->WriteServ(checkstr + " awaymsg " + targuser->awaymsg);
93                         }
94
95                         if (IS_OPER(targuser))
96                         {
97                                 /* user is an oper of type ____ */
98                                 user->WriteServ(checkstr + " opertype " + irc::Spacify(targuser->oper.c_str()));
99                         }
100
101                         user->WriteServ(checkstr + " onip " + targuser->GetIPString());
102                         if (IS_LOCAL(targuser))
103                         {
104                                 user->WriteServ(checkstr + " onport " + ConvToStr(targuser->GetServerPort()));
105                                 std::string classname = targuser->GetClass()->name;
106                                 if (!classname.empty())
107                                         user->WriteServ(checkstr + " connectclass " + classname);
108                         }
109
110                         chliststr = targuser->ChannelList(targuser);
111                         std::stringstream dump(chliststr);
112
113                         ServerInstance->DumpText(user,checkstr + " onchans ", dump);
114
115                         FOREACH_MOD_I(ServerInstance,I_OnSyncUser,OnSyncUser(targuser,creator,(void*)user));
116                         dumpExtra(user, checkstr, targuser);
117                 }
118                 else if (targchan)
119                 {
120                         /* /check on a channel */
121                         user->WriteServ(checkstr + " timestamp " + timestring(targchan->age));
122
123                         if (targchan->topic[0] != 0)
124                         {
125                                 /* there is a topic, assume topic related information exists */
126                                 user->WriteServ(checkstr + " topic " + targchan->topic);
127                                 user->WriteServ(checkstr + " topic_setby " + targchan->setby);
128                                 user->WriteServ(checkstr + " topic_setat " + timestring(targchan->topicset));
129                         }
130
131                         user->WriteServ(checkstr + " modes " + targchan->ChanModes(true));
132                         user->WriteServ(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
133
134                         /* now the ugly bit, spool current members of a channel. :| */
135
136                         CUList *ulist= targchan->GetUsers();
137
138                         /* note that unlike /names, we do NOT check +i vs in the channel */
139                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
140                         {
141                                 char tmpbuf[MAXBUF];
142                                 /*
143                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
144                                  */
145                                 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());
146                                 user->WriteServ(checkstr + " member " + tmpbuf);
147                         }
148
149                         FOREACH_MOD_I(ServerInstance,I_OnSyncChannel,OnSyncChannel(targchan,creator,(void*)user));
150                         dumpExtra(user, checkstr, targchan);
151                 }
152                 else
153                 {
154                         /*  /check on an IP address, or something that doesn't exist */
155                         long x = 0;
156
157                         /* hostname or other */
158                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
159                         {
160                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
161                                 {
162                                         /* host or vhost matches mask */
163                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
164                                 }
165                                 /* IP address */
166                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
167                                 {
168                                         /* same IP. */
169                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
170                                 }
171                         }
172
173                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
174                 }
175
176                 user->WriteServ(checkstr + " END " + parameters[0]);
177
178                 return CMD_LOCALONLY;
179         }
180 };
181
182
183 class ModuleCheck : public Module
184 {
185  private:
186         CommandCheck mycommand;
187  public:
188         ModuleCheck(InspIRCd* Me) : Module(Me), mycommand(Me, this)
189         {
190                 ServerInstance->AddCommand(&mycommand);
191         }
192
193         virtual ~ModuleCheck()
194         {
195         }
196
197         virtual Version GetVersion()
198         {
199                 return Version("$Id$", VF_VENDOR, API_VERSION);
200         }
201
202         virtual void ProtoSendMetaData(void* opaque, Extensible* target, const std::string& name, const std::string& value)
203         {
204                 User* user = static_cast<User*>(opaque);
205                 user->WriteServ("304 " + std::string(user->nick) + " :CHECK meta:" + name + " " + value);
206                 mycommand.meta_seen.insert(name);
207         }
208
209         virtual std::string ProtoTranslate(Extensible* item)
210         {
211                 User* u = dynamic_cast<User*>(item);
212                 Channel* c = dynamic_cast<Channel*>(item);
213                 if (u)
214                         return u->nick;
215                 if (c)
216                         return c->name;
217                 return "?";
218         }
219 };
220
221 MODULE_INIT(ModuleCheck)