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