]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_check.cpp
231064431c8a6752398318acb1b317f10ae83c1a
[user/henk/code/inspircd.git] / src / modules / m_check.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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(Module* parent) : Command(parent,"CHECK", 1)
24         {
25                 flags_needed = 'o'; syntax = "<nickname>|<ip>|<hostmask>|<channel> <server>";
26         }
27
28         std::string timestring(time_t time)
29         {
30                 char timebuf[60];
31                 struct tm *mytime = gmtime(&time);
32                 strftime(timebuf, 59, "%Y-%m-%d %H:%M:%S UTC (%s)", mytime);
33                 return std::string(timebuf);
34         }
35
36         void dumpExt(User* user, std::string checkstr, Extensible* ext)
37         {
38                 std::stringstream dumpkeys;
39                 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
40                 {
41                         ExtensionItem* item = i->first;
42                         std::string value = item->serialize(FORMAT_USER, ext, i->second);
43                         if (!value.empty())
44                                 user->SendText(checkstr + " meta:" + item->name + " " + value);
45                         else if (!item->name.empty())
46                                 dumpkeys << " " << item->name;
47                 }
48                 if (!dumpkeys.str().empty())
49                         user->SendText(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.c_str())
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                 user->SendText(checkstr + " START " + parameters[0]);
75
76                 if (targuser)
77                 {
78                         /* /check on a user */
79                         user->SendText(checkstr + " nuh " + targuser->GetFullHost());
80                         user->SendText(checkstr + " realnuh " + targuser->GetFullRealHost());
81                         user->SendText(checkstr + " realname " + targuser->fullname);
82                         user->SendText(checkstr + " modes +" + targuser->FormatModes());
83                         user->SendText(checkstr + " snomasks +" + targuser->FormatNoticeMasks());
84                         user->SendText(checkstr + " server " + targuser->server);
85                         user->SendText(checkstr + " uid " + targuser->uuid);
86                         user->SendText(checkstr + " signon " + timestring(targuser->signon));
87                         user->SendText(checkstr + " nickts " + timestring(targuser->age));
88                         if (IS_LOCAL(targuser))
89                                 user->SendText(checkstr + " lastmsg " + timestring(targuser->idle_lastmsg));
90
91                         if (IS_AWAY(targuser))
92                         {
93                                 /* user is away */
94                                 user->SendText(checkstr + " awaytime " + timestring(targuser->awaytime));
95                                 user->SendText(checkstr + " awaymsg " + targuser->awaymsg);
96                         }
97
98                         if (IS_OPER(targuser))
99                         {
100                                 /* user is an oper of type ____ */
101                                 user->SendText(checkstr + " opertype " + targuser->oper->NameStr());
102                         }
103
104                         LocalUser* loctarg = IS_LOCAL(targuser);
105                         if (loctarg)
106                         {
107                                 user->SendText(checkstr + " clientaddr " + irc::sockets::satouser(loctarg->client_sa));
108                                 user->SendText(checkstr + " serveraddr " + irc::sockets::satouser(loctarg->server_sa));
109
110                                 std::string classname = loctarg->GetClass()->name;
111                                 if (!classname.empty())
112                                         user->SendText(checkstr + " connectclass " + classname);
113                         }
114                         else
115                                 user->SendText(checkstr + " onip " + targuser->GetIPString());
116
117                         for (UCListIter i = targuser->chans.begin(); i != targuser->chans.end(); i++)
118                         {
119                                 Channel* c = *i;
120                                 chliststr.append(c->GetPrefixChar(targuser)).append(c->name).append(" ");
121                         }
122
123                         std::stringstream dump(chliststr);
124
125                         user->SendText(checkstr + " onchans", dump);
126
127                         dumpExt(user, checkstr, targuser);
128                 }
129                 else if (targchan)
130                 {
131                         /* /check on a channel */
132                         user->SendText(checkstr + " timestamp " + timestring(targchan->age));
133
134                         if (targchan->topic[0] != 0)
135                         {
136                                 /* there is a topic, assume topic related information exists */
137                                 user->SendText(checkstr + " topic " + targchan->topic);
138                                 user->SendText(checkstr + " topic_setby " + targchan->setby);
139                                 user->SendText(checkstr + " topic_setat " + timestring(targchan->topicset));
140                         }
141
142                         user->SendText(checkstr + " modes " + targchan->ChanModes(true));
143                         user->SendText(checkstr + " membercount " + ConvToStr(targchan->GetUserCounter()));
144
145                         /* now the ugly bit, spool current members of a channel. :| */
146
147                         const UserMembList *ulist= targchan->GetUsers();
148
149                         /* note that unlike /names, we do NOT check +i vs in the channel */
150                         for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
151                         {
152                                 char tmpbuf[MAXBUF];
153                                 /*
154                                  * Unlike Asuka, I define a clone as coming from the same host. --w00t
155                                  */
156                                 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());
157                                 user->SendText(checkstr + " member " + tmpbuf);
158                         }
159
160                         irc::modestacker modestack(true);
161                         std::string letter_b("b");
162                         for(BanList::iterator b = targchan->bans.begin(); b != targchan->bans.end(); ++b)
163                         {
164                                 modestack.Push('b', b->data);
165                         }
166                         std::vector<std::string> stackresult;
167                         std::vector<TranslateType> dummy;
168                         while (modestack.GetStackedLine(stackresult))
169                         {
170                                 creator->ProtoSendMode(user, TYPE_CHANNEL, targchan, stackresult, dummy);
171                                 stackresult.clear();
172                         }
173                         FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(targchan,creator,user));
174                         dumpExt(user, checkstr, targchan);
175                 }
176                 else
177                 {
178                         /*  /check on an IP address, or something that doesn't exist */
179                         long x = 0;
180
181                         /* hostname or other */
182                         for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); a++)
183                         {
184                                 if (InspIRCd::Match(a->second->host, parameters[0], ascii_case_insensitive_map) || InspIRCd::Match(a->second->dhost, parameters[0], ascii_case_insensitive_map))
185                                 {
186                                         /* host or vhost matches mask */
187                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
188                                 }
189                                 /* IP address */
190                                 else if (InspIRCd::MatchCIDR(a->second->GetIPString(), parameters[0]))
191                                 {
192                                         /* same IP. */
193                                         user->SendText(checkstr + " match " + ConvToStr(++x) + " " + a->second->GetFullRealHost());
194                                 }
195                         }
196
197                         user->SendText(checkstr + " matches " + ConvToStr(x));
198                 }
199
200                 user->SendText(checkstr + " END " + parameters[0]);
201
202                 return CMD_SUCCESS;
203         }
204
205         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
206         {
207                 if (parameters.size() > 1)
208                         return ROUTE_OPT_UCAST(parameters[1]);
209                 return ROUTE_LOCALONLY;
210         }
211 };
212
213
214 class ModuleCheck : public Module
215 {
216  private:
217         CommandCheck mycommand;
218  public:
219         ModuleCheck() : mycommand(this)
220         {
221                 ServerInstance->AddCommand(&mycommand);
222         }
223
224         ~ModuleCheck()
225         {
226         }
227
228         void ProtoSendMode(void* uv, TargetTypeFlags, void*, const std::vector<std::string>& result, const std::vector<TranslateType>&)
229         {
230                 User* user = (User*)uv;
231                 std::string checkstr(":");
232                 checkstr.append(ServerInstance->Config->ServerName);
233                 checkstr.append(" 304 ");
234                 checkstr.append(user->nick);
235                 checkstr.append(" :CHECK modelist");
236                 for(unsigned int i=0; i < result.size(); i++)
237                 {
238                         checkstr.append(" ");
239                         checkstr.append(result[i]);
240                 }
241                 user->SendText(checkstr);
242         }
243
244         Version GetVersion()
245         {
246                 return Version("CHECK command, view user/channel details", VF_VENDOR|VF_OPTCOMMON);
247         }
248 };
249
250 MODULE_INIT(ModuleCheck)