]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_ison.cpp
Merge insp20
[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 Command
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) : Command(parent,"ISON", 1) {
39                 syntax = "<nick> {nick}";
40         }
41         /** Handle command.
42          * @param parameters The parameters to the command
43          * @param user The user issuing the command
44          * @return A value from CmdResult to indicate command success or failure.
45          */
46         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
47 };
48
49 bool CommandIson::AddNick(User* user, User* toadd, std::string& reply, const std::string::size_type pos)
50 {
51         if ((toadd) && (toadd->registered == REG_ALL))
52         {
53                 reply.append(toadd->nick).push_back(' ');
54                 if (reply.length() > 450)
55                 {
56                         user->WriteServ(reply);
57                         reply.erase(pos);
58                 }
59                 return true;
60         }
61         return false;
62 }
63
64 /** Handle /ISON
65  */
66 CmdResult CommandIson::Handle (const std::vector<std::string>& parameters, User *user)
67 {
68         std::string reply = "303 " + user->nick + " :";
69         const std::string::size_type pos = reply.size();
70
71         for (std::vector<std::string>::const_iterator i = parameters.begin(); i != parameters.end(); ++i)
72         {
73                 const std::string& targetstr = *i;
74
75                 User* const u = ServerInstance->FindNickOnly(targetstr);
76                 if (!AddNick(user, u, reply, pos))
77                 {
78                         if ((i == parameters.end() - 1) && (targetstr.find(' ') != std::string::npos))
79                         {
80                                 /* Its a space seperated list of nicks (RFC1459 says to support this)
81                                  */
82                                 irc::spacesepstream list(targetstr);
83                                 std::string item;
84
85                                 while (list.GetToken(item))
86                                         AddNick(user, ServerInstance->FindNickOnly(item), reply, pos);
87                         }
88                 }
89         }
90
91         user->WriteServ(reply);
92         return CMD_SUCCESS;
93 }
94
95
96 COMMAND_INIT(CommandIson)