2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
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.
16 * IRC commands for server links
28 #include "conn-func.h"
39 #include "irc-macros.h"
40 #include "irc-write.h"
44 #include "irc-server.h"
47 * Handler for the IRC "SERVER" command.
49 * @param Client The client from which this command has been received.
50 * @param Req Request structure with prefix and all parameters.
51 * @return CONNECTED or DISCONNECTED.
54 IRC_SERVER( CLIENT *Client, REQUEST *Req )
60 assert( Client != NULL );
61 assert( Req != NULL );
63 /* Return an error if this is not a local client */
64 if (Client_Conn(Client) <= NONE)
65 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
66 Client_ID(Client), Req->command);
68 if (Client_Type(Client) == CLIENT_GOTPASS ||
69 Client_Type(Client) == CLIENT_GOTPASS_2813) {
70 /* We got a PASS command from the peer, and now a SERVER
71 * command: the peer tries to register itself as a server. */
72 LogDebug("Connection %d: got SERVER command (new server link) ...",
75 if (Req->argc != 2 && Req->argc != 3)
76 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
80 /* Get configuration index of new remote server ... */
81 for (i = 0; i < MAX_SERVERS; i++)
82 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
85 /* Make sure the remote server is configured here */
86 if (i >= MAX_SERVERS) {
88 "Connection %d: Server \"%s\" not configured here!",
89 Client_Conn(Client), Req->argv[0]);
90 Conn_Close(Client_Conn(Client), NULL,
91 "Server not configured here", true);
95 /* Check server password */
96 if (strcmp(Conn_Password(Client_Conn(Client)),
97 Conf_Server[i].pwd_in) != 0) {
99 "Connection %d: Got bad password from server \"%s\"!",
100 Client_Conn(Client), Req->argv[0]);
101 Conn_Close(Client_Conn(Client), NULL,
102 "Bad password", true);
106 /* Is there a registered server with this ID? */
107 if (!Client_CheckID(Client, Req->argv[0]))
110 /* Mark this connection as belonging to an configured server */
111 if (!Conf_SetServer(i, Client_Conn(Client)))
114 Client_SetID( Client, Req->argv[0] );
115 Client_SetHops( Client, 1 );
116 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
118 /* Is this server registering on our side, or are we connecting to
119 * a remote server? */
120 if (Client_Token(Client) != TOKEN_OUTBOUND) {
121 /* Incoming connection, send user/pass */
122 if (!IRC_WriteStrClient(Client, "PASS %s %s",
123 Conf_Server[i].pwd_out,
125 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
128 Conn_Close(Client_Conn(Client),
129 "Unexpected server behavior!",
133 Client_SetIntroducer(Client, Client);
134 Client_SetToken(Client, 1);
136 /* outgoing connect, we already sent a SERVER and PASS
137 * command to the peer */
138 Client_SetToken(Client, atoi(Req->argv[1]));
141 /* Check protocol level */
142 if (Client_Type(Client) == CLIENT_GOTPASS) {
143 /* We got a "simple" PASS command, so the peer is
144 * using the protocol as defined in RFC 1459. */
145 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
147 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
148 Client_Conn(Client), Client_ID(Client));
149 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
152 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
155 if (Client_HasFlag(Client, 'Z')
156 && !Zip_InitConn(Client_Conn(Client))) {
157 Conn_Close(Client_Conn(Client),
158 "Can't initialize compression (zlib)!",
165 if (Client_HasFlag(Client, 'H')) {
166 LogDebug("Peer supports IRC+ extended server handshake ...");
167 if (!IRC_Send_ISUPPORT(Client))
169 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
173 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
175 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
180 return IRC_Num_ENDOFMOTD(Client, Req);
182 else if( Client_Type( Client ) == CLIENT_SERVER )
184 /* New server is being introduced to the network */
186 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
188 /* check for existing server with same ID */
189 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
191 from = Client_Search( Req->prefix );
194 /* Uh, Server, that introduced the new server is unknown?! */
195 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
196 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
200 c = Client_NewRemoteServer(Client, Req->argv[0], from, atoi(Req->argv[1]), atoi(Req->argv[2]), Req->argv[3], true);
202 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
203 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
207 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
208 else strcpy( str, "" );
209 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": "" );
211 /* notify other servers */
212 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
216 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
217 Client_ID(Client), Req->command);
221 * Handler for the IRC "NJOIN" command.
223 * @param Client The client from which this command has been received.
224 * @param Req Request structure with prefix and all parameters.
225 * @return CONNECTED or DISCONNECTED.
228 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
230 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
231 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
235 assert( Client != NULL );
236 assert( Req != NULL );
238 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 2)
240 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
241 strcpy( nick_out, "" );
243 channame = Req->argv[0];
244 ptr = strtok( nick_in, "," );
247 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
249 /* cut off prefixes */
250 while(( *ptr == '~') || ( *ptr == '&' ) || ( *ptr == '@' ) ||
251 ( *ptr == '%') || ( *ptr == '+' ))
253 if( *ptr == '~' ) is_owner = true;
254 if( *ptr == '&' ) is_chanadmin = true;
255 if( *ptr == '@' ) is_op = true;
256 if( *ptr == 'h' ) is_halfop = true;
257 if( *ptr == '+' ) is_voiced = true;
261 c = Client_Search( ptr );
264 Channel_Join( c, channame );
265 chan = Channel_Search( channame );
266 assert( chan != NULL );
268 if( is_owner ) Channel_UserModeAdd( chan, c, 'q' );
269 if( is_chanadmin ) Channel_UserModeAdd( chan, c, 'a' );
270 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
271 if( is_halfop ) Channel_UserModeAdd( chan, c, 'h' );
272 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
274 /* announce to channel... */
275 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
277 /* set Channel-User-Modes */
278 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
281 /* send modes to channel */
282 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
285 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
286 if( is_owner ) strlcat( nick_out, "~", sizeof( nick_out ));
287 if( is_chanadmin ) strlcat( nick_out, "&", sizeof( nick_out ));
288 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
289 if( is_halfop ) strlcat( nick_out, "%", sizeof( nick_out ));
290 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
291 strlcat( nick_out, ptr, sizeof( nick_out ));
293 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
295 /* search for next Nick */
296 ptr = strtok( NULL, "," );
299 /* forward to other servers */
300 if (nick_out[0] != '\0')
301 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
302 "NJOIN %s :%s", Req->argv[0], nick_out);
308 * Handler for the IRC "SQUIT" command.
310 * @param Client The client from which this command has been received.
311 * @param Req Request structure with prefix and all parameters.
312 * @return CONNECTED or DISCONNECTED.
315 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
317 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
318 CLIENT *from, *target;
322 assert(Client != NULL);
325 if (Client_Type(Client) != CLIENT_SERVER
326 && !Client_HasMode(Client, 'o'))
327 return Op_NoPrivileges(Client, Req);
329 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 2)
331 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
332 from = Client_Search(Req->prefix);
333 if (Client_Type(from) != CLIENT_SERVER
334 && !Op_Check(Client, Req))
335 return Op_NoPrivileges(Client, Req);
339 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
340 Client_ID(Client), Req->prefix);
342 if (Client_Type(Client) == CLIENT_USER)
343 loglevel = LOG_NOTICE | LOG_snotice;
345 loglevel = LOG_DEBUG;
346 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
347 Client_ID(from), Req->argv[0], Req->argv[1]);
349 target = Client_Search(Req->argv[0]);
350 if (Client_Type(Client) != CLIENT_SERVER &&
351 target == Client_ThisServer())
352 return Op_NoPrivileges(Client, Req);
354 /* The server is (already) unknown */
356 "Got SQUIT from %s for unknown server \"%s\"!?",
357 Client_ID(Client), Req->argv[0]);
361 con = Client_Conn(target);
364 if (Client_NextHop(from) != Client || con > NONE)
365 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
366 Req->argv[1], Client_ID(from));
368 strlcpy(msg, Req->argv[1], sizeof(msg));
370 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
374 /* We are directly connected to the target server, so we
375 * have to tear down the connection and to inform all the
376 * other remaining servers in the network */
377 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
378 "Received SQUIT %s from %s: %s",
379 Req->argv[0], Client_ID(from),
380 Req->argv[1][0] ? Req->argv[1] : "-");
381 Conn_Close(con, NULL, msg, true);
382 if (con == Client_Conn(Client))
385 /* This server is not directly connected, so the SQUIT must
386 * be forwarded ... */
387 if (Client_Type(from) != CLIENT_SERVER) {
388 /* The origin is not an IRC server, so don't evaluate
389 * this SQUIT but simply forward it */
390 IRC_WriteStrClientPrefix(Client_NextHop(target),
391 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
393 /* SQUIT has been generated by another server, so
394 * remove the target server from the network! */
396 if (!strchr(msg, '('))
397 snprintf(logmsg, sizeof(logmsg),
398 "\"%s\" (SQUIT from %s)", Req->argv[1],
400 Client_Destroy(target, logmsg[0] ? logmsg : msg,