2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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
25 #include "conn-func.h"
35 #include "irc-write.h"
38 #include "irc-server.h"
41 * Handler for the IRC "SERVER" command.
43 * @param Client The client from which this command has been received.
44 * @param Req Request structure with prefix and all parameters.
45 * @return CONNECTED or DISCONNECTED.
48 IRC_SERVER( CLIENT *Client, REQUEST *Req )
54 assert( Client != NULL );
55 assert( Req != NULL );
57 /* Return an error if this is not a local client */
58 if (Client_Conn(Client) <= NONE)
59 return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
60 Client_ID(Client), Req->command);
62 if (Client_Type(Client) == CLIENT_GOTPASS ||
63 Client_Type(Client) == CLIENT_GOTPASS_2813) {
64 /* We got a PASS command from the peer, and now a SERVER
65 * command: the peer tries to register itself as a server. */
66 LogDebug("Connection %d: got SERVER command (new server link) ...",
69 if (Req->argc != 2 && Req->argc != 3)
70 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
74 /* Get configuration index of new remote server ... */
75 for (i = 0; i < MAX_SERVERS; i++)
76 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
79 /* Make sure the remote server is configured here */
80 if (i >= MAX_SERVERS) {
82 "Connection %d: Server \"%s\" not configured here!",
83 Client_Conn(Client), Req->argv[0]);
84 Conn_Close(Client_Conn(Client), NULL,
85 "Server not configured here", true);
89 /* Check server password */
90 if (strcmp(Conn_Password(Client_Conn(Client)),
91 Conf_Server[i].pwd_in) != 0) {
93 "Connection %d: Got bad password from server \"%s\"!",
94 Client_Conn(Client), Req->argv[0]);
95 Conn_Close(Client_Conn(Client), NULL,
96 "Bad password", true);
100 /* Is there a registered server with this ID? */
101 if (!Client_CheckID(Client, Req->argv[0]))
104 /* Mark this connection as belonging to an configured server */
105 if (!Conf_SetServer(i, Client_Conn(Client)))
108 Client_SetID( Client, Req->argv[0] );
109 Client_SetHops( Client, 1 );
110 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
112 /* Is this server registering on our side, or are we connecting to
113 * a remote server? */
114 if (Client_Token(Client) != TOKEN_OUTBOUND) {
115 /* Incoming connection, send user/pass */
116 if (!IRC_WriteStrClient(Client, "PASS %s %s",
117 Conf_Server[i].pwd_out,
119 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
122 Conn_Close(Client_Conn(Client),
123 "Unexpected server behavior!",
127 Client_SetIntroducer(Client, Client);
128 Client_SetToken(Client, 1);
130 /* outgoing connect, we already sent a SERVER and PASS
131 * command to the peer */
132 Client_SetToken(Client, atoi(Req->argv[1]));
135 /* Check protocol level */
136 if (Client_Type(Client) == CLIENT_GOTPASS) {
137 /* We got a "simple" PASS command, so the peer is
138 * using the protocol as defined in RFC 1459. */
139 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
141 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
142 Client_Conn(Client), Client_ID(Client));
143 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
146 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
149 if (Client_HasFlag(Client, 'Z')
150 && !Zip_InitConn(Client_Conn(Client))) {
151 Conn_Close(Client_Conn(Client),
152 "Can't initialize compression (zlib)!",
159 if (Client_HasFlag(Client, 'H')) {
160 LogDebug("Peer supports IRC+ extended server handshake ...");
161 if (!IRC_Send_ISUPPORT(Client))
163 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
167 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
169 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
174 return IRC_Num_ENDOFMOTD(Client, Req);
176 else if( Client_Type( Client ) == CLIENT_SERVER )
178 /* New server is being introduced to the network */
181 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
182 Client_ID(Client), Req->command);
184 /* check for existing server with same ID */
185 if (!Client_CheckID(Client, Req->argv[0]))
188 from = Client_Search( Req->prefix );
190 /* Uh, Server, that introduced the new server is unknown?! */
192 "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
193 Req->prefix, Client_Conn(Client));
194 Conn_Close(Client_Conn(Client), NULL,
195 "Unknown ID in prefix of SERVER", true);
199 c = Client_NewRemoteServer(Client, Req->argv[0], from,
200 atoi(Req->argv[1]), atoi(Req->argv[2]),
204 "Can't create client structure for server! (on connection %d)",
205 Client_Conn(Client));
206 Conn_Close(Client_Conn(Client), NULL,
207 "Can't allocate client structure for remote server",
212 if (Client_Hops(c) > 1 && Req->prefix[0])
213 snprintf(str, sizeof(str), "connected to %s, ",
217 Log(LOG_NOTICE|LOG_snotice,
218 "Server \"%s\" registered (via %s, %s%d hop%s).",
219 Client_ID(c), Client_ID(Client), str, Client_Hops(c),
220 Client_Hops(c) > 1 ? "s": "" );
222 /* notify other servers */
223 IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
224 Client_ID(c), Client_Hops(c) + 1,
225 Client_MyToken(c), Client_Info(c));
229 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
230 Client_ID(Client), Req->command);
234 * Handler for the IRC "NJOIN" command.
236 * @param Client The client from which this command has been received.
237 * @param Req Request structure with prefix and all parameters.
238 * @return CONNECTED or DISCONNECTED.
241 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
243 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
244 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
248 assert( Client != NULL );
249 assert( Req != NULL );
251 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
252 strcpy( nick_out, "" );
254 channame = Req->argv[0];
255 ptr = strtok( nick_in, "," );
258 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
260 /* cut off prefixes */
261 while(( *ptr == '~') || ( *ptr == '&' ) || ( *ptr == '@' ) ||
262 ( *ptr == '%') || ( *ptr == '+' ))
264 if( *ptr == '~' ) is_owner = true;
265 if( *ptr == '&' ) is_chanadmin = true;
266 if( *ptr == '@' ) is_op = true;
267 if( *ptr == '%' ) is_halfop = true;
268 if( *ptr == '+' ) is_voiced = true;
272 c = Client_Search( ptr );
275 Channel_Join( c, channame );
276 chan = Channel_Search( channame );
277 assert( chan != NULL );
279 if( is_owner ) Channel_UserModeAdd( chan, c, 'q' );
280 if( is_chanadmin ) Channel_UserModeAdd( chan, c, 'a' );
281 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
282 if( is_halfop ) Channel_UserModeAdd( chan, c, 'h' );
283 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
285 /* announce to channel... */
286 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
288 /* set Channel-User-Modes */
289 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
292 /* send modes to channel */
293 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
296 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
297 if( is_owner ) strlcat( nick_out, "~", sizeof( nick_out ));
298 if( is_chanadmin ) strlcat( nick_out, "&", sizeof( nick_out ));
299 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
300 if( is_halfop ) strlcat( nick_out, "%", sizeof( nick_out ));
301 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
302 strlcat( nick_out, ptr, sizeof( nick_out ));
304 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
306 /* search for next Nick */
307 ptr = strtok( NULL, "," );
310 /* forward to other servers */
311 if (nick_out[0] != '\0')
312 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
313 "NJOIN %s :%s", Req->argv[0], nick_out);
319 * Handler for the IRC "SQUIT" command.
321 * @param Client The client from which this command has been received.
322 * @param Req Request structure with prefix and all parameters.
323 * @return CONNECTED or DISCONNECTED.
326 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
328 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
329 CLIENT *from, *target;
333 assert(Client != NULL);
336 if (Client_Type(Client) != CLIENT_SERVER
337 && !Client_HasMode(Client, 'o'))
338 return Op_NoPrivileges(Client, Req);
340 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
341 from = Client_Search(Req->prefix);
342 if (Client_Type(from) != CLIENT_SERVER
343 && !Op_Check(Client, Req))
344 return Op_NoPrivileges(Client, Req);
348 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
349 Client_ID(Client), Req->prefix);
351 if (Client_Type(Client) == CLIENT_USER)
352 loglevel = LOG_NOTICE | LOG_snotice;
354 loglevel = LOG_DEBUG;
355 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
356 Client_ID(from), Req->argv[0], Req->argv[1]);
358 target = Client_Search(Req->argv[0]);
359 if (Client_Type(Client) != CLIENT_SERVER &&
360 target == Client_ThisServer())
361 return Op_NoPrivileges(Client, Req);
363 /* The server is (already) unknown */
365 "Got SQUIT from %s for unknown server \"%s\"!?",
366 Client_ID(Client), Req->argv[0]);
370 con = Client_Conn(target);
373 if (Client_NextHop(from) != Client || con > NONE)
374 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
375 Req->argv[1], Client_ID(from));
377 strlcpy(msg, Req->argv[1], sizeof(msg));
379 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
383 /* We are directly connected to the target server, so we
384 * have to tear down the connection and to inform all the
385 * other remaining servers in the network */
386 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
387 "Received SQUIT %s from %s: %s",
388 Req->argv[0], Client_ID(from),
389 Req->argv[1][0] ? Req->argv[1] : "-");
390 Conn_Close(con, NULL, msg, true);
391 if (con == Client_Conn(Client))
394 /* This server is not directly connected, so the SQUIT must
395 * be forwarded ... */
396 if (Client_Type(from) != CLIENT_SERVER) {
397 /* The origin is not an IRC server, so don't evaluate
398 * this SQUIT but simply forward it */
399 IRC_WriteStrClientPrefix(Client_NextHop(target),
400 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
402 /* SQUIT has been generated by another server, so
403 * remove the target server from the network! */
405 if (!strchr(msg, '('))
406 snprintf(logmsg, sizeof(logmsg),
407 "\"%s\" (SQUIT from %s)", Req->argv[1],
409 Client_Destroy(target, logmsg[0] ? logmsg : msg,