]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
7a7fe570bb39f809f31aee7343a4577ebf93a6af
[user/henk/code/inspircd.git] / src / modules / m_check.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18 #include "wildcard.h"
19
20 /* $ModDesc: Provides the /check command to retrieve information on a user, channel, or IP address */
21
22 /** Handle /CHECK
23  */
24 class cmd_check : public command_t
25 {
26  public:
27         cmd_check (InspIRCd* Instance) : command_t(Instance,"CHECK", 'o', 1)
28         {
29                 this->source = "m_check.so";
30                 syntax = "<nickname>|<ip>|<hostmask>|<channel>";
31         }
32
33         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
34         {
35                 userrec *targuser;
36                 chanrec *targchan;
37                 std::string checkstr;
38                 std::string chliststr;
39
40                 char timebuf[60];
41                 struct tm *mytime;
42
43
44                 checkstr = "304 " + std::string(user->nick) + " :CHECK";
45
46                 targuser = ServerInstance->FindNick(parameters[0]);
47                 targchan = ServerInstance->FindChan(parameters[0]);
48
49                 /*
50                  * Syntax of a /check reply:
51                  *  :server.name 304 target :CHECK START <target>
52                  *  :server.name 304 target :CHECK <field> <value>
53                  *  :server.name 304 target :CHECK END
54                  */
55
56                 user->WriteServ(checkstr + " START " + parameters[0]);
57
58                 if (targuser)
59                 {
60                         /* /check on a user */
61                         user->WriteServ(checkstr + " nuh " + targuser->GetFullHost());
62                         user->WriteServ(checkstr + " realnuh " + targuser->GetFullRealHost());
63                         user->WriteServ(checkstr + " realname " + targuser->fullname);
64                         user->WriteServ(checkstr + " modes +" + targuser->FormatModes());
65                         user->WriteServ(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
66                         user->WriteServ(checkstr + " server " + targuser->server);
67
68                         if (IS_AWAY(targuser))
69                         {
70                                 /* user is away */
71                                 user->WriteServ(checkstr + " awaymsg " + targuser->awaymsg);
72                         }
73
74                         if (IS_OPER(targuser))
75                         {
76                                 /* user is an oper of type ____ */
77                                 user->WriteServ(checkstr + " opertype " + irc::Spacify(targuser->oper));
78                         }
79
80                         if (IS_LOCAL(targuser))
81                         {
82                                 /* port information is only held for a local user! */
83                                 user->WriteServ(checkstr + " onport " + ConvToStr(targuser->GetPort()));
84                         }
85
86                         chliststr = targuser->ChannelList(targuser);
87                         std::stringstream dump(chliststr);
88
89                         ServerInstance->DumpText(user,checkstr + " onchans ", dump);
90                 }
91                 else if (targchan)
92                 {
93                         /* /check on a channel */
94                         time_t creation_time = targchan->created;
95                         time_t topic_time = targchan->topicset;
96
97                         mytime = gmtime(&creation_time);
98                         strftime(timebuf, 59, "%Y/%m/%d - %H:%M:%S", mytime);
99                         user->WriteServ(checkstr + " created " + timebuf);
100
101                         if (targchan->topic[0] != 0)
102                         {
103                                 /* there is a topic, assume topic related information exists */
104                                 user->WriteServ(checkstr + " topic " + targchan->topic);
105                                 user->WriteServ(checkstr + " topic_setby " + targchan->setby);
106                                 mytime = gmtime(&topic_time);
107                                 strftime(timebuf, 59, "%Y/%m/%d - %H:%M:%S", mytime);
108                                 user->WriteServ(checkstr + " topic_setat " + timebuf);
109                         }
110
111                         user->WriteServ(checkstr + " modes " + targchan->ChanModes(true));
112                         user->WriteServ(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
113                         
114                         /* now the ugly bit, spool current members of a channel. :| */
115
116                         CUList *ulist= targchan->GetUsers();
117
118                         /* note that unlike /names, we do NOT check +i vs in the channel */
119                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
120                         {
121                                 char tmpbuf[MAXBUF];
122                                 /*
123                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
124                                  */
125                                 snprintf(tmpbuf, MAXBUF, "%lu    %s%s (%s@%s) %s ", i->first->GlobalCloneCount(), targchan->GetAllPrefixChars(i->first), i->first->nick, i->first->ident, i->first->dhost, i->first->fullname);
126                                 user->WriteServ(checkstr + " member " + tmpbuf);
127                         }
128                 }
129                 else
130                 {
131                         /*  /check on an IP address, or something that doesn't exist */
132                         long x = 0;
133
134                         /* hostname or other */
135                         for (user_hash::const_iterator a = ServerInstance->clientlist->begin(); a != ServerInstance->clientlist->end(); a++)
136                         {
137                                 if (match(a->second->host, parameters[0]) || match(a->second->dhost, parameters[0]))
138                                 {
139                                         /* host or vhost matches mask */
140                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
141                                 }
142                                 /* IP address */
143                                 else if (match(a->second->GetIPString(), parameters[0], true))
144                                 {
145                                         /* same IP. */
146                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
147                                 }
148                         }
149
150                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
151                 }
152
153                 user->WriteServ(checkstr + " END " + std::string(parameters[0]));
154
155                 return CMD_LOCALONLY;
156         }
157 };
158
159
160 class ModuleCheck : public Module
161 {
162  private:
163         cmd_check *mycommand;
164  public:
165         ModuleCheck(InspIRCd* Me) : Module::Module(Me)
166         {
167                 
168                 mycommand = new cmd_check(ServerInstance);
169                 ServerInstance->AddCommand(mycommand);
170         }
171         
172         virtual ~ModuleCheck()
173         {
174         }
175         
176         virtual Version GetVersion()
177         {
178                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
179         }
180
181         void Implements(char* List)
182         {
183                 /* we don't hook anything, nothing required */
184         }
185         
186 };
187
188
189
190 class ModuleCheckFactory : public ModuleFactory
191 {
192  public:
193         ModuleCheckFactory()
194         {
195         }
196         
197         ~ModuleCheckFactory()
198         {
199         }
200         
201         virtual Module * CreateModule(InspIRCd* Me)
202         {
203                 return new ModuleCheck(Me);
204         }
205         
206 };
207
208 extern "C" void * init_module( void )
209 {
210         return new ModuleCheckFactory;
211 }
212