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