]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-server.c
remove or translate old comments
[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                 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
74
75                 /* Ist this server configured on out side? */
76                 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
77                 if( i >= MAX_SERVERS )
78                 {
79                         Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
80                         Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
81                         return DISCONNECTED;
82                 }
83                 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
84                 {
85                         /* wrong password */
86                         Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
87                         Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
88                         return DISCONNECTED;
89                 }
90                 
91                 /* Is there a registered server with this ID? */
92                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
93
94                 Client_SetID( Client, Req->argv[0] );
95                 Client_SetHops( Client, 1 );
96                 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
97
98                 /* Is this server registering on our side, or are we connecting to
99                  * a remote server? */
100                 con = Client_Conn( Client );
101                 if( Client_Token( Client ) != TOKEN_OUTBOUND )
102                 {
103                         /* Incoming connection, send user/pass */
104                         ok = true;
105                         if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
106                         else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
107                         if( ! ok )
108                         {
109                                 Conn_Close( con, "Unexpected server behavior!", NULL, false );
110                                 return DISCONNECTED;
111                         }
112                         Client_SetIntroducer( Client, Client );
113                         Client_SetToken( Client, 1 );
114                 }
115                 else
116                 {
117                         /* outgoing connect, we already sent SERVER and PASS to the peer */
118                         Client_SetToken( Client, atoi( Req->argv[1] ));
119                 }
120
121                 /* Mark this connection as belonging to an configured server */
122                 Conf_SetServer(i, con);
123
124                 /* Check protocol level */
125                 if (Client_Type(Client) == CLIENT_GOTPASS) {
126                         /* We got a "simple" PASS command, so the peer is
127                          * using the protocol as defined in RFC 1459. */
128                         if (! (Conn_Options(con) & CONN_RFC1459))
129                                 Log(LOG_INFO,
130                                     "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
131                                     con, Client_ID(Client));
132                         Conn_SetOption(con, CONN_RFC1459);
133                 }
134
135                 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
136
137 #ifdef ZLIB
138                 if (strchr(Client_Flags(Client), 'Z') && !Zip_InitConn(con)) {
139                         Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
140                         return DISCONNECTED;
141                 }
142 #endif
143
144 #ifdef IRCPLUS
145                 if (strchr(Client_Flags(Client), 'H')) {
146                         LogDebug("Peer supports IRC+ extended server handshake ...");
147                         if (!IRC_Send_ISUPPORT(Client))
148                                 return DISCONNECTED;
149                         return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
150                                                   Client_ID(Client));
151                 } else {
152 #endif
153                         if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
154                                 Log(LOG_CRIT,
155                                     "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
156 #ifdef IRCPLUS
157                 }
158 #endif
159
160                 return IRC_Num_ENDOFMOTD(Client, Req);
161         }
162         else if( Client_Type( Client ) == CLIENT_SERVER )
163         {
164                 /* New server is being introduced to the network */
165
166                 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
167
168                 /* check for existing server with same ID */
169                 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
170
171                 /* remove superfluous hostnames from Info-Text */
172                 ptr = strchr( Req->argv[3] + 2, '[' );
173                 if( ! ptr ) ptr = Req->argv[3];
174
175                 from = Client_Search( Req->prefix );
176                 if( ! from )
177                 {
178                         /* Uh, Server, that introduced the new server is unknown?! */
179                         Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
180                         Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
181                         return DISCONNECTED;
182                 }
183
184                 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
185                 if (!c) {
186                         Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
187                         Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
188                         return DISCONNECTED;
189                 }
190
191                 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
192                 else strcpy( str, "" );
193                 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": "" );
194
195                 /* notify other servers */
196                 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
197
198                 return CONNECTED;
199         } else
200                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
201                                           Client_ID(Client), Req->command);
202 } /* IRC_SERVER */
203
204
205 GLOBAL bool
206 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
207 {
208         char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
209         bool is_op, is_voiced;
210         CHANNEL *chan;
211         CLIENT *c;
212         
213         assert( Client != NULL );
214         assert( Req != NULL );
215
216         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
217
218         strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
219         strcpy( nick_out, "" );
220
221         channame = Req->argv[0];
222         ptr = strtok( nick_in, "," );
223         while( ptr )
224         {
225                 is_op = is_voiced = false;
226                 
227                 /* cut off prefixes */
228                 while(( *ptr == '@' ) || ( *ptr == '+' ))
229                 {
230                         if( *ptr == '@' ) is_op = true;
231                         if( *ptr == '+' ) is_voiced = true;
232                         ptr++;
233                 }
234
235                 c = Client_Search( ptr );
236                 if( c )
237                 {
238                         Channel_Join( c, channame );
239                         chan = Channel_Search( channame );
240                         assert( chan != NULL );
241                         
242                         if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
243                         if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
244
245                         /* announce to channel... */
246                         IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
247
248                         /* set Channel-User-Modes */
249                         strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
250                         if( modes[0] )
251                         {
252                                 /* send modes to channel */
253                                 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
254                         }
255
256                         if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
257                         if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
258                         if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
259                         strlcat( nick_out, ptr, sizeof( nick_out ));
260                 }
261                 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
262                 
263                 /* search for next Nick */
264                 ptr = strtok( NULL, "," );
265         }
266
267         /* forward to other servers */
268         if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
269
270         return CONNECTED;
271 } /* IRC_NJOIN */
272
273
274 GLOBAL bool
275 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
276 {
277         CLIENT *target;
278         char msg[LINE_LEN + 64];
279
280         assert( Client != NULL );
281         assert( Req != NULL );
282
283         if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
284
285         Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
286
287         target = Client_Search( Req->argv[0] );
288         if( ! target )
289         {
290                 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
291                 return CONNECTED;
292         }
293
294         if( Req->argv[1][0] )
295         {
296                 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
297                 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
298         }
299         else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
300
301         if( Client_Conn( target ) > NONE )
302         {
303                 /* This server has the connection */
304                 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
305                 else Conn_Close( Client_Conn( target ), msg, NULL, true);
306                 return DISCONNECTED;
307         }
308         else
309         {
310                 /* connection was on another server */
311                 Client_Destroy( target, msg, Req->argv[1], false );
312                 return CONNECTED;
313         }
314 } /* IRC_SQUIT */
315
316 /* -eof- */