]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
Enhanced (and fiexd) IRC_TRACE(): now shows operators and correct link uptimes.
[ngircd-alex.git] / src / ngircd / irc.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2003 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  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc.c,v 1.119 2003/03/19 21:16:53 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 "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 LOCAL CHAR *Option_String PARAMS(( CONN_ID Idx ));
42
43
44 GLOBAL BOOLEAN
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 GLOBAL BOOLEAN
58 IRC_KILL( CLIENT *Client, REQUEST *Req )
59 {
60         CLIENT *prefix, *c;
61         CHAR reason[COMMAND_LEN];
62         CONN_ID my_conn, conn;
63
64         assert( Client != NULL );
65         assert( Req != NULL );
66
67         /* Is the user an IRC operator? */
68         if(( Client_Type( Client ) != CLIENT_SERVER ) && ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
69
70         /* Bad number of parameters? */
71         if(( Req->argc != 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
72
73         if( Req->prefix ) prefix = Client_Search( Req->prefix );
74         else prefix = Client;
75         if( ! prefix )
76         {
77                 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!", Req->prefix );
78                 prefix = Client_ThisServer( );
79         }
80
81         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] );
82
83         /* Build reason string */
84         if( Client_Type( Client ) == CLIENT_USER ) snprintf( reason, sizeof( reason ), "KILLed by %s: %s", Client_ID( Client ), Req->argv[1] );
85         else strlcpy( reason, Req->argv[1], sizeof( reason ));
86
87         /* Inform other servers */
88         IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s", Req->argv[0], reason );
89
90         /* Save ID of this connection */
91         my_conn = Client_Conn( Client );
92         
93         /* Do we host such a client? */
94         c = Client_Search( Req->argv[0] );
95         if( c )
96         {
97                 /* Yes, there is such a client -- but is it a valid user? */
98                 if( Client_Type( c ) == CLIENT_SERVER ) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
99                 else if( Client_Type( c ) != CLIENT_USER  )IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
100                 else
101                 {
102                         /* Kill user NOW! */
103                         conn = Client_Conn( c );
104                         Client_Destroy( c, NULL, reason, FALSE );
105                         if( conn != NONE ) Conn_Close( conn, NULL, reason, TRUE );
106                 }
107         }
108         else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
109
110         /* Are we still connected or were we killed, too? */
111         if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn ))) return CONNECTED;
112         else return DISCONNECTED;
113 } /* IRC_KILL */
114
115
116 GLOBAL BOOLEAN
117 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
118 {
119         CLIENT *to, *from;
120
121         assert( Client != NULL );
122         assert( Req != NULL );
123
124         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
125
126         /* Falsche Anzahl Parameter? */
127         if( Req->argc != 2 ) return CONNECTED;
128
129         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
130         else from = Client;
131         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
132
133         to = Client_Search( Req->argv[0] );
134         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
135         {
136                 /* Okay, Ziel ist ein User */
137                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
138         }
139         else return CONNECTED;
140 } /* IRC_NOTICE */
141
142
143 GLOBAL BOOLEAN
144 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
145 {
146         CLIENT *cl, *from;
147         CHANNEL *chan;
148         
149         assert( Client != NULL );
150         assert( Req != NULL );
151
152         /* Falsche Anzahl Parameter? */
153         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
154         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
155         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
156
157         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
158         else from = Client;
159         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
160
161         cl = Client_Search( Req->argv[0] );
162         if( cl )
163         {
164                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
165                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
166
167                 /* Okay, Ziel ist ein User */
168                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
169                 {
170                         /* Ziel-User ist AWAY: Meldung verschicken */
171                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
172                 }
173
174                 /* Text senden */
175                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
176                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
177         }
178
179         chan = Channel_Search( Req->argv[0] );
180         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
181
182         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
183 } /* IRC_PRIVMSG */
184
185
186 GLOBAL BOOLEAN
187 IRC_TRACE( CLIENT *Client, REQUEST *Req )
188 {
189         CLIENT *from, *target, *c;
190         CONN_ID idx, idx2;
191
192         assert( Client != NULL );
193         assert( Req != NULL );
194
195         /* Bad number of arguments? */
196         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
197
198         /* Search sender */
199         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
200         else from = Client;
201         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
202
203         /* Search target */
204         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
205         else target = Client_ThisServer( );
206         
207         /* Forward command to other server? */
208         if( target != Client_ThisServer( ))
209         {
210                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
211
212                 /* Send RPL_TRACELINK back to initiator */
213                 idx = Client_Conn( Client ); assert( idx > NONE );
214                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
215                 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), 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;
216
217                 /* Forward command */
218                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
219                 return CONNECTED;
220         }
221
222         /* Infos about all connected servers */
223         c = Client_First( );
224         while( c )
225         {
226                 if( Client_Conn( c ) > NONE )
227                 {
228                         /* Local client */
229                         if( Client_Type( c ) == CLIENT_SERVER )
230                         {
231                                 /* Server link */
232                                 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), Client_Mask( c ), Option_String( Client_Conn( c )))) return DISCONNECTED;
233                         }
234                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
235                         {
236                                 /* IRC Operator */
237                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
238                         }
239                 }
240                 c = Client_Next( c );
241         }
242
243         /* Some information about us */
244         if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Conf_ServerName, Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( Client )))) return DISCONNECTED;
245
246         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE, VERSION, NGIRCd_DebugLevel );
247 } /* IRC_TRACE */
248
249
250 GLOBAL BOOLEAN
251 IRC_HELP( CLIENT *Client, REQUEST *Req )
252 {
253         COMMAND *cmd;
254
255         assert( Client != NULL );
256         assert( Req != NULL );
257
258         /* Bad number of arguments? */
259         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
260
261         cmd = Parse_GetCommandStruct( );
262         while( cmd->name )
263         {
264                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
265                 cmd++;
266         }
267         return CONNECTED;
268 } /* IRC_HELP */
269
270
271 LOCAL CHAR *
272 Option_String( CONN_ID Idx )
273 {
274         STATIC CHAR option_txt[8];
275         INT options;
276
277         options = Conn_Options( Idx );
278
279         strcpy( option_txt, "F" );      /* No idea what this means but the original ircd sends it ... */
280 #ifdef USE_ZLIB
281         if( options & CONN_ZIP ) strcat( option_txt, "z" );
282 #endif
283
284         return option_txt;
285 } /* Option_String */
286
287
288 /* -eof- */