]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
Remove m_testcommand too
[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         std::string timestring(time_t time)
30         {
31                 char timebuf[60];
32                 struct tm *mytime = gmtime(&time);
33                 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
34                 return std::string(timebuf);
35         }
36
37         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
38         {
39                 User *targuser;
40                 Channel *targchan;
41                 std::string checkstr;
42                 std::string chliststr;
43
44                 checkstr = "304 " + std::string(user->nick) + " :CHECK";
45
46                 targuser = ServerInstance->FindNick(parameters[0]);
47                 targchan = ServerInstance->FindChan(parameters[0]);
48
49                 /*
50                  * Syntax of a /check reply:
51                  *  :server.name 304 target :CHECK START <target>
52                  *  :server.name 304 target :CHECK <field> <value>
53                  *  :server.name 304 target :CHECK END
54                  */
55
56                 user->WriteServ(checkstr + " START " + parameters[0]);
57
58                 if (targuser)
59                 {
60                         /* /check on a user */
61                         user->WriteServ(checkstr + " nuh " + targuser->GetFullHost());
62                         user->WriteServ(checkstr + " realnuh " + targuser->GetFullRealHost());
63                         user->WriteServ(checkstr + " realname " + targuser->fullname);
64                         user->WriteServ(checkstr + " modes +" + targuser->FormatModes());
65                         user->WriteServ(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
66                         user->WriteServ(checkstr + " server " + targuser->server);
67                         user->WriteServ(checkstr + " uid " + targuser->uuid);
68                         user->WriteServ(checkstr + " signon " + timestring(targuser->signon));
69                         user->WriteServ(checkstr + " nickts " + timestring(targuser->age));
70                         if (IS_LOCAL(targuser))
71                                 user->WriteServ(checkstr + " lastmsg " + timestring(targuser->idle_lastmsg));
72
73                         if (IS_AWAY(targuser))
74                         {
75                                 /* user is away */
76                                 user->WriteServ(checkstr + " awaytime " + timestring(targuser->awaytime));
77                                 user->WriteServ(checkstr + " awaymsg " + targuser->awaymsg);
78                         }
79
80                         if (IS_OPER(targuser))
81                         {
82                                 /* user is an oper of type ____ */
83                                 user->WriteServ(checkstr + " opertype " + irc::Spacify(targuser->oper.c_str()));
84                         }
85
86                         user->WriteServ(checkstr + " onip " + targuser->GetIPString());
87                         if (IS_LOCAL(targuser))
88                         {
89                                 user->WriteServ(checkstr + " onport " + ConvToStr(targuser->GetPort()));
90                                 std::string classname = targuser->GetClass()->GetName();
91                                 if (!classname.empty())
92                                         user->WriteServ(checkstr + " connectclass " + classname);
93                         }
94
95                         chliststr = targuser->ChannelList(targuser);
96                         std::stringstream dump(chliststr);
97
98                         ServerInstance->DumpText(user,checkstr + " onchans ", dump);
99                 }
100                 else if (targchan)
101                 {
102                         /* /check on a channel */
103                         user->WriteServ(checkstr + " timestamp " + timestring(targchan->age));
104
105                         if (targchan->topic[0] != 0)
106                         {
107                                 /* there is a topic, assume topic related information exists */
108                                 user->WriteServ(checkstr + " topic " + targchan->topic);
109                                 user->WriteServ(checkstr + " topic_setby " + targchan->setby);
110                                 user->WriteServ(checkstr + " topic_setat " + timestring(targchan->topicset));
111                         }
112
113                         user->WriteServ(checkstr + " modes " + targchan->ChanModes(true));
114                         user->WriteServ(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 tmpbuf[MAXBUF];
124                                 /*
125                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
126                                  */
127                                 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());
128                                 user->WriteServ(checkstr + " member " + tmpbuf);
129                         }
130                 }
131                 else
132                 {
133                         /*  /check on an IP address, or something that doesn't exist */
134                         long x = 0;
135
136                         /* hostname or other */
137                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
138                         {
139                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
140                                 {
141                                         /* host or vhost matches mask */
142                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
143                                 }
144                                 /* IP address */
145                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
146                                 {
147                                         /* same IP. */
148                                         user->WriteServ(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
149                                 }
150                         }
151
152                         user->WriteServ(checkstr + " matches " + ConvToStr(x));
153                 }
154
155                 user->WriteServ(checkstr + " END " + parameters[0]);
156
157                 return CMD_LOCALONLY;
158         }
159 };
160
161
162 class ModuleCheck : public Module
163 {
164  private:
165         CommandCheck *mycommand;
166  public:
167         ModuleCheck(InspIRCd* Me) : Module(Me)
168         {
169
170                 mycommand = new CommandCheck(ServerInstance);
171                 ServerInstance->AddCommand(mycommand);
172
173         }
174
175         virtual ~ModuleCheck()
176         {
177         }
178
179         virtual Version GetVersion()
180         {
181                 return Version("$Id$", VF_VENDOR, API_VERSION);
182         }
183
184
185 };
186
187 MODULE_INIT(ModuleCheck)