2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2007 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.45 2007/11/20 20:02:41 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);
87 * Handler for the IRC command "SERVER".
88 * See RFC 2813 section 4.1.2.
91 IRC_SERVER( CLIENT *Client, REQUEST *Req )
93 char str[LINE_LEN], *ptr, *modes, *topic;
94 CLIENT *from, *c, *cl;
101 assert( Client != NULL );
102 assert( Req != NULL );
104 /* Return an error if this is not a local client */
105 if (Client_Conn(Client) <= NONE)
106 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
107 Client_ID(Client), Req->command);
109 if (Client_Type(Client) == CLIENT_GOTPASS) {
110 /* We got a PASS command from the peer, and now a SERVER
111 * command: the peer tries to register itself as a server. */
112 LogDebug("Connection %d: got SERVER command (new server link) ...",
113 Client_Conn(Client));
115 /* Falsche Anzahl Parameter? */
116 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
118 /* Ist dieser Server bei uns konfiguriert? */
119 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
120 if( i >= MAX_SERVERS )
122 /* Server ist nicht konfiguriert! */
123 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
124 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
127 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
129 /* Falsches Passwort */
130 Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
131 Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
135 /* Ist ein Server mit dieser ID bereits registriert? */
136 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
138 /* Server-Strukturen fuellen ;-) */
139 Client_SetID( Client, Req->argv[0] );
140 Client_SetHops( Client, 1 );
141 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
143 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
144 * selber die Verbindung zu einem anderen Server auf)? */
145 con = Client_Conn( Client );
146 if( Client_Token( Client ) != TOKEN_OUTBOUND )
148 /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
150 if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
151 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
154 Conn_Close( con, "Unexpected server behavior!", NULL, false );
157 Client_SetIntroducer( Client, Client );
158 Client_SetToken( Client, 1 );
162 /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
163 * an die Gegenseite uerbermittelt */
164 Client_SetToken( Client, atoi( Req->argv[1] ));
167 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
169 Client_SetType( Client, CLIENT_SERVER );
170 Conf_SetServer( i, con );
173 /* Kompression initialisieren, wenn erforderlich */
174 if( strchr( Client_Flags( Client ), 'Z' ))
176 if( ! Zip_InitConn( con ))
179 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
185 /* maximalen Hop Count ermitteln */
190 if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
191 c = Client_Next( c );
194 /* Alle bisherigen Server dem neuen Server bekannt machen,
195 * die bisherigen Server ueber den neuen informierenn */
196 for( i = 0; i < ( max_hops + 1 ); i++ )
201 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
203 if( Client_Conn( c ) > NONE )
205 /* Dem gefundenen Server gleich den neuen
206 * Server bekannt machen */
207 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
210 /* Inform the new server about this one */
211 if (! IRC_WriteStrClientPrefix(Client,
212 Client_Hops(c) == 1 ? Client_ThisServer() : Client_TopServer(c),
213 "SERVER %s %d %d :%s",
214 Client_ID(c), Client_Hops(c) + 1,
215 Client_MyToken(c), Client_Info(c)))
218 c = Client_Next( c );
222 /* alle User dem neuen Server bekannt machen */
226 if( Client_Type( c ) == CLIENT_USER )
228 /* User an neuen Server melden */
229 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;
231 c = Client_Next( c );
234 /* Channels dem neuen Server bekannt machen */
235 chan = Channel_First( );
239 /* Send CHANINFO if the peer supports it */
240 if( strchr( Client_Flags( Client ), 'C' ))
243 Log( LOG_DEBUG, "Sending CHANINFO commands ..." );
245 modes = Channel_Modes( chan );
246 topic = Channel_Topic( chan );
248 if( *modes || *topic )
251 if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )) && ( ! *topic ))
253 /* "CHANINFO <chan> +<modes>" */
254 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s", Channel_Name( chan ), modes )) return DISCONNECTED;
256 else if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )))
258 /* "CHANINFO <chan> +<modes> :<topic>" */
259 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), modes, topic )) return DISCONNECTED;
263 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
264 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s %s %lu :%s",
265 Channel_Name( chan ), modes,
266 strchr( Channel_Modes( chan ), 'k' ) ? Channel_Key( chan ) : "*",
267 strchr( Channel_Modes( chan ), 'l' ) ? Channel_MaxUsers( chan ) : 0, topic ))
276 /* alle Member suchen */
277 cl2chan = Channel_FirstMember( chan );
278 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
281 cl = Channel_GetClient( cl2chan );
282 assert( cl != NULL );
284 /* Nick, ggf. mit Modes, anhaengen */
285 if( str[strlen( str ) - 1] != ':' ) strlcat( str, ",", sizeof( str ));
286 if( strchr( Channel_UserModes( chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
287 if( strchr( Channel_UserModes( chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
288 strlcat( str, Client_ID( cl ), sizeof( str ));
290 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
293 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
294 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
297 cl2chan = Channel_NextMember( chan, cl2chan );
301 if( str[strlen( str ) - 1] != ':')
303 /* Ja; Also senden ... */
304 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
307 /* Get next channel ... */
308 chan = Channel_Next(chan);
312 if (strchr(Client_Flags(Client), 'L')) {
315 "Synchronizing INVITE- and BAN-lists ...");
317 /* Synchronize INVITE- and BAN-lists */
318 if (!Synchronize_Lists(Client))
325 else if( Client_Type( Client ) == CLIENT_SERVER )
327 /* Neuer Server wird im Netz angekuendigt */
329 /* Falsche Anzahl Parameter? */
330 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
332 /* Ist ein Server mit dieser ID bereits registriert? */
333 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
335 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
336 ptr = strchr( Req->argv[3] + 2, '[' );
337 if( ! ptr ) ptr = Req->argv[3];
339 from = Client_Search( Req->prefix );
342 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
343 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
344 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
348 /* Neue Client-Struktur anlegen */
349 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
352 /* Neue Client-Struktur konnte nicht angelegt werden */
353 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
354 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
358 /* Log-Meldung zusammenbauen und ausgeben */
359 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
360 else strcpy( str, "" );
361 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": "" );
363 /* Andere Server informieren */
364 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
368 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
373 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
375 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
376 bool is_op, is_voiced;
380 assert( Client != NULL );
381 assert( Req != NULL );
383 /* Falsche Anzahl Parameter? */
384 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
386 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
387 strcpy( nick_out, "" );
389 channame = Req->argv[0];
390 ptr = strtok( nick_in, "," );
393 is_op = is_voiced = false;
395 /* Prefixe abschneiden */
396 while(( *ptr == '@' ) || ( *ptr == '+' ))
398 if( *ptr == '@' ) is_op = true;
399 if( *ptr == '+' ) is_voiced = true;
403 c = Client_Search( ptr );
406 Channel_Join( c, channame );
407 chan = Channel_Search( channame );
408 assert( chan != NULL );
410 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
411 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
413 /* im Channel bekannt machen */
414 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
416 /* Channel-User-Modes setzen */
417 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
420 /* Modes im Channel bekannt machen */
421 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
424 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
425 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
426 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
427 strlcat( nick_out, ptr, sizeof( nick_out ));
429 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
431 /* naechsten Nick suchen */
432 ptr = strtok( NULL, "," );
435 /* an andere Server weiterleiten */
436 if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
443 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
446 char msg[LINE_LEN + 64];
448 assert( Client != NULL );
449 assert( Req != NULL );
451 /* Falsche Anzahl Parameter? */
452 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
454 Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
456 target = Client_Search( Req->argv[0] );
459 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
460 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
464 if( Req->argv[1][0] )
466 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
467 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
469 else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
471 if( Client_Conn( target ) > NONE )
473 /* dieser Server hat die Connection */
474 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
475 else Conn_Close( Client_Conn( target ), msg, NULL, true);
480 /* Verbindung hielt anderer Server */
481 Client_Destroy( target, msg, Req->argv[1], false );