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 for server links
12 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: irc-server.c,v 1.37 2004/05/11 00:01:11 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
26 #include "defines.h"
27 #include "resolve.h"
28 #include "conn.h"
29 #include "conn-zip.h"
30 #include "conf.h"
31 #include "client.h"
32 #include "channel.h"
33 #include "irc-write.h"
34 #include "lists.h"
35 #include "log.h"
36 #include "messages.h"
37 #include "parse.h"
38 #include "ngircd.h"
40 #include "exp.h"
41 #include "irc-server.h"
44 GLOBAL BOOLEAN
45 IRC_SERVER( CLIENT *Client, REQUEST *Req )
46 {
47 CHAR str[LINE_LEN], *ptr, *modes, *topic;
48 CLIENT *from, *c, *cl;
49 CL2CHAN *cl2chan;
50 INT max_hops, i;
51 CHANNEL *chan;
52 BOOLEAN ok;
53 CONN_ID con;
55 assert( Client != NULL );
56 assert( Req != NULL );
58 /* Fehler liefern, wenn kein lokaler Client */
59 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
61 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
62 {
63 /* Verbindung soll als Server-Server-Verbindung registriert werden */
64 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
66 /* Falsche Anzahl Parameter? */
67 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
69 /* Ist dieser Server bei uns konfiguriert? */
70 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
71 if( i >= MAX_SERVERS )
72 {
73 /* Server ist nicht konfiguriert! */
74 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
75 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
76 return DISCONNECTED;
77 }
78 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
79 {
80 /* Falsches Passwort */
81 Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
82 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
83 return DISCONNECTED;
84 }
86 /* Ist ein Server mit dieser ID bereits registriert? */
87 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
89 /* Server-Strukturen fuellen ;-) */
90 Client_SetID( Client, Req->argv[0] );
91 Client_SetHops( Client, 1 );
92 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
94 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
95 * selber die Verbindung zu einem anderen Server auf)? */
96 con = Client_Conn( Client );
97 if( Client_Token( Client ) != TOKEN_OUTBOUND )
98 {
99 /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
100 ok = TRUE;
101 if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = FALSE;
102 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
103 if( ! ok )
105 Conn_Close( con, "Unexpected server behavior!", NULL, FALSE );
106 return DISCONNECTED;
108 Client_SetIntroducer( Client, Client );
109 Client_SetToken( Client, 1 );
111 else
113 /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
114 * an die Gegenseite uerbermittelt */
115 Client_SetToken( Client, atoi( Req->argv[1] ));
118 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
120 Client_SetType( Client, CLIENT_SERVER );
121 Conf_SetServer( i, con );
123 #ifdef ZLIB
124 /* Kompression initialisieren, wenn erforderlich */
125 if( strchr( Client_Flags( Client ), 'Z' ))
127 if( ! Zip_InitConn( con ))
129 /* Fehler! */
130 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, FALSE );
131 return DISCONNECTED;
134 #endif
136 /* maximalen Hop Count ermitteln */
137 max_hops = 0;
138 c = Client_First( );
139 while( c )
141 if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
142 c = Client_Next( c );
145 /* Alle bisherigen Server dem neuen Server bekannt machen,
146 * die bisherigen Server ueber den neuen informierenn */
147 for( i = 0; i < ( max_hops + 1 ); i++ )
149 c = Client_First( );
150 while( c )
152 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
154 if( Client_Conn( c ) > NONE )
156 /* Dem gefundenen Server gleich den neuen
157 * Server bekannt machen */
158 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
161 /* Den neuen Server ueber den alten informieren */
162 if( ! IRC_WriteStrClientPrefix( Client, Client_Hops( c ) == 1 ? Client_ThisServer( ) : Client_Introducer( c ), "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ))) return DISCONNECTED;
164 c = Client_Next( c );
168 /* alle User dem neuen Server bekannt machen */
169 c = Client_First( );
170 while( c )
172 if( Client_Type( c ) == CLIENT_USER )
174 /* User an neuen Server melden */
175 if( ! IRC_WriteStrClient( Client, "NICK %s %d %s %s %d +%s :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_User( c ), Client_Hostname( c ), Client_MyToken( Client_Introducer( c )), Client_Modes( c ), Client_Info( c ))) return DISCONNECTED;
177 c = Client_Next( c );
180 /* Channels dem neuen Server bekannt machen */
181 chan = Channel_First( );
182 while( chan )
184 #ifdef IRCPLUS
185 /* Send CHANINFO if the peer supports it */
186 if( strchr( Client_Flags( Client ), 'C' ))
188 #ifdef DEBUG
189 Log( LOG_DEBUG, "Sending CHANINFO commands ..." );
190 #endif
191 modes = Channel_Modes( chan );
192 topic = Channel_Topic( chan );
194 if( *modes || *topic )
196 /* send CHANINFO */
197 if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )) && ( ! *topic ))
199 /* "CHANINFO <chan> +<modes>" */
200 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s", Channel_Name( chan ), modes )) return DISCONNECTED;
202 else if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )))
204 /* "CHANINFO <chan> +<modes> :<topic>" */
205 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), modes, topic )) return DISCONNECTED;
207 else
209 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
210 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s %s %ld :%s", Channel_Name( chan ), modes, strchr( Channel_Modes( chan ), 'k' ) ? Channel_Key( chan ) : "*", strchr( Channel_Modes( chan ), 'l' ) ? Channel_MaxUsers( chan ) : 0L, topic )) return DISCONNECTED;
214 #endif
216 /* alle Member suchen */
217 cl2chan = Channel_FirstMember( chan );
218 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
219 while( cl2chan )
221 cl = Channel_GetClient( cl2chan );
222 assert( cl != NULL );
224 /* Nick, ggf. mit Modes, anhaengen */
225 if( str[strlen( str ) - 1] != ':' ) strlcat( str, ",", sizeof( str ));
226 if( strchr( Channel_UserModes( chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
227 if( strchr( Channel_UserModes( chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
228 strlcat( str, Client_ID( cl ), sizeof( str ));
230 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
232 /* Zeile senden */
233 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
234 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
237 cl2chan = Channel_NextMember( chan, cl2chan );
240 /* noch Daten da? */
241 if( str[strlen( str ) - 1] != ':')
243 /* Ja; Also senden ... */
244 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
247 #ifdef IRCPLUS
248 if( strchr( Client_Flags( Client ), 'L' ))
250 #ifdef DEBUG
251 Log( LOG_DEBUG, "Synchronizing INVITE- and BAN-lists ..." );
252 #endif
253 /* Synchronize INVITE- and BAN-lists */
254 if( ! Lists_SendInvites( Client )) return DISCONNECTED;
255 if( ! Lists_SendBans( Client )) return DISCONNECTED;
257 #endif
259 /* naechsten Channel suchen */
260 chan = Channel_Next( chan );
263 return CONNECTED;
265 else if( Client_Type( Client ) == CLIENT_SERVER )
267 /* Neuer Server wird im Netz angekuendigt */
269 /* Falsche Anzahl Parameter? */
270 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
272 /* Ist ein Server mit dieser ID bereits registriert? */
273 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
275 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
276 ptr = strchr( Req->argv[3] + 2, '[' );
277 if( ! ptr ) ptr = Req->argv[3];
279 from = Client_Search( Req->prefix );
280 if( ! from )
282 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
283 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
284 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", TRUE );
285 return DISCONNECTED;
288 /* Neue Client-Struktur anlegen */
289 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
290 if( ! c )
292 /* Neue Client-Struktur konnte nicht angelegt werden */
293 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
294 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
295 return DISCONNECTED;
298 /* Log-Meldung zusammenbauen und ausgeben */
299 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
300 else strcpy( str, "" );
301 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (via %s, %s%d hop%s).", Client_ID( c ), Client_ID( Client ), str, Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
303 /* Andere Server informieren */
304 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
306 return CONNECTED;
308 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
309 } /* IRC_SERVER */
312 GLOBAL BOOLEAN
313 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
315 CHAR nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
316 BOOLEAN is_op, is_voiced;
317 CHANNEL *chan;
318 CLIENT *c;
320 assert( Client != NULL );
321 assert( Req != NULL );
323 /* Falsche Anzahl Parameter? */
324 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
326 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
327 strcpy( nick_out, "" );
329 channame = Req->argv[0];
330 ptr = strtok( nick_in, "," );
331 while( ptr )
333 is_op = is_voiced = FALSE;
335 /* Prefixe abschneiden */
336 while(( *ptr == '@' ) || ( *ptr == '+' ))
338 if( *ptr == '@' ) is_op = TRUE;
339 if( *ptr == '+' ) is_voiced = TRUE;
340 ptr++;
343 c = Client_Search( ptr );
344 if( c )
346 Channel_Join( c, channame );
347 chan = Channel_Search( channame );
348 assert( chan != NULL );
350 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
351 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
353 /* im Channel bekannt machen */
354 IRC_WriteStrChannelPrefix( Client, chan, c, FALSE, "JOIN :%s", channame );
356 /* Channel-User-Modes setzen */
357 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
358 if( modes[0] )
360 /* Modes im Channel bekannt machen */
361 IRC_WriteStrChannelPrefix( Client, chan, Client, FALSE, "MODE %s +%s %s", channame, modes, Client_ID( c ));
364 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
365 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
366 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
367 strlcat( nick_out, ptr, sizeof( nick_out ));
369 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
371 /* naechsten Nick suchen */
372 ptr = strtok( NULL, "," );
375 /* an andere Server weiterleiten */
376 if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
378 return CONNECTED;
379 } /* IRC_NJOIN */
382 GLOBAL BOOLEAN
383 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
385 CLIENT *target;
386 CHAR msg[LINE_LEN + 64];
388 assert( Client != NULL );
389 assert( Req != NULL );
391 /* Falsche Anzahl Parameter? */
392 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
394 Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
396 target = Client_Search( Req->argv[0] );
397 if( ! target )
399 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
400 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
401 return CONNECTED;
404 if( Req->argv[1][0] )
406 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
407 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
409 else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
411 if( Client_Conn( target ) > NONE )
413 /* dieser Server hat die Connection */
414 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], TRUE );
415 else Conn_Close( Client_Conn( target ), msg, NULL, TRUE );
416 return DISCONNECTED;
418 else
420 /* Verbindung hielt anderer Server */
421 Client_Destroy( target, msg, Req->argv[1], FALSE );
422 return CONNECTED;
424 } /* IRC_SQUIT */
427 /* -eof- */