//# 0 errors, 154 messages
//#
/*
    //#EventHandler.java:1:1: class: com.dmdirc.EventHandler
    //#EventHandler.java:1:1: method: com.dmdirc.EventHandler.com.dmdirc.EventHandler__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc;

import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.logger.Logger;
import com.dmdirc.parser.irc.IRCParser;
import com.dmdirc.parser.irc.callbacks.CallbackManager;
import com.dmdirc.parser.irc.callbacks.CallbackNotFoundException;
import com.dmdirc.parser.irc.callbacks.interfaces.ICallbackInterface;

/**
 * Abstracts some behaviour used by Event Handlers.
 * 
 * @author chris
 */
public abstract class EventHandler implements ICallbackInterface {
    //#EventHandler.java:37: method: void com.dmdirc.EventHandler.com.dmdirc.EventHandler()
    //#EventHandler.java:37: end of method: void com.dmdirc.EventHandler.com.dmdirc.EventHandler()
    
    /** The prefix indicating that the interface is a parser callback. */
    private static final String CALLBACK_PREFIX = "com.dmdirc.parser.irc.callbacks.interfaces.I";
    
    /**
     * Registers all callbacks that this event handler implements with the
     * owner's parser.
     */
    public void registerCallbacks() {
        final CallbackManager cbm = getServer().getParser().getCallbackManager();
    //#EventHandler.java:47: method: void com.dmdirc.EventHandler.registerCallbacks()
    //#EventHandler.java:47: Warning: method not available - call not analyzed
    //#    call on CallbackManager com.dmdirc.parser.irc.IRCParser:getCallbackManager()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.EventHandler
    //#    method: void registerCallbacks()
    //#    unanalyzed callee: CallbackManager com.dmdirc.parser.irc.IRCParser:getCallbackManager()
    //#EventHandler.java:47: Warning: suspicious precondition
    //#    The precondition for this.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: com.dmdirc.EventHandler
    //#    method: void registerCallbacks()
    //#    suspicious precondition index: [2]
    //#input(void registerCallbacks()): "on"._tainted
    //#input(void registerCallbacks()): __Descendant_Table[com/dmdirc/ChannelEventHandler]
    //#input(void registerCallbacks()): __Descendant_Table[com/dmdirc/EventHandler]
    //#input(void registerCallbacks()): __Descendant_Table[com/dmdirc/ServerEventHandler]
    //#input(void registerCallbacks()): __Descendant_Table[others]
    //#input(void registerCallbacks()): __Dispatch_Table.addCallback(Lcom/dmdirc/parser/irc/callbacks/CallbackManager;Ljava/lang/String;)V
    //#input(void registerCallbacks()): __Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void registerCallbacks()): com.dmdirc.logger.ErrorLevel.FATAL
    //#input(void registerCallbacks()): com/dmdirc/Channel.__Descendant_Table[com/dmdirc/Channel]
    //#input(void registerCallbacks()): com/dmdirc/Channel.__Descendant_Table[others]
    //#input(void registerCallbacks()): com/dmdirc/Channel.__Dispatch_Table.getChannelInfo()Lcom/dmdirc/parser/irc/ChannelInfo;
    //#input(void registerCallbacks()): com/dmdirc/Channel.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void registerCallbacks()): com/dmdirc/ChannelEventHandler.__Dispatch_Table.addCallback(Lcom/dmdirc/parser/irc/callbacks/CallbackManager;Ljava/lang/String;)V
    //#input(void registerCallbacks()): com/dmdirc/ChannelEventHandler.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void registerCallbacks()): com/dmdirc/Server.__Descendant_Table[com/dmdirc/Server]
    //#input(void registerCallbacks()): com/dmdirc/Server.__Descendant_Table[others]
    //#input(void registerCallbacks()): com/dmdirc/Server.__Dispatch_Table.getParser()Lcom/dmdirc/parser/irc/IRCParser;
    //#input(void registerCallbacks()): com/dmdirc/ServerEventHandler.__Dispatch_Table.addCallback(Lcom/dmdirc/parser/irc/callbacks/CallbackManager;Ljava/lang/String;)V
    //#input(void registerCallbacks()): com/dmdirc/ServerEventHandler.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void registerCallbacks()): this
    //#input(void registerCallbacks()): this.__Tag
    //#input(void registerCallbacks()): this.owner
    //#input(void registerCallbacks()): this.owner.__Tag
    //#input(void registerCallbacks()): this.owner.parser
    //#input(void registerCallbacks()): this.owner.server
    //#input(void registerCallbacks()): this.owner.server.__Tag
    //#input(void registerCallbacks()): this.owner.server.parser
    //#pre[2] (void registerCallbacks()): this.__Tag in {com/dmdirc/ChannelEventHandler, com/dmdirc/EventHandler, com/dmdirc/ServerEventHandler}
    //#pre[3] (void registerCallbacks()): this.owner != null
    //#pre[8] (void registerCallbacks()): (soft) this.owner.server != null
    //#presumption(void registerCallbacks()): arr$.length@50 <= 4_294_967_295
    //#presumption(void registerCallbacks()): arr$[i$]@50 != null
    //#presumption(void registerCallbacks()): init'ed(com.dmdirc.logger.ErrorLevel.FATAL)
    //#presumption(void registerCallbacks()): com.dmdirc.parser.irc.IRCParser:getCallbackManager(...)@47 != null
    //#presumption(void registerCallbacks()): getServer(...).__Tag@47 == com/dmdirc/Server
    //#presumption(void registerCallbacks()): getServer(...).parser@47 != null
    //#presumption(void registerCallbacks()): java.lang.Class:getInterfaces(...)@50 != null
    //#presumption(void registerCallbacks()): java.lang.Class:getName(...)@51 != null
    //#presumption(void registerCallbacks()): java.lang.Class:getName(...)@52 != null
    //#presumption(void registerCallbacks()): java.lang.Object:getClass(...)@50 != null
    //#unanalyzed(void registerCallbacks()): Effects-of-calling:com.dmdirc.parser.irc.callbacks.CallbackManager:addCallback
    //#unanalyzed(void registerCallbacks()): Effects-of-calling:getServer
    //#unanalyzed(void registerCallbacks()): Effects-of-calling:getChannelInfo
    //#unanalyzed(void registerCallbacks()): Effects-of-calling:java.lang.String:equals
    //#unanalyzed(void registerCallbacks()): Effects-of-calling:com.dmdirc.parser.irc.ChannelInfo:getName
    //#test_vector(void registerCallbacks()): java.lang.String:startsWith(...)@51: {0}, {1}
        
        try {
            for (Class iface : this.getClass().getInterfaces()) {
                if (iface.getName().startsWith(CALLBACK_PREFIX)) {
                    addCallback(cbm, "on"
                            + iface.getName().substring(CALLBACK_PREFIX.length()));
                }
            }
        } catch (CallbackNotFoundException exception) {
            Logger.appError(ErrorLevel.FATAL, "Unable to register callbacks",
    //#EventHandler.java:57: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.logger.Logger:appError(ErrorLevel, String, Throwable)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.EventHandler
    //#    method: void registerCallbacks()
    //#    unanalyzed callee: void com.dmdirc.logger.Logger:appError(ErrorLevel, String, Throwable)
                    exception);
        }
    }
    //#EventHandler.java:60: end of method: void com.dmdirc.EventHandler.registerCallbacks()
    
    /**
     * Unregisters all callbacks that have been registered by this event handler.
     */
    public void unregisterCallbacks() {
        if (getServer().getParser() != null) {
    //#EventHandler.java:66: method: void com.dmdirc.EventHandler.unregisterCallbacks()
    //#EventHandler.java:66: Warning: suspicious precondition
    //#    The precondition for this.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: com.dmdirc.EventHandler
    //#    method: void unregisterCallbacks()
    //#    suspicious precondition index: [2]
    //#input(void unregisterCallbacks()): __Descendant_Table[com/dmdirc/ChannelEventHandler]
    //#input(void unregisterCallbacks()): __Descendant_Table[com/dmdirc/EventHandler]
    //#input(void unregisterCallbacks()): __Descendant_Table[com/dmdirc/ServerEventHandler]
    //#input(void unregisterCallbacks()): __Descendant_Table[others]
    //#input(void unregisterCallbacks()): __Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void unregisterCallbacks()): com/dmdirc/Channel.__Descendant_Table[com/dmdirc/Channel]
    //#input(void unregisterCallbacks()): com/dmdirc/Channel.__Descendant_Table[others]
    //#input(void unregisterCallbacks()): com/dmdirc/Channel.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void unregisterCallbacks()): com/dmdirc/ChannelEventHandler.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void unregisterCallbacks()): com/dmdirc/Server.__Descendant_Table[com/dmdirc/Server]
    //#input(void unregisterCallbacks()): com/dmdirc/Server.__Descendant_Table[others]
    //#input(void unregisterCallbacks()): com/dmdirc/Server.__Dispatch_Table.getParser()Lcom/dmdirc/parser/irc/IRCParser;
    //#input(void unregisterCallbacks()): com/dmdirc/ServerEventHandler.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void unregisterCallbacks()): this
    //#input(void unregisterCallbacks()): this.__Tag
    //#input(void unregisterCallbacks()): this.owner
    //#input(void unregisterCallbacks()): this.owner.__Tag
    //#input(void unregisterCallbacks()): this.owner.parser
    //#input(void unregisterCallbacks()): this.owner.server
    //#input(void unregisterCallbacks()): this.owner.server.__Tag
    //#input(void unregisterCallbacks()): this.owner.server.parser
    //#pre[2] (void unregisterCallbacks()): this.__Tag in {com/dmdirc/ChannelEventHandler, com/dmdirc/EventHandler, com/dmdirc/ServerEventHandler}
    //#pre[3] (void unregisterCallbacks()): this.owner != null
    //#pre[7] (void unregisterCallbacks()): (soft) this.owner.server != null
    //#presumption(void unregisterCallbacks()): com.dmdirc.parser.irc.IRCParser:getCallbackManager(...)@67 != null
    //#presumption(void unregisterCallbacks()): getServer(...).__Tag@66 == com/dmdirc/Server
    //#presumption(void unregisterCallbacks()): getServer(...).__Tag@67 == com/dmdirc/Server
    //#presumption(void unregisterCallbacks()): getServer(...).parser@67 != null
    //#unanalyzed(void unregisterCallbacks()): Effects-of-calling:getServer
    //#test_vector(void unregisterCallbacks()): getServer(...).parser@66: Addr_Set{null}, Inverse{null}
            getServer().getParser().getCallbackManager().delAllCallback(this);
    //#EventHandler.java:67: Warning: method not available - call not analyzed
    //#    call on CallbackManager com.dmdirc.parser.irc.IRCParser:getCallbackManager()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.EventHandler
    //#    method: void unregisterCallbacks()
    //#    unanalyzed callee: CallbackManager com.dmdirc.parser.irc.IRCParser:getCallbackManager()
    //#EventHandler.java:67: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.parser.irc.callbacks.CallbackManager:delAllCallback(ICallbackInterface)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.EventHandler
    //#    method: void unregisterCallbacks()
    //#    unanalyzed callee: void com.dmdirc.parser.irc.callbacks.CallbackManager:delAllCallback(ICallbackInterface)
        }
    }
    //#EventHandler.java:69: end of method: void com.dmdirc.EventHandler.unregisterCallbacks()
    
    /**
     * Adds a callback to this event handler.
     * 
     * @param cbm The callback manager to use
     * @param name The name of the callback to be added
     * @throws com.dmdirc.parser.irc.callbacks.CallbackNotFoundException
     * if the specified callback isn't found
     */
    protected abstract void addCallback(CallbackManager cbm, String name) 
            throws CallbackNotFoundException;
    
    /**
     * Retrieves the server belonging to this EventHandler's owner.
     *
     * @since 0.6.3m1
     * @return This EventHandler's expected server
     */
    protected abstract Server getServer();
    
    /**
     * Checks that the specified parser is the same as the one the server is
     * currently claiming to be using. If it isn't, we raise an exception to
     * prevent further (erroneous) processing.
     * 
     * @param parser The parser to check
     */
    protected void checkParser(final IRCParser parser) {
        if (parser != getServer().getParser()) {
    //#EventHandler.java:98: method: void com.dmdirc.EventHandler.checkParser(IRCParser)
    //#EventHandler.java:98: Warning: suspicious precondition
    //#    The precondition for this.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: com.dmdirc.EventHandler
    //#    method: void checkParser(IRCParser)
    //#    suspicious precondition index: [3]
    //#input(void checkParser(IRCParser)): "Event called from a parser that's not in use.&#10;&#10; "._tainted
    //#input(void checkParser(IRCParser)): __Descendant_Table[com/dmdirc/ChannelEventHandler]
    //#input(void checkParser(IRCParser)): __Descendant_Table[com/dmdirc/EventHandler]
    //#input(void checkParser(IRCParser)): __Descendant_Table[com/dmdirc/ServerEventHandler]
    //#input(void checkParser(IRCParser)): __Descendant_Table[others]
    //#input(void checkParser(IRCParser)): __Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void checkParser(IRCParser)): com/dmdirc/Channel.__Descendant_Table[com/dmdirc/Channel]
    //#input(void checkParser(IRCParser)): com/dmdirc/Channel.__Descendant_Table[others]
    //#input(void checkParser(IRCParser)): com/dmdirc/Channel.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void checkParser(IRCParser)): com/dmdirc/ChannelEventHandler.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void checkParser(IRCParser)): com/dmdirc/Server.__Descendant_Table[com/dmdirc/Server]
    //#input(void checkParser(IRCParser)): com/dmdirc/Server.__Descendant_Table[others]
    //#input(void checkParser(IRCParser)): com/dmdirc/Server.__Dispatch_Table.getParser()Lcom/dmdirc/parser/irc/IRCParser;
    //#input(void checkParser(IRCParser)): com/dmdirc/Server.__Dispatch_Table.getStatus()Lcom/dmdirc/ServerStatus;
    //#input(void checkParser(IRCParser)): com/dmdirc/ServerEventHandler.__Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#input(void checkParser(IRCParser)): com/dmdirc/ServerStatus.__Descendant_Table[com/dmdirc/ServerStatus]
    //#input(void checkParser(IRCParser)): com/dmdirc/ServerStatus.__Descendant_Table[others]
    //#input(void checkParser(IRCParser)): com/dmdirc/ServerStatus.__Dispatch_Table.getTransitionHistory()Ljava/lang/String;
    //#input(void checkParser(IRCParser)): parser
    //#input(void checkParser(IRCParser)): this
    //#input(void checkParser(IRCParser)): this.__Tag
    //#input(void checkParser(IRCParser)): this.owner
    //#input(void checkParser(IRCParser)): this.owner.__Tag
    //#input(void checkParser(IRCParser)): this.owner.myState
    //#input(void checkParser(IRCParser)): this.owner.myState.__Tag
    //#input(void checkParser(IRCParser)): this.owner.myState.history
    //#input(void checkParser(IRCParser)): this.owner.parser
    //#input(void checkParser(IRCParser)): this.owner.server
    //#input(void checkParser(IRCParser)): this.owner.server.__Tag
    //#input(void checkParser(IRCParser)): this.owner.server.myState
    //#input(void checkParser(IRCParser)): this.owner.server.myState.__Tag
    //#input(void checkParser(IRCParser)): this.owner.server.myState.history
    //#input(void checkParser(IRCParser)): this.owner.server.parser
    //#pre[3] (void checkParser(IRCParser)): this.__Tag in {com/dmdirc/ChannelEventHandler, com/dmdirc/EventHandler, com/dmdirc/ServerEventHandler}
    //#pre[4] (void checkParser(IRCParser)): this.owner != null
    //#pre[8] (void checkParser(IRCParser)): (soft) this.owner.server != null
    //#presumption(void checkParser(IRCParser)): getServer(...).__Tag@98 == com/dmdirc/Server
    //#presumption(void checkParser(IRCParser)): parser == getServer(...).parser@98
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:java.lang.StringBuilder
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:com.dmdirc.util.RollingList:getList
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:java.util.List:iterator
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:java.util.Iterator:hasNext
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:java.util.Iterator:next
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:java.lang.StringBuilder:length
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:java.lang.StringBuilder:append
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:java.lang.StringBuilder:toString
    //#unanalyzed(void checkParser(IRCParser)): Effects-of-calling:getServer
            parser.disconnect("Shouldn't be in use");
            throw new IllegalArgumentException("Event called from a parser that's not in use."
                    + "\n\n " + getServer().getStatus().getTransitionHistory());
        }
    }    
    //#EventHandler.java:103: end of method: void com.dmdirc.EventHandler.checkParser(IRCParser)

}
    //#output(com.dmdirc.EventHandler__static_init): __Descendant_Table[com/dmdirc/EventHandler]
    //#output(com.dmdirc.EventHandler__static_init): __Dispatch_Table.addCallback(Lcom/dmdirc/parser/irc/callbacks/CallbackManager;Ljava/lang/String;)V
    //#output(com.dmdirc.EventHandler__static_init): __Dispatch_Table.checkParser(Lcom/dmdirc/parser/irc/IRCParser;)V
    //#output(com.dmdirc.EventHandler__static_init): __Dispatch_Table.getServer()Lcom/dmdirc/Server;
    //#output(com.dmdirc.EventHandler__static_init): __Dispatch_Table.registerCallbacks()V
    //#output(com.dmdirc.EventHandler__static_init): __Dispatch_Table.unregisterCallbacks()V
    //#post(com.dmdirc.EventHandler__static_init): __Descendant_Table[com/dmdirc/EventHandler] == &__Dispatch_Table
    //#post(com.dmdirc.EventHandler__static_init): __Dispatch_Table.addCallback(Lcom/dmdirc/parser/irc/callbacks/CallbackManager;Ljava/lang/String;)V == &addCallback
    //#post(com.dmdirc.EventHandler__static_init): __Dispatch_Table.checkParser(Lcom/dmdirc/parser/irc/IRCParser;)V == &checkParser
    //#post(com.dmdirc.EventHandler__static_init): __Dispatch_Table.getServer()Lcom/dmdirc/Server; == &getServer
    //#post(com.dmdirc.EventHandler__static_init): __Dispatch_Table.registerCallbacks()V == &registerCallbacks
    //#post(com.dmdirc.EventHandler__static_init): __Dispatch_Table.unregisterCallbacks()V == &unregisterCallbacks
    //#EventHandler.java:: end of method: com.dmdirc.EventHandler.com.dmdirc.EventHandler__static_init
    //#EventHandler.java:: end of class: com.dmdirc.EventHandler
