2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2006 Alexander Barton (alex@barton.de)
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.
11 * IRC commands for server links
17 static char UNUSED id[] = "$Id: irc-server.c,v 1.44 2007/11/18 15:05:35 alex Exp $";
33 #include "irc-write.h"
41 #include "irc-server.h"
46 Synchronize_Lists( CLIENT *Client )
49 struct list_head *head;
50 struct list_elem *elem;
52 assert( Client != NULL );
57 head = Channel_GetListBans(c);
59 elem = Lists_GetFirst(head);
61 if( ! IRC_WriteStrClient( Client, "MODE %s +b %s",
62 Channel_Name(c), Lists_GetMask(elem)))
66 elem = Lists_GetNext(elem);
69 head = Channel_GetListInvites(c);
70 elem = Lists_GetFirst(head);
72 if( ! IRC_WriteStrClient( Client, "MODE %s +I %s",
73 Channel_Name( c ), Lists_GetMask(elem)))
77 elem = Lists_GetNext(elem);
89 * Handler for the IRC command "SERVER".
90 * See RFC 2813 section 4.1.2.
93 IRC_SERVER( CLIENT *Client, REQUEST *Req )
95 char str[LINE_LEN], *ptr, *modes, *topic;
96 CLIENT *from, *c, *cl;
103 assert( Client != NULL );
104 assert( Req != NULL );
106 /* Return an error if this is not a local client */
107 if (Client_Conn(Client) <= NONE)
108 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
109 Client_ID(Client), Req->command);
111 if (Client_Type(Client) == CLIENT_GOTPASS) {
112 /* We got a PASS command from the peer, and now a SERVER
113 * command: the peer tries to register itself as a server. */
114 LogDebug("Connection %d: got SERVER command (new server link) ...",
115 Client_Conn(Client));
117 /* Falsche Anzahl Parameter? */
118 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
120 /* Ist dieser Server bei uns konfiguriert? */
121 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
122 if( i >= MAX_SERVERS )
124 /* Server ist nicht konfiguriert! */
125 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
126 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
129 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
131 /* Falsches Passwort */
132 Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
133 Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
137 /* Ist ein Server mit dieser ID bereits registriert? */
138 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
140 /* Server-Strukturen fuellen ;-) */
141 Client_SetID( Client, Req->argv[0] );
142 Client_SetHops( Client, 1 );
143 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
145 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
146 * selber die Verbindung zu einem anderen Server auf)? */
147 con = Client_Conn( Client );
148 if( Client_Token( Client ) != TOKEN_OUTBOUND )
150 /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
152 if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
153 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
156 Conn_Close( con, "Unexpected server behavior!", NULL, false );
159 Client_SetIntroducer( Client, Client );
160 Client_SetToken( Client, 1 );
164 /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
165 * an die Gegenseite uerbermittelt */
166 Client_SetToken( Client, atoi( Req->argv[1] ));
169 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
171 Client_SetType( Client, CLIENT_SERVER );
172 Conf_SetServer( i, con );
175 /* Kompression initialisieren, wenn erforderlich */
176 if( strchr( Client_Flags( Client ), 'Z' ))
178 if( ! Zip_InitConn( con ))
181 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
187 /* maximalen Hop Count ermitteln */
192 if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
193 c = Client_Next( c );
196 /* Alle bisherigen Server dem neuen Server bekannt machen,
197 * die bisherigen Server ueber den neuen informierenn */
198 for( i = 0; i < ( max_hops + 1 ); i++ )
203 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
205 if( Client_Conn( c ) > NONE )
207 /* Dem gefundenen Server gleich den neuen
208 * Server bekannt machen */
209 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
212 /* Den neuen Server ueber den alten informieren */
213 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;
215 c = Client_Next( c );
219 /* alle User dem neuen Server bekannt machen */
223 if( Client_Type( c ) == CLIENT_USER )
225 /* User an neuen Server melden */
226 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;
228 c = Client_Next( c );
231 /* Channels dem neuen Server bekannt machen */
232 chan = Channel_First( );
236 /* Send CHANINFO if the peer supports it */
237 if( strchr( Client_Flags( Client ), 'C' ))
240 Log( LOG_DEBUG, "Sending CHANINFO commands ..." );
242 modes = Channel_Modes( chan );
243 topic = Channel_Topic( chan );
245 if( *modes || *topic )
248 if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )) && ( ! *topic ))
250 /* "CHANINFO <chan> +<modes>" */
251 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s", Channel_Name( chan ), modes )) return DISCONNECTED;
253 else if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )))
255 /* "CHANINFO <chan> +<modes> :<topic>" */
256 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), modes, topic )) return DISCONNECTED;
260 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
261 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s %s %lu :%s",
262 Channel_Name( chan ), modes,
263 strchr( Channel_Modes( chan ), 'k' ) ? Channel_Key( chan ) : "*",
264 strchr( Channel_Modes( chan ), 'l' ) ? Channel_MaxUsers( chan ) : 0, topic ))
273 /* alle Member suchen */
274 cl2chan = Channel_FirstMember( chan );
275 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
278 cl = Channel_GetClient( cl2chan );
279 assert( cl != NULL );
281 /* Nick, ggf. mit Modes, anhaengen */
282 if( str[strlen( str ) - 1] != ':' ) strlcat( str, ",", sizeof( str ));
283 if( strchr( Channel_UserModes( chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
284 if( strchr( Channel_UserModes( chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
285 strlcat( str, Client_ID( cl ), sizeof( str ));
287 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
290 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
291 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
294 cl2chan = Channel_NextMember( chan, cl2chan );
298 if( str[strlen( str ) - 1] != ':')
300 /* Ja; Also senden ... */
301 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
304 /* Get next channel ... */
305 chan = Channel_Next(chan);
309 if (strchr(Client_Flags(Client), 'L')) {
312 "Synchronizing INVITE- and BAN-lists ...");
314 /* Synchronize INVITE- and BAN-lists */
315 if (!Synchronize_Lists(Client))
322 else if( Client_Type( Client ) == CLIENT_SERVER )
324 /* Neuer Server wird im Netz angekuendigt */
326 /* Falsche Anzahl Parameter? */
327 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
329 /* Ist ein Server mit dieser ID bereits registriert? */
330 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
332 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
333 ptr = strchr( Req->argv[3] + 2, '[' );
334 if( ! ptr ) ptr = Req->argv[3];
336 from = Client_Search( Req->prefix );
339 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
340 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
341 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
345 /* Neue Client-Struktur anlegen */
346 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
349 /* Neue Client-Struktur konnte nicht angelegt werden */
350 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
351 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
355 /* Log-Meldung zusammenbauen und ausgeben */
356 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
357 else strcpy( str, "" );
358 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": "" );
360 /* Andere Server informieren */
361 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
365 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
370 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
372 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
373 bool is_op, is_voiced;
377 assert( Client != NULL );
378 assert( Req != NULL );
380 /* Falsche Anzahl Parameter? */
381 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
383 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
384 strcpy( nick_out, "" );
386 channame = Req->argv[0];
387 ptr = strtok( nick_in, "," );
390 is_op = is_voiced = false;
392 /* Prefixe abschneiden */
393 while(( *ptr == '@' ) || ( *ptr == '+' ))
395 if( *ptr == '@' ) is_op = true;
396 if( *ptr == '+' ) is_voiced = true;
400 c = Client_Search( ptr );
403 Channel_Join( c, channame );
404 chan = Channel_Search( channame );
405 assert( chan != NULL );
407 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
408 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
410 /* im Channel bekannt machen */
411 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
413 /* Channel-User-Modes setzen */
414 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
417 /* Modes im Channel bekannt machen */
418 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
421 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
422 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
423 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
424 strlcat( nick_out, ptr, sizeof( nick_out ));
426 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
428 /* naechsten Nick suchen */
429 ptr = strtok( NULL, "," );
432 /* an andere Server weiterleiten */
433 if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
440 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
443 char msg[LINE_LEN + 64];
445 assert( Client != NULL );
446 assert( Req != NULL );
448 /* Falsche Anzahl Parameter? */
449 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
451 Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
453 target = Client_Search( Req->argv[0] );
456 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
457 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
461 if( Req->argv[1][0] )
463 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
464 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
466 else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
468 if( Client_Conn( target ) > NONE )
470 /* dieser Server hat die Connection */
471 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
472 else Conn_Close( Client_Conn( target ), msg, NULL, true);
477 /* Verbindung hielt anderer Server */
478 Client_Destroy( target, msg, Req->argv[1], false );