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