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