import os
import subprocess

from irctest import authentication
from irctest.basecontrollers import BaseClientController, DirectoryBasedController

TEMPLATE_CONFIG = """
supybot.directories.conf: {directory}/conf
supybot.directories.data: {directory}/data
supybot.log.stdout.level: {loglevel}
supybot.networks: testnet
supybot.networks.testnet.servers: {hostname}:{port}
supybot.networks.testnet.sasl.username: {username}
supybot.networks.testnet.sasl.password: {password}
supybot.networks.testnet.sasl.ecdsa_key: {directory}/ecdsa_key.pem
supybot.networks.testnet.sasl.mechanisms: {mechanisms}
"""

class LimnoriaController(BaseClientController, DirectoryBasedController):
    supported_sasl_mechanisms = {
            'PLAIN', 'ECDSA-NIST256P-CHALLENGE', 'EXTERNAL',
            }
    def create_config(self):
        super().create_config()
        with self.open_file('bot.conf'):
            pass
        with self.open_file('conf/users.conf'):
            pass

    def run(self, hostname, port, auth):
        # Runs a client with the config given as arguments
        assert self.proc is None
        self.create_config()
        if auth:
            mechanisms = ' '.join(map(authentication.Mechanisms.as_string,
                auth.mechanisms))
            if auth.ecdsa_key:
                with self.open_file('ecdsa_key.pem') as fd:
                    fd.write(auth.ecdsa_key)
        else:
            mechanisms = ''
        with self.open_file('bot.conf') as fd:
            fd.write(TEMPLATE_CONFIG.format(
                directory=self.directory,
                loglevel='CRITICAL',
                hostname=hostname,
                port=port,
                username=auth.username if auth else '',
                password=auth.password if auth else '',
                mechanisms=mechanisms.lower(),
                ))
        self.proc = subprocess.Popen(['supybot',
            os.path.join(self.directory, 'bot.conf')])

def get_irctest_controller_class():
    return LimnoriaController

@@ -163,16 +163,129 @@
 G = %22%22%22%0A
+supybot.plugins: Authentication%0Asupybot.plugins.Authentication: True%0Asupybot.plugins.Authentication.public: True%0A
 supybot.

Load Limnoria's Authentication plugin.