# -*- coding: utf-8 -*-
import ServerInfo
from enum import Enum
import re


class TargetTypes(Enum):
    CHANNEL = 1
    USER = 2


class IRCChannel(object):
    def __init__(self, name):
        """
        @type name: str
        """
        self.Name = name
        self.Topic = ''
        self.TopicSetBy = ''
        self.Users = {}
        self.Ranks = {}
        self.Modes = {}

    def __str__(self):
        return self.Name

    def getHighestStatusOfUser(self, nickname):
        if not self.Ranks[nickname]:
            return None

        for mode in ServerInfo.StatusOrder:
            if mode in self.Ranks[nickname]:
                return mode

        return None


class IRCUser(object):
    def __init__(self, user):
        """
        @type user: str
        """
        self.User = None
        self.Hostmask = None

        if '!' in user:
            userArray = user.split('!')
            self.Name = userArray[0]
            if len(userArray) > 1:
                userArray = userArray[1].split('@')
                self.User = userArray[0]
                self.Hostmask = userArray[1]
            self.String = "{}!{}@{}".format(self.Name, self.User, self.Hostmask)
        else:
            self.Name = user
            self.String = "{}!{}@{}".format(self.Name, None, None)
            

class IRCMessage(object):

    def __init__(self, msgType, user, channel, message, bot):
        """
        @type msgType: str
        @type user: str
        @type channel: IRCChannel
        @type message: unicode
        @type bot: MoronBot
        """
        try:
            unicodeMessage = message.decode('utf-8', 'ignore')
        except UnicodeEncodeError:  # Already utf-8?
            unicodeMessage = message
        self.Type = msgType
        self.MessageList = unicodeMessage.strip().split(' ')
        self.MessageString = unicodeMessage
        self.User = IRCUser(user)

        self.Channel = None
        if channel is None:
            self.ReplyTo = self.User.Name
            self.TargetType = TargetTypes.USER
        else:
            self.Channel = channel
            # I would like to set this to the channel object but I would probably break functionality if I did :I
            self.ReplyTo = channel.Name
            self.TargetType = TargetTypes.CHANNEL

        self.Command = ''
        self.Parameters = ''
        self.ParameterList = []

        if self.MessageList[0].startswith(bot.commandChar):
            self.Command = self.MessageList[0][len(bot.commandChar):]
            if self.Command == '':
                self.Command = self.MessageList[1]
                self.Parameters = u' '.join(self.MessageList[2:])
            else:
                self.Parameters = u' '.join(self.MessageList[1:])
        elif self.MessageList[0].startswith(bot.nickname) and len(self.MessageList) > 1:
            self.Command = self.MessageList[1]
            self.Parameters = u' '.join(self.MessageList[2:])

        if self.Parameters.strip():
            self.ParameterList = self.Parameters.split(' ')

            self.ParameterList = [param for param in self.ParameterList if param != '']

            if len(self.ParameterList) == 1 and not self.ParameterList[0]:
                self.ParameterList = []
                
    def aliasedMessage(self, bot):
        if self.Command in bot.moduleHandler.commandAliases.keys():
            alias = bot.moduleHandler.commandAliases[self.Command]
            newMsg = re.sub(self.Command, " ".join(alias), self.MessageString, count=1)
            if "$sender" in newMsg:
                newMsg = newMsg.replace("$sender", self.User.Name)
            if "$channel" in newMsg:
                newMsg = newMsg.replace("$channel", self.Channel)
            if "$0" in newMsg:
                newMsg = newMsg.replace(self.Parameters, "")
                newMsg = newMsg.replace("$0", self.Parameters)
            elif len(self.ParameterList) >= 1:
                newMsg = newMsg.replace(self.Parameters, "")
                for i, param in enumerate(self.ParameterList):
                    newMsg = newMsg.replace("${}".format(i+1), param)
            return IRCMessage(self.Type, self.User.String, self.Channel, newMsg, bot)

@@ -3262,967 +3262,4 @@
  %5B%5D%0A
-                %0A    def aliasedMessage(self, bot):%0A        if self.Command in bot.moduleHandler.commandAliases.keys():%0A            alias = bot.moduleHandler.commandAliases%5Bself.Command%5D%0A            newMsg = re.sub(self.Command, %22 %22.join(alias), self.MessageString, count=1)%0A            if %22$sender%22 in newMsg:%0A                newMsg = newMsg.replace(%22$sender%22, self.User.Name)%0A            if %22$channel%22 in newMsg:%0A                newMsg = newMsg.replace(%22$channel%22, self.Channel)%0A            if %22$0%22 in newMsg:%0A                newMsg = newMsg.replace(self.Parameters, %22%22)%0A                newMsg = newMsg.replace(%22$0%22, self.Parameters)%0A            elif len(self.ParameterList) %3E= 1:%0A                newMsg = newMsg.replace(self.Parameters, %22%22)%0A                for i, param in enumerate(self.ParameterList):%0A                    newMsg = newMsg.replace(%22$%7B%7D%22.format(i+1), param)%0A            return IRCMessage(self.Type, self.User.String, self.Channel, newMsg, bot)%0A

Remove aliasing from IRCMessage