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