]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-op.c
Simplify mode checking on channels and users within a channel
[ngircd-alex.git] / src / ngircd / irc-op.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Channel operator commands
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <string.h>
22 #include <stdio.h>
23
24 #include "defines.h"
25 #include "conn.h"
26 #include "channel.h"
27 #include "irc-macros.h"
28 #include "irc-write.h"
29 #include "lists.h"
30 #include "log.h"
31 #include "messages.h"
32 #include "parse.h"
33
34 #include "exp.h"
35 #include "irc-op.h"
36
37 /* Local functions */
38
39 static bool
40 try_kick(CLIENT *peer, CLIENT* from, const char *nick, const char *channel,
41          const char *reason)
42 {
43         CLIENT *target = Client_Search(nick);
44
45         if (!target)
46                 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG, Client_ID(from), nick);
47
48         Channel_Kick(peer, target, from, channel, reason);
49         return true;
50 }
51
52 /* Global functions */
53
54 /**
55  * Handler for the IRC command "KICK".
56  *
57  * @param Client The client from which this command has been received.
58  * @param Req Request structure with prefix and all parameters.
59  * @return CONNECTED or DISCONNECTED.
60  */
61 GLOBAL bool
62 IRC_KICK(CLIENT *Client, REQUEST *Req)
63 {
64         CLIENT *from;
65         char *itemList = Req->argv[0];
66         const char* currentNick, *currentChannel, *reason;
67         unsigned int channelCount = 1;
68         unsigned int nickCount = 1;
69
70         assert( Client != NULL );
71         assert( Req != NULL );
72
73         _IRC_ARGC_BETWEEN_OR_RETURN_(Client, Req, 2, 3)
74         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
75
76         while (*itemList) {
77                 if (*itemList == ',') {
78                         *itemList = '\0';
79                         channelCount++;
80                 }
81                 itemList++;
82         }
83
84         itemList = Req->argv[1];
85         while (*itemList) {
86                 if (*itemList == ',') {
87                         *itemList = '\0';
88                         nickCount++;
89                 }
90                 itemList++;
91         }
92
93         reason = Req->argc == 3 ? Req->argv[2] : Client_ID(from);
94         currentNick = Req->argv[1];
95         currentChannel = Req->argv[0];
96         if (channelCount == 1) {
97                 while (nickCount > 0) {
98                         if (!try_kick(Client, from, currentNick,
99                                       currentChannel, reason))
100                                 return false;
101
102                         while (*currentNick)
103                                 currentNick++;
104
105                         currentNick++;
106                         nickCount--;
107                 }
108         } else if (channelCount == nickCount) {
109                 while (nickCount > 0) {
110                         if (!try_kick(Client, from, currentNick,
111                                       currentChannel, reason))
112                                 return false;
113
114                         while (*currentNick)
115                                 currentNick++;
116
117                         while (*currentChannel)
118                                 currentChannel++;
119
120                         currentNick++;
121                         currentChannel++;
122                         nickCount--;
123                 }
124         } else {
125                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
126                                         Client_ID(Client), Req->command);
127         }
128         return true;
129 } /* IRC_KICK */
130
131 /**
132  * Handler for the IRC command "INVITE".
133  *
134  * @param Client The client from which this command has been received.
135  * @param Req Request structure with prefix and all parameters.
136  * @return CONNECTED or DISCONNECTED.
137  */
138 GLOBAL bool
139 IRC_INVITE(CLIENT *Client, REQUEST *Req)
140 {
141         CHANNEL *chan;
142         CLIENT *target, *from;
143         const char *colon_if_necessary;
144         bool remember = false;
145
146         assert( Client != NULL );
147         assert( Req != NULL );
148
149         _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 2)
150         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
151
152         /* Search user */
153         target = Client_Search(Req->argv[0]);
154         if (!target || (Client_Type(target) != CLIENT_USER))
155                 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
156                                 Client_ID(Client), Req->argv[0]);
157
158         chan = Channel_Search(Req->argv[1]);
159         if (chan) {
160                 /* Channel exists. Is the user a valid member of the channel? */
161                 if (!Channel_IsMemberOf(chan, from))
162                         return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG, Client_ID(Client), Req->argv[1]);
163
164                 /* Is the channel "invite-disallow"? */
165                 if (Channel_HasMode(chan, 'V'))
166                         return IRC_WriteStrClient(from, ERR_NOINVITE_MSG,
167                                 Client_ID(from), Channel_Name(chan));
168
169                 /* Is the channel "invite-only"? */
170                 if (Channel_HasMode(chan, 'i')) {
171                         /* Yes. The user must be channel owner/admin/operator/halfop! */
172                         if (!Channel_UserHasMode(chan, from, 'q') &&
173                             !Channel_UserHasMode(chan, from, 'a') &&
174                             !Channel_UserHasMode(chan, from, 'o') &&
175                             !Channel_UserHasMode(chan, from, 'h'))
176                                 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
177                                                 Client_ID(from), Channel_Name(chan));
178                         remember = true;
179                 }
180
181                 /* Is the target user already member of the channel? */
182                 if (Channel_IsMemberOf(chan, target))
183                         return IRC_WriteStrClient(from, ERR_USERONCHANNEL_MSG,
184                                         Client_ID(from), Req->argv[0], Req->argv[1]);
185
186                 /* If the target user is banned on that channel: remember invite */
187                 if (Lists_Check(Channel_GetListBans(chan), target))
188                         remember = true;
189
190                 if (remember) {
191                         /* We must remember this invite */
192                         if (!Channel_AddInvite(chan, Client_Mask(target), true))
193                                 return CONNECTED;
194                 }
195         }
196
197         LogDebug("User \"%s\" invites \"%s\" to \"%s\" ...", Client_Mask(from),
198                  Req->argv[0], Req->argv[1]);
199
200         /*
201          * RFC 2812 says:
202          * 'There is no requirement that the channel [..] must exist or be a valid channel'
203          * The problem with this is that this allows the "channel" to contain spaces,
204          * in which case we must prefix its name with a colon to make it clear that
205          * it is only a single argument.
206          */
207         colon_if_necessary = strchr(Req->argv[1], ' ') ? ":":"";
208         /* Inform target client */
209         IRC_WriteStrClientPrefix(target, from, "INVITE %s %s%s", Req->argv[0],
210                                         colon_if_necessary, Req->argv[1]);
211
212         if (Client_Conn(target) > NONE) {
213                 /* The target user is local, so we have to send the status code */
214                 if (!IRC_WriteStrClientPrefix(from, target, RPL_INVITING_MSG,
215                         Client_ID(from), Req->argv[0], colon_if_necessary, Req->argv[1]))
216                         return DISCONNECTED;
217
218                 if (Client_HasMode(target, 'a') &&
219                         !IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from),
220                                         Client_ID(target), Client_Away(target)))
221                                 return DISCONNECTED;
222         }
223         return CONNECTED;
224 } /* IRC_INVITE */
225
226 /* -eof- */