Don't forward NJOIN's for unknown nick names.
[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.32 2003/04/20 23:09:26 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 "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"
36
37 #include "exp.h"
38 #include "irc-server.h"
39
40
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;
51         
52         assert( Client != NULL );
53         assert( Req != NULL );
54
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 );
57
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 ));
62
63                 /* Falsche Anzahl Parameter? */
64                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
65
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                 }
82                 
83                 /* Ist ein Server mit dieser ID bereits registriert? */
84                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
85
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] );
90
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 )
101                         {
102                                 Conn_Close( con, "Unexpected server behavior!", NULL, FALSE );
103                                 return DISCONNECTED;
104                         }
105                         Client_SetIntroducer( Client, Client );
106                         Client_SetToken( Client, 1 );
107                 }
108                 else
109                 {
110                         /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
111                          * an die Gegenseite uerbermittelt */
112                         Client_SetToken( Client, atoi( Req->argv[1] ));
113                 }
114
115                 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
116
117                 Client_SetType( Client, CLIENT_SERVER );
118                 Conf_SetServer( i, con );
119
120 #ifdef USE_ZLIB
121                 /* Kompression initialisieren, wenn erforderlich */
122                 if( strchr( Client_Flags( Client ), 'Z' ))
123                 {
124                         if( ! Zip_InitConn( con ))
125                         {
126                                 /* Fehler! */
127                                 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, FALSE );
128                                 return DISCONNECTED;
129                         }
130                 }
131 #endif
132
133                 /* maximalen Hop Count ermitteln */
134                 max_hops = 0;
135                 c = Client_First( );
136                 while( c )
137                 {
138                         if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
139                         c = Client_Next( c );
140                 }
141                 
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++ )
145                 {
146                         c = Client_First( );
147                         while( c )
148                         {
149                                 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
150                                 {
151                                         if( Client_Conn( c ) > NONE )
152                                         {
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;
156                                         }
157                                         
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;
160                                 }
161                                 c = Client_Next( c );
162                         }
163                 }
164
165                 /* alle User dem neuen Server bekannt machen */
166                 c = Client_First( );
167                 while( c )
168                 {
169                         if( Client_Type( c ) == CLIENT_USER )
170                         {
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;
173                         }
174                         c = Client_Next( c );
175                 }
176
177                 /* Channels dem neuen Server bekannt machen */
178                 chan = Channel_First( );
179                 while( chan )
180                 {
181 #ifdef IRCPLUS
182                         /* Send CHANINFO if the peer supports it */
183                         if( strchr( Client_Flags( Client ), 'C' ))
184                         {
185                                 modes = Channel_Modes( chan );
186                                 topic = Channel_Topic( chan );
187
188                                 if( *modes || *topic )
189                                 {
190                                         /* send CHANINFO */
191                                         if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )) && ( ! *topic ))
192                                         {
193                                                 /* "CHANINFO <chan> +<modes>" */
194                                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s", Channel_Name( chan ), modes )) return DISCONNECTED;
195                                         }
196                                         else if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )))
197                                         {
198                                                 /* "CHANINFO <chan> +<modes> :<topic>" */
199                                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), modes, topic )) return DISCONNECTED;
200                                         }
201                                         else
202                                         {
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;
205                                         }
206                                 }
207                         }
208 #endif
209
210                         /* alle Member suchen */
211                         cl2chan = Channel_FirstMember( chan );
212                         snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
213                         while( cl2chan )
214                         {
215                                 cl = Channel_GetClient( cl2chan );
216                                 assert( cl != NULL );
217
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 ));
223
224                                 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
225                                 {
226                                         /* Zeile senden */
227                                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
228                                         snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
229                                 }
230                                 
231                                 cl2chan = Channel_NextMember( chan, cl2chan );
232                         }
233
234                         /* noch Daten da? */
235                         if( str[strlen( str ) - 1] != ':')
236                         {
237                                 /* Ja; Also senden ... */
238                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
239                         }
240
241                         /* naechsten Channel suchen */
242                         chan = Channel_Next( chan );
243                 }
244                 
245                 return CONNECTED;
246         }
247         else if( Client_Type( Client ) == CLIENT_SERVER )
248         {
249                 /* Neuer Server wird im Netz angekuendigt */
250
251                 /* Falsche Anzahl Parameter? */
252                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
253
254                 /* Ist ein Server mit dieser ID bereits registriert? */
255                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
256
257                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
258                 ptr = strchr( Req->argv[3] + 2, '[' );
259                 if( ! ptr ) ptr = Req->argv[3];
260
261                 from = Client_Search( Req->prefix );
262                 if( ! from )
263                 {
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;
268                 }
269
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 )
273                 {
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;
278                 }
279
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": "" );
284
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 ));
287
288                 return CONNECTED;
289         }
290         else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
291 } /* IRC_SERVER */
292
293
294 GLOBAL BOOLEAN
295 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
296 {
297         CHAR nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
298         BOOLEAN is_op, is_voiced;
299         CHANNEL *chan;
300         CLIENT *c;
301         
302         assert( Client != NULL );
303         assert( Req != NULL );
304
305         /* Falsche Anzahl Parameter? */
306         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
307
308         strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
309         strcpy( nick_out, "" );
310
311         channame = Req->argv[0];
312         ptr = strtok( nick_in, "," );
313         while( ptr )
314         {
315                 is_op = is_voiced = FALSE;
316                 
317                 /* Prefixe abschneiden */
318                 while(( *ptr == '@' ) || ( *ptr == '+' ))
319                 {
320                         if( *ptr == '@' ) is_op = TRUE;
321                         if( *ptr == '+' ) is_voiced = TRUE;
322                         ptr++;
323                 }
324
325                 c = Client_Search( ptr );
326                 if( c )
327                 {
328                         Channel_Join( c, channame );
329                         chan = Channel_Search( channame );
330                         assert( chan != NULL );
331                         
332                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
333                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
334
335                         /* im Channel bekannt machen */
336                         IRC_WriteStrChannelPrefix( Client, chan, c, FALSE, "JOIN :%s", channame );
337
338                         /* Channel-User-Modes setzen */
339                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
340                         if( modes[0] )
341                         {
342                                 /* Modes im Channel bekannt machen */
343                                 IRC_WriteStrChannelPrefix( Client, chan, Client, FALSE, "MODE %s +%s %s", channame, modes, Client_ID( c ));
344                         }
345
346                         if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
347                         strlcat( nick_out, ptr, sizeof( nick_out ));
348                 }
349                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
350                 
351                 /* naechsten Nick suchen */
352                 ptr = strtok( NULL, "," );
353         }
354
355         /* an andere Server weiterleiten */
356         if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
357
358         return CONNECTED;
359 } /* IRC_NJOIN */
360
361
362 GLOBAL BOOLEAN
363 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
364 {
365         CLIENT *target;
366         CHAR msg[LINE_LEN + 64];
367
368         assert( Client != NULL );
369         assert( Req != NULL );
370
371         /* Falsche Anzahl Parameter? */
372         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
373
374         Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
375
376         target = Client_Search( Req->argv[0] );
377         if( ! target )
378         {
379                 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
380                 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
381                 return CONNECTED;
382         }
383
384         if( Req->argv[1][0] )
385         {
386                 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
387                 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
388         }
389         else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
390
391         if( Client_Conn( target ) > NONE )
392         {
393                 /* dieser Server hat die Connection */
394                 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], TRUE );
395                 else Conn_Close( Client_Conn( target ), msg, NULL, TRUE );
396                 return DISCONNECTED;
397         }
398         else
399         {
400                 /* Verbindung hielt anderer Server */
401                 Client_Destroy( target, msg, Req->argv[1], FALSE );
402                 return CONNECTED;
403         }
404 } /* IRC_SQUIT */
405
406
407 /* -eof- */