]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
Change /CHECK <#channel> to correctly report timestamp since it might have been TS...
[user/henk/code/inspircd.git] / src / modules / m_check.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides the /check command to retrieve information on a user, channel, or IP address */
17
18 /** Handle /CHECK
19  */
20 class CommandCheck : public Command
21 {
22  public:
23         CommandCheck (InspIRCd* Instance) : Command(Instance,"CHECK", "o", 1)
24         {
25                 this->source = "m_check.so";
26                 syntax = "<nickname>|<ip>|<hostmask>|<channel>";
27         }
28
29         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
30         {
31                 User *targuser;
32                 Channel *targchan;
33                 std::string checkstr;
34                 std::string chliststr;
35
36                 char timebuf[60];
37                 struct tm *mytime;
38
39
40                 checkstr = "304 " + std::string(user->nick) + " :CHECK";
41
42                 targuser = ServerInstance->FindNick(parameters[0]);
43                 targchan = ServerInstance->FindChan(parameters[0]);
44
45                 /*
46                  * Syntax of a /check reply:
47                  *  :server.name 304 target :CHECK START <target>
48                  *  :server.name 304 target :CHECK <field> <value>
49                  *  :server.name 304 target :CHECK END
50                  */
51
52                 user->WriteServ(checkstr + " START " + parameters[0]);
53
54                 if (targuser)
55                 {
56                         /* /check on a user */
57                         user->WriteServ(checkstr + " nuh " + targuser->GetFullHost());
58                         user->WriteServ(checkstr + " realnuh " + targuser->GetFullRealHost());
59                         user->WriteServ(checkstr + " realname " + targuser->fullname);
60                         user->WriteServ(checkstr + " modes +" + targuser->FormatModes());
61                         user->WriteServ(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
62                         user->WriteServ(checkstr + " server " + targuser->server);
63
64                         if (IS_AWAY(targuser))
65                         {
66                                 /* user is away */
67                                 user->WriteServ(checkstr + " awaymsg " + targuser->awaymsg);
68                         }
69
70                         if (IS_OPER(targuser))
71                         {
72                                 /* user is an oper of type ____ */
73                                 user->WriteServ(checkstr + " opertype " + irc::Spacify(targuser->oper.c_str()));
74                         }
75
76                         if (IS_LOCAL(targuser))
77                         {
78                                 /* port information is only held for a local user! */
79                                 user->WriteServ(checkstr + " onport " + ConvToStr(targuser->GetPort()));
80                         }
81
82                         chliststr = targuser->ChannelList(targuser);
83                         std::stringstream dump(chliststr);
84
85                         ServerInstance->DumpText(user,checkstr + " onchans ", dump);
86                 }
87                 else if (targchan)
88                 {
89                         /* /check on a channel */
90                         time_t creation_time = targchan->age;
91                         time_t topic_time = targchan->topicset;
92
93                         mytime = gmtime(&creation_time);
94                         strftime(timebuf, 59, "%Y/%m/%d - %H:%M:%S", mytime);
95                         user->WriteServ(checkstr + " timestamp " + timebuf);
96
97                         if (targchan->topic[0] != 0)
98                         {
99                                 /* there is a topic, assume topic related information exists */
100                                 user->WriteServ(checkstr + " topic " + targchan->topic);
101                                 user->WriteServ(checkstr + " topic_setby " + targchan->setby);
102                                 mytime = gmtime(&topic_time);
103                                 strftime(timebuf, 59, "%Y/%m/%d - %H:%M:%S", mytime);
104                                 user->WriteServ(checkstr + " topic_setat " + timebuf);
105                         }
106
107                         user->WriteServ(checkstr + " modes " + targchan->ChanModes(true));
108                         user->WriteServ(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
109
110                         /* now the ugly bit, spool current members of a channel. :| */
111
112                         CUList *ulist= targchan->GetUsers();
113
114                         /* note that unlike /names, we do NOT check +i vs in the channel */
115                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
116                         {
117                                 char tmpbuf[MAXBUF];
118                                 /*
119                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
120                                  */
121                                 snprintf(tmpbuf, MAXBUF, "%lu    %s%s (%s@%s) %s ", ServerInstance->Users->GlobalCloneCount(i->first), targchan->GetAllPrefixChars(i->first), i->first->nick.c_str(), i->first->ident.c_str(), i->first->dhost.c_str(), i->first->fullname.c_str());
122                                 user->WriteServ(checkstr + " member " + tmpbuf);
123                         }
124                 }
125                 else
126                 {
127                         /*  /check on an IP address, or something that doesn't exist */
128                         long x = 0;
129
130                         /* hostname or other */
131                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
132                         {
133                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
134                                 {
135                                         /* host or vhost matches mask */
136                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
137                                 }
138                                 /* IP address */
139                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
140                                 {
141                                         /* same IP. */
142                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
143                                 }
144                         }
145
146                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
147                 }
148
149                 user->WriteServ(checkstr + " END " + parameters[0]);
150
151                 return CMD_LOCALONLY;
152         }
153 };
154
155
156 class ModuleCheck : public Module
157 {
158  private:
159         CommandCheck *mycommand;
160  public:
161         ModuleCheck(InspIRCd* Me) : Module(Me)
162         {
163
164                 mycommand = new CommandCheck(ServerInstance);
165                 ServerInstance->AddCommand(mycommand);
166
167         }
168
169         virtual ~ModuleCheck()
170         {
171         }
172
173         virtual Version GetVersion()
174         {
175                 return Version("$Id$", VF_VENDOR, API_VERSION);
176         }
177
178
179 };
180
181 MODULE_INIT(ModuleCheck)