+/**
+ * Set user channel modes.
+ *
+ * @param chan Channel
+ * @param target User to set modes for
+ * @param flags Channel modes to add
+ */
+static void
+join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
+{
+ if (flags) {
+ while (*flags) {
+ Channel_UserModeAdd(chan, target, *flags);
+ flags++;
+ }
+ }
+
+ /* If the channel is persistent (+P) and client is an IRC op:
+ * make client chanop, if not disabled in configuration. */
+ if (Channel_HasMode(chan, 'P') && Conf_OperChanPAutoOp
+ && Client_HasMode(target, 'o'))
+ Channel_UserModeAdd(chan, target, 'o');
+} /* join_set_channelmodes */
+
+/**
+ * Forward JOIN command to a specific server
+ *
+ * This function differentiates between servers using RFC 2813 mode that
+ * support the JOIN command with appended ASCII 7 character and channel
+ * modes, and servers using RFC 1459 protocol which require separate JOIN
+ * and MODE commands.
+ *
+ * @param To Forward JOIN (and MODE) command to this peer server
+ * @param Prefix Client used to prefix the generated commands
+ * @param Data Parameters of JOIN command to forward, probably
+ * containing channel modes separated by ASCII 7.
+ */
+static void
+cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
+{
+ CONN_ID conn;
+ char str[COMMAND_LEN], *ptr = NULL;
+
+ strlcpy(str, (char *)Data, sizeof(str));
+ conn = Client_Conn(To);
+
+ if (Conn_Options(conn) & CONN_RFC1459) {
+ /* RFC 1459 compatibility mode, appended modes are NOT
+ * supported, so strip them off! */
+ ptr = strchr(str, 0x7);
+ if (ptr)
+ *ptr++ = '\0';
+ }
+
+ IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
+ if (ptr && *ptr)
+ IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
+ Client_ID(Prefix));
+} /* cb_join_forward */
+
+/**
+ * Forward JOIN command to all servers
+ *
+ * This function calls cb_join_forward(), which differentiates between
+ * protocol implementations (e.g. RFC 2812, RFC 1459).
+ *
+ * @param Client Client used to prefix the generated commands
+ * @param target Forward JOIN (and MODE) command to this peer server
+ * @param chan Channel structure
+ * @param channame Channel name
+ */
+static void
+join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
+ const char *channame)
+{
+ char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
+
+ /* RFC 2813, 4.2.1: channel modes are separated from the channel
+ * name with ASCII 7, if any, and not spaces: */
+ strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
+ if (modes[1])
+ modes[0] = 0x7;
+ else
+ modes[0] = '\0';
+
+ /* forward to other servers (if it is not a local channel) */
+ if (!Channel_IsLocal(chan)) {
+ snprintf(str, sizeof(str), "%s%s", channame, modes);
+ IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
+ cb_join_forward, str);
+ }
+
+ /* tell users in this channel about the new client */
+ IRC_WriteStrChannelPrefix(Client, chan, target, false,
+ "JOIN :%s", channame);
+
+ /* synchronize channel modes */
+ if (modes[1]) {
+ IRC_WriteStrChannelPrefix(Client, chan, target, false,
+ "MODE %s +%s %s", channame,
+ &modes[1], Client_ID(target));
+ }
+} /* join_forward */
+
+/**
+ * Send channel TOPIC and NAMES list to a newly (N)JOIN'ed client.
+ *
+ * @param Client Client used to prefix the generated commands
+ * @param Chan Channel structure
+ */
+GLOBAL bool
+IRC_Send_Channel_Info(CLIENT *Client, CHANNEL *Chan)
+{
+ const char *topic;
+
+ /* Send the topic (if any) to the new client: */
+ topic = Channel_Topic(Chan);
+ assert(topic != NULL);
+ if (*topic) {
+ if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
+ Client_ID(Client), Channel_Name(Chan), topic))
+ return false;
+#ifndef STRICT_RFC
+ if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
+ Client_ID(Client), Channel_Name(Chan),
+ Channel_TopicWho(Chan),
+ Channel_TopicTime(Chan)))
+ return false;
+#endif
+ }
+
+ /* Send list of channel members to the new client: */
+ if (!IRC_Send_NAMES(Client, Chan))
+ return false;
+ return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG,
+ Client_ID(Client), Channel_Name(Chan));
+}
+
+/**
+ * Handler for the IRC "JOIN" command.
+ *
+ * @param Client The client from which this command has been received.
+ * @param Req Request structure with prefix and all parameters.
+ * @return CONNECTED or DISCONNECTED.
+ */