]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_invite.cpp
Replace OnAccessCheck with OnPreMode to remove a number of redundant checks
[user/henk/code/inspircd.git] / src / commands / cmd_invite.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 #ifndef __CMD_INVITE_H__
17 #define __CMD_INVITE_H__
18
19 // include the common header files
20
21 #include "users.h"
22 #include "channels.h"
23
24 /** Handle /INVITE. These command handlers can be reloaded by the core,
25  * and handle basic RFC1459 commands. Commands within modules work
26  * the same way, however, they can be fully unloaded, where these
27  * may not.
28  */
29 class CommandInvite : public Command
30 {
31  public:
32         /** Constructor for invite.
33          */
34         CommandInvite (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"INVITE", 0, 0, false, 4) { syntax = "[<nick> <channel>]"; }
35         /** Handle command.
36          * @param parameters The parameters to the comamnd
37          * @param pcnt The number of parameters passed to teh command
38          * @param user The user issuing the command
39          * @return A value from CmdResult to indicate command success or failure.
40          */
41         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
42 };
43
44 #endif
45
46
47 /** Handle /INVITE
48  */
49 CmdResult CommandInvite::Handle (const std::vector<std::string>& parameters, User *user)
50 {
51         ModResult MOD_RESULT;
52
53         if (parameters.size() == 2 || parameters.size() == 3)
54         {
55                 User* u = ServerInstance->FindNick(parameters[0]);
56                 Channel* c = ServerInstance->FindChan(parameters[1]);
57                 time_t timeout = 0;
58                 if (parameters.size() == 3)
59                 {
60                         if (IS_LOCAL(user))
61                                 timeout = ServerInstance->Time() + ServerInstance->Duration(parameters[2]);
62                         else
63                                 timeout = ConvToInt(parameters[2]);
64                 }
65
66                 if ((!c) || (!u))
67                 {
68                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(), c ? parameters[0].c_str() : parameters[1].c_str());
69                         return CMD_FAILURE;
70                 }
71
72                 if (c->HasUser(u))
73                 {
74                         user->WriteNumeric(ERR_USERONCHANNEL, "%s %s %s :is already on channel",user->nick.c_str(),u->nick.c_str(),c->name.c_str());
75                         return CMD_FAILURE;
76                 }
77
78                 if ((IS_LOCAL(user)) && (!c->HasUser(user)))
79                 {
80                         user->WriteNumeric(ERR_NOTONCHANNEL, "%s %s :You're not on that channel!",user->nick.c_str(), c->name.c_str());
81                         return CMD_FAILURE;
82                 }
83
84                 FIRST_MOD_RESULT(ServerInstance, OnUserPreInvite, MOD_RESULT, (user,u,c,timeout));
85
86                 if (MOD_RESULT == MOD_RES_DENY)
87                 {
88                         return CMD_FAILURE;
89                 }
90                 else if (MOD_RESULT == MOD_RES_PASSTHRU)
91                 {
92                         if (IS_LOCAL(user))
93                         {
94                                 int rank = c->GetPrefixValue(user);
95                                 if (rank < HALFOP_VALUE)
96                                 {
97                                         user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must be a channel %soperator",
98                                                 user->nick.c_str(), c->name.c_str(), rank >= HALFOP_VALUE ? "" : "half-");
99                                         return CMD_FAILURE;
100                                 }
101                         }
102                 }
103
104                 u->InviteTo(c->name.c_str(), timeout);
105                 u->WriteFrom(user,"INVITE %s :%s",u->nick.c_str(),c->name.c_str());
106                 user->WriteNumeric(RPL_INVITING, "%s %s %s",user->nick.c_str(),u->nick.c_str(),c->name.c_str());
107                 switch (ServerInstance->Config->AnnounceInvites)
108                 {
109                         case ServerConfig::INVITE_ANNOUNCE_ALL:
110                                 c->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :*** %s invited %s into the channel", c->name.c_str(), user->nick.c_str(), u->nick.c_str());
111                         break;
112                         case ServerConfig::INVITE_ANNOUNCE_OPS:
113                                 c->WriteAllExceptSender(user, true, '@', "NOTICE %s :*** %s invited %s into the channel", c->name.c_str(), user->nick.c_str(), u->nick.c_str());
114                         break;
115                         case ServerConfig::INVITE_ANNOUNCE_DYNAMIC:
116                                 if (c->IsModeSet('i'))
117                                         c->WriteAllExceptSender(user, true, '@', "NOTICE %s :*** %s invited %s into the channel", c->name.c_str(), user->nick.c_str(), u->nick.c_str());
118                                 else
119                                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :*** %s invited %s into the channel", c->name.c_str(), user->nick.c_str(), u->nick.c_str());
120                         break;
121                         default:
122                                 /* Nobody */
123                         break;
124                 }
125                 FOREACH_MOD(I_OnUserInvite,OnUserInvite(user,u,c,timeout));
126         }
127         else
128         {
129                 // pinched from ircu - invite with not enough parameters shows channels
130                 // youve been invited to but haven't joined yet.
131                 InvitedList* il = user->GetInviteList();
132                 for (InvitedList::iterator i = il->begin(); i != il->end(); i++)
133                 {
134                         user->WriteNumeric(RPL_INVITELIST, "%s :%s",user->nick.c_str(),i->first.c_str());
135                 }
136                 user->WriteNumeric(RPL_ENDOFINVITELIST, "%s :End of INVITE list",user->nick.c_str());
137         }
138         return CMD_SUCCESS;
139 }
140
141
142 COMMAND_INIT(CommandInvite)