]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/cmd_invite.cpp
7bf669b296837b469ea56072acf4209e34ad7589
[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
26 CommandInvite::CommandInvite(Module* parent)
27         : Command(parent, "INVITE", 0, 0)
28 {
29         Penalty = 4;
30         syntax = "[<nick> <channel>]";
31 }
32
33 /** Handle /INVITE
34  */
35 CmdResult CommandInvite::Handle (const std::vector<std::string>& parameters, User *user)
36 {
37         ModResult MOD_RESULT;
38
39         if (parameters.size() >= 2)
40         {
41                 User* u;
42                 if (IS_LOCAL(user))
43                         u = ServerInstance->FindNickOnly(parameters[0]);
44                 else
45                         u = ServerInstance->FindNick(parameters[0]);
46
47                 Channel* c = ServerInstance->FindChan(parameters[1]);
48                 time_t timeout = 0;
49                 if (parameters.size() >= 3)
50                 {
51                         if (IS_LOCAL(user))
52                                 timeout = ServerInstance->Time() + InspIRCd::Duration(parameters[2]);
53                         else if (parameters.size() > 3)
54                                 timeout = ConvToInt(parameters[3]);
55                 }
56
57                 if ((!c) || (!u) || (u->registered != REG_ALL))
58                 {
59                         user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", c ? parameters[0].c_str() : parameters[1].c_str());
60                         return CMD_FAILURE;
61                 }
62
63                 // Verify channel timestamp if the INVITE is coming from a remote server
64                 if (!IS_LOCAL(user))
65                 {
66                         // Remote INVITE commands must carry a channel timestamp
67                         if (parameters.size() < 3)
68                                 return CMD_INVALID;
69
70                         // Drop the invite if our channel TS is lower
71                         time_t RemoteTS = ConvToInt(parameters[2]);
72                         if (c->age < RemoteTS)
73                                 return CMD_FAILURE;
74                 }
75
76                 if ((IS_LOCAL(user)) && (!c->HasUser(user)))
77                 {
78                         user->WriteNumeric(ERR_NOTONCHANNEL, "%s :You're not on that channel!", c->name.c_str());
79                         return CMD_FAILURE;
80                 }
81
82                 if (c->HasUser(u))
83                 {
84                         user->WriteNumeric(ERR_USERONCHANNEL, "%s %s :is already on channel", u->nick.c_str(), c->name.c_str());
85                         return CMD_FAILURE;
86                 }
87
88                 FIRST_MOD_RESULT(OnUserPreInvite, MOD_RESULT, (user,u,c,timeout));
89
90                 if (MOD_RESULT == MOD_RES_DENY)
91                 {
92                         return CMD_FAILURE;
93                 }
94                 else if (MOD_RESULT == MOD_RES_PASSTHRU)
95                 {
96                         if (IS_LOCAL(user))
97                         {
98                                 unsigned int rank = c->GetPrefixValue(user);
99                                 if (rank < HALFOP_VALUE)
100                                 {
101                                         // Check whether halfop mode is available and phrase error message accordingly
102                                         ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
103                                         user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You must be a channel %soperator",
104                                                 c->name.c_str(), (mh && mh->name == "halfop" ? "half-" : ""));
105                                         return CMD_FAILURE;
106                                 }
107                         }
108                 }
109
110                 if (IS_LOCAL(u))
111                 {
112                         Invitation::Create(c, IS_LOCAL(u), timeout);
113                         u->WriteFrom(user,"INVITE %s :%s",u->nick.c_str(),c->name.c_str());
114                 }
115
116                 if (IS_LOCAL(user))
117                 {
118                         user->WriteNumeric(RPL_INVITING, "%s %s", u->nick.c_str(),c->name.c_str());
119                         if (u->IsAway())
120                                 user->WriteNumeric(RPL_AWAY, "%s :%s", u->nick.c_str(), u->awaymsg.c_str());
121                 }
122
123                 if (ServerInstance->Config->AnnounceInvites != ServerConfig::INVITE_ANNOUNCE_NONE)
124                 {
125                         char prefix;
126                         switch (ServerInstance->Config->AnnounceInvites)
127                         {
128                                 case ServerConfig::INVITE_ANNOUNCE_OPS:
129                                 {
130                                         prefix = '@';
131                                         break;
132                                 }
133                                 case ServerConfig::INVITE_ANNOUNCE_DYNAMIC:
134                                 {
135                                         PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
136                                         prefix = (mh && mh->name == "halfop" ? mh->GetPrefix() : '@');
137                                         break;
138                                 }
139                                 default:
140                                 {
141                                         prefix = 0;
142                                         break;
143                                 }
144                         }
145                         c->WriteAllExceptSender(user, true, prefix, "NOTICE %s :*** %s invited %s into the channel", c->name.c_str(), user->nick.c_str(), u->nick.c_str());
146                 }
147                 FOREACH_MOD(OnUserInvite, (user,u,c,timeout));
148         }
149         else if (IS_LOCAL(user))
150         {
151                 // pinched from ircu - invite with not enough parameters shows channels
152                 // youve been invited to but haven't joined yet.
153                 InviteList& il = IS_LOCAL(user)->GetInviteList();
154                 for (InviteList::const_iterator i = il.begin(); i != il.end(); ++i)
155                 {
156                         user->WriteNumeric(RPL_INVITELIST, ":%s", (*i)->chan->name.c_str());
157                 }
158                 user->WriteNumeric(RPL_ENDOFINVITELIST, ":End of INVITE list");
159         }
160         return CMD_SUCCESS;
161 }
162
163 RouteDescriptor CommandInvite::GetRouting(User* user, const std::vector<std::string>& parameters)
164 {
165         return (IS_LOCAL(user) ? ROUTE_LOCALONLY : ROUTE_BROADCAST);
166 }