]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
86f5852779cf682f8b41989183cfa9a256453e1a
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2004 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  * IRC commands
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "defines.h"
31 #include "irc-write.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "parse.h"
35
36 #include "exp.h"
37 #include "irc.h"
38
39
40 static char *Option_String PARAMS(( CONN_ID Idx ));
41
42
43 GLOBAL bool
44 IRC_ERROR( CLIENT *Client, REQUEST *Req )
45 {
46         assert( Client != NULL );
47         assert( Req != NULL );
48
49         if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
50         else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
51
52         return CONNECTED;
53 } /* IRC_ERROR */
54
55
56 /**
57  * Kill client on request.
58  * This function implements the IRC command "KILL" wich is used to selectively
59  * disconnect clients. It can be used by IRC operators and servers, for example
60  * to "solve" nick collisions after netsplits.
61  * Please note that this function is also called internally, without a real
62  * KILL command beeing received over the network! Client is Client_ThisServer()
63  * in this case. */
64 GLOBAL bool
65 IRC_KILL( CLIENT *Client, REQUEST *Req )
66 {
67         CLIENT *prefix, *c;
68         char reason[COMMAND_LEN], *msg;
69         CONN_ID my_conn, conn;
70
71         assert( Client != NULL );
72         assert( Req != NULL );
73
74         if(( Client_Type( Client ) != CLIENT_SERVER ) &&
75            ( ! Client_OperByMe( Client )))
76         {
77                 /* The originator of the KILL is neither an IRC operator of
78                  * this server nor a server. */
79                 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
80                                            Client_ID( Client ));
81         }
82
83         if( Req->argc != 2 )
84         {
85                 /* This command requires exactly 2 parameters! */
86                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
87                                            Client_ID( Client ), Req->command );
88         }
89
90         if( Req->prefix ) prefix = Client_Search( Req->prefix );
91         else prefix = Client;
92         if( ! prefix )
93         {
94                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
95                      Req->prefix );
96                 prefix = Client_ThisServer( );
97         }
98
99         if( Client != Client_ThisServer( ))
100         {
101                 /* This is a "real" KILL received from the network. */
102                 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
103                      Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
104         }
105
106         /* Build reason string */
107         if( Client_Type( Client ) == CLIENT_USER )
108         {
109                 /* Prefix the "reason" if the originator is a regular user,
110                  * so users can't spoof KILLs of servers. */
111                 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
112                           Client_ID( Client ), Req->argv[1] );
113         }
114         else
115                 strlcpy( reason, Req->argv[1], sizeof( reason ));
116
117         /* Inform other servers */
118         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
119                                    Req->argv[0], reason );
120
121         /* Save ID of this connection */
122         my_conn = Client_Conn( Client );
123
124         /* Do we host such a client? */
125         c = Client_Search( Req->argv[0] );
126         if( c )
127         {
128                 if(( Client_Type( c ) != CLIENT_USER ) &&
129                    ( Client_Type( c ) != CLIENT_GOTNICK ))
130                 {
131                         /* Target of this KILL is not a regular user, this is
132                          * invalid! So we ignore this case if we received a
133                          * regular KILL from the network and try to kill the
134                          * client/connection anyway (but log an error!) if the
135                          * origin is the local server. */
136
137                         if( Client != Client_ThisServer( ))
138                         {
139                                 /* Invalid KILL received from remote */
140                                 if( Client_Type( c ) == CLIENT_SERVER )
141                                         msg = ERR_CANTKILLSERVER_MSG;
142                                 else
143                                         msg = ERR_NOPRIVILEGES_MSG;
144                                 return IRC_WriteStrClient( Client, msg,
145                                         Client_ID( Client ));
146                         }
147
148                         Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
149                              Client_Type( c ), Req->argv[0] );
150                 }
151
152                 /* Kill client NOW! */
153                 conn = Client_Conn( c );
154                 Client_Destroy( c, NULL, reason, false );
155                 if( conn > NONE )
156                         Conn_Close( conn, NULL, reason, true );
157         }
158         else
159                 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
160
161         /* Are we still connected or were we killed, too? */
162         if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
163                 return CONNECTED;
164         else
165                 return DISCONNECTED;
166 } /* IRC_KILL */
167
168
169 GLOBAL bool
170 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
171 {
172         CLIENT *to, *from;
173         CHANNEL *chan;
174
175         assert( Client != NULL );
176         assert( Req != NULL );
177
178         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
179
180         /* Falsche Anzahl Parameter? */
181         if( Req->argc != 2 ) return CONNECTED;
182
183         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
184         else from = Client;
185         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
186
187         to = Client_Search( Req->argv[0] );
188         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
189         {
190                 /* Okay, Ziel ist ein User */
191                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
192         }
193         else
194         {
195                 chan = Channel_Search(Req->argv[0]);
196                 if (chan)
197                         return Channel_Notice(chan, from, Client, Req->argv[1]);
198         }
199
200         return CONNECTED;
201 } /* IRC_NOTICE */
202
203
204 GLOBAL bool
205 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
206 {
207         CLIENT *cl, *from;
208         CHANNEL *chan;
209         
210         assert( Client != NULL );
211         assert( Req != NULL );
212
213         /* Falsche Anzahl Parameter? */
214         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
215         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
216         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
217
218         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
219         else from = Client;
220         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
221
222         cl = Client_Search( Req->argv[0] );
223         if( cl )
224         {
225                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
226                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
227
228                 /* Okay, Ziel ist ein User */
229                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
230                 {
231                         /* Ziel-User ist AWAY: Meldung verschicken */
232                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
233                 }
234
235                 /* Text senden */
236                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
237                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
238         }
239
240         chan = Channel_Search( Req->argv[0] );
241         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
242
243         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
244 } /* IRC_PRIVMSG */
245
246
247 GLOBAL bool
248 IRC_TRACE( CLIENT *Client, REQUEST *Req )
249 {
250         CLIENT *from, *target, *c;
251         CONN_ID idx, idx2;
252         char user[CLIENT_USER_LEN];
253
254         assert( Client != NULL );
255         assert( Req != NULL );
256
257         /* Bad number of arguments? */
258         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
259
260         /* Search sender */
261         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
262         else from = Client;
263         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
264
265         /* Search target */
266         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
267         else target = Client_ThisServer( );
268         
269         /* Forward command to other server? */
270         if( target != Client_ThisServer( ))
271         {
272                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
273
274                 /* Send RPL_TRACELINK back to initiator */
275                 idx = Client_Conn( Client ); assert( idx > NONE );
276                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
277                 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
278
279                 /* Forward command */
280                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
281                 return CONNECTED;
282         }
283
284         /* Infos about all connected servers */
285         c = Client_First( );
286         while( c )
287         {
288                 if( Client_Conn( c ) > NONE )
289                 {
290                         /* Local client */
291                         if( Client_Type( c ) == CLIENT_SERVER )
292                         {
293                                 /* Server link */
294                                 strlcpy( user, Client_User( c ), sizeof( user ));
295                                 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
296                                 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
297                         }
298                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
299                         {
300                                 /* IRC Operator */
301                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
302                         }
303                 }
304                 c = Client_Next( c );
305         }
306
307         IRC_SetPenalty( Client, 3 );
308         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
309 } /* IRC_TRACE */
310
311
312 GLOBAL bool
313 IRC_HELP( CLIENT *Client, REQUEST *Req )
314 {
315         COMMAND *cmd;
316
317         assert( Client != NULL );
318         assert( Req != NULL );
319
320         /* Bad number of arguments? */
321         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
322
323         cmd = Parse_GetCommandStruct( );
324         while( cmd->name )
325         {
326                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
327                 cmd++;
328         }
329         
330         IRC_SetPenalty( Client, 2 );
331         return CONNECTED;
332 } /* IRC_HELP */
333
334
335 static char *
336 Option_String( CONN_ID Idx )
337 {
338         static char option_txt[8];
339         UINT16 options;
340
341         options = Conn_Options(Idx);
342
343         strcpy(option_txt, "F");        /* No idea what this means, but the
344                                          * original ircd sends it ... */
345 #ifdef ZLIB
346         if(options & CONN_ZIP)          /* zlib compression supported. */
347                 strcat(option_txt, "z");
348 #endif
349
350         return option_txt;
351 } /* Option_String */
352
353
354 /* -eof- */