]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_ison.cpp
3f05720ff2c919f6022c5cdc6f5aa3e8d199b28a
[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()-1; ++i)
72         {
73                 const std::string& targetstr = *i;
74
75                 User* const u = ServerInstance->FindNickOnly(targetstr);
76                 AddNick(user, u, reply, pos);
77         }
78
79         // Last parameter can be a space separated list
80         irc::spacesepstream ss(parameters.back());
81         for (std::string token; ss.GetToken(token); )
82                 AddNick(user, ServerInstance->FindNickOnly(token), reply, pos);
83
84         user->WriteServ(reply);
85         return CMD_SUCCESS;
86 }
87
88
89 COMMAND_INIT(CommandIson)