import atexit
import glob
import logging
import os
import random
import re
import signal
import subprocess
import sys
import time

import irc.client


class Client(irc.client.SimpleIRCClient):
    def __init__(self):
        irc.client.SimpleIRCClient.__init__(self)
        self.flags = {
            'queued': False,
            'finished': False,
            'ident': None,
        }

    def on_nicknameinuse(self, connection, event):
        connection.nick('{}{}'.format(connection.get_nickname(),
                                      random.randint(0, 99))
        )

    def on_welcome(self, connection, event):
        connection.join('#atbot-test')

    def on_join(self, connection, event):
        channel = event.target
        nickname = event.source.nick

        if nickname == 'atbot':
            connection.privmsg(
                channel,
                '{}?{}'.format('!ao http://localhost:8866',
                               random.randint(0, 1000))
            )

    def on_part(self, connection, event):
        channel = event.target
        nickname = event.source.nick

    def on_quit(self, connection, event):
        nickname = event.source.nick

    def on_kick(self, connection, event):
        channel = event.target
        nickname = self.get_nick_if_possible(event.source)
        kicked_nickname = event.arguments[0]

    def on_mode(self, connection, event):
        channel = event.target
        modes_str = ' '.join(event.arguments)
        nickname = self.get_nick_if_possible(event.source)

    def on_pubmsg(self, connection, event):
        channel = event.target

        if not irc.client.is_channel(channel):