]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
8b3ee3edf7e3c817600ef2804a31e615b6e0ab1a
[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                         if (IS_LOCAL(targuser))
102                         {
103                                 user->WriteServ(checkstr + " clientaddr " + irc::sockets::satouser(&targuser->client_sa));
104                                 user->WriteServ(checkstr + " serveraddr " + irc::sockets::satouser(&targuser->server_sa));
105
106                                 std::string classname = targuser->GetClass()->name;
107                                 if (!classname.empty())
108                                         user->WriteServ(checkstr + " connectclass " + classname);
109                         }
110                         else
111                                 user->WriteServ(checkstr + " onip " + targuser->GetIPString());
112
113                         chliststr = targuser->ChannelList(targuser);
114                         std::stringstream dump(chliststr);
115
116                         ServerInstance->DumpText(user,checkstr + " onchans ", dump);
117
118                         FOREACH_MOD_I(ServerInstance,I_OnSyncUser,OnSyncUser(targuser,creator,(void*)user));
119                         dumpExtra(user, checkstr, targuser);
120                 }
121                 else if (targchan)
122                 {
123                         /* /check on a channel */
124                         user->WriteServ(checkstr + " timestamp " + timestring(targchan->age));
125
126                         if (targchan->topic[0] != 0)
127                         {
128                                 /* there is a topic, assume topic related information exists */
129                                 user->WriteServ(checkstr + " topic " + targchan->topic);
130                                 user->WriteServ(checkstr + " topic_setby " + targchan->setby);
131                                 user->WriteServ(checkstr + " topic_setat " + timestring(targchan->topicset));
132                         }
133
134                         user->WriteServ(checkstr + " modes " + targchan->ChanModes(true));
135                         user->WriteServ(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
136
137                         /* now the ugly bit, spool current members of a channel. :| */
138
139                         CUList *ulist= targchan->GetUsers();
140
141                         /* note that unlike /names, we do NOT check +i vs in the channel */
142                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
143                         {
144                                 char tmpbuf[MAXBUF];
145                                 /*
146                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
147                                  */
148                                 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());
149                                 user->WriteServ(checkstr + " member " + tmpbuf);
150                         }
151
152                         FOREACH_MOD_I(ServerInstance,I_OnSyncChannel,OnSyncChannel(targchan,creator,(void*)user));
153                         dumpExtra(user, checkstr, targchan);
154                 }
155                 else
156                 {
157                         /*  /check on an IP address, or something that doesn't exist */
158                         long x = 0;
159
160                         /* hostname or other */
161                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
162                         {
163                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
164                                 {
165                                         /* host or vhost matches mask */
166                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
167                                 }
168                                 /* IP address */
169                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
170                                 {
171                                         /* same IP. */
172                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
173                                 }
174                         }
175
176                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
177                 }
178
179                 user->WriteServ(checkstr + " END " + parameters[0]);
180
181                 return CMD_LOCALONLY;
182         }
183 };
184
185
186 class ModuleCheck : public Module
187 {
188  private:
189         CommandCheck mycommand;
190  public:
191         ModuleCheck(InspIRCd* Me) : Module(Me), mycommand(Me, this)
192         {
193                 ServerInstance->AddCommand(&mycommand);
194         }
195
196         virtual ~ModuleCheck()
197         {
198         }
199
200         virtual Version GetVersion()
201         {
202                 return Version("$Id$", VF_VENDOR, API_VERSION);
203         }
204
205         virtual void ProtoSendMetaData(void* opaque, Extensible* target, const std::string& name, const std::string& value)
206         {
207                 User* user = static_cast<User*>(opaque);
208                 user->WriteServ("304 " + std::string(user->nick) + " :CHECK meta:" + name + " " + value);
209                 mycommand.meta_seen.insert(name);
210         }
211
212         virtual std::string ProtoTranslate(Extensible* item)
213         {
214                 User* u = dynamic_cast<User*>(item);
215                 Channel* c = dynamic_cast<Channel*>(item);
216                 if (u)
217                         return u->nick;
218                 if (c)
219                         return c->name;
220                 return "?";
221         }
222 };
223
224 MODULE_INIT(ModuleCheck)