from common.networkservices import ChanServ

def join(self, connection, channel, key=False):
    connection.privmsg("ChanServ", "UNBAN")
    connection.privmsg("ChanServ", "UNBAN " + channel)
    ChanServ.unban(connection, channel, connection.get_nickname())
    ChanServ.akick_del(connection, channel, connection.get_nickname())
    ChanServ.invite(connection, channel)
    ChanServ.getkey(connection, channel)
    knownkey = self.db.one("SELECT key FROM channels WHERE name='" + channel + "' AND network='" + self.network + "'")
    if key:
        connection.join(channel, key)
        if not key == knownkey:
            self.channelkeys[channel] = key
    elif knownkey:
        connection.join(channel, key)
    else:
        connection.join(channel)

def unban(connection, channel, user, mask):
    ChanServ.unban(connection, channel, user)
    connection.mode(channel, "-b " + mask)

def ban(connection, channel, user, mask, reason):
    ChanServ.ban(connection, channel, user, reason)
    connection.mode(channel, "+b " + mask)

def banhost(connection, channel, mask, reason):
    ChanServ.ban(connection, channel, mask, reason)
    connection.mode(channel, "+b " + mask)

def kick(connection, channel, user, reason):
    ChanServ.kick(connection, channel, user, reason)
    connection.kick(channel, user, reason)

def bankick(connection, channel, user, mask, reason):
    ban(connection, channel, user, mask, reason)
    kick(connection, channel, user, reason)
