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