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