34b0eb0f8e1fea9e00a0087947088b0978a3711e
[ngircd-alex.git] / src / ngircd / irc-op.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de)
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
13 #include "portab.h"
14
15 /**
16  * @file
17  * Channel operator commands
18  */
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <string.h>
23 #include <stdio.h>
24
25 #include "defines.h"
26 #include "conn.h"
27 #include "channel.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
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
52 GLOBAL bool
53 IRC_KICK(CLIENT *Client, REQUEST *Req)
54 {
55         CLIENT *from;
56         char *itemList = Req->argv[0];
57         const char* currentNick, *currentChannel, *reason;
58         unsigned int channelCount = 1;
59         unsigned int nickCount = 1;
60
61         assert( Client != NULL );
62         assert( Req != NULL );
63
64         if ((Req->argc < 2) || (Req->argc > 3))
65                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
66                                         Client_ID(Client), Req->command);
67
68         while (*itemList) {
69                 if (*itemList == ',') {
70                         *itemList = '\0';
71                         channelCount++;
72                 }
73                 itemList++;
74         }
75
76         itemList = Req->argv[1];
77         while (*itemList) {
78                 if (*itemList == ',') {
79                         *itemList = '\0';
80                         nickCount++;
81                 }
82                 itemList++;
83         }
84
85         if (Client_Type(Client) == CLIENT_SERVER)
86                 from = Client_Search(Req->prefix);
87         else
88                 from = Client;
89
90         if (!from)
91                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
92                                         Client_ID(Client), Req->prefix);
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_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
127                                         Client_ID(Client), Req->command);
128         }
129         return true;
130 } /* IRC_KICK */
131
132
133 GLOBAL bool
134 IRC_INVITE(CLIENT *Client, REQUEST *Req)
135 {
136         CHANNEL *chan;
137         CLIENT *target, *from;
138         const char *colon_if_necessary;
139         bool remember = false;
140
141         assert( Client != NULL );
142         assert( Req != NULL );
143
144         if (Req->argc != 2)
145                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
146                                         Client_ID(Client), Req->command);
147
148         if (Client_Type(Client) == CLIENT_SERVER)
149                 from = Client_Search(Req->prefix);
150         else
151                 from = Client;
152         if (!from)
153                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
154                                         Client_ID(Client), Req->prefix);
155
156         /* Search user */
157         target = Client_Search(Req->argv[0]);
158         if (!target || (Client_Type(target) != CLIENT_USER))
159                 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
160                                 Client_ID(Client), Req->argv[0]);
161
162         chan = Channel_Search(Req->argv[1]);
163         if (chan) {
164                 /* Channel exists. Is the user a valid member of the channel? */
165                 if (!Channel_IsMemberOf(chan, from))
166                         return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG, Client_ID(Client), Req->argv[1]);
167
168                 /* Is the channel "invite-only"? */
169                 if (strchr(Channel_Modes(chan), 'i')) {
170                         /* Yes. The user must be channel operator! */
171                         if (!strchr(Channel_UserModes(chan, from), 'o'))
172                                 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
173                                                 Client_ID(from), Channel_Name(chan));
174                         remember = true;
175                 }
176
177                 /* Is the target user already member of the channel? */
178                 if (Channel_IsMemberOf(chan, target))
179                         return IRC_WriteStrClient(from, ERR_USERONCHANNEL_MSG,
180                                         Client_ID(from), Req->argv[0], Req->argv[1]);
181
182                 /* If the target user is banned on that channel: remember invite */
183                 if (Lists_Check(Channel_GetListBans(chan), target))
184                         remember = true;
185
186                 if (remember) {
187                         /* We must remember this invite */
188                         if (!Channel_AddInvite(chan, Client_Mask(target), true))
189                                 return CONNECTED;
190                 }
191         }
192
193         LogDebug("User \"%s\" invites \"%s\" to \"%s\" ...", Client_Mask(from), Req->argv[0], Req->argv[1]);
194
195
196         /*
197          * RFC 2812 says:
198          * 'There is no requirement that the channel [..] must exist or be a valid channel'
199          * The problem with this is that this allows the "channel" to contain spaces,
200          * in which case we must prefix its name with a colon to make it clear that
201          * it is only a single argument.
202          */
203         colon_if_necessary = strchr(Req->argv[1], ' ') ? ":":"";
204         /* Inform target client */
205         IRC_WriteStrClientPrefix(target, from, "INVITE %s %s%s", Req->argv[0],
206                                         colon_if_necessary, Req->argv[1]);
207
208         if (Client_Conn(target) > NONE) {
209                 /* The target user is local, so we have to send the status code */
210                 if (!IRC_WriteStrClientPrefix(from, target, RPL_INVITING_MSG,
211                         Client_ID(from), Req->argv[0], colon_if_necessary, Req->argv[1]))
212                         return DISCONNECTED;
213
214                 if (strchr(Client_Modes(target), 'a') &&
215                         !IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from),
216                                         Client_ID(target), Client_Away(target)))
217                                 return DISCONNECTED;
218         }
219         return CONNECTED;
220 } /* IRC_INVITE */
221
222
223 /* -eof- */