]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/cmd_invite.cpp
88b2cf86cead712a798680165344d6908f86c20d
[user/henk/code/inspircd.git] / src / coremods / core_channel / cmd_invite.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "core_channel.h"
25 #include "invite.h"
26
27 CommandInvite::CommandInvite(Module* parent, Invite::APIImpl& invapiimpl)
28         : Command(parent, "INVITE", 0, 0)
29         , invapi(invapiimpl)
30 {
31         Penalty = 4;
32         syntax = "[<nick> <channel>]";
33 }
34
35 /** Handle /INVITE
36  */
37 CmdResult CommandInvite::Handle (const std::vector<std::string>& parameters, User *user)
38 {
39         ModResult MOD_RESULT;
40
41         if (parameters.size() >= 2)
42         {
43                 User* u;
44                 if (IS_LOCAL(user))
45                         u = ServerInstance->FindNickOnly(parameters[0]);
46                 else
47                         u = ServerInstance->FindNick(parameters[0]);
48
49                 Channel* c = ServerInstance->FindChan(parameters[1]);
50                 time_t timeout = 0;
51                 if (parameters.size() >= 3)
52                 {
53                         if (IS_LOCAL(user))
54                                 timeout = ServerInstance->Time() + InspIRCd::Duration(parameters[2]);
55                         else if (parameters.size() > 3)
56                                 timeout = ConvToInt(parameters[3]);
57                 }
58
59                 if (!c)
60                 {
61                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[1]));
62                         return CMD_FAILURE;
63                 }
64                 if ((!u) || (u->registered != REG_ALL))
65                 {
66                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
67                         return CMD_FAILURE;
68                 }
69
70                 // Verify channel timestamp if the INVITE is coming from a remote server
71                 if (!IS_LOCAL(user))
72                 {
73                         // Remote INVITE commands must carry a channel timestamp
74                         if (parameters.size() < 3)
75                                 return CMD_INVALID;
76
77                         // Drop the invite if our channel TS is lower
78                         time_t RemoteTS = ConvToInt(parameters[2]);
79                         if (c->age < RemoteTS)
80                                 return CMD_FAILURE;
81                 }
82
83                 if ((IS_LOCAL(user)) && (!c->HasUser(user)))
84                 {
85                         user->WriteNumeric(ERR_NOTONCHANNEL, c->name, "You're not on that channel!");
86                         return CMD_FAILURE;
87                 }
88
89                 if (c->HasUser(u))
90                 {
91                         user->WriteNumeric(ERR_USERONCHANNEL, u->nick, c->name, "is already on channel");
92                         return CMD_FAILURE;
93                 }
94
95                 FIRST_MOD_RESULT(OnUserPreInvite, MOD_RESULT, (user,u,c,timeout));
96
97                 if (MOD_RESULT == MOD_RES_DENY)
98                 {
99                         return CMD_FAILURE;
100                 }
101                 else if (MOD_RESULT == MOD_RES_PASSTHRU)
102                 {
103                         if (IS_LOCAL(user))
104                         {
105                                 unsigned int rank = c->GetPrefixValue(user);
106                                 if (rank < HALFOP_VALUE)
107                                 {
108                                         // Check whether halfop mode is available and phrase error message accordingly
109                                         ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
110                                         user->WriteNumeric(ERR_CHANOPRIVSNEEDED, c->name, InspIRCd::Format("You must be a channel %soperator",
111                                                 (mh && mh->name == "halfop" ? "half-" : "")));
112                                         return CMD_FAILURE;
113                                 }
114                         }
115                 }
116
117                 if (IS_LOCAL(u))
118                 {
119                         invapi.Create(IS_LOCAL(u), c, timeout);
120                         u->WriteFrom(user,"INVITE %s :%s",u->nick.c_str(),c->name.c_str());
121                 }
122
123                 if (IS_LOCAL(user))
124                 {
125                         user->WriteNumeric(RPL_INVITING, u->nick, c->name);
126                         if (u->IsAway())
127                                 user->WriteNumeric(RPL_AWAY, u->nick, u->awaymsg);
128                 }
129
130                 char prefix = 0;
131                 unsigned int minrank = 0;
132                 switch (ServerInstance->Config->AnnounceInvites)
133                 {
134                         case ServerConfig::INVITE_ANNOUNCE_OPS:
135                         {
136                                 prefix = '@';
137                                 minrank = OP_VALUE;
138                                 break;
139                         }
140                         case ServerConfig::INVITE_ANNOUNCE_DYNAMIC:
141                         {
142                                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
143                                 if ((mh) && (mh->name == "halfop"))
144                                 {
145                                         prefix = mh->GetPrefix();
146                                         minrank = mh->GetPrefixRank();
147                                 }
148                                 break;
149                         }
150                         default:
151                         {
152                         }
153                 }
154
155                 CUList excepts;
156                 FOREACH_MOD(OnUserInvite, (user, u, c, timeout, minrank, excepts));
157
158                 if (ServerInstance->Config->AnnounceInvites != ServerConfig::INVITE_ANNOUNCE_NONE)
159                         c->WriteAllExcept(user, true, prefix, excepts, "NOTICE %s :*** %s invited %s into the channel", c->name.c_str(), user->nick.c_str(), u->nick.c_str());
160         }
161         else if (IS_LOCAL(user))
162         {
163                 // pinched from ircu - invite with not enough parameters shows channels
164                 // youve been invited to but haven't joined yet.
165                 const Invite::List* list = invapi.GetList(IS_LOCAL(user));
166                 if (list)
167                 {
168                         for (Invite::List::const_iterator i = list->begin(); i != list->end(); ++i)
169                                 user->WriteNumeric(RPL_INVITELIST, (*i)->chan->name);
170                 }
171                 user->WriteNumeric(RPL_ENDOFINVITELIST, "End of INVITE list");
172         }
173         return CMD_SUCCESS;
174 }
175
176 RouteDescriptor CommandInvite::GetRouting(User* user, const std::vector<std::string>& parameters)
177 {
178         return (IS_LOCAL(user) ? ROUTE_LOCALONLY : ROUTE_BROADCAST);
179 }