//# 0 errors, 49 messages
//#
/*
    //#DNSPlugin.java:1:1: class: com.dmdirc.addons.dns.DNSPlugin
    //#DNSPlugin.java:1:1: method: com.dmdirc.addons.dns.DNSPlugin.com.dmdirc.addons.dns.DNSPlugin__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.addons.dns;

import com.dmdirc.commandparser.CommandManager;
import com.dmdirc.plugins.Plugin;

import java.net.InetAddress;
import java.net.UnknownHostException;

import java.util.ArrayList;
import java.util.List;

/**
 * DNS plugin.
 */
public final class DNSPlugin extends Plugin {
    
    /** The DNSCommand we've registered. */
    private DNSCommand command;
    
    /** Creates a new instance of DNSPlugin. */
    public DNSPlugin() {
        super();
    //#DNSPlugin.java:44: method: void com.dmdirc.addons.dns.DNSPlugin.com.dmdirc.addons.dns.DNSPlugin()
    //#DNSPlugin.java:44: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.plugins.Plugin()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.dns.DNSPlugin
    //#    method: void com.dmdirc.addons.dns.DNSPlugin()
    //#    unanalyzed callee: void com.dmdirc.plugins.Plugin()
    //#input(void com.dmdirc.addons.dns.DNSPlugin()): this
    }
    //#DNSPlugin.java:45: end of method: void com.dmdirc.addons.dns.DNSPlugin.com.dmdirc.addons.dns.DNSPlugin()
    
    /** {@inheritDoc} */
    @Override
    public void onLoad() {
        command = new DNSCommand();
    //#DNSPlugin.java:50: method: void com.dmdirc.addons.dns.DNSPlugin.onLoad()
    //#input(void onLoad()): this
    //#output(void onLoad()): new DNSCommand(onLoad#1) num objects
    //#output(void onLoad()): this.command.__Tag
    //#output(void onLoad()): this.command
    //#new obj(void onLoad()): new DNSCommand(onLoad#1)
    //#post(void onLoad()): this.command == &new DNSCommand(onLoad#1)
    //#post(void onLoad()): new DNSCommand(onLoad#1) num objects == 1
    //#post(void onLoad()): this.command.__Tag == com/dmdirc/addons/dns/DNSCommand
    //#unanalyzed(void onLoad()): Effects-of-calling:com.dmdirc.commandparser.commands.GlobalCommand
    //#unanalyzed(void onLoad()): Effects-of-calling:com.dmdirc.commandparser.CommandManager:registerCommand
    }
    //#DNSPlugin.java:51: end of method: void com.dmdirc.addons.dns.DNSPlugin.onLoad()
    
    /** {@inheritDoc} */
    @Override
    public void onUnload() {
        CommandManager.unregisterCommand(command);
    //#DNSPlugin.java:56: method: void com.dmdirc.addons.dns.DNSPlugin.onUnload()
    //#DNSPlugin.java:56: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.commandparser.CommandManager:unregisterCommand(CommandInfo)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.dns.DNSPlugin
    //#    method: void onUnload()
    //#    unanalyzed callee: void com.dmdirc.commandparser.CommandManager:unregisterCommand(CommandInfo)
    //#input(void onUnload()): this
    //#input(void onUnload()): this.command
    //#pre[2] (void onUnload()): init'ed(this.command)
    }
    //#DNSPlugin.java:57: end of method: void com.dmdirc.addons.dns.DNSPlugin.onUnload()
    
    /**
     * Returns the IP(s) for a hostname.
     *
     * @param hostname Hostname to resolve.
     *
     * @return Resolved IP(s)
     */
    public static String getIPs(final String hostname) {
        List<String> results = new ArrayList<String>();
    //#DNSPlugin.java:67: method: String com.dmdirc.addons.dns.DNSPlugin.getIPs(String)
    //#input(String getIPs(String)): hostname
    //#output(String getIPs(String)): java.lang.Object:toString(...)._tainted
    //#output(String getIPs(String)): return_value
    //#new obj(String getIPs(String)): java.lang.Object:toString(...)
    //#presumption(String getIPs(String)): arr$.length@70 <= 4_294_967_295
    //#presumption(String getIPs(String)): arr$[i$]@70 != null
    //#presumption(String getIPs(String)): java.net.InetAddress:getAllByName(...)@70 != null
    //#post(String getIPs(String)): java.lang.Object:toString(...)._tainted == 0
    //#post(String getIPs(String)): return_value == &java.lang.Object:toString(...)
        
        try {
            final InetAddress[] ips = InetAddress.getAllByName(hostname);
            
            for (InetAddress ip : ips) {
                results.add(ip.getHostAddress());
            }
            
        } catch (UnknownHostException ex) {
            results = new ArrayList<String>();
        }
        
        return results.toString();
    //#DNSPlugin.java:80: end of method: String com.dmdirc.addons.dns.DNSPlugin.getIPs(String)
    }
    
    /**
     * Returns the hostname for an ip.
     *
     * @param ip IP to resolve
     *
     * @return Resolved hostname
     */
    public static String getHostname(final String ip) {
        try {
            return InetAddress.getByName(ip).getHostName();
    //#DNSPlugin.java:92: method: String com.dmdirc.addons.dns.DNSPlugin.getHostname(String)
    //#input(String getHostname(String)): ip
    //#output(String getHostname(String)): return_value
    //#presumption(String getHostname(String)): java.net.InetAddress:getByName(...)@92 != null
    //#post(String getHostname(String)): init'ed(return_value)
        } catch (UnknownHostException ex) {
            return "";
    //#DNSPlugin.java:94: end of method: String com.dmdirc.addons.dns.DNSPlugin.getHostname(String)
        }
    }
    
}
    //#output(com.dmdirc.addons.dns.DNSPlugin__static_init): __Descendant_Table[com/dmdirc/addons/dns/DNSPlugin]
    //#output(com.dmdirc.addons.dns.DNSPlugin__static_init): __Dispatch_Table.onLoad()V
    //#output(com.dmdirc.addons.dns.DNSPlugin__static_init): __Dispatch_Table.onUnload()V
    //#post(com.dmdirc.addons.dns.DNSPlugin__static_init): __Descendant_Table[com/dmdirc/addons/dns/DNSPlugin] == &__Dispatch_Table
    //#post(com.dmdirc.addons.dns.DNSPlugin__static_init): __Dispatch_Table.onLoad()V == &onLoad
    //#post(com.dmdirc.addons.dns.DNSPlugin__static_init): __Dispatch_Table.onUnload()V == &onUnload
    //#DNSPlugin.java:: end of method: com.dmdirc.addons.dns.DNSPlugin.com.dmdirc.addons.dns.DNSPlugin__static_init
    //#DNSPlugin.java:: end of class: com.dmdirc.addons.dns.DNSPlugin
