Search in sources :

Example 1 with PircBotX

use of org.pircbotx.PircBotX in project ircbot-plugin by jenkinsci.

the class IRCConnection method connect.

// @Override
public boolean connect() {
    try {
        LOGGER.info(String.format("Connecting to %s:%s as %s using charset %s", this.descriptor.getHost(), this.descriptor.getPort(), this.descriptor.getNick(), this.descriptor.getCharset()));
        if (botThread != null) {
            botThread.interrupt();
        }
        final CountDownLatch connectLatch = new CountDownLatch(1);
        ListenerAdapter<PircBotX> connectListener = new ListenerAdapter<PircBotX>() {

            @Override
            public void onConnect(ConnectEvent<PircBotX> event) throws Exception {
                connectLatch.countDown();
                LOGGER.info("connected to IRC");
            }
        };
        cfg.addListener(connectListener);
        botThread = new Thread("IRC Bot") {

            public void run() {
                pircConnection = new PircBotX(cfg.buildConfiguration());
                try {
                    pircConnection.startBot();
                } catch (Exception e) {
                    LOGGER.warning("Error connecting to irc: " + e);
                }
            }
        };
        botThread.start();
        try {
            boolean connected = connectLatch.await(2, TimeUnit.MINUTES);
            if (!connected) {
                LOGGER.warning("Time out waiting for connecting to irc");
                close();
                return false;
            }
        } catch (InterruptedException e) {
            LOGGER.warning("Interrupted waiting for connecting to irc: " + e);
            Thread.currentThread().interrupt();
        }
        pircConnection.getConfiguration().getListenerManager().removeListener(connectListener);
        // final String nickServPassword = this.descriptor.getNickServPassword();
        // if(Util.fixEmpty(nickServPassword) != null) {
        // this.pircConnection.identify(nickServPassword);
        // 
        // if (!this.groupChats.isEmpty()) {
        // // Sleep some time so chances are good we're already identified
        // // when we try to join the channels.
        // // Unfortunately there seems to be no standard way in IRC to recognize
        // // if one has been identified already.
        // LOGGER.fine("Sleeping some time to wait for being authenticated");
        // try {
        // Thread.sleep(TimeUnit.SECONDS.toMillis(5));
        // } catch (InterruptedException e) {
        // // ignore
        // }
        // }
        // }
        joinGroupChats();
        return pircConnection.isConnected();
    } catch (RuntimeException e) {
        LOGGER.log(WARNING, "Error connecting to irc", e);
        return false;
    }
}
Also used : ListenerAdapter(org.pircbotx.hooks.ListenerAdapter) ConnectEvent(org.pircbotx.hooks.events.ConnectEvent) CountDownLatch(java.util.concurrent.CountDownLatch) PircBotX(org.pircbotx.PircBotX) IMException(hudson.plugins.im.IMException)

Example 2 with PircBotX

use of org.pircbotx.PircBotX in project LanteaBot by PC-Logix.

the class Twitter method onMessage.

@Override
public void onMessage(final MessageEvent event) throws Exception {
    PircBotX bot = event.getBot();
    if (Config.getTwitCKey() != null || Config.getTwitCSecret() != null || Config.getTwitToken() != null || Config.getTwitTSecret() != null) {
        if (!IRCBot.isIgnored(event.getUser().getNick())) {
            String prefix = Config.commandprefix;
            String ourinput = event.getMessage().toLowerCase();
            String trigger = ourinput.trim();
            if (trigger.length() > 1) {
                String[] firstWord = StringUtils.split(trigger);
                String triggerWord = firstWord[0];
                String param = event.getMessage().substring(event.getMessage().indexOf(triggerWord) + triggerWord.length()).trim();
                if (triggerWord.equals(prefix + "twitter")) {
                    boolean isOp = Permissions.isOp(event.getBot(), event.getUser());
                    if (isOp || Helper.isChannelOp(event)) {
                        if (param.equals("enable") && !enabledChannels.contains(event.getChannel().getName())) {
                            try {
                                enabledChannels.add(event.getChannel().getName());
                                PreparedStatement enableHook = IRCBot.getInstance().getPreparedStatement("enableHook");
                                enableHook.setString(1, "Twitter");
                                enableHook.setString(2, event.getChannel().getName());
                                enableHook.executeUpdate();
                                event.respond("Enabled Twitter");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return;
                        } else if (param.equals("disable") && enabledChannels.contains(event.getChannel().getName())) {
                            try {
                                enabledChannels.remove(event.getChannel().getName());
                                PreparedStatement disableHook = IRCBot.getInstance().getPreparedStatement("disableHook");
                                disableHook.setString(1, "Twitter");
                                disableHook.setString(2, event.getChannel().getName());
                                disableHook.executeUpdate();
                                event.respond("Disabled Twitter");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return;
                        }
                    }
                }
            }
            if (enabledChannels.contains(event.getChannel().getName()) && twitter != null) {
                String[] splitMessage = event.getMessage().split(" ");
                for (String aSplitMessage : splitMessage) {
                    if (aSplitMessage.contains("twitter.com") && aSplitMessage.contains("/status/")) {
                        int index = aSplitMessage.lastIndexOf("/") + 1;
                        long status = Long.parseLong(aSplitMessage.substring(index));
                        try {
                            Status lookup = twitter.showStatus(status);
                            // Don't double-embed this in Discord.
                            String pattern = "(https?:\\/\\/\\S+)";
                            String text = lookup.getText().replaceAll(pattern, "<$1>");
                            bot.sendIRC().message(event.getChannel().getName(), lookup.getCreatedAt() + " @" + lookup.getUser().getScreenName() + ": " + text);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
Also used : Status(twitter4j.Status) PreparedStatement(java.sql.PreparedStatement) PircBotX(org.pircbotx.PircBotX)

Aggregations

PircBotX (org.pircbotx.PircBotX)2 IMException (hudson.plugins.im.IMException)1 PreparedStatement (java.sql.PreparedStatement)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ListenerAdapter (org.pircbotx.hooks.ListenerAdapter)1 ConnectEvent (org.pircbotx.hooks.events.ConnectEvent)1 Status (twitter4j.Status)1