]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-op.c
Implement IRC commands SERVICE, SERVLIST, and SQUERY as dummy functions
[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* from, const char *nick, const char *channel, 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(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(from, currentNick, currentChannel, reason))
97                                 return false;
98
99                         while (*currentNick)
100                                 currentNick++;
101
102                         currentNick++;
103                         nickCount--;
104                 }
105         } else if (channelCount == nickCount) {
106                 while (nickCount > 0) {
107                         if (!try_kick(from, currentNick, currentChannel, reason))
108                                 return false;
109
110                         while (*currentNick)
111                                 currentNick++;
112
113                         while (*currentChannel)
114                                 currentChannel++;
115
116                         currentNick++;
117                         currentChannel++;
118                         nickCount--;
119                 }
120         } else {
121                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
122                                         Client_ID(Client), Req->command);
123         }
124         return true;
125 } /* IRC_KICK */
126
127
128 GLOBAL bool
129 IRC_INVITE(CLIENT *Client, REQUEST *Req)
130 {
131         CHANNEL *chan;
132         CLIENT *target, *from;
133         const char *colon_if_necessary;
134         bool remember = false;
135
136         assert( Client != NULL );
137         assert( Req != NULL );
138
139         if (Req->argc != 2)
140                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
141                                         Client_ID(Client), Req->command);
142
143         if (Client_Type(Client) == CLIENT_SERVER)
144                 from = Client_Search(Req->prefix);
145         else
146                 from = Client;
147         if (!from)
148                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
149                                         Client_ID(Client), Req->prefix);
150
151         /* Search user */
152         target = Client_Search(Req->argv[0]);
153         if (!target || (Client_Type(target) != CLIENT_USER))
154                 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
155                                 Client_ID(Client), Req->argv[0]);
156
157         chan = Channel_Search(Req->argv[1]);
158         if (chan) {
159                 /* Channel exists. Is the user a valid member of the channel? */
160                 if (!Channel_IsMemberOf(chan, from))
161                         return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG, Client_ID(Client), Req->argv[1]);
162
163                 /* Is the channel "invite-only"? */
164                 if (strchr(Channel_Modes(chan), 'i')) {
165                         /* Yes. The user must be channel operator! */
166                         if (!strchr(Channel_UserModes(chan, from), 'o'))
167                                 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
168                                                 Client_ID(from), Channel_Name(chan));
169                         remember = true;
170                 }
171
172                 /* Is the target user already member of the channel? */
173                 if (Channel_IsMemberOf(chan, target))
174                         return IRC_WriteStrClient(from, ERR_USERONCHANNEL_MSG,
175                                         Client_ID(from), Req->argv[0], Req->argv[1]);
176
177                 /* If the target user is banned on that channel: remember invite */
178                 if (Lists_Check(Channel_GetListBans(chan), target))
179                         remember = true;
180
181                 if (remember) {
182                         /* We must remember this invite */
183                         if (!Channel_AddInvite(chan, Client_Mask(target), true))
184                                 return CONNECTED;
185                 }
186         }
187
188         LogDebug("User \"%s\" invites \"%s\" to \"%s\" ...", Client_Mask(from), Req->argv[0], Req->argv[1]);
189
190
191         /*
192          * RFC 2812 says:
193          * 'There is no requirement that the channel [..] must exist or be a valid channel'
194          * The problem with this is that this allows the "channel" to contain spaces,
195          * in which case we must prefix its name with a colon to make it clear that
196          * it is only a single argument.
197          */
198         colon_if_necessary = strchr(Req->argv[1], ' ') ? ":":"";
199         /* Inform target client */
200         IRC_WriteStrClientPrefix(target, from, "INVITE %s %s%s", Req->argv[0],
201                                         colon_if_necessary, Req->argv[1]);
202
203         if (Client_Conn(target) > NONE) {
204                 /* The target user is local, so we have to send the status code */
205                 if (!IRC_WriteStrClientPrefix(from, target, RPL_INVITING_MSG,
206                         Client_ID(from), Req->argv[0], colon_if_necessary, Req->argv[1]))
207                         return DISCONNECTED;
208
209                 if (strchr(Client_Modes(target), 'a') &&
210                         !IRC_WriteStrClient(from, RPL_AWAY_MSG, Client_ID(from),
211                                         Client_ID(target), Client_Away(target)))
212                                 return DISCONNECTED;
213         }
214         return CONNECTED;
215 } /* IRC_INVITE */
216
217
218 /* -eof- */