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