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