]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/cmd_invite.cpp
c26318337597d389f9491697904ceb7d800a4b31
[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(User* user, const Params& parameters)
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                         {
55                                 unsigned long duration;
56                                 if (!InspIRCd::Duration(parameters[2], duration))
57                                 {
58                                         user->WriteNotice("*** Invalid duration for invite");
59                                         return CMD_FAILURE;
60                                 }
61                                 timeout = ServerInstance->Time() + duration;
62                         }
63                         else if (parameters.size() > 3)
64                                 timeout = ConvToNum<time_t>(parameters[3]);
65                 }
66
67                 if (!c)
68                 {
69                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[1]));
70                         return CMD_FAILURE;
71                 }
72                 if ((!u) || (u->registered != REG_ALL))
73                 {
74                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
75                         return CMD_FAILURE;
76                 }
77
78                 // Verify channel timestamp if the INVITE is coming from a remote server
79                 if (!IS_LOCAL(user))
80                 {
81                         // Remote INVITE commands must carry a channel timestamp
82                         if (parameters.size() < 3)
83                                 return CMD_INVALID;
84
85                         // Drop the invite if our channel TS is lower
86                         time_t RemoteTS = ConvToNum<time_t>(parameters[2]);
87                         if (c->age < RemoteTS)
88                                 return CMD_FAILURE;
89                 }
90
91                 if ((IS_LOCAL(user)) && (!c->HasUser(user)))
92                 {
93                         user->WriteNumeric(ERR_NOTONCHANNEL, c->name, "You're not on that channel!");
94                         return CMD_FAILURE;
95                 }
96
97                 if (c->HasUser(u))
98                 {
99                         user->WriteNumeric(ERR_USERONCHANNEL, u->nick, c->name, "is already on channel");
100                         return CMD_FAILURE;
101                 }
102
103                 FIRST_MOD_RESULT(OnUserPreInvite, MOD_RESULT, (user,u,c,timeout));
104
105                 if (MOD_RESULT == MOD_RES_DENY)
106                 {
107                         return CMD_FAILURE;
108                 }
109                 else if (MOD_RESULT == MOD_RES_PASSTHRU)
110                 {
111                         if (IS_LOCAL(user))
112                         {
113                                 unsigned int rank = c->GetPrefixValue(user);
114                                 if (rank < HALFOP_VALUE)
115                                 {
116                                         // Check whether halfop mode is available and phrase error message accordingly
117                                         ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
118                                         user->WriteNumeric(ERR_CHANOPRIVSNEEDED, c->name, InspIRCd::Format("You must be a channel %soperator",
119                                                 (mh && mh->name == "halfop" ? "half-" : "")));
120                                         return CMD_FAILURE;
121                                 }
122                         }
123                 }
124
125                 LocalUser* const localtargetuser = IS_LOCAL(u);
126                 if (localtargetuser)
127                 {
128                         invapi.Create(localtargetuser, c, timeout);
129                         ClientProtocol::Messages::Invite invitemsg(user, localtargetuser, c);
130                         localtargetuser->Send(ServerInstance->GetRFCEvents().invite, invitemsg);
131                 }
132
133                 if (IS_LOCAL(user))
134                 {
135                         user->WriteNumeric(RPL_INVITING, u->nick, c->name);
136                         if (u->IsAway())
137                                 user->WriteNumeric(RPL_AWAY, u->nick, u->awaymsg);
138                 }
139
140                 char prefix = 0;
141                 unsigned int minrank = 0;
142                 switch (announceinvites)
143                 {
144                         case Invite::ANNOUNCE_OPS:
145                         {
146                                 prefix = '@';
147                                 minrank = OP_VALUE;
148                                 break;
149                         }
150                         case Invite::ANNOUNCE_DYNAMIC:
151                         {
152                                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
153                                 if ((mh) && (mh->name == "halfop"))
154                                 {
155                                         prefix = mh->GetPrefix();
156                                         minrank = mh->GetPrefixRank();
157                                 }
158                                 break;
159                         }
160                         default:
161                         {
162                         }
163                 }
164
165                 CUList excepts;
166                 FOREACH_MOD(OnUserInvite, (user, u, c, timeout, minrank, excepts));
167
168                 if (announceinvites != Invite::ANNOUNCE_NONE)
169                 {
170                         excepts.insert(user);
171                         ClientProtocol::Messages::Privmsg privmsg(ServerInstance->FakeClient, c, InspIRCd::Format("*** %s invited %s into the channel", user->nick.c_str(), u->nick.c_str()), MSG_NOTICE);
172                         c->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, prefix, excepts);
173                 }
174         }
175         else if (IS_LOCAL(user))
176         {
177                 // pinched from ircu - invite with not enough parameters shows channels
178                 // youve been invited to but haven't joined yet.
179                 const Invite::List* list = invapi.GetList(IS_LOCAL(user));
180                 if (list)
181                 {
182                         for (Invite::List::const_iterator i = list->begin(); i != list->end(); ++i)
183                                 user->WriteNumeric(RPL_INVITELIST, (*i)->chan->name);
184                 }
185                 user->WriteNumeric(RPL_ENDOFINVITELIST, "End of INVITE list");
186         }
187         return CMD_SUCCESS;
188 }
189
190 RouteDescriptor CommandInvite::GetRouting(User* user, const Params& parameters)
191 {
192         return (IS_LOCAL(user) ? ROUTE_LOCALONLY : ROUTE_BROADCAST);
193 }