Search in sources :

Example 1 with OAuth

use of irc.account.OAuth in project Botnak by Gocnak.

the class GUIAuthorizeAccount method doneButtonActionPerformed.

private void doneButtonActionPerformed(ActionEvent e) {
    String oauth = new String(oauthField.getPassword());
    if (!oauth.startsWith("oauth:"))
        oauth = "oauth:" + oauth;
    String name = accountNameField.getText().trim().toLowerCase();
    if (!name.isEmpty() && oauth.length() == 36) {
        if (isForBotAccount) {
            if (getAccount(true) == null) {
                Settings.accountManager.setBotAccount(new Account(name, new Oauth(oauth, false, false, false, false)));
                Settings.accountManager.addTask(new Task(null, Task.Type.CREATE_BOT_ACCOUNT, null));
                if (GUIMain.settings != null) {
                    GUISettings.botUser.setText(name);
                    GUISettings.botPass.setText(oauth);
                }
            }
        } else {
            if (getAccount(false) == null) {
                Settings.accountManager.setUserAccount(new Account(name, new Oauth(oauth, boxEditStatus.isSelected(), boxCommercial.isSelected(), boxReadSubs.isSelected(), boxFollowed.isSelected())));
                Settings.accountManager.addTask(new Task(null, Task.Type.CREATE_VIEWER_ACCOUNT, null));
                if (GUIMain.settings != null) {
                    GUISettings.normUser.setText(name);
                    GUISettings.normPass.setText(oauth);
                }
            }
        }
    }
    dispose();
}
Also used : Oauth(irc.account.Oauth) Account(irc.account.Account) Task(irc.account.Task)

Example 2 with OAuth

use of irc.account.OAuth in project Botnak by Gocnak.

the class IRCBot method onMessage.

@Override
public void onMessage(String channel, String sender, String message) {
    if (message != null && channel != null && sender != null && Settings.accountManager.getViewer() != null) {
        String botnakUserName = Settings.accountManager.getUserAccount().getName();
        sender = sender.toLowerCase();
        if (!channel.contains(botnakUserName.toLowerCase())) {
            //in other channels
            int replyType = Settings.botReplyType.getValue();
            if (replyType == 0)
                return;
            //0 = reply to nobody (just spectate), 1 = reply to just the Botnak user, 2 = reply to everyone
            if (replyType == 1 && !sender.equalsIgnoreCase(botnakUserName))
                return;
        }
        boolean senderIsBot = sender.equalsIgnoreCase(getBot().getNick());
        boolean userIsBot = botnakUserName.equalsIgnoreCase(Settings.accountManager.getBotAccount().getName());
        //the user account is NOT the bot, just return, we don't want the bot to trigger anything
        if (senderIsBot && !userIsBot)
            return;
        //raffles
        User u = Settings.channelManager.getUser(sender, true);
        if (!raffles.isEmpty()) {
            if (!winners.contains(u.getNick().toLowerCase())) {
                for (Raffle r : raffles) {
                    if (r.isDone()) {
                        continue;
                    }
                    String key = r.getKeyword();
                    if (message.contains(key)) {
                        //TODO this will change to EXCLUDEDPerms and whatnot
                        int permBase = r.getPermission();
                        if (Permissions.hasAtLeast(Permissions.getUserPermissions(u, channel), permBase)) {
                            r.addUser(u.getLowerNick());
                        }
                    }
                }
            }
            ArrayList<Raffle> toRemove = new ArrayList<>();
            raffles.stream().filter(Raffle::isDone).forEach(r -> {
                winners.add(r.getWinner());
                toRemove.add(r);
            });
            if (!toRemove.isEmpty()) {
                toRemove.forEach(raffles::remove);
                toRemove.clear();
            }
        }
        Oauth key = Settings.accountManager.getUserAccount().getKey();
        String[] split = message.split(" ");
        //URL Checking
        boolean ytVidDetail = Settings.botShowYTVideoDetails.getValue();
        boolean twitchVOD = Settings.botShowTwitchVODDetails.getValue();
        boolean unshortenURLs = Settings.botUnshortenURLs.getValue();
        if (ytVidDetail || twitchVOD || unshortenURLs) {
            ThreadEngine.submit(() -> {
                int count = 0;
                for (String part : split) {
                    //only allowing 2 requests here; don't want spam
                    if (count > 1)
                        break;
                    if (part.startsWith("http") || part.startsWith("www")) {
                        if (ytVidDetail && (part.contains("youtu.be") || part.contains("youtube.com/watch") || part.contains("youtube.com/v") || part.contains("youtube.com/embed/"))) {
                            getBot().sendMessage(channel, APIRequests.YouTube.getVideoData(part).getResponseText());
                            count++;
                        } else if (unshortenURLs && (part.contains("bit.ly") || part.contains("tinyurl") || part.contains("goo.gl"))) {
                            getBot().sendMessage(channel, APIRequests.UnshortenIt.getUnshortened(part).getResponseText());
                            count++;
                        } else if (twitchVOD && part.contains("twitch.tv/")) {
                            if (part.contains("/v/") || part.contains("/c/") || part.contains("/b/")) {
                                getBot().sendMessage(channel, APIRequests.Twitch.getTitleOfVOD(part).getResponseText());
                                count++;
                            }
                        }
                    }
                }
            });
        }
        String first = "";
        if (split.length > 1)
            first = split[1];
        //commands
        if (message.startsWith("!")) {
            String trigger = message.substring(1).split(" ")[0].toLowerCase();
            String mess = message.substring(1);
            //sound
            if (SoundEngine.getEngine().soundTrigger(trigger, sender, channel)) {
                SoundEngine.getEngine().playSound(new Sound(SoundEngine.getEngine().getSoundMap().get(trigger)));
            }
            ConsoleCommand consoleCommand = Utils.getConsoleCommand(trigger, channel, u);
            if (consoleCommand != null) {
                Response commandResponse = null;
                switch(consoleCommand.getAction()) {
                    case ADD_FACE:
                    case CHANGE_FACE:
                        commandResponse = FaceManager.handleFace(mess);
                        if (commandResponse.isSuccessful())
                            Settings.FACES.save();
                        break;
                    case REMOVE_FACE:
                        commandResponse = FaceManager.removeFace(first);
                        if (commandResponse.isSuccessful())
                            Settings.FACES.save();
                        break;
                    case TOGGLE_FACE:
                        commandResponse = FaceManager.toggleFace(first);
                        break;
                    case ADD_SOUND:
                    case CHANGE_SOUND:
                        commandResponse = SoundEngine.getEngine().handleSound(mess, consoleCommand.getAction() == ConsoleCommand.Action.CHANGE_SOUND);
                        break;
                    case REMOVE_SOUND:
                        commandResponse = SoundEngine.getEngine().removeSound(first);
                        break;
                    case SET_SOUND_DELAY:
                        commandResponse = SoundEngine.getEngine().setSoundDelay(first);
                        break;
                    case TOGGLE_SOUND:
                        boolean individualSound = split.length > 1;
                        commandResponse = SoundEngine.getEngine().toggleSound(individualSound ? first : null, individualSound);
                        break;
                    case STOP_SOUND:
                    case STOP_ALL_SOUNDS:
                        commandResponse = SoundEngine.getEngine().stopSound(consoleCommand.getAction() == ConsoleCommand.Action.STOP_ALL_SOUNDS);
                        break;
                    case SEE_SOUND_STATE:
                        commandResponse = SoundEngine.getEngine().getSoundState(first);
                        break;
                    case ADD_KEYWORD:
                    case REMOVE_KEYWORD:
                        commandResponse = Utils.handleKeyword(mess);
                        if (commandResponse.isSuccessful())
                            Settings.KEYWORDS.save();
                        break;
                    case SET_USER_COL:
                        commandResponse = Utils.handleColor(sender, mess, u.getColor());
                        if (commandResponse.isSuccessful())
                            Settings.USER_COLORS.save();
                        break;
                    case SET_COMMAND_PERMISSION:
                        commandResponse = Utils.setCommandPermission(mess);
                        if (commandResponse.isSuccessful())
                            Settings.saveConCommands();
                        break;
                    case ADD_TEXT_COMMAND:
                        commandResponse = Utils.addCommands(mess);
                        if (commandResponse.isSuccessful())
                            Settings.COMMANDS.save();
                        break;
                    case REMOVE_TEXT_COMMAND:
                        commandResponse = Utils.removeCommands(first);
                        if (commandResponse.isSuccessful())
                            Settings.COMMANDS.save();
                        break;
                    case ADD_DONATION:
                        commandResponse = Settings.donationManager.parseDonation(split);
                        break;
                    case SET_SOUND_PERMISSION:
                        commandResponse = SoundEngine.getEngine().setSoundPermission(first);
                        break;
                    case SET_NAME_FACE:
                        if (first.startsWith("http")) {
                            commandResponse = FaceManager.downloadFace(first, Settings.nameFaceDir.getAbsolutePath(), Utils.setExtension(sender, ".png"), sender, FaceManager.FACE_TYPE.NAME_FACE);
                        }
                        break;
                    case REMOVE_NAME_FACE:
                        if (FaceManager.nameFaceMap.containsKey(sender)) {
                            try {
                                Face f = FaceManager.nameFaceMap.remove(sender);
                                if (f != null && new File(f.getFilePath()).delete())
                                    getBot().sendMessage(channel, "Removed face for user: " + sender + " !");
                            } catch (Exception e) {
                                getBot().sendMessage(channel, "Name face for user " + sender + " could not be removed due to an exception!");
                            }
                        } else {
                            getBot().sendMessage(channel, "The user " + sender + " has no name face!");
                        }
                        break;
                    case SET_STREAM_TITLE:
                        commandResponse = APIRequests.Twitch.setStreamStatus(key, channel, message, true);
                        if (commandResponse.isSuccessful()) {
                            if (GUIMain.statusGUI != null && GUIMain.statusGUI.isVisible()) {
                                GUIMain.statusGUI.updateStatusComponents();
                            }
                        }
                        break;
                    case SEE_STREAM_TITLE:
                        String title = APIRequests.Twitch.getTitleOfStream(channel);
                        if (!"".equals(title)) {
                            getBot().sendMessage(channel, "The title of the stream is: " + title);
                        } else {
                            getBot().sendMessage(channel, "The stream currently has no title!");
                        }
                        break;
                    case SEE_STREAM_GAME:
                        String game = APIRequests.Twitch.getGameOfStream(channel);
                        if ("".equals(game)) {
                            getBot().sendMessage(channel, "The streamer is currently not playing a game!");
                        } else {
                            getBot().sendMessage(channel, "The current game is: " + game);
                        }
                        break;
                    case SET_STREAM_GAME:
                        commandResponse = APIRequests.Twitch.setStreamStatus(key, channel, message, false);
                        if (commandResponse.isSuccessful()) {
                            if (GUIMain.statusGUI != null && GUIMain.statusGUI.isVisible()) {
                                GUIMain.statusGUI.updateStatusComponents();
                            }
                        }
                        break;
                    case PLAY_ADVERT:
                        if (key != null) {
                            commandResponse = playAdvert(key, first, channel);
                        }
                        break;
                    case START_RAFFLE:
                        if (split.length > 2) {
                            String timeString = split[2];
                            int time = Utils.getTime(timeString);
                            if (time < 1) {
                                getBot().sendMessage(channel, "Failed to start raffle, usage: !startraffle (key) (time) (permission?)");
                                break;
                            }
                            //TODO select a parameter in Settings GUI that defines the default raffle
                            int perm = 0;
                            if (split.length == 4) {
                                //because right now it's just "Everyone" unless specified with the int param
                                try {
                                    perm = Integer.parseInt(split[3]);
                                    perm = Utils.capNumber(0, 3, perm);
                                } catch (Exception ignored) {
                                //default to the specified value
                                }
                            }
                            Raffle r = new Raffle(getBot(), first, time, channel, perm);
                            r.start();
                            raffles.add(r);
                            //print the blarb
                            getBot().sendMessage(channel, r.getStartMessage());
                            getBot().sendMessage(channel, "NOTE: This is a promotion from " + channel.substring(1) + ". Twitch does not sponsor or endorse broadcaster promotions and is not responsible for them.");
                        } else {
                            getBot().sendMessage(channel, "Failed to start raffle, usage: !startraffle (key) (time) (permission?)");
                        }
                        break;
                    case ADD_RAFFLE_WINNER:
                        if (!winners.contains(first)) {
                            winners.add(first);
                            getBot().sendMessage(channel, "The user " + first + " has been added to the winners pool!");
                        } else {
                            getBot().sendMessage(channel, "The user " + first + " is already in the winners pool!");
                        }
                        break;
                    case STOP_RAFFLE:
                        Raffle toRemove = null;
                        for (Raffle r : raffles) {
                            if (r.getKeyword().equalsIgnoreCase(first)) {
                                r.setDone(true);
                                r.interrupt();
                                toRemove = r;
                                getBot().sendMessage(channel, "The raffle with key " + first + " has been stopped!");
                                break;
                            }
                        }
                        if (toRemove != null) {
                            raffles.remove(toRemove);
                        } else {
                            getBot().sendMessage(channel, "There is no such raffle \"" + first + "\" !");
                        }
                        break;
                    case REMOVE_RAFFLE_WINNER:
                        if (winners.contains(first)) {
                            if (winners.remove(first)) {
                                getBot().sendMessage(channel, "The user " + first + " was removed from the winners pool!");
                            }
                        } else {
                            getBot().sendMessage(channel, "The user " + first + " is not in the winners pool!");
                        }
                        break;
                    case SEE_WINNERS:
                        if (!winners.isEmpty()) {
                            StringBuilder stanSB = new StringBuilder();
                            stanSB.append("The current raffle winners are: ");
                            for (String name : winners) {
                                stanSB.append(name);
                                stanSB.append(", ");
                            }
                            getBot().sendMessage(channel, stanSB.toString().substring(0, stanSB.length() - 2) + " .");
                        } else {
                            getBot().sendMessage(channel, "There are no recorded winners!");
                        }
                        break;
                    case START_POLL:
                        if (poll != null) {
                            if (poll.isDone()) {
                                createPoll(channel, message);
                            } else {
                                getBot().sendMessage(channel, "Cannot start a poll with one currently running!");
                            }
                        } else {
                            createPoll(channel, message);
                        }
                        break;
                    case POLL_RESULT:
                        if (poll != null) {
                            poll.printResults();
                        } else {
                            getBot().sendMessage(channel, "There never was a poll!");
                        }
                        break;
                    case CANCEL_POLL:
                        if (poll != null) {
                            if (poll.isDone()) {
                                getBot().sendMessage(channel, "The poll is already finished!");
                            } else {
                                poll.interrupt();
                                getBot().sendMessage(channel, "The poll has been stopped.");
                            }
                        } else {
                            getBot().sendMessage(channel, "There is no current poll!");
                        }
                        break;
                    case VOTE_POLL:
                        if (poll != null) {
                            if (!poll.isDone()) {
                                int option;
                                try {
                                    option = Integer.parseInt(first);
                                } catch (Exception e) {
                                    break;
                                }
                                poll.addVote(sender, option);
                            }
                        }
                        break;
                    case NOW_PLAYING:
                        commandResponse = APIRequests.LastFM.getCurrentlyPlaying();
                        break;
                    case SHOW_UPTIME:
                        commandResponse = APIRequests.Twitch.getUptimeString(channel.substring(1));
                        break;
                    case SEE_PREV_SOUND_DON:
                        if (Settings.botShowPreviousDonSound.getValue()) {
                            if (Settings.loadedDonationSounds)
                                commandResponse = SoundEngine.getEngine().getLastDonationSound();
                        }
                        break;
                    case SEE_PREV_SOUND_SUB:
                        if (Settings.botShowPreviousSubSound.getValue()) {
                            if (Settings.loadedSubSounds)
                                commandResponse = SoundEngine.getEngine().getLastSubSound();
                        }
                        break;
                    case SEE_OR_SET_REPLY_TYPE:
                        commandResponse = parseReplyType(first, botnakUserName);
                        break;
                    case SEE_OR_SET_VOLUME:
                        if ("".equals(first)) {
                            getBot().sendMessage(channel, "The current Sound volume is " + String.format("%.1f", Settings.soundVolumeGain.getValue()));
                        } else {
                            try {
                                Float volume = Float.parseFloat(first);
                                volume = Utils.capNumber(0F, 100F, volume);
                                Settings.soundVolumeGain.setValue(volume);
                                getBot().sendMessage(channel, "The Sound volume was successfully set to " + String.format("%.1f", Settings.soundVolumeGain.getValue()));
                            } catch (Exception e) {
                                getBot().sendMessage(channel, "Failed to change Sound volume! Usage: \"!volume (number)\"");
                            }
                        }
                        break;
                    default:
                        break;
                }
                if (commandResponse != null && !"".equals(commandResponse.getResponseText()))
                    getBot().sendMessage(channel, commandResponse.getResponseText());
            }
            //text command
            Command c = Utils.getCommand(trigger);
            //could infinite loop (two commands calling each other over and over)
            if (c != null && !senderIsBot && c.getMessage().data.length > 0 && !c.getDelayTimer().isRunning()) {
                StringArray sa = c.getMessage();
                if (c.hasArguments()) {
                    //build arguments if it has any
                    int argAmount = c.countArguments();
                    if ((split.length - 1) < argAmount) {
                        getBot().sendMessage(channel, "Missing command arguments! Command format: " + c.printCommand());
                        return;
                    }
                    String[] definedArguments = new String[argAmount];
                    System.arraycopy(split, 1, definedArguments, 0, argAmount);
                    sa = c.buildMessage(sa, definedArguments);
                }
                //send the message
                for (String s : sa.data) {
                    getBot().sendMessage(channel, s);
                }
                c.getDelayTimer().reset();
            }
        }
    }
}
Also used : User(lib.pircbot.User) ArrayList(java.util.ArrayList) Sound(sound.Sound) ConsoleCommand(util.comm.ConsoleCommand) Oauth(irc.account.Oauth) ConsoleCommand(util.comm.ConsoleCommand) Command(util.comm.Command) Raffle(util.misc.Raffle) Face(face.Face) File(java.io.File)

Example 3 with OAuth

use of irc.account.OAuth in project Botnak by Gocnak.

the class Settings method savePropData.

public static void savePropData(int type) {
    Properties p = new Properties();
    File writerFile = null;
    String detail = "";
    switch(type) {
        case 0:
            Account user = accountManager.getUserAccount();
            Account bot = accountManager.getBotAccount();
            if (user != null) {
                Oauth key = user.getKey();
                p.put("UserNorm", user.getName());
                p.put("UserNormPass", key.getKey());
                p.put("CanStatus", String.valueOf(key.canSetTitle()));
                p.put("CanCommercial", String.valueOf(key.canPlayAd()));
                p.put("CanParseSubscribers", String.valueOf(key.canReadSubscribers()));
                p.put("CanParseFollowedStreams", String.valueOf(key.canReadFollowed()));
            }
            if (bot != null) {
                Oauth key = bot.getKey();
                p.put("UserBot", bot.getName());
                p.put("UserBotPass", key.getKey());
            }
            writerFile = accountsFile;
            detail = "Account Info";
            break;
        case 1:
            settings.forEach(s -> s.save(p));
            writerFile = defaultsFile;
            detail = "Defaults/Other Settings";
            break;
        default:
            break;
    }
    if (writerFile != null) {
        try {
            p.store(new FileWriter(writerFile), detail);
        } catch (Exception e) {
            GUIMain.log(e);
        }
    } else {
        GUIMain.log("Failed to store settings due to some unforseen exception!");
    }
}
Also used : Oauth(irc.account.Oauth) Account(irc.account.Account)

Example 4 with OAuth

use of irc.account.OAuth in project Botnak by Gocnak.

the class Settings method loadPropData.

/**
     * *********VOIDS*************
     */
public static void loadPropData(int type) {
    Properties p = new Properties();
    if (type == 0) {
        //accounts
        try {
            p.load(new FileInputStream(accountsFile));
            String userNorm = p.getProperty("UserNorm", "").toLowerCase();
            String userNormPass = p.getProperty("UserNormPass", "");
            String status = p.getProperty("CanStatus", "false");
            String commercial = p.getProperty("CanCommercial", "false");
            if (!userNorm.equals("") && !userNormPass.equals("") && userNormPass.contains("oauth")) {
                boolean stat = Boolean.parseBoolean(status);
                if (!stat) {
                    GUIMain.instance.updateStatusOption.setEnabled(false);
                    GUIMain.instance.updateStatusOption.setToolTipText("Enable \"Edit Title and Game\" in the Authorize GUI to use this feature.");
                }
                boolean ad = Boolean.parseBoolean(commercial);
                if (!ad) {
                    GUIMain.instance.runAdMenu.setEnabled(false);
                    GUIMain.instance.runAdMenu.setToolTipText("Enable \"Play Commercials\" in the Authorize GUI to use this feature.");
                }
                boolean subs = Boolean.parseBoolean(p.getProperty("CanParseSubscribers", "false"));
                boolean followed = Boolean.parseBoolean(p.getProperty("CanParseFollowedStreams", "false"));
                if (followed)
                    trackFollowers.setValue(true);
                accountManager.setUserAccount(new Account(userNorm, new Oauth(userNormPass, stat, ad, subs, followed)));
            }
            String userBot = p.getProperty("UserBot", "").toLowerCase();
            String userBotPass = p.getProperty("UserBotPass", "");
            if (!userBot.equals("") && !userBotPass.equals("") && userBotPass.contains("oauth")) {
                accountManager.setBotAccount(new Account(userBot, new Oauth(userBotPass, false, false, false, false)));
            }
            if (accountManager.getUserAccount() != null) {
                accountManager.addTask(new Task(null, Task.Type.CREATE_VIEWER_ACCOUNT, null));
            }
            if (accountManager.getBotAccount() != null) {
                accountManager.addTask(new Task(null, Task.Type.CREATE_BOT_ACCOUNT, null));
            }
        } catch (Exception e) {
            GUIMain.log(e);
        }
    }
    if (type == 1) {
        //defaults
        try {
            p.load(new FileInputStream(defaultsFile));
            settings.forEach(s -> s.load(p));
            if (logChat.getValue())
                logDir.mkdirs();
            GUIMain.log("Loaded defaults!");
        } catch (Exception e) {
            GUIMain.log(e);
        }
    }
}
Also used : Oauth(irc.account.Oauth) Account(irc.account.Account) Task(irc.account.Task)

Aggregations

Oauth (irc.account.Oauth)4 Account (irc.account.Account)3 Task (irc.account.Task)2 Face (face.Face)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 User (lib.pircbot.User)1 Sound (sound.Sound)1 Command (util.comm.Command)1 ConsoleCommand (util.comm.ConsoleCommand)1 Raffle (util.misc.Raffle)1