]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
7fe9bc8d57d08bf66ed197da2bb6f09d2b52d156
[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://wiki.inspircd.org/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                         user->WriteServ(checkstr + " uid " + targuser->uuid);
64                         user->WriteServ(checkstr + " signon " + ConvToStr(targuser->signon));
65                         user->WriteServ(checkstr + " nickts " + ConvToStr(targuser->age));
66                         if (IS_LOCAL(targuser))
67                                 user->WriteServ(checkstr + " lastmsg " + ConvToStr(targuser->idle_lastmsg));
68
69                         if (IS_AWAY(targuser))
70                         {
71                                 /* user is away */
72                                 user->WriteServ(checkstr + " awaytime " + ConvToStr(targuser->awaytime));
73                                 user->WriteServ(checkstr + " awaymsg " + targuser->awaymsg);
74                         }
75
76                         if (IS_OPER(targuser))
77                         {
78                                 /* user is an oper of type ____ */
79                                 user->WriteServ(checkstr + " opertype " + irc::Spacify(targuser->oper.c_str()));
80                         }
81
82                         user->WriteServ(checkstr + " onip " + targuser->GetIPString());
83                         if (IS_LOCAL(targuser))
84                         {
85                                 user->WriteServ(checkstr + " onport " + ConvToStr(targuser->GetPort()));
86                                 std::string classname = targuser->GetClass()->GetName();
87                                 if (!classname.empty())
88                                         user->WriteServ(checkstr + " connectclass " + classname);
89                         }
90
91                         chliststr = targuser->ChannelList(targuser);
92                         std::stringstream dump(chliststr);
93
94                         ServerInstance->DumpText(user,checkstr + " onchans ", dump);
95                 }
96                 else if (targchan)
97                 {
98                         /* /check on a channel */
99                         time_t creation_time = targchan->age;
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 + " timestamp " + 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 tmpbuf[MAXBUF];
127                                 /*
128                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
129                                  */
130                                 snprintf(tmpbuf, MAXBUF, "%-3lu %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());
131                                 user->WriteServ(checkstr + " member " + tmpbuf);
132                         }
133                 }
134                 else
135                 {
136                         /*  /check on an IP address, or something that doesn't exist */
137                         long x = 0;
138
139                         /* hostname or other */
140                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
141                         {
142                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
143                                 {
144                                         /* host or vhost matches mask */
145                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
146                                 }
147                                 /* IP address */
148                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
149                                 {
150                                         /* same IP. */
151                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
152                                 }
153                         }
154
155                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
156                 }
157
158                 user->WriteServ(checkstr + " END " + parameters[0]);
159
160                 return CMD_LOCALONLY;
161         }
162 };
163
164
165 class ModuleCheck : public Module
166 {
167  private:
168         CommandCheck *mycommand;
169  public:
170         ModuleCheck(InspIRCd* Me) : Module(Me)
171         {
172
173                 mycommand = new CommandCheck(ServerInstance);
174                 ServerInstance->AddCommand(mycommand);
175
176         }
177
178         virtual ~ModuleCheck()
179         {
180         }
181
182         virtual Version GetVersion()
183         {
184                 return Version("$Id$", VF_VENDOR, API_VERSION);
185         }
186
187
188 };
189
190 MODULE_INIT(ModuleCheck)