<commit_before>package com.github.otbproject.otbproject.channel;

import com.github.otbproject.otbproject.App;
import com.github.otbproject.otbproject.bot.Bot;
import com.github.otbproject.otbproject.bot.Control;
import com.github.otbproject.otbproject.command.parser.ResponseParserUtil;
import com.github.otbproject.otbproject.config.*;
import com.github.otbproject.otbproject.fs.Setup;

import java.io.IOException;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Channels {
    private static final Lock lock = new ReentrantLock();

    public static boolean in(String channelName) {
        Optional<Channel> optional = get(channelName);
        return optional.isPresent() && optional.get().isInChannel();
    }

    public static Optional<Channel> get(String channel) {
        return Optional.ofNullable(Control.getBot().getChannels().get(channel));
    }

    public static Channel getOrThrow(String channel) throws ChannelNotFoundException {
        return get(channel).orElseThrow(ChannelNotFoundException::new);
    }

    public static boolean join(String channelName) {
        return join(channelName, JoinCheck.ALL_CHECKS);
    }

    public static boolean join(String channelName, EnumSet<JoinCheck> checks) {
        return doJoin(channelName.toLowerCase(), checks);
    }

    private static boolean doJoin(final String channelName, EnumSet<JoinCheck> checks) {
        App.logger.info("Attempting to join channel: " + channelName);

        lock.lock();
        try {
            if (in(channelName)) {
                App.logger.info("Failed to join channel: " + channelName + ". Already in channel");
                return false;
            }

            Bot bot = Control.getBot();
            boolean isBotChannel = channelName.equals(bot.getUserName());

            // Check if bot is connected
            if (!bot.isConnected()) {
                App.logger.warn("Not connected to " + ResponseParserUtil.wordCap(Configs.getFromGeneralConfig(GeneralConfig::getService).toString(), true));
                return false;
            }

            // Check whitelist/blacklist
            //BotConfig botConfig = Configs.getBotConfig();
            ChannelJoinSetting channelJoinSetting = Configs.getFromBotConfig(BotConfig::getChannelJoinSetting);
            if (!isBotChannel) {
                if (checks.contains(JoinCheck.WHITELIST)
                        && (channelJoinSetting == ChannelJoinSetting.WHITELIST)
                        && !Configs.getFromBotConfig(BotConfig::getWhitelist).contains(channelName)) {
                    App.logger.info("Failed to join channel: " + channelName + ". Not whitelisted.");
                    return false;
                } else if (checks.contains(JoinCheck.BLACKLIST)
                        && (channelJoinSetting == ChannelJoinSetting.BLACKLIST)
                        && Configs.getFromBotConfig(BotConfig::getBlacklist).contains(channelName)) {
                    App.logger.info("Failed to join channel: " + channelName + ". Blacklisted.");
                    return false;
                }
            }

            if (checks.contains(JoinCheck.IS_CHANNEL) && !bot.isChannel(channelName)) {
                App.logger.info("Failed to join channel: " + channelName + ". Channel does not exist.");
                return false;<commit_msg>Remove commented-out code<commit_after>package com.github.otbproject.otbproject.channel;

import com.github.otbproject.otbproject.App;
import com.github.otbproject.otbproject.bot.Bot;
import com.github.otbproject.otbproject.bot.Control;
import com.github.otbproject.otbproject.command.parser.ResponseParserUtil;
import com.github.otbproject.otbproject.config.*;
import com.github.otbproject.otbproject.fs.Setup;

import java.io.IOException;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Channels {
    private static final Lock lock = new ReentrantLock();

    public static boolean in(String channelName) {
        Optional<Channel> optional = get(channelName);
        return optional.isPresent() && optional.get().isInChannel();
    }

    public static Optional<Channel> get(String channel) {
        return Optional.ofNullable(Control.getBot().getChannels().get(channel));
    }

    public static Channel getOrThrow(String channel) throws ChannelNotFoundException {
        return get(channel).orElseThrow(ChannelNotFoundException::new);
    }

    public static boolean join(String channelName) {
        return join(channelName, JoinCheck.ALL_CHECKS);
    }

    public static boolean join(String channelName, EnumSet<JoinCheck> checks) {
        return doJoin(channelName.toLowerCase(), checks);
    }

    private static boolean doJoin(final String channelName, EnumSet<JoinCheck> checks) {
        App.logger.info("Attempting to join channel: " + channelName);

        lock.lock();
        try {
            if (in(channelName)) {
                App.logger.info("Failed to join channel: " + channelName + ". Already in channel");
                return false;
            }

            Bot bot = Control.getBot();
            boolean isBotChannel = channelName.equals(bot.getUserName());

            // Check if bot is connected
            if (!bot.isConnected()) {
                App.logger.warn("Not connected to " + ResponseParserUtil.wordCap(Configs.getFromGeneralConfig(GeneralConfig::getService).toString(), true));
                return false;
            }

            // Check whitelist/blacklist
            ChannelJoinSetting channelJoinSetting = Configs.getFromBotConfig(BotConfig::getChannelJoinSetting);
            if (!isBotChannel) {
                if (checks.contains(JoinCheck.WHITELIST)
                        && (channelJoinSetting == ChannelJoinSetting.WHITELIST)
                        && !Configs.getFromBotConfig(BotConfig::getWhitelist).contains(channelName)) {
                    App.logger.info("Failed to join channel: " + channelName + ". Not whitelisted.");
                    return false;
                } else if (checks.contains(JoinCheck.BLACKLIST)
                        && (channelJoinSetting == ChannelJoinSetting.BLACKLIST)
                        && Configs.getFromBotConfig(BotConfig::getBlacklist).contains(channelName)) {
                    App.logger.info("Failed to join channel: " + channelName + ". Blacklisted.");
                    return false;
                }
            }

            if (checks.contains(JoinCheck.IS_CHANNEL) && !bot.isChannel(channelName)) {
                App.logger.info("Failed to join channel: " + channelName + ". Channel does not exist.");
                return false;
{'fraction_non_alphanumeric': 0.10480349344978165, 'fraction_numerical': 0.0, 'mean_word_length': 2.614413466596528, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6203393', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1744, 'n_words': 358}