]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-server.c
93ae5ee174bf13ed3b2e6317647f7208a4610087
[ngircd-alex.git] / src / ngircd / irc-server.c
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  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: irc-server.c,v 1.27 2002/12/26 17:04:54 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "resolve.h"
26 #include "conf.h"
27 #include "conn.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "irc-write.h"
31 #include "log.h"
32 #include "messages.h"
33 #include "parse.h"
34 #include "ngircd.h"
35
36 #include "exp.h"
37 #include "irc-server.h"
38
39
40 GLOBAL BOOLEAN
41 IRC_SERVER( CLIENT *Client, REQUEST *Req )
42 {
43         CHAR str[LINE_LEN], *ptr;
44         CLIENT *from, *c, *cl;
45         CL2CHAN *cl2chan;
46         INT max_hops, i;
47         CHANNEL *chan;
48         BOOLEAN ok;
49         CONN_ID con;
50         
51         assert( Client != NULL );
52         assert( Req != NULL );
53
54         /* Fehler liefern, wenn kein lokaler Client */
55         if( Client_Conn( Client ) <= NONE ) return IRC_WriteStrClient( Client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( Client ), Req->command );
56
57         if( Client_Type( Client ) == CLIENT_GOTPASSSERVER )
58         {
59                 /* Verbindung soll als Server-Server-Verbindung registriert werden */
60                 Log( LOG_DEBUG, "Connection %d: got SERVER command (new server link) ...", Client_Conn( Client ));
61
62                 /* Falsche Anzahl Parameter? */
63                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
64
65                 /* Ist dieser Server bei uns konfiguriert? */
66                 for( i = 0; i < Conf_Server_Count; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
67                 if( i >= Conf_Server_Count )
68                 {
69                         /* Server ist nicht konfiguriert! */
70                         Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
71                         Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", TRUE );
72                         return DISCONNECTED;
73                 }
74                 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
75                 {
76                         /* Falsches Passwort */
77                         Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
78                         Conn_Close( Client_Conn( Client ), NULL, "Bad password", TRUE );
79                         return DISCONNECTED;
80                 }
81                 
82                 /* Ist ein Server mit dieser ID bereits registriert? */
83                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
84
85                 /* Server-Strukturen fuellen ;-) */
86                 Client_SetID( Client, Req->argv[0] );
87                 Client_SetHops( Client, 1 );
88                 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
89
90                 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
91                  * selber die Verbindung zu einem anderen Server auf)? */
92                 con = Client_Conn( Client );
93                 if( Client_Token( Client ) != TOKEN_OUTBOUND )
94                 {
95                         /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
96                         ok = TRUE;
97                         if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = FALSE;
98                         else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
99                         if( ! ok )
100                         {
101                                 Conn_Close( con, "Unexpected server behavior!", NULL, FALSE );
102                                 return DISCONNECTED;
103                         }
104                         Client_SetIntroducer( Client, Client );
105                         Client_SetToken( Client, 1 );
106                 }
107                 else
108                 {
109                         /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
110                          * an die Gegenseite uerbermittelt */
111                         Client_SetToken( Client, atoi( Req->argv[1] ));
112                 }
113
114                 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
115
116                 Client_SetType( Client, CLIENT_SERVER );
117                 Conn_SetServer( con, i );
118
119 #ifdef USE_ZLIB
120                 /* Kompression initialisieren, wenn erforderlich */
121                 if( strchr( Client_Flags( Client ), 'Z' ))
122                 {
123                         if( ! Conn_InitZip( con ))
124                         {
125                                 /* Fehler! */
126                                 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, FALSE );
127                                 return DISCONNECTED;
128                         }
129                 }
130 #endif
131
132                 /* maximalen Hop Count ermitteln */
133                 max_hops = 0;
134                 c = Client_First( );
135                 while( c )
136                 {
137                         if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
138                         c = Client_Next( c );
139                 }
140                 
141                 /* Alle bisherigen Server dem neuen Server bekannt machen,
142                  * die bisherigen Server ueber den neuen informierenn */
143                 for( i = 0; i < ( max_hops + 1 ); i++ )
144                 {
145                         c = Client_First( );
146                         while( c )
147                         {
148                                 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
149                                 {
150                                         if( Client_Conn( c ) > NONE )
151                                         {
152                                                 /* Dem gefundenen Server gleich den neuen
153                                                  * Server bekannt machen */
154                                                 if( ! IRC_WriteStrClient( c, "SERVER %s %d %d :%s", Client_ID( Client ), Client_Hops( Client ) + 1, Client_MyToken( Client ), Client_Info( Client ))) return DISCONNECTED;
155                                         }
156                                         
157                                         /* Den neuen Server ueber den alten informieren */
158                                         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;
159                                 }
160                                 c = Client_Next( c );
161                         }
162                 }
163
164                 /* alle User dem neuen Server bekannt machen */
165                 c = Client_First( );
166                 while( c )
167                 {
168                         if( Client_Type( c ) == CLIENT_USER )
169                         {
170                                 /* User an neuen Server melden */
171                                 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;
172                         }
173                         c = Client_Next( c );
174                 }
175
176                 /* Channels dem neuen Server bekannt machen */
177                 chan = Channel_First( );
178                 while( chan )
179                 {
180 #ifdef IRCPLUS
181                         /* Wenn unterstuetzt, CHANINFO senden */
182                         if( strchr( Client_Flags( Client ), 'C' ))
183                         {
184                                 /* CHANINFO senden */
185                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), Channel_Modes( chan ), Channel_Topic( chan ))) return DISCONNECTED;
186                         }
187 #endif
188
189                         /* alle Member suchen */
190                         cl2chan = Channel_FirstMember( chan );
191                         sprintf( str, "NJOIN %s :", Channel_Name( chan ));
192                         while( cl2chan )
193                         {
194                                 cl = Channel_GetClient( cl2chan );
195                                 assert( cl != NULL );
196
197                                 /* Nick, ggf. mit Modes, anhaengen */
198                                 if( str[strlen( str ) - 1] != ':' ) strlcat( str, ",", sizeof( str ));
199                                 if( strchr( Channel_UserModes( chan, cl ), 'v' )) strlcat( str, "+", sizeof( str ));
200                                 if( strchr( Channel_UserModes( chan, cl ), 'o' )) strlcat( str, "@", sizeof( str ));
201                                 strlcat( str, Client_ID( cl ), sizeof( str ));
202
203                                 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
204                                 {
205                                         /* Zeile senden */
206                                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
207                                         sprintf( str, "NJOIN %s :", Channel_Name( chan ));
208                                 }
209                                 
210                                 cl2chan = Channel_NextMember( chan, cl2chan );
211                         }
212
213                         /* noch Daten da? */
214                         if( str[strlen( str ) - 1] != ':')
215                         {
216                                 /* Ja; Also senden ... */
217                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
218                         }
219
220                         /* naechsten Channel suchen */
221                         chan = Channel_Next( chan );
222                 }
223                 
224                 return CONNECTED;
225         }
226         else if( Client_Type( Client ) == CLIENT_SERVER )
227         {
228                 /* Neuer Server wird im Netz angekuendigt */
229
230                 /* Falsche Anzahl Parameter? */
231                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
232
233                 /* Ist ein Server mit dieser ID bereits registriert? */
234                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
235
236                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
237                 ptr = strchr( Req->argv[3] + 2, '[' );
238                 if( ! ptr ) ptr = Req->argv[3];
239
240                 from = Client_Search( Req->prefix );
241                 if( ! from )
242                 {
243                         /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
244                         Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
245                         Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", TRUE );
246                         return DISCONNECTED;
247                 }
248
249                 /* Neue Client-Struktur anlegen */
250                 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, TRUE );
251                 if( ! c )
252                 {
253                         /* Neue Client-Struktur konnte nicht angelegt werden */
254                         Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
255                         Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", TRUE );
256                         return DISCONNECTED;
257                 }
258
259                 /* Log-Meldung zusammenbauen und ausgeben */
260                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) sprintf( str, "connected to %s, ", Client_ID( from ));
261                 else strcpy( str, "" );
262                 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": "" );
263
264                 /* Andere Server informieren */
265                 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
266
267                 return CONNECTED;
268         }
269         else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
270 } /* IRC_SERVER */
271
272
273 GLOBAL BOOLEAN
274 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
275 {
276         CHAR str[COMMAND_LEN], *channame, *ptr, modes[8];
277         BOOLEAN is_op, is_voiced;
278         CHANNEL *chan;
279         CLIENT *c;
280         
281         assert( Client != NULL );
282         assert( Req != NULL );
283
284         /* Falsche Anzahl Parameter? */
285         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
286
287         strlcpy( str, Req->argv[1], sizeof( str ));
288
289         channame = Req->argv[0];
290         ptr = strtok( str, "," );
291         while( ptr )
292         {
293                 is_op = is_voiced = FALSE;
294                 
295                 /* Prefixe abschneiden */
296                 while(( *ptr == '@' ) || ( *ptr == '+' ))
297                 {
298                         if( *ptr == '@' ) is_op = TRUE;
299                         if( *ptr == '+' ) is_voiced = TRUE;
300                         ptr++;
301                 }
302
303                 c = Client_Search( ptr );
304                 if( c )
305                 {
306                         Channel_Join( c, channame );
307                         chan = Channel_Search( channame );
308                         assert( chan != NULL );
309                         
310                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
311                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
312
313                         /* im Channel bekannt machen */
314                         IRC_WriteStrChannelPrefix( Client, chan, c, FALSE, "JOIN :%s", channame );
315
316                         /* Channel-User-Modes setzen */
317                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
318                         if( modes[0] )
319                         {
320                                 /* Modes im Channel bekannt machen */
321                                 IRC_WriteStrChannelPrefix( Client, chan, Client, FALSE, "MODE %s +%s %s", channame, modes, Client_ID( c ));
322                         }
323                 }
324                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
325                 
326                 /* naechsten Nick suchen */
327                 ptr = strtok( NULL, "," );
328         }
329
330         /* an andere Server weiterleiten */
331         IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], Req->argv[1] );
332
333         return CONNECTED;
334 } /* IRC_NJOIN */
335
336
337 GLOBAL BOOLEAN
338 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
339 {
340         CLIENT *target;
341         CHAR msg[LINE_LEN + 64];
342
343         assert( Client != NULL );
344         assert( Req != NULL );
345
346         /* Falsche Anzahl Parameter? */
347         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
348
349         Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
350
351         target = Client_Search( Req->argv[0] );
352         if( ! target )
353         {
354                 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
355                 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
356                 return CONNECTED;
357         }
358
359         if( Req->argv[1][0] )
360         {
361                 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
362                 sprintf( msg, "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
363         }
364         else sprintf( msg, "Got SQUIT from %s.", Client_ID( Client ));
365
366         if( Client_Conn( target ) > NONE )
367         {
368                 /* dieser Server hat die Connection */
369                 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], TRUE );
370                 else Conn_Close( Client_Conn( target ), msg, NULL, TRUE );
371                 return DISCONNECTED;
372         }
373         else
374         {
375                 /* Verbindung hielt anderer Server */
376                 Client_Destroy( target, msg, Req->argv[1], FALSE );
377                 return CONNECTED;
378         }
379 } /* IRC_SQUIT */
380
381
382 /* -eof- */