]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc.c
Removed "USE_" prefixes of configuration #defines.
[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.123 2003/12/26 15:55:07 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 )
99                 {
100                         if( Client != Client_ThisServer( )) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
101                         else
102                         {
103                                 /* Oops, I should kill another server!? */
104                                 Log( LOG_ERR, "Can't KILL server \"%s\"!", Req->argv[0] );
105                                 conn = Client_Conn( Client_NextHop( c ));
106                                 assert( conn > NONE );
107                                 Conn_Close( conn, NULL, "Nick collision for server!?", TRUE );
108                         }
109                 }
110                 else if( Client_Type( c ) != CLIENT_USER  )
111                 {
112                         if( Client != Client_ThisServer( )) IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
113                         else
114                         {
115                                 /* Oops, what sould I close?? */
116                                 Log( LOG_ERR, "Can't KILL \"%s\": invalid client type!", Req->argv[0] );
117                                 conn = Client_Conn( Client_NextHop( c ));
118                                 assert( conn > NONE );
119                                 Conn_Close( conn, NULL, "Collision for invalid client type!?", TRUE );
120                         }
121                 }
122                 else
123                 {
124                         /* Kill user NOW! */
125                         conn = Client_Conn( c );
126                         Client_Destroy( c, NULL, reason, FALSE );
127                         if( conn != NONE ) Conn_Close( conn, NULL, reason, TRUE );
128                 }
129         }
130         else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
131
132         /* Are we still connected or were we killed, too? */
133         if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn ))) return CONNECTED;
134         else return DISCONNECTED;
135 } /* IRC_KILL */
136
137
138 GLOBAL BOOLEAN
139 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
140 {
141         CLIENT *to, *from;
142
143         assert( Client != NULL );
144         assert( Req != NULL );
145
146         if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
147
148         /* Falsche Anzahl Parameter? */
149         if( Req->argc != 2 ) return CONNECTED;
150
151         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
152         else from = Client;
153         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
154
155         to = Client_Search( Req->argv[0] );
156         if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
157         {
158                 /* Okay, Ziel ist ein User */
159                 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
160         }
161         else return CONNECTED;
162 } /* IRC_NOTICE */
163
164
165 GLOBAL BOOLEAN
166 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
167 {
168         CLIENT *cl, *from;
169         CHANNEL *chan;
170         
171         assert( Client != NULL );
172         assert( Req != NULL );
173
174         /* Falsche Anzahl Parameter? */
175         if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
176         if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
177         if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
178
179         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
180         else from = Client;
181         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
182
183         cl = Client_Search( Req->argv[0] );
184         if( cl )
185         {
186                 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
187                 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
188
189                 /* Okay, Ziel ist ein User */
190                 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
191                 {
192                         /* Ziel-User ist AWAY: Meldung verschicken */
193                         if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
194                 }
195
196                 /* Text senden */
197                 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
198                 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
199         }
200
201         chan = Channel_Search( Req->argv[0] );
202         if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
203
204         return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
205 } /* IRC_PRIVMSG */
206
207
208 GLOBAL BOOLEAN
209 IRC_TRACE( CLIENT *Client, REQUEST *Req )
210 {
211         CLIENT *from, *target, *c;
212         CONN_ID idx, idx2;
213
214         assert( Client != NULL );
215         assert( Req != NULL );
216
217         /* Bad number of arguments? */
218         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
219
220         /* Search sender */
221         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
222         else from = Client;
223         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
224
225         /* Search target */
226         if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
227         else target = Client_ThisServer( );
228         
229         /* Forward command to other server? */
230         if( target != Client_ThisServer( ))
231         {
232                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
233
234                 /* Send RPL_TRACELINK back to initiator */
235                 idx = Client_Conn( Client ); assert( idx > NONE );
236                 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
237                 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;
238
239                 /* Forward command */
240                 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
241                 return CONNECTED;
242         }
243
244         /* Infos about all connected servers */
245         c = Client_First( );
246         while( c )
247         {
248                 if( Client_Conn( c ) > NONE )
249                 {
250                         /* Local client */
251                         if( Client_Type( c ) == CLIENT_SERVER )
252                         {
253                                 /* Server link */
254                                 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), Client_Mask( c ), Option_String( Client_Conn( c )))) return DISCONNECTED;
255                         }
256                         if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
257                         {
258                                 /* IRC Operator */
259                                 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
260                         }
261                 }
262                 c = Client_Next( c );
263         }
264
265         /* Some information about us */
266         if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Conf_ServerName, Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( Client )))) return DISCONNECTED;
267
268         IRC_SetPenalty( Client, 3 );
269         return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
270 } /* IRC_TRACE */
271
272
273 GLOBAL BOOLEAN
274 IRC_HELP( CLIENT *Client, REQUEST *Req )
275 {
276         COMMAND *cmd;
277
278         assert( Client != NULL );
279         assert( Req != NULL );
280
281         /* Bad number of arguments? */
282         if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
283
284         cmd = Parse_GetCommandStruct( );
285         while( cmd->name )
286         {
287                 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
288                 cmd++;
289         }
290         
291         IRC_SetPenalty( Client, 2 );
292         return CONNECTED;
293 } /* IRC_HELP */
294
295
296 LOCAL CHAR *
297 Option_String( CONN_ID Idx )
298 {
299         STATIC CHAR option_txt[8];
300         INT options;
301
302         options = Conn_Options( Idx );
303
304         strcpy( option_txt, "F" );      /* No idea what this means but the original ircd sends it ... */
305 #ifdef ZLIB
306         if( options & CONN_ZIP ) strcat( option_txt, "z" );
307 #endif
308
309         return option_txt;
310 } /* Option_String */
311
312
313 /* -eof- */