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