]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
e41e60b804260db264e0d973764aabed85351c0a
[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 "message.h"
23 #include "commands.h"
24 #include "inspircd.h"
25 #include "helperfuncs.h"
26
27 /* $ModDesc: Provides the /check command to retrieve information on a user, channel, or IP address */
28
29 extern user_hash clientlist;
30 extern bool match(const char *, const char *);
31
32 static Server *Srv;
33
34 class cmd_check : public command_t
35 {
36  public:
37         cmd_check() : command_t("CHECK", 'o', 1)
38         {
39                 this->source = "m_check.so";
40                 syntax = "<nickname>|<ip>|<hostmask>|<channel>";
41         }
42
43         void Handle (const char** parameters, int pcnt, userrec *user)
44         {
45                 userrec *targuser;
46                 chanrec *targchan;
47                 std::string checkstr;
48                 std::string chliststr;
49
50                 char timebuf[60];
51                 struct tm *mytime;
52
53
54                 checkstr = "304 " + std::string(user->nick) + " :CHECK";
55
56                 targuser = Srv->FindNick(std::string(parameters[0]));
57                 targchan = Srv->FindChannel(std::string(parameters[0]));
58
59                 /*
60                  * Syntax of a /check reply:
61                  *  :server.name 304 target :CHECK START <target>
62                  *  :server.name 304 target :CHECK <field> <value>
63                  *  :server.name 304 target :CHECK END
64                  */
65
66                 Srv->SendTo(NULL, user, checkstr + " START " + parameters[0]);
67
68                 if (targuser)
69                 {
70                         /* /check on a user */
71                         Srv->SendTo(NULL, user, checkstr + " nuh " + targuser->GetFullHost());
72                         Srv->SendTo(NULL, user, checkstr + " realnuh " + targuser->GetFullRealHost());
73                         Srv->SendTo(NULL, user, checkstr + " realname " + targuser->fullname);
74                         Srv->SendTo(NULL, user, checkstr + " modes +" + targuser->FormatModes());
75                         Srv->SendTo(NULL, user, checkstr + " server " + targuser->server);
76                         if (targuser->awaymsg[0] != 0)
77                         {
78                                 /* user is away */
79                                 Srv->SendTo(NULL, user, checkstr + " awaymsg " + targuser->awaymsg);
80                         }
81                         if (targuser->oper[0] != 0)
82                         {
83                                 /* user is an oper of type ____ */
84                                 Srv->SendTo(NULL, user, checkstr + " opertype " + targuser->oper);
85                         }
86                         if (IS_LOCAL(targuser))
87                         {
88                                 /* port information is only held for a local user! */
89                                 Srv->SendTo(NULL, user, checkstr + " onport " + ConvToStr(targuser->port));
90                         }
91
92                         chliststr = chlist(targuser, targuser);
93                         std::stringstream dump(chliststr);
94
95                         Srv->DumpText(user,checkstr + " onchans ", dump);
96                 }
97                 else if (targchan)
98                 {
99                         /* /check on a channel */
100                         time_t creation_time = targchan->created;
101                         time_t topic_time = targchan->topicset;
102
103                         mytime = gmtime(&creation_time);
104                         strftime(timebuf, 59, "%Y/%m/%d - %H:%M:%S", mytime);
105                         Srv->SendTo(NULL, user, checkstr + " created " + timebuf);
106
107                         if (targchan->topic[0] != 0)
108                         {
109                                 /* there is a topic, assume topic related information exists */
110                                 Srv->SendTo(NULL, user, checkstr + " topic " + targchan->topic);
111                                 Srv->SendTo(NULL, user, checkstr + " topic_setby " + targchan->setby);
112                                 mytime = gmtime(&topic_time);
113                                 strftime(timebuf, 59, "%Y/%m/%d - %H:%M:%S", mytime);
114                                 Srv->SendTo(NULL, user, checkstr + " topic_setat " + timebuf);
115                         }
116
117                         Srv->SendTo(NULL, user, checkstr + " modes " + chanmodes(targchan, true));
118                         Srv->SendTo(NULL, user, checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
119                         
120                         /* now the ugly bit, spool current members of a channel. :| */
121
122                         CUList *ulist= targchan->GetUsers();
123
124                         /* note that unlike /names, we do NOT check +i vs in the channel */
125                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
126                         {
127                                 char list[MAXBUF];
128                                 char tmpbuf[MAXBUF];
129                                 char* ptr = list;
130                                 int flags = cflags(i->second, targchan);
131                                 /*
132                                  * find how many connections from this user's IP -- unlike Asuka,
133                                  * I define a clone as coming from the same host. --w00t
134                                  */
135                                 sprintf(ptr, "%lu    ", FindMatchingGlobal(i->second));
136                                 
137                                 if (flags & UCMODE_OP)
138                                 {
139                                         strcat(ptr, "@");
140                                 }
141                                 
142                                 if (flags & UCMODE_HOP)
143                                 {
144                                         strcat(ptr, "%");
145                                 }
146                                 
147                                 if (flags & UCMODE_VOICE)
148                                 {
149                                         strcat(ptr, "+");
150                                 }
151                                 
152                                 sprintf(tmpbuf, "%s (%s@%s) %s ", i->second->nick, i->second->ident, i->second->dhost, i->second->fullname);
153                                 strcat(ptr, tmpbuf);
154                                 
155                                 Srv->SendTo(NULL, user, checkstr + " member " + ptr);
156                         }
157                 }
158                 else
159                 {
160                         /*  /check on an IP address, or something that doesn't exist */
161                         insp_sockaddr addr;
162                         long x = 0;
163 #ifdef IPV6
164                         if (insp_aton(parameters[0], &addr.sin6_addr) == 0)
165 #else
166                         if (insp_aton(parameters[0], &addr.sin_addr) == 0)
167 #endif
168                         {
169                                 /* hostname or other */
170                                 for (user_hash::const_iterator a = clientlist.begin(); a != clientlist.end(); a++)
171                                 {
172                                         if (match(a->second->host, parameters[0]) || match(a->second->dhost, parameters[0]))
173                                         {
174                                                 /* host or vhost matches mask */
175                                                 Srv->SendTo(NULL, user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
176                                         }
177                                 }
178                         }
179                         else
180                         {
181                                 /* IP address */
182                                 for (user_hash::const_iterator a = clientlist.begin(); a != clientlist.end(); a++)
183                                 {
184 #ifdef IPV6
185                                         /* TODO: Clone matching for IPV6 ips */
186 #else
187                                         if (addr.sin_addr.s_addr == a->second->ip4.s_addr)
188                                         {
189                                                 /* same IP. */
190                                                 Srv->SendTo(NULL, user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
191                                         }
192 #endif
193                                 }
194                         }
195
196                         Srv->SendTo(NULL, user, checkstr + " matches " + ConvToStr(x));
197                 }
198
199                 Srv->SendTo(NULL, user, checkstr + " END " + std::string(parameters[0]));
200         }
201 };
202
203
204 class ModuleCheck : public Module
205 {
206  private:
207         cmd_check *mycommand;
208  public:
209         ModuleCheck(Server* Me) : Module::Module(Me)
210         {
211                 Srv = Me;
212                 mycommand = new cmd_check();
213                 Srv->AddCommand(mycommand);
214         }
215         
216         virtual ~ModuleCheck()
217         {
218         }
219         
220         virtual Version GetVersion()
221         {
222                 return Version(1, 0, 0, 0, VF_VENDOR);
223         }
224
225         void Implements(char* List)
226         {
227                 /* we don't hook anything, nothing required */
228         }
229         
230 };
231
232
233
234 class ModuleCheckFactory : public ModuleFactory
235 {
236  public:
237         ModuleCheckFactory()
238         {
239         }
240         
241         ~ModuleCheckFactory()
242         {
243         }
244         
245         virtual Module * CreateModule(Server* Me)
246         {
247                 return new ModuleCheck(Me);
248         }
249         
250 };
251
252 extern "C" void * init_module( void )
253 {
254         return new ModuleCheckFactory;
255 }
256