<commit_before>/**
 * Copyright (C) 2013 Oldterns
 *
 * This file may be modified and distributed under the terms
 * of the MIT license. See the LICENSE file for details.
 */
package com.oldterns.vilebot.handlers.user;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.oldterns.vilebot.db.ChurchDB;
import com.oldterns.vilebot.db.QuoteFactDB;
import com.oldterns.vilebot.util.BaseNick;
import com.oldterns.vilebot.util.Ignore;

import net.engio.mbassy.listener.Handler;

import ca.szc.keratin.bot.annotation.HandlerContainer;
import ca.szc.keratin.core.event.message.interfaces.Replyable;
import ca.szc.keratin.core.event.message.recieve.ReceiveJoin;
import ca.szc.keratin.core.event.message.recieve.ReceivePrivmsg;

@HandlerContainer
public class QuotesAndFacts
{
    private static final Pattern nounPattern = Pattern.compile( "\\S+" );

    private static final Pattern addPattern = Pattern.compile( "^!(fact|quote)add (" + nounPattern + ") (.+)$" );

    private static final Pattern dumpPattern = Pattern.compile( "^!(fact|quote)dump (" + nounPattern + ")\\s*$" );

    private static final Pattern queryPattern = Pattern.compile( "^!(fact|quote) (" + nounPattern + ")\\s*$" );

    private static final Pattern searchPattern = Pattern.compile( "^!(fact|quote)search (" + nounPattern + ") (.*)$" );

    private static final Random random = new Random();

    @Handler
    private void factQuoteAdd( ReceivePrivmsg event )
    {
        Matcher matcher = addPattern.matcher( event.getText() );

        if ( matcher.matches() )
        {
            String mode = matcher.group( 1 );
            String noun = BaseNick.toBaseNick( matcher.group( 2 ) );
            String text = matcher.group( 3 );

            text = trimChars( text, " '\"" );

            if ( "fact".equals( mode ) )
            {
                QuoteFactDB.addFact( noun, text );
                event.reply( formatFactReply( noun, text ) );
            }
            else
            {
                QuoteFactDB.addQuote( noun, text );
                event.reply( formatQuoteReply( noun, text ) );
            }
        }
    }

    @Handler
    private void factQuoteDump( ReceivePrivmsg event )
    {
        Matcher matcher = dumpPattern.matcher( event.getText() );

        if ( matcher.matches() )
        {
            String mode = matcher.group( 1 );
            String queried = BaseNick.toBaseNick( matcher.group( 2 ) );

            if ( "fact".equals( mode ) )<commit_msg>Disallow self adding commands<commit_after>/**
 * Copyright (C) 2013 Oldterns
 *
 * This file may be modified and distributed under the terms
 * of the MIT license. See the LICENSE file for details.
 */
package com.oldterns.vilebot.handlers.user;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.oldterns.vilebot.db.ChurchDB;
import com.oldterns.vilebot.db.QuoteFactDB;
import com.oldterns.vilebot.util.BaseNick;
import com.oldterns.vilebot.util.Ignore;

import net.engio.mbassy.listener.Handler;

import ca.szc.keratin.bot.annotation.HandlerContainer;
import ca.szc.keratin.core.event.message.interfaces.Replyable;
import ca.szc.keratin.core.event.message.recieve.ReceiveJoin;
import ca.szc.keratin.core.event.message.recieve.ReceivePrivmsg;

@HandlerContainer
public class QuotesAndFacts
{
    private static final Pattern nounPattern = Pattern.compile( "\\S+" );

    private static final Pattern addPattern = Pattern.compile( "^!(fact|quote)add (" + nounPattern + ") (.+)$" );

    private static final Pattern dumpPattern = Pattern.compile( "^!(fact|quote)dump (" + nounPattern + ")\\s*$" );

    private static final Pattern queryPattern = Pattern.compile( "^!(fact|quote) (" + nounPattern + ")\\s*$" );

    private static final Pattern searchPattern = Pattern.compile( "^!(fact|quote)search (" + nounPattern + ") (.*)$" );

    private static final Random random = new Random();

    @Handler
    private void factQuoteAdd( ReceivePrivmsg event )
    {
        Matcher matcher = addPattern.matcher( event.getText() );

        if ( matcher.matches() )
        {
            String mode = matcher.group( 1 );
            String noun = BaseNick.toBaseNick( matcher.group( 2 ) );
            String text = matcher.group( 3 );
            String sender = BaseNick.toBaseNick( event.getSender() );

            if ( !sender.equals( noun ) ) {
                text = trimChars( text, " '\"" );

                if ( "fact".equals( mode ) )
                {
                    QuoteFactDB.addFact( noun, text );
                    event.reply( formatFactReply( noun, text ) );
                }
                else
                {
                    QuoteFactDB.addQuote( noun, text );
                    event.reply( formatQuoteReply( noun, text ) );
                }
            }
        }
    }

    @Handler
    private void factQuoteDump( ReceivePrivmsg event )
    {
        Matcher matcher = dumpPattern.matcher( event.getText() );

        if ( matcher.matches() )
        {
            String mode = matcher.group( 1 );
            String queried = BaseNick.toBaseNick( matcher.group( 2 ) );

            if ( "fact".equals( mode ) )
{'fraction_non_alphanumeric': 0.12255257432922408, 'fraction_numerical': 0.0032632342277012327, 'mean_word_length': 2.584795321637427, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2772351', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1582, 'n_words': 376}