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.31 2003/01/08 22:04:43 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "resolve.h"
26 #include "conn.h"
27 #include "conn-zip.h"
28 #include "conf.h"
29 #include "client.h"
30 #include "channel.h"
31 #include "irc-write.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "parse.h"
35 #include "ngircd.h"
37 #include "exp.h"
38 #include "irc-server.h"
41 GLOBAL BOOLEAN
42 IRC_SERVER( CLIENT *Client, REQUEST *Req )
43 {
44 CHAR str[LINE_LEN], *ptr, *modes, *topic;
45 CLIENT *from, *c, *cl;
46 CL2CHAN *cl2chan;
47 INT max_hops, i;
48 CHANNEL *chan;
49 BOOLEAN ok;
50 CONN_ID con;
52 assert( Client != NULL );
53 assert( Req != NULL );
55 /* Fehler liefern, wenn kein lokaler Client */
56 if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
58 if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
59 {
60 /* Verbindung soll als Server-Server-Verbindung registriert werden */
61 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
63 /* Falsche Anzahl Parameter? */
64 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
66 /* Ist dieser Server bei uns konfiguriert? */
67 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
68 if( i >= MAX_SERVERS )
69 {
70 /* Server ist nicht konfiguriert! */
71 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
72 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
73 return DISCONNECTED;
74 }
75 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
76 {
77 /* Falsches Passwort */
78 Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
79 Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
80 return DISCONNECTED;
81 }
83 /* Ist ein Server mit dieser ID bereits registriert? */
84 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
86 /* Server-Strukturen fuellen ;-) */
87 Client_SetID( Client, Req->argv[0] );
88 Client_SetHops( Client, 1 );
89 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
91 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
92 * selber die Verbindung zu einem anderen Server auf)? */
93 con = Client_Conn( Client );
94 if( Client_Token( Client ) != TOKEN_OUTBOUND )
95 {
96 /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
97 ok = TRUE;
98 if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = FALSE;
99 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
100 if( ! ok )
102 Conn_Close( con, "Unexpected server behavior!", NULL, FALSE );
103 return DISCONNECTED;
105 Client_SetIntroducer( Client, Client );
106 Client_SetToken( Client, 1 );
108 else
110 /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
111 * an die Gegenseite uerbermittelt */
112 Client_SetToken( Client, atoi( Req->argv[1] ));
115 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
117 Client_SetType( Client, CLIENT_SERVER );
118 Conf_SetServer( i, con );
120 #ifdef USE_ZLIB
121 /* Kompression initialisieren, wenn erforderlich */
122 if( strchr( Client_Flags( Client ), 'Z' ))
124 if( ! Zip_InitConn( con ))
126 /* Fehler! */
127 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, FALSE );
128 return DISCONNECTED;
131 #endif
133 /* maximalen Hop Count ermitteln */
134 max_hops = 0;
135 c = Client_First( );
136 while( c )
138 if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
139 c = Client_Next( c );
142 /* Alle bisherigen Server dem neuen Server bekannt machen,
143 * die bisherigen Server ueber den neuen informierenn */
144 for( i = 0; i < ( max_hops + 1 ); i++ )
146 c = Client_First( );
147 while( c )
149 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
151 if( Client_Conn( c ) > NONE )
153 /* Dem gefundenen Server gleich den neuen
154 * Server bekannt machen */
155 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
158 /* Den neuen Server ueber den alten informieren */
159 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;
161 c = Client_Next( c );
165 /* alle User dem neuen Server bekannt machen */
166 c = Client_First( );
167 while( c )
169 if( Client_Type( c ) == CLIENT_USER )
171 /* User an neuen Server melden */
172 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;
174 c = Client_Next( c );
177 /* Channels dem neuen Server bekannt machen */
178 chan = Channel_First( );
179 while( chan )
181 #ifdef IRCPLUS
182 /* Send CHANINFO if the peer supports it */
183 if( strchr( Client_Flags( Client ), 'C' ))
185 modes = Channel_Modes( chan );
186 topic = Channel_Topic( chan );
188 if( *modes || *topic )
190 /* send CHANINFO */
191 if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )) && ( ! *topic ))
193 /* "CHANINFO <chan> +<modes>" */
194 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s", Channel_Name( chan ), modes )) return DISCONNECTED;
196 else if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )))
198 /* "CHANINFO <chan> +<modes> :<topic>" */
199 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), modes, topic )) return DISCONNECTED;
201 else
203 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
204 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;
208 #endif
210 /* alle Member suchen */
211 cl2chan = Channel_FirstMember( chan );
212 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
213 while( cl2chan )
215 cl = Channel_GetClient( cl2chan );
216 assert( cl != NULL );
218 /* Nick, ggf. mit Modes, anhaengen */
219 if( str[strlen( str ) - 1] != ':' ) strlcat( str, ",", sizeof( str ));
220 if( strchr( Channel_UserModes( chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
221 if( strchr( Channel_UserModes( chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
222 strlcat( str, Client_ID( cl ), sizeof( str ));
224 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
226 /* Zeile senden */
227 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
228 snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
231 cl2chan = Channel_NextMember( chan, cl2chan );
234 /* noch Daten da? */
235 if( str[strlen( str ) - 1] != ':')
237 /* Ja; Also senden ... */
238 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
241 /* naechsten Channel suchen */
242 chan = Channel_Next( chan );
245 return CONNECTED;
247 else if( Client_Type( Client ) == CLIENT_SERVER )
249 /* Neuer Server wird im Netz angekuendigt */
251 /* Falsche Anzahl Parameter? */
252 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
254 /* Ist ein Server mit dieser ID bereits registriert? */
255 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
257 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
258 ptr = strchr( Req->argv[3] + 2, '[' );
259 if( ! ptr ) ptr = Req->argv[3];
261 from = Client_Search( Req->prefix );
262 if( ! from )
264 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
265 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
266 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", TRUE );
267 return DISCONNECTED;
270 /* Neue Client-Struktur anlegen */
271 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
272 if( ! c )
274 /* Neue Client-Struktur konnte nicht angelegt werden */
275 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
276 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
277 return DISCONNECTED;
280 /* Log-Meldung zusammenbauen und ausgeben */
281 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
282 else strcpy( str, "" );
283 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": "" );
285 /* Andere Server informieren */
286 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
288 return CONNECTED;
290 else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
291 } /* IRC_SERVER */
294 GLOBAL BOOLEAN
295 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
297 CHAR str[COMMAND_LEN], *channame, *ptr, modes[8];
298 BOOLEAN is_op, is_voiced;
299 CHANNEL *chan;
300 CLIENT *c;
302 assert( Client != NULL );
303 assert( Req != NULL );
305 /* Falsche Anzahl Parameter? */
306 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
308 strlcpy( str, Req->argv[1], sizeof( str ));
310 channame = Req->argv[0];
311 ptr = strtok( str, "," );
312 while( ptr )
314 is_op = is_voiced = FALSE;
316 /* Prefixe abschneiden */
317 while(( *ptr == '@' ) || ( *ptr == '+' ))
319 if( *ptr == '@' ) is_op = TRUE;
320 if( *ptr == '+' ) is_voiced = TRUE;
321 ptr++;
324 c = Client_Search( ptr );
325 if( c )
327 Channel_Join( c, channame );
328 chan = Channel_Search( channame );
329 assert( chan != NULL );
331 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
332 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
334 /* im Channel bekannt machen */
335 IRC_WriteStrChannelPrefix( Client, chan, c, FALSE, "JOIN :%s", channame );
337 /* Channel-User-Modes setzen */
338 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
339 if( modes[0] )
341 /* Modes im Channel bekannt machen */
342 IRC_WriteStrChannelPrefix( Client, chan, Client, FALSE, "MODE %s +%s %s", channame, modes, Client_ID( c ));
345 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
347 /* naechsten Nick suchen */
348 ptr = strtok( NULL, "," );
351 /* an andere Server weiterleiten */
352 IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], Req->argv[1] );
354 return CONNECTED;
355 } /* IRC_NJOIN */
358 GLOBAL BOOLEAN
359 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
361 CLIENT *target;
362 CHAR msg[LINE_LEN + 64];
364 assert( Client != NULL );
365 assert( Req != NULL );
367 /* Falsche Anzahl Parameter? */
368 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
370 Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
372 target = Client_Search( Req->argv[0] );
373 if( ! target )
375 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
376 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
377 return CONNECTED;
380 if( Req->argv[1][0] )
382 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
383 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
385 else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
387 if( Client_Conn( target ) > NONE )
389 /* dieser Server hat die Connection */
390 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], TRUE );
391 else Conn_Close( Client_Conn( target ), msg, NULL, TRUE );
392 return DISCONNECTED;
394 else
396 /* Verbindung hielt anderer Server */
397 Client_Destroy( target, msg, Req->argv[1], FALSE );
398 return CONNECTED;
400 } /* IRC_SQUIT */
403 /* -eof- */