]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
Add support for syntax of commands in place of the text 'Not enough parameters' in...
[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                         struct sockaddr_in addr;
162                         long x = 0;
163
164                         if (inet_aton(parameters[0], &addr.sin_addr) == 0)
165                         {
166                                 /* hostname or other */
167                                 for (user_hash::const_iterator a = clientlist.begin(); a != clientlist.end(); a++)
168                                 {
169                                         if (match(a->second->host, parameters[0]) || match(a->second->dhost, parameters[0]))
170                                         {
171                                                 /* host or vhost matches mask */
172                                                 Srv->SendTo(NULL, user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
173                                         }
174                                 }
175                         }
176                         else
177                         {
178                                 /* IP address */
179                                 for (user_hash::const_iterator a = clientlist.begin(); a != clientlist.end(); a++)
180                                 {
181                                         if (addr.sin_addr.s_addr == a->second->ip4.s_addr)
182                                         {
183                                                 /* same IP. */
184                                                 Srv->SendTo(NULL, user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
185                                         }
186                                 }
187                         }
188
189                         Srv->SendTo(NULL, user, checkstr + " matches " + ConvToStr(x));
190                 }
191
192                 Srv->SendTo(NULL, user, checkstr + " END " + std::string(parameters[0]));
193         }
194 };
195
196
197 class ModuleCheck : public Module
198 {
199  private:
200         cmd_check *mycommand;
201  public:
202         ModuleCheck(Server* Me) : Module::Module(Me)
203         {
204                 Srv = Me;
205                 mycommand = new cmd_check();
206                 Srv->AddCommand(mycommand);
207         }
208         
209         virtual ~ModuleCheck()
210         {
211         }
212         
213         virtual Version GetVersion()
214         {
215                 return Version(1, 0, 0, 0, VF_VENDOR);
216         }
217
218         void Implements(char* List)
219         {
220                 /* we don't hook anything, nothing required */
221         }
222         
223 };
224
225
226
227 class ModuleCheckFactory : public ModuleFactory
228 {
229  public:
230         ModuleCheckFactory()
231         {
232         }
233         
234         ~ModuleCheckFactory()
235         {
236         }
237         
238         virtual Module * CreateModule(Server* Me)
239         {
240                 return new ModuleCheck(Me);
241         }
242         
243 };
244
245 extern "C" void * init_module( void )
246 {
247         return new ModuleCheckFactory;
248 }
249