com.techcavern.pircbotz.cap.EnableCapHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.techcavern.pircbotz.cap.EnableCapHandler.java

Source

/**
 * Copyright (C) 2014 Julian Zhou <jzhou at techcavern.com>
 *
 * This file is part of PircBotZ.
 *
 * PircBotZ 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.
 *
 * PircBotZ 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 PircBotZ. If not, see <http://www.gnu.org/licenses/>.
 */
package com.techcavern.pircbotz.cap;

import com.google.common.collect.ImmutableList;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import com.techcavern.pircbotz.PircBotZ;
import com.techcavern.pircbotz.exception.CAPException;

/**
 * Enables the specified capability with the server. This handler should cover
 * almost all CAP features except SASL since most only need to be requested.
 * @author Originally by:
 * <a href="http://pircbotx.googlecode.com">Leon Blakey <lord.quackstar at gmail.com> in PircBotX</a>
 * <p>Forked and Maintained by Julian Zhou <jzhou at techcavern.com> in <a href="https://github.com/TechCavern/PircBotZ">PircBotZ</a> */
@Slf4j
@RequiredArgsConstructor
@ToString
public class EnableCapHandler implements CapHandler {
    @Getter
    protected final String cap;
    protected final boolean ignoreFail;

    /**
     * Create EnableCapHandler not ignoring if server doesn't support the requested
     * capability
     */
    public EnableCapHandler(String cap) {
        this.cap = cap;
        this.ignoreFail = false;
    }

    public boolean handleLS(PircBotZ bot, ImmutableList<String> capabilities) throws CAPException {
        if (capabilities.contains(cap))
            //Server supports capability, send request to use it
            bot.sendCAP().request(cap);
        else if (!ignoreFail)
            throw new CAPException(CAPException.Reason.UnsupportedCapability, cap);
        else {
            //Server doesn't support capability but were ignoring exceptions
            log.debug("Unsupported capability " + cap);
            return true;
        }
        log.debug("Supported capability " + cap);
        //Not finished yet
        return false;
    }

    public boolean handleACK(PircBotZ bot, ImmutableList<String> capabilities) throws CAPException {
        //Finished if the server is acknowledging the capability
        return capabilities.contains(cap);
    }

    public boolean handleNAK(PircBotZ bot, ImmutableList<String> capabilities) throws CAPException {
        if (capabilities.contains(cap)) {
            //Make sure the bot didn't register this capability
            bot.getEnabledCapabilities().remove(cap);
            if (!ignoreFail)
                throw new CAPException(CAPException.Reason.UnsupportedCapability, cap);
            else
                //Nothing more to do
                return true;
        }
        //Not applicable to us
        return false;
    }

    public boolean handleUnknown(PircBotZ bot, String rawLine) {
        return false;
    }
}