]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_ison.cpp
1336cd222cd95b1a3a9281062346ac3232d48a68
[user/henk/code/inspircd.git] / src / coremods / core_ison.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22
23 /** Handle /ISON.
24  */
25 class CommandIson : public SplitCommand
26 {
27         /** Helper function to append a nick to an ISON reply
28          * @param user User doing the /ISON
29          * @param toadd User to append to the ISON reply
30          * @param reply Reply string to append the nick to
31          * @param pos If the reply gets too long it is sent to the user and truncated from this position
32          */
33         static bool AddNick(User* user, User* toadd, std::string& reply, const std::string::size_type pos);
34
35  public:
36         /** Constructor for ison.
37          */
38         CommandIson(Module* parent)
39                 : SplitCommand(parent, "ISON", 1)
40         {
41                 syntax = "<nick> {nick}";
42         }
43         /** Handle command.
44          * @param parameters The parameters to the command
45          * @param user The user issuing the command
46          * @return A value from CmdResult to indicate command success or failure.
47          */
48         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
49 };
50
51 bool CommandIson::AddNick(User* user, User* toadd, std::string& reply, const std::string::size_type pos)
52 {
53         if ((toadd) && (toadd->registered == REG_ALL))
54         {
55                 reply.append(toadd->nick).push_back(' ');
56                 if (reply.length() > 450)
57                 {
58                         user->WriteServ(reply);
59                         reply.erase(pos);
60                 }
61                 return true;
62         }
63         return false;
64 }
65
66 /** Handle /ISON
67  */
68 CmdResult CommandIson::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
69 {
70         std::string reply = "303 " + user->nick + " :";
71         const std::string::size_type pos = reply.size();
72
73         for (std::vector<std::string>::const_iterator i = parameters.begin(); i != parameters.end()-1; ++i)
74         {
75                 const std::string& targetstr = *i;
76
77                 User* const u = ServerInstance->FindNickOnly(targetstr);
78                 AddNick(user, u, reply, pos);
79         }
80
81         // Last parameter can be a space separated list
82         irc::spacesepstream ss(parameters.back());
83         for (std::string token; ss.GetToken(token); )
84                 AddNick(user, ServerInstance->FindNickOnly(token), reply, pos);
85
86         user->WriteServ(reply);
87         return CMD_SUCCESS;
88 }
89
90
91 COMMAND_INIT(CommandIson)