<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rpkit.chat.bukkit

import com.rpkit.chat.bukkit.chatchannel.RPKChatChannelProvider
import com.rpkit.chat.bukkit.chatchannel.RPKChatChannelProviderImpl
import com.rpkit.chat.bukkit.chatchannel.directed.*
import com.rpkit.chat.bukkit.chatchannel.undirected.IRCComponent
import com.rpkit.chat.bukkit.chatchannel.undirected.LogComponent
import com.rpkit.chat.bukkit.chatchannel.undirected.UndirectedFormatComponent
import com.rpkit.chat.bukkit.chatchannel.undirected.WebComponent
import com.rpkit.chat.bukkit.chatgroup.RPKChatGroupProviderImpl
import com.rpkit.chat.bukkit.command.chatchannel.ChatChannelCommand
import com.rpkit.chat.bukkit.command.chatgroup.ChatGroupCommand
import com.rpkit.chat.bukkit.command.listchatchannels.ListChatChannelsCommand
import com.rpkit.chat.bukkit.command.message.MessageCommand
import com.rpkit.chat.bukkit.command.mute.MuteCommand
import com.rpkit.chat.bukkit.command.reply.ReplyCommand
import com.rpkit.chat.bukkit.command.snoop.SnoopCommand
import com.rpkit.chat.bukkit.command.unmute.UnmuteCommand
import com.rpkit.chat.bukkit.database.table.*
import com.rpkit.chat.bukkit.irc.RPKIRCProvider
import com.rpkit.chat.bukkit.irc.RPKIRCProviderImpl
import com.rpkit.chat.bukkit.listener.AsyncPlayerChatListener
import com.rpkit.chat.bukkit.listener.PlayerCommandPreprocessListener
import com.rpkit.chat.bukkit.mute.RPKChatChannelMuteProvider
import com.rpkit.chat.bukkit.prefix.RPKPrefixProvider
import com.rpkit.chat.bukkit.prefix.RPKPrefixProviderImpl
import com.rpkit.chat.bukkit.servlet.ChatServlet
import com.rpkit.chat.bukkit.servlet.websocket.ChatWebSocketServlet
import com.rpkit.chat.bukkit.servlet.websocket.RPKChatWebSocketProvider
import com.rpkit.chat.bukkit.snooper.RPKSnooperProviderImpl
import com.rpkit.chat.bukkit.speaker.RPKChatChannelSpeakerProvider
import com.rpkit.core.bukkit.plugin.RPKBukkitPlugin
import com.rpkit.core.database.Database
import com.rpkit.core.web.NavigationLink
import org.bstats.bukkit.Metrics
import org.bukkit.configuration.serialization.ConfigurationSerialization
import org.bukkit.permissions.Permission
import org.bukkit.permissions.PermissionDefault
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder

/**
 * RPK chat plugin default implementation.
 */
class RPKChatBukkit: RPKBukkitPlugin() {

    override fun onEnable() {
        Metrics(this)
        ConfigurationSerialization.registerClass(DirectedFormatComponent::class.java, "DirectedFormatComponent")
        ConfigurationSerialization.registerClass(DrunkenSlurComponent::class.java, "DrunkenSlurComponent")
        ConfigurationSerialization.registerClass(GarbleComponent::class.java, "GarbleComponent")
        ConfigurationSerialization.registerClass(RadiusFilterComponent::class.java, "RadiusFilterComponent")
        ConfigurationSerialization.registerClass(SendMessageComponent::class.java, "SendMessageComponent")
        ConfigurationSerialization.registerClass(SnoopComponent::class.java, "SnoopComponent")
        ConfigurationSerialization.registerClass(IRCComponent::class.java, "IRCComponent")
        ConfigurationSerialization.registerClass(LogComponent::class.java, "LogComponent")
        ConfigurationSerialization.registerClass(UndirectedFormatComponent::class.java, "UndirectedFormatComponent")
        ConfigurationSerialization.registerClass(WebComponent::class.java, "WebComponent")
        saveDefaultConfig()
        if (config.getBoolean("irc.enabled")) {
            val ircProvider = RPKIRCProviderImpl(this)
            val prefixProvider = RPKPrefixProviderImpl(this)
            val chatChannelProvider = RPKChatChannelProviderImpl(this)
            val chatChannelMuteProvider = RPKChatChannelMuteProvider(this)
            val chatChannelSpeakerProvider = RPKChatChannelSpeakerProvider(this)
            val chatGroupProvider = RPKChatGroupProviderImpl(this)
            val snooperProvider =  RPKSnooperProviderImpl(this)
            val chatWebSocketProvider = RPKChatWebSocketProvider()
            serviceProviders = arrayOf(
                    ircProvider,
                    prefixProvider,
                    chatChannelProvider,
                    chatChannelMuteProvider,
                    chatChannelSpeakerProvider,
                    chatGroupProvider,
                    snooperProvider,
                    chatWebSocketProvider
            )
            registerChatChannelPermissions(chatChannelProvider)
            registerPrefixPermissions(prefixProvider)
        } else {
            val prefixProvider = RPKPrefixProviderImpl(this)
            val chatChannelProvider = RPKChatChannelProviderImpl(this)
            val chatChannelMuteProvider = RPKChatChannelMuteProvider(this)
            val chatChannelSpeakerProvider = RPKChatChannelSpeakerProvider(this)
            val chatGroupProvider = RPKChatGroupProviderImpl(this)
            val snooperProvider =  RPKSnooperProviderImpl(this)
            val chatWebSocketProvider = RPKChatWebSocketProvider()
            serviceProviders = arrayOf(
                    prefixProvider,
                    chatChannelProvider,
                    chatChannelMuteProvider,
                    chatChannelSpeakerProvider,
                    chatGroupProvider,
                    snooperProvider,
                    chatWebSocketProvider
            )
            registerChatChannelPermissions(chatChannelProvider)
            registerPrefixPermissions(prefixProvider)
        }
        servlets = arrayOf(
                ChatServlet(this)
        )
    }

    override fun onPostEnable() {
        core.web.navigationBar.add(NavigationLink("Chat", "/chat/"))
        val originalClassLoader = Thread.currentThread().contextClassLoader
        Thread.currentThread().contextClassLoader = javaClass.classLoader
        (core.web.server.handler as ServletContextHandler).addServlet(ServletHolder(ChatWebSocketServlet(this)), "/chat/ws")
        Thread.currentThread().contextClassLoader = originalClassLoader
    }

    override fun onDisable() {
        if (config.getBoolean("irc.enabled")) {
            core.serviceManager.getServiceProvider(RPKIRCProvider::class).ircBot.sendIRC().quitServer(messages["irc-quit"])
        }
    }

    override fun registerCommands() {
        getCommand("chatchannel")?.setExecutor(ChatChannelCommand(this))
        getCommand("mute")?.setExecutor(MuteCommand(this))
        getCommand("unmute")?.setExecutor(UnmuteCommand(this))
        getCommand("listchatchannels")?.setExecutor(ListChatChannelsCommand(this))
        getCommand("chatgroup")?.setExecutor(ChatGroupCommand(this))
        getCommand("message")?.setExecutor(MessageCommand(this))
        getCommand("reply")?.setExecutor(ReplyCommand(this))
        getCommand("snoop")?.setExecutor(SnoopCommand(this))
    }

    override fun registerListeners() {
        registerListeners(
                AsyncPlayerChatListener(this),
                PlayerCommandPreprocessListener(this)<commit_msg>Stop IRC bot from reconnecting when shutting the server down, closes #330<commit_after>/*
 * Copyright 2019 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rpkit.chat.bukkit

import com.rpkit.chat.bukkit.chatchannel.RPKChatChannelProvider
import com.rpkit.chat.bukkit.chatchannel.RPKChatChannelProviderImpl
import com.rpkit.chat.bukkit.chatchannel.directed.*
import com.rpkit.chat.bukkit.chatchannel.undirected.IRCComponent
import com.rpkit.chat.bukkit.chatchannel.undirected.LogComponent
import com.rpkit.chat.bukkit.chatchannel.undirected.UndirectedFormatComponent
import com.rpkit.chat.bukkit.chatchannel.undirected.WebComponent
import com.rpkit.chat.bukkit.chatgroup.RPKChatGroupProviderImpl
import com.rpkit.chat.bukkit.command.chatchannel.ChatChannelCommand
import com.rpkit.chat.bukkit.command.chatgroup.ChatGroupCommand
import com.rpkit.chat.bukkit.command.listchatchannels.ListChatChannelsCommand
import com.rpkit.chat.bukkit.command.message.MessageCommand
import com.rpkit.chat.bukkit.command.mute.MuteCommand
import com.rpkit.chat.bukkit.command.reply.ReplyCommand
import com.rpkit.chat.bukkit.command.snoop.SnoopCommand
import com.rpkit.chat.bukkit.command.unmute.UnmuteCommand
import com.rpkit.chat.bukkit.database.table.*
import com.rpkit.chat.bukkit.irc.RPKIRCProvider
import com.rpkit.chat.bukkit.irc.RPKIRCProviderImpl
import com.rpkit.chat.bukkit.listener.AsyncPlayerChatListener
import com.rpkit.chat.bukkit.listener.PlayerCommandPreprocessListener
import com.rpkit.chat.bukkit.mute.RPKChatChannelMuteProvider
import com.rpkit.chat.bukkit.prefix.RPKPrefixProvider
import com.rpkit.chat.bukkit.prefix.RPKPrefixProviderImpl
import com.rpkit.chat.bukkit.servlet.ChatServlet
import com.rpkit.chat.bukkit.servlet.websocket.ChatWebSocketServlet
import com.rpkit.chat.bukkit.servlet.websocket.RPKChatWebSocketProvider
import com.rpkit.chat.bukkit.snooper.RPKSnooperProviderImpl
import com.rpkit.chat.bukkit.speaker.RPKChatChannelSpeakerProvider
import com.rpkit.core.bukkit.plugin.RPKBukkitPlugin
import com.rpkit.core.database.Database
import com.rpkit.core.web.NavigationLink
import org.bstats.bukkit.Metrics
import org.bukkit.configuration.serialization.ConfigurationSerialization
import org.bukkit.permissions.Permission
import org.bukkit.permissions.PermissionDefault
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.servlet.ServletHolder

/**
 * RPK chat plugin default implementation.
 */
class RPKChatBukkit: RPKBukkitPlugin() {

    override fun onEnable() {
        Metrics(this)
        ConfigurationSerialization.registerClass(DirectedFormatComponent::class.java, "DirectedFormatComponent")
        ConfigurationSerialization.registerClass(DrunkenSlurComponent::class.java, "DrunkenSlurComponent")
        ConfigurationSerialization.registerClass(GarbleComponent::class.java, "GarbleComponent")
        ConfigurationSerialization.registerClass(RadiusFilterComponent::class.java, "RadiusFilterComponent")
        ConfigurationSerialization.registerClass(SendMessageComponent::class.java, "SendMessageComponent")
        ConfigurationSerialization.registerClass(SnoopComponent::class.java, "SnoopComponent")
        ConfigurationSerialization.registerClass(IRCComponent::class.java, "IRCComponent")
        ConfigurationSerialization.registerClass(LogComponent::class.java, "LogComponent")
        ConfigurationSerialization.registerClass(UndirectedFormatComponent::class.java, "UndirectedFormatComponent")
        ConfigurationSerialization.registerClass(WebComponent::class.java, "WebComponent")
        saveDefaultConfig()
        if (config.getBoolean("irc.enabled")) {
            val ircProvider = RPKIRCProviderImpl(this)
            val prefixProvider = RPKPrefixProviderImpl(this)
            val chatChannelProvider = RPKChatChannelProviderImpl(this)
            val chatChannelMuteProvider = RPKChatChannelMuteProvider(this)
            val chatChannelSpeakerProvider = RPKChatChannelSpeakerProvider(this)
            val chatGroupProvider = RPKChatGroupProviderImpl(this)
            val snooperProvider =  RPKSnooperProviderImpl(this)
            val chatWebSocketProvider = RPKChatWebSocketProvider()
            serviceProviders = arrayOf(
                    ircProvider,
                    prefixProvider,
                    chatChannelProvider,
                    chatChannelMuteProvider,
                    chatChannelSpeakerProvider,
                    chatGroupProvider,
                    snooperProvider,
                    chatWebSocketProvider
            )
            registerChatChannelPermissions(chatChannelProvider)
            registerPrefixPermissions(prefixProvider)
        } else {
            val prefixProvider = RPKPrefixProviderImpl(this)
            val chatChannelProvider = RPKChatChannelProviderImpl(this)
            val chatChannelMuteProvider = RPKChatChannelMuteProvider(this)
            val chatChannelSpeakerProvider = RPKChatChannelSpeakerProvider(this)
            val chatGroupProvider = RPKChatGroupProviderImpl(this)
            val snooperProvider =  RPKSnooperProviderImpl(this)
            val chatWebSocketProvider = RPKChatWebSocketProvider()
            serviceProviders = arrayOf(
                    prefixProvider,
                    chatChannelProvider,
                    chatChannelMuteProvider,
                    chatChannelSpeakerProvider,
                    chatGroupProvider,
                    snooperProvider,
                    chatWebSocketProvider
            )
            registerChatChannelPermissions(chatChannelProvider)
            registerPrefixPermissions(prefixProvider)
        }
        servlets = arrayOf(
                ChatServlet(this)
        )
    }

    override fun onPostEnable() {
        core.web.navigationBar.add(NavigationLink("Chat", "/chat/"))
        val originalClassLoader = Thread.currentThread().contextClassLoader
        Thread.currentThread().contextClassLoader = javaClass.classLoader
        (core.web.server.handler as ServletContextHandler).addServlet(ServletHolder(ChatWebSocketServlet(this)), "/chat/ws")
        Thread.currentThread().contextClassLoader = originalClassLoader
    }

    override fun onDisable() {
        if (config.getBoolean("irc.enabled")) {
            val ircBot = core.serviceManager.getServiceProvider(RPKIRCProvider::class).ircBot
            ircBot.stopBotReconnect()
            ircBot.sendIRC().quitServer(messages["irc-quit"])
        }
    }

    override fun registerCommands() {
        getCommand("chatchannel")?.setExecutor(ChatChannelCommand(this))
        getCommand("mute")?.setExecutor(MuteCommand(this))
        getCommand("unmute")?.setExecutor(UnmuteCommand(this))
        getCommand("listchatchannels")?.setExecutor(ListChatChannelsCommand(this))
        getCommand("chatgroup")?.setExecutor(ChatGroupCommand(this))
        getCommand("message")?.setExecutor(MessageCommand(this))
        getCommand("reply")?.setExecutor(ReplyCommand(this))
        getCommand("snoop")?.setExecutor(SnoopCommand(this))
    }

    override fun registerListeners() {
        registerListeners(
                AsyncPlayerChatListener(this),
                PlayerCommandPreprocessListener(this)
{'fraction_non_alphanumeric': 0.08118321605420901, 'fraction_numerical': 0.0012379463122230909, 'mean_word_length': 3.737345679012346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3959269', 'n_tokens_mistral': 4138, 'n_tokens_neox': 3977, 'n_words': 626}