]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-server.c
moved invite/ban lists to channel structure
[ngircd-alex.git] / src / ngircd / irc-server.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2006 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.43 2006/12/07 17:57:20 fw Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
25
26 #include "defines.h"
27 #include "resolve.h"
28 #include "conn.h"
29 #include "conn-zip.h"
30 #include "conf.h"
31 #include "client.h"
32 #include "channel.h"
33 #include "irc-write.h"
34 #include "lists.h"
35 #include "log.h"
36 #include "messages.h"
37 #include "parse.h"
38 #include "ngircd.h"
39
40 #include "exp.h"
41 #include "irc-server.h"
42
43
44 #ifdef IRCPLUS
45 static bool
46 Synchronize_Lists( CLIENT *Client )
47 {
48         CHANNEL *c;
49         struct list_head *head;
50         struct list_elem *elem;
51
52         assert( Client != NULL );
53
54         c = Channel_First();
55
56         while (c) {
57                 head = Channel_GetListBans(c);
58
59                 elem = Lists_GetFirst(head);
60                 while (elem) {
61                         if( ! IRC_WriteStrClient( Client, "MODE %s +b %s",
62                                         Channel_Name(c), Lists_GetMask(elem)))
63                         {
64                                 return false;
65                         }
66                         elem = Lists_GetNext(elem);
67                 }
68
69                 head = Channel_GetListInvites(c);
70                 elem = Lists_GetFirst(head);
71                 while (elem) {
72                         if( ! IRC_WriteStrClient( Client, "MODE %s +I %s",
73                                         Channel_Name( c ), Lists_GetMask(elem)))
74                         {
75                                 return false;
76                         }
77                         elem = Lists_GetNext(elem);
78                 }
79                 c = Channel_Next(c);
80         }
81         return true;
82 }
83 #endif
84
85
86
87
88 /**
89  * Handler for the IRC command "SERVER".
90  * See RFC 2813 section 4.1.2.
91  */
92 GLOBAL bool
93 IRC_SERVER( CLIENT *Client, REQUEST *Req )
94 {
95         char str[LINE_LEN], *ptr, *modes, *topic;
96         CLIENT *from, *c, *cl;
97         CL2CHAN *cl2chan;
98         int max_hops, i;
99         CHANNEL *chan;
100         bool ok;
101         CONN_ID con;
102         
103         assert( Client != NULL );
104         assert( Req != NULL );
105
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);
110
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));
116
117                 /* Falsche Anzahl Parameter? */
118                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
119
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 )
123                 {
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);
127                         return DISCONNECTED;
128                 }
129                 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
130                 {
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);
134                         return DISCONNECTED;
135                 }
136                 
137                 /* Ist ein Server mit dieser ID bereits registriert? */
138                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
139
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] );
144
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 )
149                 {
150                         /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
151                         ok = true;
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 );
154                         if( ! ok )
155                         {
156                                 Conn_Close( con, "Unexpected server behavior!", NULL, false );
157                                 return DISCONNECTED;
158                         }
159                         Client_SetIntroducer( Client, Client );
160                         Client_SetToken( Client, 1 );
161                 }
162                 else
163                 {
164                         /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
165                          * an die Gegenseite uerbermittelt */
166                         Client_SetToken( Client, atoi( Req->argv[1] ));
167                 }
168
169                 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (connection %d, 1 hop - direct link).", Client_ID( Client ), con );
170
171                 Client_SetType( Client, CLIENT_SERVER );
172                 Conf_SetServer( i, con );
173
174 #ifdef ZLIB
175                 /* Kompression initialisieren, wenn erforderlich */
176                 if( strchr( Client_Flags( Client ), 'Z' ))
177                 {
178                         if( ! Zip_InitConn( con ))
179                         {
180                                 /* Fehler! */
181                                 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
182                                 return DISCONNECTED;
183                         }
184                 }
185 #endif
186
187                 /* maximalen Hop Count ermitteln */
188                 max_hops = 0;
189                 c = Client_First( );
190                 while( c )
191                 {
192                         if( Client_Hops( c ) > max_hops ) max_hops = Client_Hops( c );
193                         c = Client_Next( c );
194                 }
195                 
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++ )
199                 {
200                         c = Client_First( );
201                         while( c )
202                         {
203                                 if(( Client_Type( c ) == CLIENT_SERVER ) && ( c != Client ) && ( c != Client_ThisServer( )) && ( Client_Hops( c ) == i ))
204                                 {
205                                         if( Client_Conn( c ) > NONE )
206                                         {
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;
210                                         }
211                                         
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;
214                                 }
215                                 c = Client_Next( c );
216                         }
217                 }
218
219                 /* alle User dem neuen Server bekannt machen */
220                 c = Client_First( );
221                 while( c )
222                 {
223                         if( Client_Type( c ) == CLIENT_USER )
224                         {
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;
227                         }
228                         c = Client_Next( c );
229                 }
230
231                 /* Channels dem neuen Server bekannt machen */
232                 chan = Channel_First( );
233                 while( chan )
234                 {
235 #ifdef IRCPLUS
236                         /* Send CHANINFO if the peer supports it */
237                         if( strchr( Client_Flags( Client ), 'C' ))
238                         {
239 #ifdef DEBUG
240                                 Log( LOG_DEBUG, "Sending CHANINFO commands ..." );
241 #endif
242                                 modes = Channel_Modes( chan );
243                                 topic = Channel_Topic( chan );
244
245                                 if( *modes || *topic )
246                                 {
247                                         /* send CHANINFO */
248                                         if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )) && ( ! *topic ))
249                                         {
250                                                 /* "CHANINFO <chan> +<modes>" */
251                                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s", Channel_Name( chan ), modes )) return DISCONNECTED;
252                                         }
253                                         else if(( ! strchr( Channel_Modes( chan ), 'k' )) && ( ! strchr( Channel_Modes( chan ), 'l' )))
254                                         {
255                                                 /* "CHANINFO <chan> +<modes> :<topic>" */
256                                                 if( ! IRC_WriteStrClient( Client, "CHANINFO %s +%s :%s", Channel_Name( chan ), modes, topic )) return DISCONNECTED;
257                                         }
258                                         else
259                                         {
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 ) : 0UL, topic ))
265                                                 {
266                                                         return DISCONNECTED;
267                                                 }
268                                         }
269                                 }
270                         }
271 #endif
272
273                         /* alle Member suchen */
274                         cl2chan = Channel_FirstMember( chan );
275                         snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
276                         while( cl2chan )
277                         {
278                                 cl = Channel_GetClient( cl2chan );
279                                 assert( cl != NULL );
280
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 ));
286
287                                 if( strlen( str ) > ( LINE_LEN - CLIENT_NICK_LEN - 8 ))
288                                 {
289                                         /* Zeile senden */
290                                         if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
291                                         snprintf( str, sizeof( str ), "NJOIN %s :", Channel_Name( chan ));
292                                 }
293                                 
294                                 cl2chan = Channel_NextMember( chan, cl2chan );
295                         }
296
297                         /* noch Daten da? */
298                         if( str[strlen( str ) - 1] != ':')
299                         {
300                                 /* Ja; Also senden ... */
301                                 if( ! IRC_WriteStrClient( Client, "%s", str )) return DISCONNECTED;
302                         }
303
304                         /* Get next channel ... */
305                         chan = Channel_Next(chan);
306                 }
307
308 #ifdef IRCPLUS
309                 if (strchr(Client_Flags(Client), 'L')) {
310 #ifdef DEBUG
311                         Log(LOG_DEBUG,
312                             "Synchronizing INVITE- and BAN-lists ...");
313 #endif
314                         /* Synchronize INVITE- and BAN-lists */
315                         if (!Synchronize_Lists(Client))
316                                 return DISCONNECTED;
317                 }
318 #endif
319
320                 return CONNECTED;
321         }
322         else if( Client_Type( Client ) == CLIENT_SERVER )
323         {
324                 /* Neuer Server wird im Netz angekuendigt */
325
326                 /* Falsche Anzahl Parameter? */
327                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
328
329                 /* Ist ein Server mit dieser ID bereits registriert? */
330                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
331
332                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
333                 ptr = strchr( Req->argv[3] + 2, '[' );
334                 if( ! ptr ) ptr = Req->argv[3];
335
336                 from = Client_Search( Req->prefix );
337                 if( ! from )
338                 {
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);
342                         return DISCONNECTED;
343                 }
344
345                 /* Neue Client-Struktur anlegen */
346                 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
347                 if( ! c )
348                 {
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);
352                         return DISCONNECTED;
353                 }
354
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": "" );
359
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 ));
362
363                 return CONNECTED;
364         }
365         else return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
366 } /* IRC_SERVER */
367
368
369 GLOBAL bool
370 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
371 {
372         char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
373         bool is_op, is_voiced;
374         CHANNEL *chan;
375         CLIENT *c;
376         
377         assert( Client != NULL );
378         assert( Req != NULL );
379
380         /* Falsche Anzahl Parameter? */
381         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
382
383         strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
384         strcpy( nick_out, "" );
385
386         channame = Req->argv[0];
387         ptr = strtok( nick_in, "," );
388         while( ptr )
389         {
390                 is_op = is_voiced = false;
391                 
392                 /* Prefixe abschneiden */
393                 while(( *ptr == '@' ) || ( *ptr == '+' ))
394                 {
395                         if( *ptr == '@' ) is_op = true;
396                         if( *ptr == '+' ) is_voiced = true;
397                         ptr++;
398                 }
399
400                 c = Client_Search( ptr );
401                 if( c )
402                 {
403                         Channel_Join( c, channame );
404                         chan = Channel_Search( channame );
405                         assert( chan != NULL );
406                         
407                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
408                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
409
410                         /* im Channel bekannt machen */
411                         IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
412
413                         /* Channel-User-Modes setzen */
414                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
415                         if( modes[0] )
416                         {
417                                 /* Modes im Channel bekannt machen */
418                                 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
419                         }
420
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 ));
425                 }
426                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
427                 
428                 /* naechsten Nick suchen */
429                 ptr = strtok( NULL, "," );
430         }
431
432         /* an andere Server weiterleiten */
433         if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
434
435         return CONNECTED;
436 } /* IRC_NJOIN */
437
438
439 GLOBAL bool
440 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
441 {
442         CLIENT *target;
443         char msg[LINE_LEN + 64];
444
445         assert( Client != NULL );
446         assert( Req != NULL );
447
448         /* Falsche Anzahl Parameter? */
449         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
450
451         Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
452
453         target = Client_Search( Req->argv[0] );
454         if( ! target )
455         {
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] );
458                 return CONNECTED;
459         }
460
461         if( Req->argv[1][0] )
462         {
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 ));
465         }
466         else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
467
468         if( Client_Conn( target ) > NONE )
469         {
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);
473                 return DISCONNECTED;
474         }
475         else
476         {
477                 /* Verbindung hielt anderer Server */
478                 Client_Destroy( target, msg, Req->argv[1], false );
479                 return CONNECTED;
480         }
481 } /* IRC_SQUIT */
482
483
484 /* -eof- */