]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
Remote user messaging fixes
[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         std::set<std::string> meta_seen;
24         CommandCheck (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"CHECK", "o", 1)
25         {
26                 syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
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         void dumpExtra(User* user, std::string checkstr, Extensible* ext)
38         {
39                 std::deque<std::string> extlist;
40                 ext->GetExtList(extlist);
41                 std::stringstream dumpkeys;
42                 for(std::deque<std::string>::iterator i = extlist.begin(); i != extlist.end(); i++)
43                 {
44                         if (meta_seen.find(*i) == meta_seen.end())
45                                 dumpkeys << " " << *i;
46                 }
47                 meta_seen.clear();
48                 if (!dumpkeys.str().empty())
49                         ServerInstance->DumpText(user,checkstr + " metadata", dumpkeys);
50         }
51
52         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
53         {
54                 if (parameters.size() > 1 && parameters[1] != ServerInstance->Config->ServerName)
55                         return CMD_SUCCESS;
56
57                 User *targuser;
58                 Channel *targchan;
59                 std::string checkstr;
60                 std::string chliststr;
61
62                 checkstr = std::string(":") + ServerInstance->Config->ServerName + " 304 " + std::string(user->nick) + " :CHECK";
63
64                 targuser = ServerInstance->FindNick(parameters[0]);
65                 targchan = ServerInstance->FindChan(parameters[0]);
66
67                 /*
68                  * Syntax of a /check reply:
69                  *  :server.name 304 target :CHECK START <target>
70                  *  :server.name 304 target :CHECK <field> <value>
71                  *  :server.name 304 target :CHECK END
72                  */
73
74                 ServerInstance->DumpText(user, checkstr + " START " + parameters[0]);
75
76                 if (targuser)
77                 {
78                         /* /check on a user */
79                         ServerInstance->DumpText(user, checkstr + " nuh " + targuser->GetFullHost());
80                         ServerInstance->DumpText(user, checkstr + " realnuh " + targuser->GetFullRealHost());
81                         ServerInstance->DumpText(user, checkstr + " realname " + targuser->fullname);
82                         ServerInstance->DumpText(user, checkstr + " modes +" + targuser->FormatModes());
83                         ServerInstance->DumpText(user, checkstr + " snomasks +" + targuser->FormatNoticeMasks());
84                         ServerInstance->DumpText(user, checkstr + " server " + targuser->server);
85                         ServerInstance->DumpText(user, checkstr + " uid " + targuser->uuid);
86                         ServerInstance->DumpText(user, checkstr + " signon " + timestring(targuser->signon));
87                         ServerInstance->DumpText(user, checkstr + " nickts " + timestring(targuser->age));
88                         if (IS_LOCAL(targuser))
89                                 ServerInstance->DumpText(user, checkstr + " lastmsg " + timestring(targuser->idle_lastmsg));
90
91                         if (IS_AWAY(targuser))
92                         {
93                                 /* user is away */
94                                 ServerInstance->DumpText(user, checkstr + " awaytime " + timestring(targuser->awaytime));
95                                 ServerInstance->DumpText(user, checkstr + " awaymsg " + targuser->awaymsg);
96                         }
97
98                         if (IS_OPER(targuser))
99                         {
100                                 /* user is an oper of type ____ */
101                                 ServerInstance->DumpText(user, checkstr + " opertype " + irc::Spacify(targuser->oper.c_str()));
102                         }
103
104                         if (IS_LOCAL(targuser))
105                         {
106                                 ServerInstance->DumpText(user, checkstr + " clientaddr " + irc::sockets::satouser(&targuser->client_sa));
107                                 ServerInstance->DumpText(user, checkstr + " serveraddr " + irc::sockets::satouser(&targuser->server_sa));
108
109                                 std::string classname = targuser->GetClass()->name;
110                                 if (!classname.empty())
111                                         ServerInstance->DumpText(user, checkstr + " connectclass " + classname);
112                         }
113                         else
114                                 ServerInstance->DumpText(user, checkstr + " onip " + targuser->GetIPString());
115
116                         chliststr = targuser->ChannelList(targuser);
117                         std::stringstream dump(chliststr);
118
119                         ServerInstance->DumpText(user,checkstr + " onchans", dump);
120
121                         FOREACH_MOD_I(ServerInstance,I_OnSyncUser,OnSyncUser(targuser,creator,(void*)user));
122                         dumpExtra(user, checkstr, targuser);
123                 }
124                 else if (targchan)
125                 {
126                         /* /check on a channel */
127                         ServerInstance->DumpText(user, checkstr + " timestamp " + timestring(targchan->age));
128
129                         if (targchan->topic[0] != 0)
130                         {
131                                 /* there is a topic, assume topic related information exists */
132                                 ServerInstance->DumpText(user, checkstr + " topic " + targchan->topic);
133                                 ServerInstance->DumpText(user, checkstr + " topic_setby " + targchan->setby);
134                                 ServerInstance->DumpText(user, checkstr + " topic_setat " + timestring(targchan->topicset));
135                         }
136
137                         ServerInstance->DumpText(user, checkstr + " modes " + targchan->ChanModes(true));
138                         ServerInstance->DumpText(user, checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
139
140                         /* now the ugly bit, spool current members of a channel. :| */
141
142                         CUList *ulist= targchan->GetUsers();
143
144                         /* note that unlike /names, we do NOT check +i vs in the channel */
145                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
146                         {
147                                 char tmpbuf[MAXBUF];
148                                 /*
149                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
150                                  */
151                                 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());
152                                 ServerInstance->DumpText(user, checkstr + " member " + tmpbuf);
153                         }
154
155                         FOREACH_MOD_I(ServerInstance,I_OnSyncChannel,OnSyncChannel(targchan,creator,(void*)user));
156                         dumpExtra(user, checkstr, targchan);
157                 }
158                 else
159                 {
160                         /*  /check on an IP address, or something that doesn't exist */
161                         long x = 0;
162
163                         /* hostname or other */
164                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
165                         {
166                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
167                                 {
168                                         /* host or vhost matches mask */
169                                         ServerInstance->DumpText(user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
170                                 }
171                                 /* IP address */
172                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
173                                 {
174                                         /* same IP. */
175                                         ServerInstance->DumpText(user, checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
176                                 }
177                         }
178
179                         ServerInstance->DumpText(user, checkstr + " matches " + ConvToStr(x));
180                 }
181
182                 ServerInstance->DumpText(user, checkstr + " END " + parameters[0]);
183
184                 return CMD_SUCCESS;
185         }
186
187         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
188         {
189                 if (parameters.size() > 1)
190                         return ROUTE_OPT_UCAST(parameters[1]);
191                 return ROUTE_LOCALONLY;
192         }
193 };
194
195
196 class ModuleCheck : public Module
197 {
198  private:
199         CommandCheck mycommand;
200  public:
201         ModuleCheck(InspIRCd* Me) : Module(Me), mycommand(Me, this)
202         {
203                 ServerInstance->AddCommand(&mycommand);
204         }
205
206         virtual ~ModuleCheck()
207         {
208         }
209
210         virtual Version GetVersion()
211         {
212                 return Version("$Id$", VF_VENDOR|VF_OPTCOMMON, API_VERSION);
213         }
214
215         virtual void ProtoSendMetaData(void* opaque, Extensible* target, const std::string& name, const std::string& value)
216         {
217                 User* user = static_cast<User*>(opaque);
218                 ServerInstance->DumpText(user, std::string(":") + ServerInstance->Config->ServerName + " 304 " + std::string(user->nick)
219                         + " :CHECK meta:" + name + " " + value);
220                 mycommand.meta_seen.insert(name);
221         }
222
223         virtual std::string ProtoTranslate(Extensible* item)
224         {
225                 User* u = dynamic_cast<User*>(item);
226                 Channel* c = dynamic_cast<Channel*>(item);
227                 if (u)
228                         return u->nick;
229                 if (c)
230                         return c->name;
231                 return "?";
232         }
233 };
234
235 MODULE_INIT(ModuleCheck)