]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
Don't include conn.h, conn-func.h is enough.
[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.131 2006/07/23 14:55:40 alex 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
174         assert( Client != NULL );
175         assert( Req != NULL );
176
177         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
178
179         /* Falsche Anzahl Parameter? */
180         if( Req->argc != 2 ) return CONNECTED;
181
182         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
183         else from = Client;
184         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
185
186         to = Client_Search( Req->argv[0] );
187         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
188         {
189                 /* Okay, Ziel ist ein User */
190                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
191         }
192         else return CONNECTED;
193 } /* IRC_NOTICE */
194
195
196 GLOBAL bool
197 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
198 {
199         CLIENT *cl, *from;
200         CHANNEL *chan;
201         
202         assert( Client != NULL );
203         assert( Req != NULL );
204
205         /* Falsche Anzahl Parameter? */
206         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
207         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
208         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
209
210         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
211         else from = Client;
212         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
213
214         cl = Client_Search( Req->argv[0] );
215         if( cl )
216         {
217                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
218                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
219
220                 /* Okay, Ziel ist ein User */
221                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
222                 {
223                         /* Ziel-User ist AWAY: Meldung verschicken */
224                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
225                 }
226
227                 /* Text senden */
228                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
229                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
230         }
231
232         chan = Channel_Search( Req->argv[0] );
233         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
234
235         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
236 } /* IRC_PRIVMSG */
237
238
239 GLOBAL bool
240 IRC_TRACE( CLIENT *Client, REQUEST *Req )
241 {
242         CLIENT *from, *target, *c;
243         CONN_ID idx, idx2;
244         char user[CLIENT_USER_LEN];
245
246         assert( Client != NULL );
247         assert( Req != NULL );
248
249         /* Bad number of arguments? */
250         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
251
252         /* Search sender */
253         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
254         else from = Client;
255         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
256
257         /* Search target */
258         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
259         else target = Client_ThisServer( );
260         
261         /* Forward command to other server? */
262         if( target != Client_ThisServer( ))
263         {
264                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
265
266                 /* Send RPL_TRACELINK back to initiator */
267                 idx = Client_Conn( Client ); assert( idx > NONE );
268                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
269                 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;
270
271                 /* Forward command */
272                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
273                 return CONNECTED;
274         }
275
276         /* Infos about all connected servers */
277         c = Client_First( );
278         while( c )
279         {
280                 if( Client_Conn( c ) > NONE )
281                 {
282                         /* Local client */
283                         if( Client_Type( c ) == CLIENT_SERVER )
284                         {
285                                 /* Server link */
286                                 strlcpy( user, Client_User( c ), sizeof( user ));
287                                 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
288                                 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;
289                         }
290                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
291                         {
292                                 /* IRC Operator */
293                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
294                         }
295                 }
296                 c = Client_Next( c );
297         }
298
299         IRC_SetPenalty( Client, 3 );
300         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
301 } /* IRC_TRACE */
302
303
304 GLOBAL bool
305 IRC_HELP( CLIENT *Client, REQUEST *Req )
306 {
307         COMMAND *cmd;
308
309         assert( Client != NULL );
310         assert( Req != NULL );
311
312         /* Bad number of arguments? */
313         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
314
315         cmd = Parse_GetCommandStruct( );
316         while( cmd->name )
317         {
318                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
319                 cmd++;
320         }
321         
322         IRC_SetPenalty( Client, 2 );
323         return CONNECTED;
324 } /* IRC_HELP */
325
326
327 static char *
328 Option_String( CONN_ID Idx )
329 {
330         static char option_txt[8];
331         UINT16 options;
332
333         options = Conn_Options(Idx);
334
335         strcpy(option_txt, "F");        /* No idea what this means, but the
336                                          * original ircd sends it ... */
337 #ifdef ZLIB
338         if(options & CONN_ZIP)          /* zlib compression supported. */
339                 strcat(option_txt, "z");
340 #endif
341
342         return option_txt;
343 } /* Option_String */
344
345
346 /* -eof- */