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