]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-op.c
Update #include's: remove unused and add missing ones
[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 <assert.h>
20 #include <string.h>
21
22 #include "conn.h"
23 #include "channel.h"
24 #include "irc-macros.h"
25 #include "irc-write.h"
26 #include "lists.h"
27 #include "log.h"
28 #include "messages.h"
29 #include "parse.h"
30
31 /* Local functions */
32
33 static bool
34 try_kick(CLIENT *peer, CLIENT* from, const char *nick, const char *channel,
35          const char *reason)
36 {
37         CLIENT *target = Client_Search(nick);
38
39         if (!target)
40                 return IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
41                                           Client_ID(from), nick);
42
43         Channel_Kick(peer, target, from, channel, reason);
44         return true;
45 }
46
47 /* Global functions */
48
49 /**
50  * Handler for the IRC command "KICK".
51  *
52  * @param Client The client from which this command has been received.
53  * @param Req Request structure with prefix and all parameters.
54  * @return CONNECTED or DISCONNECTED.
55  */
56 GLOBAL bool
57 IRC_KICK(CLIENT *Client, REQUEST *Req)
58 {
59         CLIENT *from;
60         char *itemList = Req->argv[0];
61         const char* currentNick, *currentChannel, *reason;
62         unsigned int channelCount = 1;
63         unsigned int nickCount = 1;
64
65         assert( Client != NULL );
66         assert( Req != NULL );
67
68         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
69
70         while (*itemList) {
71                 if (*itemList == ',') {
72                         *itemList = '\0';
73                         channelCount++;
74                 }
75                 itemList++;
76         }
77
78         itemList = Req->argv[1];
79         while (*itemList) {
80                 if (*itemList == ',') {
81                         *itemList = '\0';
82                         nickCount++;
83                 }
84                 itemList++;
85         }
86
87         reason = Req->argc == 3 ? Req->argv[2] : Client_ID(from);
88         currentNick = Req->argv[1];
89         currentChannel = Req->argv[0];
90         if (channelCount == 1) {
91                 while (nickCount > 0) {
92                         if (!try_kick(Client, from, currentNick,
93                                       currentChannel, reason))
94                                 return false;
95
96                         while (*currentNick)
97                                 currentNick++;
98
99                         currentNick++;
100                         nickCount--;
101                 }
102         } else if (channelCount == nickCount) {
103                 while (nickCount > 0) {
104                         if (!try_kick(Client, from, currentNick,
105                                       currentChannel, reason))
106                                 return false;
107
108                         while (*currentNick)
109                                 currentNick++;
110
111                         while (*currentChannel)
112                                 currentChannel++;
113
114                         currentNick++;
115                         currentChannel++;
116                         nickCount--;
117                 }
118         } else {
119                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
120                                         Client_ID(Client), Req->command);
121         }
122         return true;
123 } /* IRC_KICK */
124
125 /**
126  * Handler for the IRC command "INVITE".
127  *
128  * @param Client The client from which this command has been received.
129  * @param Req Request structure with prefix and all parameters.
130  * @return CONNECTED or DISCONNECTED.
131  */
132 GLOBAL bool
133 IRC_INVITE(CLIENT *Client, REQUEST *Req)
134 {
135         CHANNEL *chan;
136         CLIENT *target, *from;
137         const char *colon_if_necessary;
138         bool remember = false;
139
140         assert( Client != NULL );
141         assert( Req != NULL );
142
143         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
144
145         /* Search user */
146         target = Client_Search(Req->argv[0]);
147         if (!target || (Client_Type(target) != CLIENT_USER))
148                 return IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
149                                           Client_ID(Client), Req->argv[0]);
150
151         chan = Channel_Search(Req->argv[1]);
152         if (chan) {
153                 /* Channel exists. Is the user a valid member of the channel? */
154                 if (!Channel_IsMemberOf(chan, from))
155                         return IRC_WriteErrClient(from, ERR_NOTONCHANNEL_MSG,
156                                                   Client_ID(Client),
157                                                   Req->argv[1]);
158
159                 /* Is the channel "invite-disallow"? */
160                 if (Channel_HasMode(chan, 'V'))
161                         return IRC_WriteErrClient(from, ERR_NOINVITE_MSG,
162                                                   Client_ID(from),
163                                                   Channel_Name(chan));
164
165                 /* Is the channel "invite-only"? */
166                 if (Channel_HasMode(chan, 'i')) {
167                         /* Yes. The user issuing the INVITE command must be
168                          * channel owner/admin/operator/halfop! */
169                         if (!Channel_UserHasMode(chan, from, 'q') &&
170                             !Channel_UserHasMode(chan, from, 'a') &&
171                             !Channel_UserHasMode(chan, from, 'o') &&
172                             !Channel_UserHasMode(chan, from, 'h'))
173                                 return IRC_WriteErrClient(from,
174                                                           ERR_CHANOPRIVSNEEDED_MSG,
175                                                           Client_ID(from),
176                                                           Channel_Name(chan));
177                         remember = true;
178                 }
179
180                 /* Is the target user already member of the channel? */
181                 if (Channel_IsMemberOf(chan, target))
182                         return IRC_WriteErrClient(from, ERR_USERONCHANNEL_MSG,
183                                                   Client_ID(from),
184                                                   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_MaskCloaked(target),
193                                                 true))
194                                 return CONNECTED;
195                 }
196         }
197
198         LogDebug("User \"%s\" invites \"%s\" to \"%s\" ...", Client_Mask(from),
199                  Req->argv[0], Req->argv[1]);
200
201         /*
202          * RFC 2812 states:
203          * 'There is no requirement that the channel [..] must exist or be a
204          * valid channel'. The problem with this is that this allows the
205          * "channel" to contain spaces, in which case we must prefix its name
206          * with a colon to make it clear that it is only a single argument.
207          */
208         colon_if_necessary = strchr(Req->argv[1], ' ') ? ":":"";
209         /* Inform target client */
210         IRC_WriteStrClientPrefix(target, from, "INVITE %s %s%s", Req->argv[0],
211                                   colon_if_necessary, Req->argv[1]);
212
213         if (Client_Conn(target) > NONE) {
214                 /* The target user is local, so we have to send the status code */
215                 if (!IRC_WriteStrClientPrefix(from, target, RPL_INVITING_MSG,
216                                                Client_ID(from), Req->argv[0],
217                                                colon_if_necessary, Req->argv[1]))
218                         return DISCONNECTED;
219
220                 if (Client_HasMode(target, 'a') &&
221                         !IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from),
222                                         Client_ID(target), Client_Away(target)))
223                                 return DISCONNECTED;
224         }
225         return CONNECTED;
226 } /* IRC_INVITE */
227
228 /* -eof- */