]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-server.c
Allow pre-defined server local channels ("&").
[ngircd-alex.git] / src / ngircd / irc-server.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2007 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 #include "imp.h"
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23
24 #include "defines.h"
25 #include "resolve.h"
26 #include "conn.h"
27 #include "conn-func.h"
28 #include "conn-zip.h"
29 #include "conf.h"
30 #include "client.h"
31 #include "channel.h"
32 #include "irc-write.h"
33 #include "lists.h"
34 #include "log.h"
35 #include "messages.h"
36 #include "parse.h"
37 #include "numeric.h"
38 #include "ngircd.h"
39 #include "irc-info.h"
40
41 #include "exp.h"
42 #include "irc-server.h"
43
44
45 /**
46  * Handler for the IRC command "SERVER".
47  * See RFC 2813 section 4.1.2.
48  */
49 GLOBAL bool
50 IRC_SERVER( CLIENT *Client, REQUEST *Req )
51 {
52         char str[LINE_LEN], *ptr;
53         CLIENT *from, *c;
54         bool ok;
55         int i;
56         CONN_ID con;
57         
58         assert( Client != NULL );
59         assert( Req != NULL );
60
61         /* Return an error if this is not a local client */
62         if (Client_Conn(Client) <= NONE)
63                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64                                           Client_ID(Client), Req->command);
65
66         if (Client_Type(Client) == CLIENT_GOTPASS ||
67             Client_Type(Client) == CLIENT_GOTPASS_2813) {
68                 /* We got a PASS command from the peer, and now a SERVER
69                  * command: the peer tries to register itself as a server. */
70                 LogDebug("Connection %d: got SERVER command (new server link) ...",
71                         Client_Conn(Client));
72
73                 /* Falsche Anzahl Parameter? */
74                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
75
76                 /* Ist dieser Server bei uns konfiguriert? */
77                 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
78                 if( i >= MAX_SERVERS )
79                 {
80                         /* Server ist nicht konfiguriert! */
81                         Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
82                         Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
83                         return DISCONNECTED;
84                 }
85                 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
86                 {
87                         /* Falsches Passwort */
88                         Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
89                         Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
90                         return DISCONNECTED;
91                 }
92                 
93                 /* Ist ein Server mit dieser ID bereits registriert? */
94                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
95
96                 /* Server-Strukturen fuellen ;-) */
97                 Client_SetID( Client, Req->argv[0] );
98                 Client_SetHops( Client, 1 );
99                 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
100
101                 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
102                  * selber die Verbindung zu einem anderen Server auf)? */
103                 con = Client_Conn( Client );
104                 if( Client_Token( Client ) != TOKEN_OUTBOUND )
105                 {
106                         /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
107                         ok = true;
108                         if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
109                         else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
110                         if( ! ok )
111                         {
112                                 Conn_Close( con, "Unexpected server behavior!", NULL, false );
113                                 return DISCONNECTED;
114                         }
115                         Client_SetIntroducer( Client, Client );
116                         Client_SetToken( Client, 1 );
117                 }
118                 else
119                 {
120                         /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
121                          * an die Gegenseite uerbermittelt */
122                         Client_SetToken( Client, atoi( Req->argv[1] ));
123                 }
124
125                 /* Mark this connection as belonging to an configured server */
126                 Conf_SetServer(i, con);
127
128                 /* Check protocol level */
129                 if (Client_Type(Client) == CLIENT_GOTPASS) {
130                         /* We got a "simple" PASS command, so the peer is
131                          * using the protocol as defined in RFC 1459. */
132                         if (! (Conn_Options(con) & CONN_RFC1459))
133                                 Log(LOG_INFO,
134                                     "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
135                                     con, Client_ID(Client));
136                         Conn_SetOption(con, CONN_RFC1459);
137                 }
138
139                 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
140
141 #ifdef ZLIB
142                 /* Kompression initialisieren, wenn erforderlich */
143                 if( strchr( Client_Flags( Client ), 'Z' ))
144                 {
145                         if( ! Zip_InitConn( con ))
146                         {
147                                 /* Fehler! */
148                                 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
149                                 return DISCONNECTED;
150                         }
151                 }
152 #endif
153
154 #ifdef IRCPLUS
155                 if (strchr(Client_Flags(Client), 'H')) {
156                         LogDebug("Peer supports IRC+ extended server handshake ...");
157                         if (!IRC_Send_ISUPPORT(Client))
158                                 return DISCONNECTED;
159                         return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
160                                                   Client_ID(Client));
161                 } else {
162 #endif
163                         if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
164                                 Log(LOG_CRIT,
165                                     "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
166 #ifdef IRCPLUS
167                 }
168 #endif
169
170                 return IRC_Num_ENDOFMOTD(Client, Req);
171         }
172         else if( Client_Type( Client ) == CLIENT_SERVER )
173         {
174                 /* Neuer Server wird im Netz angekuendigt */
175
176                 /* Falsche Anzahl Parameter? */
177                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
178
179                 /* Ist ein Server mit dieser ID bereits registriert? */
180                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
181
182                 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
183                 ptr = strchr( Req->argv[3] + 2, '[' );
184                 if( ! ptr ) ptr = Req->argv[3];
185
186                 from = Client_Search( Req->prefix );
187                 if( ! from )
188                 {
189                         /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
190                         Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
191                         Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
192                         return DISCONNECTED;
193                 }
194
195                 /* Neue Client-Struktur anlegen */
196                 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
197                 if( ! c )
198                 {
199                         /* Neue Client-Struktur konnte nicht angelegt werden */
200                         Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
201                         Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
202                         return DISCONNECTED;
203                 }
204
205                 /* Log-Meldung zusammenbauen und ausgeben */
206                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
207                 else strcpy( str, "" );
208                 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": "" );
209
210                 /* Andere Server informieren */
211                 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
212
213                 return CONNECTED;
214         } else
215                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
216                                           Client_ID(Client), Req->command);
217 } /* IRC_SERVER */
218
219
220 GLOBAL bool
221 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
222 {
223         char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
224         bool is_op, is_voiced;
225         CHANNEL *chan;
226         CLIENT *c;
227         
228         assert( Client != NULL );
229         assert( Req != NULL );
230
231         /* Falsche Anzahl Parameter? */
232         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
233
234         strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
235         strcpy( nick_out, "" );
236
237         channame = Req->argv[0];
238         ptr = strtok( nick_in, "," );
239         while( ptr )
240         {
241                 is_op = is_voiced = false;
242                 
243                 /* Prefixe abschneiden */
244                 while(( *ptr == '@' ) || ( *ptr == '+' ))
245                 {
246                         if( *ptr == '@' ) is_op = true;
247                         if( *ptr == '+' ) is_voiced = true;
248                         ptr++;
249                 }
250
251                 c = Client_Search( ptr );
252                 if( c )
253                 {
254                         Channel_Join( c, channame );
255                         chan = Channel_Search( channame );
256                         assert( chan != NULL );
257                         
258                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
259                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
260
261                         /* im Channel bekannt machen */
262                         IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
263
264                         /* Channel-User-Modes setzen */
265                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
266                         if( modes[0] )
267                         {
268                                 /* Modes im Channel bekannt machen */
269                                 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
270                         }
271
272                         if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
273                         if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
274                         if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
275                         strlcat( nick_out, ptr, sizeof( nick_out ));
276                 }
277                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
278                 
279                 /* naechsten Nick suchen */
280                 ptr = strtok( NULL, "," );
281         }
282
283         /* an andere Server weiterleiten */
284         if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
285
286         return CONNECTED;
287 } /* IRC_NJOIN */
288
289
290 GLOBAL bool
291 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
292 {
293         CLIENT *target;
294         char msg[LINE_LEN + 64];
295
296         assert( Client != NULL );
297         assert( Req != NULL );
298
299         /* Falsche Anzahl Parameter? */
300         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
301
302         Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
303
304         target = Client_Search( Req->argv[0] );
305         if( ! target )
306         {
307                 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
308                 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
309                 return CONNECTED;
310         }
311
312         if( Req->argv[1][0] )
313         {
314                 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
315                 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
316         }
317         else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
318
319         if( Client_Conn( target ) > NONE )
320         {
321                 /* dieser Server hat die Connection */
322                 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
323                 else Conn_Close( Client_Conn( target ), msg, NULL, true);
324                 return DISCONNECTED;
325         }
326         else
327         {
328                 /* Verbindung hielt anderer Server */
329                 Client_Destroy( target, msg, Req->argv[1], false );
330                 return CONNECTED;
331         }
332 } /* IRC_SQUIT */
333
334
335 /* -eof- */