]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
Annotations
[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 list[MAXBUF];
123                                 char tmpbuf[MAXBUF];
124                                 char* ptr = list;
125                                 int flags = targchan->GetStatusFlags(i->second);
126                                 /*
127                                  * find how many connections from this user's IP -- unlike Asuka,
128                                  * I define a clone as coming from the same host. --w00t
129                                  */
130                                 sprintf(ptr, "%lu    ", i->second->GlobalCloneCount());
131                                 
132                                 if (flags & UCMODE_OP)
133                                 {
134                                         strcat(ptr, "@");
135                                 }
136                                 
137                                 if (flags & UCMODE_HOP)
138                                 {
139                                         strcat(ptr, "%");
140                                 }
141                                 
142                                 if (flags & UCMODE_VOICE)
143                                 {
144                                         strcat(ptr, "+");
145                                 }
146                                 
147                                 sprintf(tmpbuf, "%s (%s@%s) %s ", i->second->nick, i->second->ident, i->second->dhost, i->second->fullname);
148                                 strcat(ptr, tmpbuf);
149                                 
150                                 user->WriteServ(checkstr + " member " + ptr);
151                         }
152                 }
153                 else
154                 {
155                         /*  /check on an IP address, or something that doesn't exist */
156                         long x = 0;
157
158                         /* hostname or other */
159                         for (user_hash::const_iterator a = ServerInstance->clientlist.begin(); a != ServerInstance->clientlist.end(); a++)
160                         {
161                                 if (match(a->second->host, parameters[0]) || match(a->second->dhost, parameters[0]))
162                                 {
163                                         /* host or vhost matches mask */
164                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
165                                 }
166                                 /* IP address */
167                                 else if (match(a->second->GetIPString(), parameters[0]))
168                                 {
169                                         /* same IP. */
170                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
171                                 }
172                         }
173
174                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
175                 }
176
177                 user->WriteServ(checkstr + " END " + std::string(parameters[0]));
178
179                 return CMD_SUCCESS;
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