Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 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 * IRC commands
12 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: irc.c,v 1.117 2003/01/13 18:56:30 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
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"
37 #include "exp.h"
38 #include "irc.h"
41 GLOBAL BOOLEAN
42 IRC_ERROR( CLIENT *Client, REQUEST *Req )
43 {
44 assert( Client != NULL );
45 assert( Req != NULL );
47 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
48 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
50 return CONNECTED;
51 } /* IRC_ERROR */
54 GLOBAL BOOLEAN
55 IRC_KILL( CLIENT *Client, REQUEST *Req )
56 {
57 CLIENT *prefix, *c;
58 CHAR reason[COMMAND_LEN];
59 CONN_ID my_conn, conn;
61 assert( Client != NULL );
62 assert( Req != NULL );
64 /* Is the user an IRC operator? */
65 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
67 /* Bad number of parameters? */
68 if(( Req->argc != 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
70 if( Req->prefix ) prefix = Client_Search( Req->prefix );
71 else prefix = Client;
72 if( ! prefix )
73 {
74 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!", Req->prefix );
75 prefix = Client_ThisServer( );
76 }
78 if( Client != Client_ThisServer( )) Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s", Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
80 /* Build reason string */
81 if( Client_Type( Client ) == CLIENT_USER ) snprintf( reason, sizeof( reason ), "KILLed by %s: %s", Client_ID( Client ), Req->argv[1] );
82 else strlcpy( reason, Req->argv[1], sizeof( reason ));
84 /* Inform other servers */
85 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s", Req->argv[0], reason );
87 /* Save ID of this connection */
88 my_conn = Client_Conn( Client );
90 /* Do we host such a client? */
91 c = Client_Search( Req->argv[0] );
92 if( c )
93 {
94 /* Yes, there is such a client -- but is it a valid user? */
95 if( Client_Type( c ) == CLIENT_SERVER ) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
96 else if( Client_Type( c ) != CLIENT_USER )IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
97 else
98 {
99 /* Kill user NOW! */
100 conn = Client_Conn( c );
101 Client_Destroy( c, NULL, reason, FALSE );
102 if( conn != NONE ) Conn_Close( conn, NULL, reason, TRUE );
105 else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
107 /* Are we still connected or were we killed, too? */
108 if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn ))) return CONNECTED;
109 else return DISCONNECTED;
110 } /* IRC_KILL */
113 GLOBAL BOOLEAN
114 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
116 CLIENT *to, *from;
118 assert( Client != NULL );
119 assert( Req != NULL );
121 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
123 /* Falsche Anzahl Parameter? */
124 if( Req->argc != 2 ) return CONNECTED;
126 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
127 else from = Client;
128 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
130 to = Client_Search( Req->argv[0] );
131 if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
133 /* Okay, Ziel ist ein User */
134 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
136 else return CONNECTED;
137 } /* IRC_NOTICE */
140 GLOBAL BOOLEAN
141 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
143 CLIENT *cl, *from;
144 CHANNEL *chan;
146 assert( Client != NULL );
147 assert( Req != NULL );
149 /* Falsche Anzahl Parameter? */
150 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
151 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
152 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
154 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
155 else from = Client;
156 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
158 cl = Client_Search( Req->argv[0] );
159 if( cl )
161 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
162 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
164 /* Okay, Ziel ist ein User */
165 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
167 /* Ziel-User ist AWAY: Meldung verschicken */
168 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
171 /* Text senden */
172 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
173 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
176 chan = Channel_Search( Req->argv[0] );
177 if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
179 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
180 } /* IRC_PRIVMSG */
183 GLOBAL BOOLEAN
184 IRC_TRACE( CLIENT *Client, REQUEST *Req )
186 CLIENT *from, *target;
187 CONN_ID idx, idx2;
188 CHAR ver[64], *ptr;
190 assert( Client != NULL );
191 assert( Req != NULL );
193 /* Bad number of arguments? */
194 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
196 /* Search sender */
197 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
198 else from = Client;
199 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
201 /* Search target */
202 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
203 else target = Client_ThisServer( );
205 strlcpy( ver, NGIRCd_VersionAddition( ), sizeof( ver ));
206 ptr = strchr( ver, '-' );
207 if( ptr ) *ptr = '\0';
209 /* Forward command to other server? */
210 if( target != Client_ThisServer( ))
212 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
214 /* Send RPL_TRACELINK back to initiator */
215 idx = Client_Conn( Client ); assert( idx > NONE );
216 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
217 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE, VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), ver, time( NULL ) - Conn_StartTime( idx ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
219 /* Forward command */
220 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
221 return CONNECTED;
224 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Conf_ServerName, Client_Mask( Client_ThisServer( )), ver )) return DISCONNECTED;
225 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE, VERSION, NGIRCd_DebugLevel );
226 } /* IRC_TRACE */
229 /* -eof- */