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