]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
251faf58a88113f96bb858738d7456df9eda49bc
[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 #include "helperfuncs.h"
25 #include "wildcard.h"
26
27 /* $ModDesc: Provides the /check command to retrieve information on a user, channel, or IP address */
28
29 extern InspIRCd* ServerInstance;
30
31 static Server *Srv;
32
33 class cmd_check : public command_t
34 {
35  public:
36         cmd_check() : command_t("CHECK", 'o', 1)
37         {
38                 this->source = "m_check.so";
39                 syntax = "<nickname>|<ip>|<hostmask>|<channel>";
40         }
41
42         void Handle (const 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 = ServerInstance->FindNick(parameters[0]);
56                 targchan = ServerInstance->FindChan(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                 user->WriteServ(checkstr + " START " + parameters[0]);
66
67                 if (targuser)
68                 {
69                         /* /check on a user */
70                         user->WriteServ(checkstr + " nuh " + targuser->GetFullHost());
71                         user->WriteServ(checkstr + " realnuh " + targuser->GetFullRealHost());
72                         user->WriteServ(checkstr + " realname " + targuser->fullname);
73                         user->WriteServ(checkstr + " modes +" + targuser->FormatModes());
74                         user->WriteServ(checkstr + " server " + targuser->server);
75                         if (targuser->awaymsg[0] != 0)
76                         {
77                                 /* user is away */
78                                 user->WriteServ(checkstr + " awaymsg " + targuser->awaymsg);
79                         }
80                         if (targuser->oper[0] != 0)
81                         {
82                                 /* user is an oper of type ____ */
83                                 user->WriteServ(checkstr + " opertype " + targuser->oper);
84                         }
85                         if (IS_LOCAL(targuser))
86                         {
87                                 /* port information is only held for a local user! */
88                                 user->WriteServ(checkstr + " onport " + ConvToStr(targuser->GetPort()));
89                         }
90
91                         chliststr = targuser->ChannelList(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                         user->WriteServ(checkstr + " created " + timebuf);
105
106                         if (targchan->topic[0] != 0)
107                         {
108                                 /* there is a topic, assume topic related information exists */
109                                 user->WriteServ(checkstr + " topic " + targchan->topic);
110                                 user->WriteServ(checkstr + " topic_setby " + targchan->setby);
111                                 mytime = gmtime(&topic_time);
112                                 strftime(timebuf, 59, "%Y/%m/%d - %H:%M:%S", mytime);
113                                 user->WriteServ(checkstr + " topic_setat " + timebuf);
114                         }
115
116                         user->WriteServ(checkstr + " modes " + targchan->ChanModes(true));
117                         user->WriteServ(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 = targchan->GetStatusFlags(i->second);
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    ", i->second->GlobalCloneCount());
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                                 user->WriteServ(checkstr + " member " + ptr);
155                         }
156                 }
157                 else
158                 {
159                         /*  /check on an IP address, or something that doesn't exist */
160                         long x = 0;
161
162                         /* hostname or other */
163                         for (user_hash::const_iterator a = ServerInstance->clientlist.begin(); a != ServerInstance->clientlist.end(); a++)
164                         {
165                                 if (match(a->second->host, parameters[0]) || match(a->second->dhost, parameters[0]))
166                                 {
167                                         /* host or vhost matches mask */
168                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
169                                 }
170                                 /* IP address */
171                                 else if (match(a->second->GetIPString(), parameters[0]))
172                                 {
173                                         /* same IP. */
174                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
175                                 }
176                         }
177
178                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
179                 }
180
181                 user->WriteServ(checkstr + " END " + std::string(parameters[0]));
182         }
183 };
184
185
186 class ModuleCheck : public Module
187 {
188  private:
189         cmd_check *mycommand;
190  public:
191         ModuleCheck(InspIRCd* Me) : Module::Module(Me)
192         {
193                 
194                 mycommand = new cmd_check();
195                 Srv->AddCommand(mycommand);
196         }
197         
198         virtual ~ModuleCheck()
199         {
200         }
201         
202         virtual Version GetVersion()
203         {
204                 return Version(1, 0, 0, 0, VF_VENDOR);
205         }
206
207         void Implements(char* List)
208         {
209                 /* we don't hook anything, nothing required */
210         }
211         
212 };
213
214
215
216 class ModuleCheckFactory : public ModuleFactory
217 {
218  public:
219         ModuleCheckFactory()
220         {
221         }
222         
223         ~ModuleCheckFactory()
224         {
225         }
226         
227         virtual Module * CreateModule(InspIRCd* Me)
228         {
229                 return new ModuleCheck(Me);
230         }
231         
232 };
233
234 extern "C" void * init_module( void )
235 {
236         return new ModuleCheckFactory;
237 }
238