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