//# 0 errors, 112 messages
//#
/*
    //#IRCAuthenticator.java:1:1: class: com.dmdirc.parser.irc.IRCAuthenticator
 * 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.parser.irc;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

import java.util.Map;
import java.util.HashMap;

/**
 * Handles proxy authentication for the parser.
 * 
 * @author Shane Mc Cormack
 * @see IRCParser
 */
public class IRCAuthenticator extends Authenticator {
	/**
	 * A version number for this class. It should be changed whenever the class
	 * structure is changed (or anything else that would prevent serialized
	 * objects being unserialized with the new class).
	 */
	private static final long serialVersionUID = 1;

	/** Singleton instance of IRCAuthenticator. */
	private static IRCAuthenticator me = null;
    //#IRCAuthenticator.java:46: method: com.dmdirc.parser.irc.IRCAuthenticator.com.dmdirc.parser.irc.IRCAuthenticator__static_init
    //#output(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Descendant_Table[com/dmdirc/parser/irc/IRCAuthenticator]
    //#output(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Dispatch_Table.addAuthentication(Lcom/dmdirc/parser/irc/ServerInfo;)V
    //#output(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Dispatch_Table.addAuthentication(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V
    //#output(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Dispatch_Table.getPasswordAuthentication()Ljava/net/PasswordAuthentication;
    //#output(com.dmdirc.parser.irc.IRCAuthenticator__static_init): me
    //#post(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Descendant_Table[com/dmdirc/parser/irc/IRCAuthenticator] == &__Dispatch_Table
    //#post(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Dispatch_Table.addAuthentication(Lcom/dmdirc/parser/irc/ServerInfo;)V == &addAuthentication
    //#post(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Dispatch_Table.addAuthentication(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V == &addAuthentication
    //#post(com.dmdirc.parser.irc.IRCAuthenticator__static_init): __Dispatch_Table.getPasswordAuthentication()Ljava/net/PasswordAuthentication; == &getPasswordAuthentication
    //#post(com.dmdirc.parser.irc.IRCAuthenticator__static_init): me == null
    //#IRCAuthenticator.java:46: end of method: com.dmdirc.parser.irc.IRCAuthenticator.com.dmdirc.parser.irc.IRCAuthenticator__static_init
	
	/** List of authentication replies. */
	private final Map<String,PasswordAuthentication> replies = new HashMap<String,PasswordAuthentication>();
	
	/**
	 * Create a new IRCAuthenticator.
	 *
	 * This creates an IRCAuthenticator and registers it as the default
	 * Authenticator.
	 */
	private IRCAuthenticator() {
    //#IRCAuthenticator.java:57: method: void com.dmdirc.parser.irc.IRCAuthenticator.com.dmdirc.parser.irc.IRCAuthenticator()
    //#input(void com.dmdirc.parser.irc.IRCAuthenticator()): this
    //#output(void com.dmdirc.parser.irc.IRCAuthenticator()): new HashMap(IRCAuthenticator#1) num objects
    //#output(void com.dmdirc.parser.irc.IRCAuthenticator()): this.replies
    //#new obj(void com.dmdirc.parser.irc.IRCAuthenticator()): new HashMap(IRCAuthenticator#1)
    //#post(void com.dmdirc.parser.irc.IRCAuthenticator()): this.replies == &new HashMap(IRCAuthenticator#1)
    //#post(void com.dmdirc.parser.irc.IRCAuthenticator()): new HashMap(IRCAuthenticator#1) num objects == 1
/*		try {
			final Field field = Authenticator.class.getDeclaredField("theAuthenticator");
			field.setAccessible(true);
			final Object authenticator = field.get(null);
			if (authenticator instanceof Authenticator) {
				oldAuthenticator = (Authenticator)authenticator;
			}
		} catch (NoSuchFieldException nsfe) {
		} catch (IllegalAccessException iae) {
		}*/
		Authenticator.setDefault(this);
	}
    //#IRCAuthenticator.java:69: end of method: void com.dmdirc.parser.irc.IRCAuthenticator.com.dmdirc.parser.irc.IRCAuthenticator()
	
	/**
	 * Get the instance of IRCAuthenticator.
	 *
	 * @return The IRCAuthenticator instance.
	 */
	public static synchronized IRCAuthenticator getIRCAuthenticator() {
		if (me == null) {
    //#IRCAuthenticator.java:77: method: IRCAuthenticator com.dmdirc.parser.irc.IRCAuthenticator.getIRCAuthenticator()
    //#input(IRCAuthenticator getIRCAuthenticator()): __Class_Obj.__Lock
    //#input(IRCAuthenticator getIRCAuthenticator()): me
    //#output(IRCAuthenticator getIRCAuthenticator()): me
    //#output(IRCAuthenticator getIRCAuthenticator()): new HashMap(IRCAuthenticator#1) num objects
    //#output(IRCAuthenticator getIRCAuthenticator()): new IRCAuthenticator(getIRCAuthenticator#1) num objects
    //#output(IRCAuthenticator getIRCAuthenticator()): new IRCAuthenticator(getIRCAuthenticator#1).__Tag
    //#output(IRCAuthenticator getIRCAuthenticator()): new IRCAuthenticator(getIRCAuthenticator#1).replies
    //#output(IRCAuthenticator getIRCAuthenticator()): return_value
    //#new obj(IRCAuthenticator getIRCAuthenticator()): new HashMap(IRCAuthenticator#1)
    //#new obj(IRCAuthenticator getIRCAuthenticator()): new IRCAuthenticator(getIRCAuthenticator#1)
    //#pre[1] (IRCAuthenticator getIRCAuthenticator()): init'ed(me)
    //#post(IRCAuthenticator getIRCAuthenticator()): me == One-of{old me, &new IRCAuthenticator(getIRCAuthenticator#1)}
    //#post(IRCAuthenticator getIRCAuthenticator()): me != null
    //#post(IRCAuthenticator getIRCAuthenticator()): return_value == me
    //#post(IRCAuthenticator getIRCAuthenticator()): new HashMap(IRCAuthenticator#1) num objects <= 1
    //#post(IRCAuthenticator getIRCAuthenticator()): new IRCAuthenticator(getIRCAuthenticator#1) num objects <= 1
    //#post(IRCAuthenticator getIRCAuthenticator()): new IRCAuthenticator(getIRCAuthenticator#1).__Tag == com/dmdirc/parser/irc/IRCAuthenticator
    //#post(IRCAuthenticator getIRCAuthenticator()): new IRCAuthenticator(getIRCAuthenticator#1).replies == &new HashMap(IRCAuthenticator#1)
    //#unanalyzed(IRCAuthenticator getIRCAuthenticator()): Effects-of-calling:java.net.Authenticator
    //#unanalyzed(IRCAuthenticator getIRCAuthenticator()): Effects-of-calling:java.util.HashMap
    //#unanalyzed(IRCAuthenticator getIRCAuthenticator()): Effects-of-calling:java.net.Authenticator:setDefault
    //#test_vector(IRCAuthenticator getIRCAuthenticator()): me: Inverse{null}, Addr_Set{null}
			me = new IRCAuthenticator();
		}
		return me;
    //#IRCAuthenticator.java:80: end of method: IRCAuthenticator com.dmdirc.parser.irc.IRCAuthenticator.getIRCAuthenticator()
	}

	/**
	 * Add a server to authenticate for.
	 *
	 * @param server ServerInfo object with proxy details.
	 */
	public void addAuthentication(final ServerInfo server) {
		addAuthentication(server.getProxyHost(), server.getProxyPort(), server.getProxyUser(), server.getProxyPass());
    //#IRCAuthenticator.java:89: method: void com.dmdirc.parser.irc.IRCAuthenticator.addAuthentication(ServerInfo)
    //#input(void addAuthentication(ServerInfo)): ":"._tainted
    //#input(void addAuthentication(ServerInfo)): __Descendant_Table[com/dmdirc/parser/irc/IRCAuthenticator]
    //#input(void addAuthentication(ServerInfo)): __Descendant_Table[others]
    //#input(void addAuthentication(ServerInfo)): __Dispatch_Table.addAuthentication(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V
    //#input(void addAuthentication(ServerInfo)): com/dmdirc/parser/irc/ServerInfo.__Descendant_Table[com/dmdirc/parser/irc/ServerInfo]
    //#input(void addAuthentication(ServerInfo)): com/dmdirc/parser/irc/ServerInfo.__Descendant_Table[others]
    //#input(void addAuthentication(ServerInfo)): com/dmdirc/parser/irc/ServerInfo.__Dispatch_Table.getProxyHost()Ljava/lang/String;
    //#input(void addAuthentication(ServerInfo)): com/dmdirc/parser/irc/ServerInfo.__Dispatch_Table.getProxyPass()Ljava/lang/String;
    //#input(void addAuthentication(ServerInfo)): com/dmdirc/parser/irc/ServerInfo.__Dispatch_Table.getProxyPort()I
    //#input(void addAuthentication(ServerInfo)): com/dmdirc/parser/irc/ServerInfo.__Dispatch_Table.getProxyUser()Ljava/lang/String;
    //#input(void addAuthentication(ServerInfo)): server
    //#input(void addAuthentication(ServerInfo)): server.__Tag
    //#input(void addAuthentication(ServerInfo)): server.proxyHost
    //#input(void addAuthentication(ServerInfo)): server.proxyPass
    //#input(void addAuthentication(ServerInfo)): server.proxyPort
    //#input(void addAuthentication(ServerInfo)): server.proxyUser
    //#input(void addAuthentication(ServerInfo)): this
    //#input(void addAuthentication(ServerInfo)): this.__Tag
    //#input(void addAuthentication(ServerInfo)): this.replies
    //#pre[1] (void addAuthentication(ServerInfo)): server != null
    //#pre[2] (void addAuthentication(ServerInfo)): server.__Tag == com/dmdirc/parser/irc/ServerInfo
    //#pre[4] (void addAuthentication(ServerInfo)): init'ed(server.proxyPass)
    //#pre[5] (void addAuthentication(ServerInfo)): init'ed(server.proxyPort)
    //#pre[6] (void addAuthentication(ServerInfo)): init'ed(server.proxyUser)
    //#pre[8] (void addAuthentication(ServerInfo)): this.__Tag == com/dmdirc/parser/irc/IRCAuthenticator
    //#pre[3] (void addAuthentication(ServerInfo)): (soft) server.proxyHost != null
    //#pre[9] (void addAuthentication(ServerInfo)): (soft) this.replies != null
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.lang.StringBuilder
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.lang.String:toLowerCase
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.lang.StringBuilder:append
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.lang.StringBuilder:toString
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.lang.String:isEmpty
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.lang.String:toCharArray
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.net.PasswordAuthentication
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.util.Map:containsKey
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.util.Map:remove
    //#unanalyzed(void addAuthentication(ServerInfo)): Effects-of-calling:java.util.Map:put
	}
    //#IRCAuthenticator.java:90: end of method: void com.dmdirc.parser.irc.IRCAuthenticator.addAuthentication(ServerInfo)

	/**
	 * Add a host to authenticate for.
	 *
	 * @param host Hostname
	 * @param port Port
	 * @param username Username to return for authentication
	 * @param password Password to return for authentication
	 */
	public void addAuthentication(final String host, final int port, final String username, final String password) {
		if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
    //#IRCAuthenticator.java:101: method: void com.dmdirc.parser.irc.IRCAuthenticator.addAuthentication(String, int, String, String)
    //#input(void addAuthentication(String, int, String, String)): ":"._tainted
    //#input(void addAuthentication(String, int, String, String)): host
    //#input(void addAuthentication(String, int, String, String)): password
    //#input(void addAuthentication(String, int, String, String)): port
    //#input(void addAuthentication(String, int, String, String)): this
    //#input(void addAuthentication(String, int, String, String)): this.replies
    //#input(void addAuthentication(String, int, String, String)): username
    //#pre[4] (void addAuthentication(String, int, String, String)): (soft) host != null
    //#pre[8] (void addAuthentication(String, int, String, String)): (soft) this.replies != null
    //#test_vector(void addAuthentication(String, int, String, String)): password: Addr_Set{null}, Inverse{null}
    //#test_vector(void addAuthentication(String, int, String, String)): username: Addr_Set{null}, Inverse{null}
    //#test_vector(void addAuthentication(String, int, String, String)): java.lang.String:isEmpty(...)@101: {1}, {0}
    //#test_vector(void addAuthentication(String, int, String, String)): java.lang.String:isEmpty(...)@101: {0}, {1}
    //#test_vector(void addAuthentication(String, int, String, String)): java.util.Map:containsKey(...)@107: {0}, {1}
			return;
		}
		final PasswordAuthentication pass = new PasswordAuthentication(username, password.toCharArray());
		final String fullhost = host.toLowerCase()+":"+port;
		
		if (replies.containsKey(fullhost)) {
			replies.remove(fullhost);
		}
		
		replies.put(fullhost, pass);
	}
    //#IRCAuthenticator.java:112: end of method: void com.dmdirc.parser.irc.IRCAuthenticator.addAuthentication(String, int, String, String)
	
	/** {@inheritDoc} */
	protected PasswordAuthentication getPasswordAuthentication() {
		/*
		 * getRequestingHost: 85.234.138.2
		 * getRequestingPort: 1080
		 * getRequestingPrompt: SOCKS authentication
		 * getRequestingProtocol: SOCKS5
		 * getRequestingScheme: null
		 * getRequestingSite: /85.234.138.2
		 * getRequestingURL: null
		 * getRequestorType: SERVER
		 */

		final String fullhost = getRequestingHost().toLowerCase()+":"+getRequestingPort();
    //#IRCAuthenticator.java:127: method: PasswordAuthentication com.dmdirc.parser.irc.IRCAuthenticator.getPasswordAuthentication()
    //#IRCAuthenticator.java:127: Warning: method not available - call not analyzed
    //#    call on String com.dmdirc.parser.irc.IRCAuthenticator:getRequestingHost()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.parser.irc.IRCAuthenticator
    //#    method: PasswordAuthentication getPasswordAuthentication()
    //#    unanalyzed callee: String com.dmdirc.parser.irc.IRCAuthenticator:getRequestingHost()
    //#IRCAuthenticator.java:127: Warning: method not available - call not analyzed
    //#    call on int com.dmdirc.parser.irc.IRCAuthenticator:getRequestingPort()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.parser.irc.IRCAuthenticator
    //#    method: PasswordAuthentication getPasswordAuthentication()
    //#    unanalyzed callee: int com.dmdirc.parser.irc.IRCAuthenticator:getRequestingPort()
    //#input(PasswordAuthentication getPasswordAuthentication()): ":"._tainted
    //#input(PasswordAuthentication getPasswordAuthentication()): this
    //#input(PasswordAuthentication getPasswordAuthentication()): this.replies
    //#output(PasswordAuthentication getPasswordAuthentication()): return_value
    //#pre[2] (PasswordAuthentication getPasswordAuthentication()): this.replies != null
    //#presumption(PasswordAuthentication getPasswordAuthentication()): com.dmdirc.parser.irc.IRCAuthenticator:getRequestingHost(...)@127 != null
    //#post(PasswordAuthentication getPasswordAuthentication()): init'ed(return_value)
		return replies.get(fullhost);
    //#IRCAuthenticator.java:128: end of method: PasswordAuthentication com.dmdirc.parser.irc.IRCAuthenticator.getPasswordAuthentication()
	}
}
    //#IRCAuthenticator.java:: end of class: com.dmdirc.parser.irc.IRCAuthenticator
