org.manalith.ircbot.ManalithBot.java Source code

Java tutorial

Introduction

Here is the source code for org.manalith.ircbot.ManalithBot.java

Source

/*
org.manalith.ircbot/ManalithBot.java
ManalithBot - An open source IRC bot based on the PircBot Framework.
Copyright (C) 2005, 2011  Ki-Beom, Kim
Copyright (C) 2011, 2012  Seong-ho, Cho <darkcircle.0426@gmail.com>
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.manalith.ircbot;

import java.nio.charset.Charset;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.manalith.ircbot.plugin.EventDispatcher;
import org.manalith.ircbot.plugin.relay.RelayPlugin;
import org.manalith.ircbot.resources.MessageEvent;
import org.manalith.ircbot.util.AppContextUtils;
import org.manalith.ircbot.util.UrlUtils;
import org.pircbotx.PircBotXUtf8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ManalithBot extends PircBotXUtf8 {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private Configuration configuration;

    @Autowired
    private EventDispatcher eventDispatcher;

    public ManalithBot(org.pircbotx.Configuration<ManalithBot> configuration) {
        super(configuration);
    }

    public static ManalithBot getInstance() {
        return AppContextUtils.getApplicationContext().getBean(ManalithBot.class);
    }

    public void invokeMessageEvent(MessageEvent event) {
        eventDispatcher.dispatchMessageEvent(event);
    }

    public void sendMessage(String target, String message) {
        sendMessage(target, message, true);
    }

    public void sendMessage(String target, String message, boolean needRelay) {
        sendIRC().message(target, message);

        logger.info("MESSAGE(LOCAL) : {} / {}", target, message);

        if (needRelay && RelayPlugin.RELAY_BOT != null)
            RelayPlugin.RELAY_BOT.sendIRC().message(target, message);
    }

    public Configuration getManalithBotConfiguration() {
        return configuration;
    }

    public static void main(String[] args) throws Exception {
        // ? 
        if (!Charset.defaultCharset().toString().equals("UTF-8")) {
            System.out.println("-Dfile.encoding=UTF-8   .");
            return;
        }

        //  ?
        Options options = new Options();
        options.addOption("c", true, "config file");

        CommandLineParser parser = new PosixParser();
        CommandLine cmd = parser.parse(options, args);

        String configFile = cmd.getOptionValue("c", "config.xml");

        // SSL/HTTPS 
        UrlUtils.setTrustAllHosts();

        //  
        ApplicationContext context = new FileSystemXmlApplicationContext(configFile);
        AppContextUtils.setApplicationContext(context);

        //  ?
        ManalithBot bot = context.getBean(ManalithBot.class);
        bot.startBot();
    }
}