+ channel_modes = Channel_Modes(chan);
+ if (strchr(channel_modes, 'i') && !is_invited) {
+ /* Channel is "invite-only" and client is not on invite list */
+ IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
+ Client_ID(Client), channame);
+ return false;
+ }
+
+ if (!Channel_CheckKey(chan, Client, key ? key : "")) {
+ /* Channel is protected by a channel key and the client
+ * didn't specify the correct one */
+ IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
+ Client_ID(Client), channame);
+ return false;
+ }
+
+ if (strchr(channel_modes, 'l') &&
+ (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
+ /* There are more clints joined to this channel than allowed */
+ IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
+ Client_ID(Client), channame);
+ return false;
+ }
+
+ if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
+ /* Only "secure" clients are allowed, but clients doesn't
+ * use SSL encryption */
+ IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
+ Client_ID(Client), channame);
+ return false;
+ }
+
+ if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
+ /* Only IRC operators are allowed! */
+ IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
+ Client_ID(Client), channame);
+ return false;
+ }
+
+ if (strchr(channel_modes, 'R') && !strchr(Client_Modes(Client), 'R')) {
+ /* Only registered users are allowed! */
+ IRC_WriteStrClient(Client, ERR_REGONLYCHANNEL_MSG,
+ Client_ID(Client), channame);
+ return false;
+ }
+
+ return true;
+} /* join_allowed */
+
+
+/**
+ * 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++;