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