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"
36 #include "irc-write.h"
39 #include "irc-server.h"
42 * Handler for the IRC "SERVER" command.
44 * @param Client The client from which this command has been received.
45 * @param Req Request structure with prefix and all parameters.
46 * @return CONNECTED or DISCONNECTED.
49 IRC_SERVER( CLIENT *Client, REQUEST *Req )
55 assert( Client != NULL );
56 assert( Req != NULL );
58 /* Return an error if this is not a local client */
59 if (Client_Conn(Client) <= NONE)
60 return IRC_WriteErrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
61 Client_ID(Client), Req->command);
63 if (Client_Type(Client) == CLIENT_GOTPASS ||
64 Client_Type(Client) == CLIENT_GOTPASS_2813) {
65 /* We got a PASS command from the peer, and now a SERVER
66 * command: the peer tries to register itself as a server. */
67 LogDebug("Connection %d: got SERVER command (new server link) ...",
70 if (Req->argc != 2 && Req->argc != 3)
71 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
75 /* Get configuration index of new remote server ... */
76 for (i = 0; i < MAX_SERVERS; i++)
77 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
80 /* Make sure the remote server is configured here */
81 if (i >= MAX_SERVERS) {
83 "Connection %d: Server \"%s\" not configured here!",
84 Client_Conn(Client), Req->argv[0]);
85 Conn_Close(Client_Conn(Client), NULL,
86 "Server not configured here", true);
90 /* Check server password */
91 if (strcmp(Conn_Password(Client_Conn(Client)),
92 Conf_Server[i].pwd_in) != 0) {
94 "Connection %d: Got bad password from server \"%s\"!",
95 Client_Conn(Client), Req->argv[0]);
96 Conn_Close(Client_Conn(Client), NULL,
97 "Bad password", true);
101 /* Is there a registered server with this ID? */
102 if (!Client_CheckID(Client, Req->argv[0]))
105 /* Mark this connection as belonging to an configured server */
106 if (!Conf_SetServer(i, Client_Conn(Client)))
109 Client_SetID( Client, Req->argv[0] );
110 Client_SetHops( Client, 1 );
111 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
113 /* Is this server registering on our side, or are we connecting to
114 * a remote server? */
115 if (Client_Token(Client) != TOKEN_OUTBOUND) {
116 /* Incoming connection, send user/pass */
117 if (!IRC_WriteStrClient(Client, "PASS %s %s",
118 Conf_Server[i].pwd_out,
120 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
123 Conn_Close(Client_Conn(Client),
124 "Unexpected server behavior!",
128 Client_SetIntroducer(Client, Client);
129 Client_SetToken(Client, 1);
131 /* outgoing connect, we already sent a SERVER and PASS
132 * command to the peer */
133 Client_SetToken(Client, atoi(Req->argv[1]));
136 /* Check protocol level */
137 if (Client_Type(Client) == CLIENT_GOTPASS) {
138 /* We got a "simple" PASS command, so the peer is
139 * using the protocol as defined in RFC 1459. */
140 if (! (Conn_Options(Client_Conn(Client)) & CONN_RFC1459))
142 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
143 Client_Conn(Client), Client_ID(Client));
144 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
147 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
150 if (Client_HasFlag(Client, 'Z')
151 && !Zip_InitConn(Client_Conn(Client))) {
152 Conn_Close(Client_Conn(Client),
153 "Can't initialize compression (zlib)!",
160 if (Client_HasFlag(Client, 'H')) {
161 LogDebug("Peer supports IRC+ extended server handshake ...");
162 if (!IRC_Send_ISUPPORT(Client))
164 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
168 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
170 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
175 return IRC_Num_ENDOFMOTD(Client, Req);
177 else if( Client_Type( Client ) == CLIENT_SERVER )
179 /* New server is being introduced to the network */
182 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
183 Client_ID(Client), Req->command);
185 /* check for existing server with same ID */
186 if (!Client_CheckID(Client, Req->argv[0]))
190 /* We definitely need a prefix here! */
191 Log(LOG_ALERT, "Got SERVER command without prefix! (on connection %d)",
192 Client_Conn(Client));
193 Conn_Close(Client_Conn(Client), NULL,
194 "SERVER command without prefix", true);
198 from = Client_Search( Req->prefix );
200 /* Uh, Server, that introduced the new server is unknown?! */
202 "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)",
203 Req->prefix, Client_Conn(Client));
204 Conn_Close(Client_Conn(Client), NULL,
205 "Unknown ID in prefix of SERVER", true);
209 c = Client_NewRemoteServer(Client, Req->argv[0], from,
210 atoi(Req->argv[1]), atoi(Req->argv[2]),
214 "Can't create client structure for server! (on connection %d)",
215 Client_Conn(Client));
216 Conn_Close(Client_Conn(Client), NULL,
217 "Can't allocate client structure for remote server",
222 if (Client_Hops(c) > 1 && Req->prefix[0])
223 snprintf(str, sizeof(str), "connected to %s, ",
227 Log(LOG_NOTICE|LOG_snotice,
228 "Server \"%s\" registered (via %s, %s%d hop%s).",
229 Client_ID(c), Client_ID(Client), str, Client_Hops(c),
230 Client_Hops(c) > 1 ? "s": "" );
232 /* notify other servers */
233 IRC_WriteStrServersPrefix(Client, from, "SERVER %s %d %d :%s",
234 Client_ID(c), Client_Hops(c) + 1,
235 Client_MyToken(c), Client_Info(c));
239 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
240 Client_ID(Client), Req->command);
244 * Handler for the IRC "NJOIN" command.
246 * @param Client The client from which this command has been received.
247 * @param Req Request structure with prefix and all parameters.
248 * @return CONNECTED or DISCONNECTED.
251 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
253 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8], *topic;
254 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
258 assert(Client != NULL);
261 strlcpy(nick_in, Req->argv[1], sizeof(nick_in));
262 strcpy(nick_out, "");
264 channame = Req->argv[0];
266 ptr = strtok(nick_in, ",");
268 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
270 /* cut off prefixes */
271 while ((*ptr == '~') || (*ptr == '&') || (*ptr == '@') ||
272 (*ptr == '%') || (*ptr == '+')) {
286 c = Client_Search(ptr);
288 /* Client not found? */
290 "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!",
295 if (!Channel_Join(c, channame)) {
296 /* Failed to join channel. Ooops!? */
298 "Failed to join client \"%s\" to channel \"%s\" (NJOIN): killing it!",
300 IRC_KillClient(NULL, NULL, ptr, "Internal NJOIN error!");
301 LogDebug("... done.");
305 chan = Channel_Search(channame);
306 assert(chan != NULL);
309 Channel_UserModeAdd(chan, c, 'q');
311 Channel_UserModeAdd(chan, c, 'a');
313 Channel_UserModeAdd(chan, c, 'o');
315 Channel_UserModeAdd(chan, c, 'h');
317 Channel_UserModeAdd(chan, c, 'v');
319 /* Announce client to the channel */
320 IRC_WriteStrChannelPrefix(Client, chan, c, false,
321 "JOIN :%s", channame);
323 /* If the client is connected to me... */
324 if(Client_Conn(c) != NONE) {
325 /* Send NAMES list to the joined user */
326 if(IRC_Send_NAMES(c, chan))
327 IRC_WriteStrClient(c, RPL_ENDOFNAMES_MSG, Client_ID(Client),
330 /* Send topic to the joined user */
331 topic = Channel_Topic(chan);
332 assert(topic != NULL);
334 IRC_WriteStrClient(c, RPL_TOPIC_MSG, Client_ID(c), channame, topic);
336 IRC_WriteStrClient(c, RPL_TOPICSETBY_MSG,
337 Client_ID(c), channame,
338 Channel_TopicWho(chan),
339 Channel_TopicTime(chan));
344 /* Announce "channel user modes" to the channel, if any */
345 strlcpy(modes, Channel_UserModes(chan, c), sizeof(modes));
347 IRC_WriteStrChannelPrefix(Client, chan, Client, false,
348 "MODE %s +%s %s", channame,
349 modes, Client_ID(c));
351 /* Build nick list for forwarding command */
352 if (nick_out[0] != '\0')
353 strlcat(nick_out, ",", sizeof(nick_out));
355 strlcat(nick_out, "~", sizeof(nick_out));
357 strlcat(nick_out, "&", sizeof(nick_out));
359 strlcat(nick_out, "@", sizeof(nick_out));
361 strlcat(nick_out, "%", sizeof(nick_out));
363 strlcat(nick_out, "+", sizeof(nick_out));
364 strlcat(nick_out, ptr, sizeof(nick_out));
367 /* Get next nick, if any ... */
368 ptr = strtok(NULL, ",");
371 /* forward to other servers */
372 if (nick_out[0] != '\0')
373 IRC_WriteStrServersPrefix(Client, Client_ThisServer(),
374 "NJOIN %s :%s", Req->argv[0], nick_out);
380 * Handler for the IRC "SQUIT" command.
382 * @param Client The client from which this command has been received.
383 * @param Req Request structure with prefix and all parameters.
384 * @return CONNECTED or DISCONNECTED.
387 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
389 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
390 CLIENT *from, *target;
391 CONN_ID con, client_con;
394 assert(Client != NULL);
397 if (Client_Type(Client) != CLIENT_SERVER
398 && !Client_HasMode(Client, 'o'))
399 return Op_NoPrivileges(Client, Req);
401 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
402 from = Client_Search(Req->prefix);
403 if (Client_Type(from) != CLIENT_SERVER
404 && !Op_Check(Client, Req))
405 return Op_NoPrivileges(Client, Req);
409 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
410 Client_ID(Client), Req->prefix);
412 if (Client_Type(Client) == CLIENT_USER)
413 loglevel = LOG_NOTICE | LOG_snotice;
415 loglevel = LOG_DEBUG;
416 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
417 Client_ID(from), Req->argv[0], Req->argv[1]);
419 target = Client_Search(Req->argv[0]);
420 if (Client_Type(Client) != CLIENT_SERVER &&
421 target == Client_ThisServer())
422 return Op_NoPrivileges(Client, Req);
424 /* The server is (already) unknown */
426 "Got SQUIT from %s for unknown server \"%s\"!?",
427 Client_ID(Client), Req->argv[0]);
431 client_con = Client_Conn(Client);
432 con = Client_Conn(target);
435 if (Client_NextHop(from) != Client || con > NONE)
436 snprintf(msg, sizeof(msg), "\"%s\" (SQUIT from %s)",
437 Req->argv[1], Client_ID(from));
439 strlcpy(msg, Req->argv[1], sizeof(msg));
441 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
445 /* We are directly connected to the target server, so we
446 * have to tear down the connection and to inform all the
447 * other remaining servers in the network */
448 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
449 "Received SQUIT %s from %s: %s",
450 Req->argv[0], Client_ID(from),
451 Req->argv[1][0] ? Req->argv[1] : "-");
452 Conn_Close(con, NULL, msg, true);
453 if (con == client_con)
456 /* This server is not directly connected, so the SQUIT must
457 * be forwarded ... */
458 if (Client_Type(from) != CLIENT_SERVER) {
459 /* The origin is not an IRC server, so don't evaluate
460 * this SQUIT but simply forward it */
461 IRC_WriteStrClientPrefix(Client_NextHop(target),
462 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
464 /* SQUIT has been generated by another server, so
465 * remove the target server from the network! */
467 if (!strchr(msg, '('))
468 snprintf(logmsg, sizeof(logmsg),
469 "\"%s\" (SQUIT from %s)", Req->argv[1],
471 Client_Destroy(target, logmsg[0] ? logmsg : msg,