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