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
27 #include "conn-func.h"
38 #include "irc-macros.h"
39 #include "irc-write.h"
42 #include "irc-server.h"
45 * Handler for the IRC "SERVER" command.
47 * @param Client The client from which this command has been received.
48 * @param Req Request structure with prefix and all parameters.
49 * @return CONNECTED or DISCONNECTED.
52 IRC_SERVER( CLIENT *Client, REQUEST *Req )
58 assert( Client != NULL );
59 assert( Req != NULL );
61 /* Return an error if this is not a local client */
62 if (Client_Conn(Client) <= NONE)
63 return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64 Client_ID(Client), Req->command);
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) ...",
73 if (Req->argc != 2 && Req->argc != 3)
74 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
78 /* Get configuration index of new remote server ... */
79 for (i = 0; i < MAX_SERVERS; i++)
80 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
83 /* Make sure the remote server is configured here */
84 if (i >= MAX_SERVERS) {
86 "Connection %d: Server \"%s\" not configured here!",
87 Client_Conn(Client), Req->argv[0]);
88 Conn_Close(Client_Conn(Client), NULL,
89 "Server not configured here", true);
93 /* Check server password */
94 if (strcmp(Conn_Password(Client_Conn(Client)),
95 Conf_Server[i].pwd_in) != 0) {
97 "Connection %d: Got bad password from server \"%s\"!",
98 Client_Conn(Client), Req->argv[0]);
99 Conn_Close(Client_Conn(Client), NULL,
100 "Bad password", true);
104 /* Is there a registered server with this ID? */
105 if (!Client_CheckID(Client, Req->argv[0]))
108 /* Mark this connection as belonging to an configured server */
109 if (!Conf_SetServer(i, Client_Conn(Client)))
112 Client_SetID( Client, Req->argv[0] );
113 Client_SetHops( Client, 1 );
114 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
116 /* Is this server registering on our side, or are we connecting to
117 * a remote server? */
118 if (Client_Token(Client) != TOKEN_OUTBOUND) {
119 /* Incoming connection, send user/pass */
120 if (!IRC_WriteStrClient(Client, "PASS %s %s",
121 Conf_Server[i].pwd_out,
123 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
126 Conn_Close(Client_Conn(Client),
127 "Unexpected server behavior!",
131 Client_SetIntroducer(Client, Client);
132 Client_SetToken(Client, 1);
134 /* outgoing connect, we already sent a SERVER and PASS
135 * command to the peer */
136 Client_SetToken(Client, atoi(Req->argv[1]));
139 /* Check protocol level */
140 if (Client_Type(Client) == CLIENT_GOTPASS) {
141 /* We got a "simple" PASS command, so the peer is
142 * using the protocol as defined in RFC 1459. */
143 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
145 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
146 Client_Conn(Client), Client_ID(Client));
147 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
150 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
153 if (Client_HasFlag(Client, 'Z')
154 && !Zip_InitConn(Client_Conn(Client))) {
155 Conn_Close(Client_Conn(Client),
156 "Can't initialize compression (zlib)!",
163 if (Client_HasFlag(Client, 'H')) {
164 LogDebug("Peer supports IRC+ extended server handshake ...");
165 if (!IRC_Send_ISUPPORT(Client))
167 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
171 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
173 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
178 return IRC_Num_ENDOFMOTD(Client, Req);
180 else if( Client_Type( Client ) == CLIENT_SERVER )
182 /* New server is being introduced to the network */
185 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
186 Client_ID(Client), Req->command);
188 /* check for existing server with same ID */
189 if (!Client_CheckID(Client, Req->argv[0]))
192 from = Client_Search( Req->prefix );
194 /* Uh, Server, that introduced the new server is unknown?! */
196 "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
197 Req->prefix, Client_Conn(Client));
198 Conn_Close(Client_Conn(Client), NULL,
199 "Unknown ID in prefix of SERVER", true);
203 c = Client_NewRemoteServer(Client, Req->argv[0], from,
204 atoi(Req->argv[1]), atoi(Req->argv[2]),
208 "Can't create client structure for server! (on connection %d)",
209 Client_Conn(Client));
210 Conn_Close(Client_Conn(Client), NULL,
211 "Can't allocate client structure for remote server",
216 if (Client_Hops(c) > 1 && Req->prefix[0])
217 snprintf(str, sizeof(str), "connected to %s, ",
221 Log(LOG_NOTICE|LOG_snotice,
222 "Server \"%s\" registered (via %s, %s%d hop%s).",
223 Client_ID(c), Client_ID(Client), str, Client_Hops(c),
224 Client_Hops(c) > 1 ? "s": "" );
226 /* notify other servers */
227 IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
228 Client_ID(c), Client_Hops(c) + 1,
229 Client_MyToken(c), Client_Info(c));
233 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
234 Client_ID(Client), Req->command);
238 * Handler for the IRC "NJOIN" command.
240 * @param Client The client from which this command has been received.
241 * @param Req Request structure with prefix and all parameters.
242 * @return CONNECTED or DISCONNECTED.
245 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
247 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
248 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
252 assert( Client != NULL );
253 assert( Req != NULL );
255 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
256 strcpy( nick_out, "" );
258 channame = Req->argv[0];
259 ptr = strtok( nick_in, "," );
262 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
264 /* cut off prefixes */
265 while(( *ptr == '~') || ( *ptr == '&' ) || ( *ptr == '@' ) ||
266 ( *ptr == '%') || ( *ptr == '+' ))
268 if( *ptr == '~' ) is_owner = true;
269 if( *ptr == '&' ) is_chanadmin = true;
270 if( *ptr == '@' ) is_op = true;
271 if( *ptr == 'h' ) is_halfop = true;
272 if( *ptr == '+' ) is_voiced = true;
276 c = Client_Search( ptr );
279 Channel_Join( c, channame );
280 chan = Channel_Search( channame );
281 assert( chan != NULL );
283 if( is_owner ) Channel_UserModeAdd( chan, c, 'q' );
284 if( is_chanadmin ) Channel_UserModeAdd( chan, c, 'a' );
285 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
286 if( is_halfop ) Channel_UserModeAdd( chan, c, 'h' );
287 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
289 /* announce to channel... */
290 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
292 /* set Channel-User-Modes */
293 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
296 /* send modes to channel */
297 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
300 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
301 if( is_owner ) strlcat( nick_out, "~", sizeof( nick_out ));
302 if( is_chanadmin ) strlcat( nick_out, "&", sizeof( nick_out ));
303 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
304 if( is_halfop ) strlcat( nick_out, "%", sizeof( nick_out ));
305 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
306 strlcat( nick_out, ptr, sizeof( nick_out ));
308 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
310 /* search for next Nick */
311 ptr = strtok( NULL, "," );
314 /* forward to other servers */
315 if (nick_out[0] != '\0')
316 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
317 "NJOIN %s :%s", Req->argv[0], nick_out);
323 * Handler for the IRC "SQUIT" command.
325 * @param Client The client from which this command has been received.
326 * @param Req Request structure with prefix and all parameters.
327 * @return CONNECTED or DISCONNECTED.
330 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
332 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
333 CLIENT *from, *target;
337 assert(Client != NULL);
340 if (Client_Type(Client) != CLIENT_SERVER
341 && !Client_HasMode(Client, 'o'))
342 return Op_NoPrivileges(Client, Req);
344 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
345 from = Client_Search(Req->prefix);
346 if (Client_Type(from) != CLIENT_SERVER
347 && !Op_Check(Client, Req))
348 return Op_NoPrivileges(Client, Req);
352 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
353 Client_ID(Client), Req->prefix);
355 if (Client_Type(Client) == CLIENT_USER)
356 loglevel = LOG_NOTICE | LOG_snotice;
358 loglevel = LOG_DEBUG;
359 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
360 Client_ID(from), Req->argv[0], Req->argv[1]);
362 target = Client_Search(Req->argv[0]);
363 if (Client_Type(Client) != CLIENT_SERVER &&
364 target == Client_ThisServer())
365 return Op_NoPrivileges(Client, Req);
367 /* The server is (already) unknown */
369 "Got SQUIT from %s for unknown server \"%s\"!?",
370 Client_ID(Client), Req->argv[0]);
374 con = Client_Conn(target);
377 if (Client_NextHop(from) != Client || con > NONE)
378 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
379 Req->argv[1], Client_ID(from));
381 strlcpy(msg, Req->argv[1], sizeof(msg));
383 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
387 /* We are directly connected to the target server, so we
388 * have to tear down the connection and to inform all the
389 * other remaining servers in the network */
390 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
391 "Received SQUIT %s from %s: %s",
392 Req->argv[0], Client_ID(from),
393 Req->argv[1][0] ? Req->argv[1] : "-");
394 Conn_Close(con, NULL, msg, true);
395 if (con == Client_Conn(Client))
398 /* This server is not directly connected, so the SQUIT must
399 * be forwarded ... */
400 if (Client_Type(from) != CLIENT_SERVER) {
401 /* The origin is not an IRC server, so don't evaluate
402 * this SQUIT but simply forward it */
403 IRC_WriteStrClientPrefix(Client_NextHop(target),
404 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
406 /* SQUIT has been generated by another server, so
407 * remove the target server from the network! */
409 if (!strchr(msg, '('))
410 snprintf(logmsg, sizeof(logmsg),
411 "\"%s\" (SQUIT from %s)", Req->argv[1],
413 Client_Destroy(target, logmsg[0] ? logmsg : msg,