---------------------------------------------------------------------------------
--
-- Prat - A framework for World of Warcraft chat mods
--
-- Copyright (C) 2006-2007  Prat Development Team
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to:
--
-- Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor,
-- Boston, MA  02110-1301, USA.
--
--
-------------------------------------------------------------------------------


--[[
Name: Prat_Alias
Revision: $Revision: $
Author(s): Fin (fin@instinct.org)
Website: http://files.wowace.com/Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#Alias
SVN: http://svn.wowace.com/wowace/trunk/Prat/
Discussions: http://groups.google.com/group/wow-prat
Issues and feature requests: http://code.google.com/p/prat/issues/list
Description: Adds the command /alias, which can be used to alias slash commands in a similar way to the Unix alias command (default=off).
Dependencies: Prat
]]

-- Get Utility Libraries
local util, DBG, CLR = GetPratUtils()

-- crappy code, one of the first things I started fiddling with. should clean it up and stuff.

-- set prat module name
local PRAT_MODULE = Prat:RequestModuleName("Alias")

if PRAT_MODULE == nil then
    return
end

-- define localized strings
local L = AceLibrary("AceLocale-2.2"):new(PRAT_MODULE)

L:RegisterTranslations("enUS", function() return {
	["Alias"] = true,
	["Adds the command /alias, which can be used to alias slash commands in a similar way to the Unix alias command."] = true,
	["add"] = true,
	["add an alias"] = true,
	['<command>[ <value>] - alias <command> to be executed as <value>, or return the value of the currently defined alias for <command> if <command> has not been assigned a value. eg: "/alias /examplehello /say hello there" - typing "/examplehello" will now cause your character to say "hello there"; "/alias examplehello" - prints "/examplehello is aliased to /say hello there" (cmd aliases: /addalias)'] = true,
	["unalias"] = true,
	["remove an alias"] = true,
	['<alias> - remove the alias <alias> (cmd aliases: /delalias, /remalias)'] = true,
	["listaliases"] = true,
	["list all aliases"] = true,
	['findaliases'] = true,
	['find aliases matching a given search term'] = true,
	['<keyword> - finds all aliases matching <keyword> (cmd aliases: /findalias)'] = true,
	['verbose'] = true,
	['Display extra information in the chat frame when commands are dealiased'] = true,
	['inline'] = true,
	['Expand aliases as you are typing'] = true,
	["Options for altering the behaviour of Alias"] = true,
	['Options'] = true,
	['noclobber'] = true,
	["Don't overwrite existing aliases when using /addalias"] = true,
	[' - list all aliases; supply <keyword> to search for matching aliases (cmd aliases: /listallaliases)'] = true,
} end)

L:RegisterTranslations("esES", function() return {
	["Alias"] = "Alias",
	["Adds the command /alias, which can be used to alias slash commands in a similar way to the Unix alias command."] = "Añade el comando /alias, que puede ser usado para crear alias de comandos de un modo similar al comando alias de Unix.",
	['<command>[ <value>] - alias <command> to be executed as <value>, or return the value of the currently defined alias for <command> if <command> has not been assigned a value. eg: "/alias /examplehello /say hello there" - typing "/examplehello" will now cause your character to say "hello there"; "/alias examplehello" - prints "/examplehello is aliased to /say hello there"'] = "<comando>[ <valor>] - alias de <comando> que se ejecutará como <valor>, o devuelve el valor del alias actualmente definido para <comando> si <comando> no tiene un valor asignado. Ej: '/alias /ejemplohola /decir Hola' - al escribir '/ejemplohola' tu personaje dirá 'Hola'; '/alias ejemplohola' - muestra '/ejemplohola es el alias de /decir Hola'",
} end)

L:RegisterTranslations("deDE", function() return {
	["Alias"] = "Alias",
	["Adds the command /alias, which can be used to alias slash commands in a similar way to the Unix alias command."] = "Fügt das Chatkommando /alias hinzu, das dazu benutzt werden kann selbst definierte Wörter für Chatkommandos zu verwenden. Ähnlich wie es das UNIX Alias Kommando tut.",
	["add"] = "Hinzufügen",
	["add an alias"] = "Füge ein Alias Kommando hinzu.",
	['<command>[ <value>] - alias <command> to be executed as <value>, or return the value of the currently defined alias for <command> if <command> has not been assigned a value. eg: "/alias /examplehello /say hello there" - typing "/examplehello" will now cause your character to say "hello there"; "/alias examplehello" - prints "/examplehello is aliased to /say hello there" (cmd aliases: /addalias)'] = "<slashcommando>[ <ergebnisslashkommando>] - Füge ein <slashkommando> hinzu das das <ergebnisslashkommando> auslöst, oder dir anzeigt was für ein <slashkommando> was für ein >ergebnisslashkommando> auslösen würde. zB: '/alias /beispielhallo /s hallo zusammen' - eintippen von '/beispielhallo' würde nun deinen Charakter dazu bringen 'hallo zusammen' zu sagen; '/alias beispielhallo' - schreibt im chat '/beispielhallo ist zugeornet zu /s hallo zusammen' (cmd aliases: /addalias)",
	["unalias"] = "Entferne Alias",
	["remove an alias"] = "Ein Alias Kommando entfernen.",
	['<alias> - remove the alias <alias> (cmd aliases: /delalias, /remalias)'] = "<alias> - entferne den alias <alias>",
	["listaliases"] = "Liste Aliase",
	["list all aliases"] = "Listet alle vorhanden Aliase auf.",
	['findaliases'] = "Finde Aliase",
	['find aliases matching a given search term'] = "Finde Aliase die dem gesuchten Wort entsprechen.",
	['<keyword> - finds all aliases matching <keyword> (cmd aliases: /findalias)'] = "'<Suchwort> - finde alle Aliase die <Suchwort> entsprechen (cmd aliases: /findalias)",
	['verbose'] = "Ausführlich",
	['Display extra information in the chat frame when commands are dealiased'] = "Zeige eine zusätzliche Information im Chatfenster wenn Alias Kommandos entfernt werden.",
	['inline'] = "Ausfüllen",
	['Expand aliases as you are typing'] = "Vervollständige Aliase beim eintippen.",
	["Options for altering the behaviour of Alias"] = "Optionem um das verhalten der Aliasen zu abzuändern.",
	['Options'] = "Optionen",
	['noclobber'] = "NichtÜberschreiben",
	["Don't overwrite existing aliases when using /addalias"] = "Kein Überschreiben von existierenden Aliasen wenn /addalias benutzt wird.",
	[' - list all aliases; supply <keyword> to search for matching aliases (cmd aliases: /listallaliases)'] = " - Liste alle Aliase; benutze <Suchwortwort> um nach möglichen Aliasen zu suchen.",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Alias"] = "指令別名",
	["Adds the command /alias, which can be used to alias slash commands in a similar way to the Unix alias command."] = "增加指令 /alias ，可用以作為一般指令的別名，就像在 Unix 系統中的別名命令",
	["add"] = "添加",
	["add an alias"] = "添加一條指令別名",	
	['<command>[ <value>] - alias <command> to be executed as <value>, or return the value of the currently defined alias for <command> if <command> has not been assigned a value. eg: "/alias /examplehello /say hello there" - typing "/examplehello" will now cause your character to say "hello there"; "/alias examplehello" - prints "/examplehello is aliased to /say hello there" (cmd aliases: /addalias)'] = "<指令>[ <值>] - 將 <指令> 作為別名而執行 <值>；如果沒有指定 <值> 的話就傳回現定義的別名其 <指令> 的值。例： /alias /examplehello /say hello there - 現在鍵入/examplehello 會使你的角色說：hello there; /alias examplehello - 在對話框顯示/examplehello is aliased to /say hello there(/examplehello 已經成為 /say hello there 的別名)",
	["unalias"] = "移除指令",
	["remove an alias"] = "移除一個指令別名",
	["listaliases"] = "列出別名",
	["list all aliases"] = "列出你當前所有指令別名",	
} end)


--Chinese Translation: 月色狼影@CWDG
--CWDG site: http://Cwowaddon.com
L:RegisterTranslations("zhCN", function() return {
	["Alias"] = "指令自定义",
	["Adds the command /alias, which can be used to alias slash commands in a similar way to the Unix alias command."] = "增加指令/alias,可以作为一般指令的别名,就如在Unix系统中的自定义指令",
	["add"] = "添加",
	["add an alias"] = "添加一条自定义指令",
	['<command>[ <value>] - alias <command> to be executed as <value>, or return the value of the currently defined alias for <command> if <command> has not been assigned a value. eg: "/alias /examplehello /say hello there" - typing "/examplehello" will now cause your character to say "hello there"; "/alias examplehello" - prints "/examplehello is aliased to /say hello there" (cmd aliases: /addalias)'] = "<指令>[ <值>] - 将自定义<指令>成为可执行<值>；如果你没用指定该<值>的话，系统就会反馈给你当前自定义<指令>的值。例如：/alias /examplehello /say hello there - 输入/examplehello，你的角色会立即/say Hello there输出到聊天框中。从上面例子可以看出/examplehello已经成为/say hello there的一个自定义指令。让你聊天更加快捷方便。",
	["unalias"] = "移出指令",
	["remove an alias"] = "移出一条自定义指令",
	["listaliases"] = "列出指令",
	["list all aliases"] = "列出你当前所有自定义指令",
} end)

-- get prat module categories
local cat = Prat.Categories

-- create prat module
Prat_Alias = Prat:NewModule(PRAT_MODULE)
Prat_Alias.revision = tonumber(string.sub("$Revision:  $", 12, -3))

-- define key module values
Prat_Alias.moduleName	= L["Alias"]
Prat_Alias.moduleDesc	= L["Adds the command /alias, which can be used to alias slash commands in a similar way to the Unix alias command."]

Prat_Alias.consoleName	= string.lower(Prat_Alias.moduleName)
Prat_Alias.guiName	= Prat_Alias.moduleName

-- list here: http://groups.google.com/group/wow-prat/web/prat-categories
Prat_Alias.Categories	= { cat.TEXT, cat.BEHAVIOR, cat.ACTION }

-- init moduleOoptions (which gets populated by the other various options tables below
Prat_Alias.moduleOptions = {}

-- defined aliases
Prat_Alias.Aliases	= {}

-- default values for any settings that need them
Prat_Alias.defaultDB = {
	on	= false,
	aliases	= {},
	verbose	= false,
	inline	= true,
	noclobber = false,

	-- things we won't alias
	wontalias = {
		unalias	= 1,
		alias	= 1,
		prat	= 1,
		script	= 1,
		run	= 1,
		ace	= 1,
		ace2	= 1,
		listaliases = 1,
		quit	= 1,
		reload	= 1,
		rl	= 1,
		},
	}


-- any boolean options
Prat_Alias.toggleOptions = {
	on,
	verbose,
	inline,
	noclobber,
	}

function Prat_Alias:GetModuleOptions()
	self.moduleOptions = {
		name	= self.moduleName,
		desc	= self.moduleDesc,
		type	= "group",
		args = {
			add = {
				type	= "text",
				name	= L["add"],
				desc	= L["add an alias"],
				usage	= L['<command>[ <value>] - alias <command> to be executed as <value>, or return the value of the currently defined alias for <command> if <command> has not been assigned a value. eg: "/alias /examplehello /say hello there" - typing "/examplehello" will now cause your character to say "hello there"; "/alias examplehello" - prints "/examplehello is aliased to /say hello there" (cmd aliases: /addalias)'],

				get	= false,
				set	= function(argstr) return self:setAlias(argstr) end,
				order	= 210,
				},

			del = {
				name	= L["unalias"],
				desc	= L["remove an alias"],
				type	= "text",
				usage	= L['<alias> - remove the alias <alias> (cmd aliases: /delalias, /remalias)'],
				get	= false,
				set	= function(aliastoremove) return self:delAlias(aliastoremove) end,
				order	= 220,
				},

			find = {
				name	= L["findaliases"],
				desc	= L["find aliases matching a given search term"],
				type	= 'text',
				set	= function(q) return self:listAliases(q) end,
				get	= false,
				usage	= L['<keyword> - finds all aliases matching <keyword> (cmd aliases: /findalias)'],
				order	= 230,
				},

			list = {
				name	= L["listaliases"],
				desc	= L["list all aliases"],
				type	= 'execute',
				func	= function() return self:listAliases() end,
				usage	= L[' - list all aliases; supply <keyword> to search for matching aliases (cmd aliases: /listallaliases)'],
				order	= 240,
				},


			blankheader = {
				order	= 499,
				type	= 'header',
				},
			--[[ OPTIONS ]]--
			optionsheader = {
				name	= L["Options"],
				desc	= L["Options for altering the behaviour of Alias"],
				type	= 'header',
				order	= 500,
				},


			inline = {
				name	= L['inline'],
				desc	= L['Expand aliases as you are typing'],
				type	= 'toggle',
				set	= function() self.db.profile.inline = not self.db.profile.inline end,
				get	= function() return self.db.profile.inline end,
				order	= 510,
				},

			noclobber = {
				name	= L['noclobber'],
				desc	= L["Don't overwrite existing aliases when using /addalias"],
				type	= 'toggle',
				set	= function() self.db.profile.noclobber = not self.db.profile.noclobber end,
				get	= function() return self.db.profile.noclobber end,
				order	= 520,
				},

			verbose = {
				name	= L['verbose'],
				desc	= L['Display extra information in the chat frame when commands are dealiased'],
				type	= 'toggle',
				set	= function() self.db.profile.verbose = not self.db.profile.verbose end,
				get	= function() return self.db.profile.verbose end,
				order	= 530,
				},
			}
		}

	return self.moduleOptions
end

local clralias = function(aliasname)
	aliasname = aliasname or ''
	return '|cff64ff64' .. string.lower(aliasname) .. '|r'
end

local clrexpansion = function(expansion)
	expansion = expansion or ''
	return '|cff64ffff' .. string.lower(expansion) .. '|r'
end

local clrmodname = function(modname)
	modname = modname or ''
	-- return '|cffffff64' .. modname .. '|r'
	return '|cffff8080' .. modname .. '|r'
end

-- things to do when the module is enabled
function Prat_Alias:OnModuleEnable()
	table.sort(self.db.profile.aliases)

	for k, v in pairs(self.db.profile.aliases) do
		self.Aliases[k] = v
	end

	self.WontAlias = self.db.profile.wontalias
	for naughtyalias, justtrue in pairs(self.WontAlias) do
		self.WontAlias[string.lower(naughtyalias)] = 1
	end

	self:Hook('ChatEdit_HandleChatType', true)

	Prat:RegisterChatCommand({ '/alias', '/addalias', }, self.moduleOptions.args.add, 'PRATALIAS')
	Prat:RegisterChatCommand({ '/unalias', '/delalias', '/remalias' }, self.moduleOptions.args.del, 'PRATUNALIAS')
	Prat:RegisterChatCommand({ '/listaliases', '/listallaliases' }, self.moduleOptions.args.list, 'PRATLISTALIASES')
	Prat:RegisterChatCommand({ '/findaliases', '/findalias' }, self.moduleOptions.args.find, 'PRATFINDALIASES')
end

-- things to do when the module is disabled
function Prat_Alias:OnModuleDisable()
	-- unregister events
	self:UnregisterAllEvents()

	self.db.profile['on'] = false
	self.Aliases = {}
end

--[[------------------------------------------------
    Core Functions
------------------------------------------------]]--

function Prat_Alias:splitAliasArgs(str)
	-- str should be "<command>[=value| value]"
	local name, value
	local args = {
		name	= "",
		value	= "",
		}

	-- if it doesn't match, args is left with default blank strings for values
	--for alias, command in str:find("(%w+)%s*=?%s*(.-)%s*$") do
	for alias, command in str:gmatch("/?(%w+)%s*[%s=]%s*/?(.-)$") do
		-- either matches both alias and command (may match command as a blank string)
		args['name']	= string.lower(alias)
		args['value']	= command or ""
		-- util:print('name ' .. args['name'])
		-- util:print('value ' .. args['value'])
	end

	return args
end

function Prat_Alias:checkArgStr(funcname, argstr)
	if argstr == nil then
		self:warnUser(string.format("%s() called with nil argument!", funcname))
		return false
	end

	if argstr == "" then
		self:warnUser(string.format("%s() called with blank string!", funcname))
		return false
	end

	return true
end

function Prat_Alias:setAlias(argstr)
	-- argstr should be "<command>[ <value]"
	if not self:checkArgStr('setAlias', argstr) then
		return false
	end

	local alias = self:splitAliasArgs(argstr)

	-- check to see if the user is defining an alias or not
	if not alias['value'] or (alias['value'] == "") then
		local name = argstr

		-- called as: /alias <command> - check for alias called <command> to display
		if self.Aliases[name] then
			-- alias found; show it :)
			self:showAlias(name)
			return true
		else
			-- no alias found called <command>; tell user
			self:reportUndefinedAlias(name)
		end
	elseif self.WontAlias[string.lower(alias['name'])] then
		-- user is defining an alias called <command>, but it's potentially bad
		self:warnUser(string.format('refusing to alias "/%s" to anything in the interests of Not Buggering Everything Up', clralias(alias['name'])))
		return false
	elseif self.db.profile.noclobber and self.Aliases[string.lower(alias['name'])] then
		self:warnUser(string.format('noclobber set - skipping new alias: /%s already expands to /%s', clralias(alias['name']), clrexpansion(alias['value'])))
		return false
	else
		-- it's not listed as bad, so create or update the aliases tables
		-- called as /alias <command> <value> - define alias <command> as <value>
		if self.Aliases[alias['name']] then
			-- specified alias already exists, warn user and print old setting
			self:warnUser(string.format('overwriting existing alias "/%s" (was aliased to "/%s")', clralias(alias['name']), clrexpansion(self.Aliases[alias['name']])))
		end

		-- now (re?)define the alias <command> to <value>
		self.Aliases[alias['name']] = alias['value']
		self.db.profile.aliases[alias['name']] = alias['value']

		table.sort(self.db.profile.aliases)
		table.sort(self.Aliases)

		self:warnUser(string.format("/%s aliased to: /%s", clralias(alias['name']), clrexpansion(alias['value'])))
	end
end

function Prat_Alias:delAlias(aliasname)
	if not self:checkArgStr('delAlias', aliasname) then
		return false
	end

	-- remove unecessary /s at the beginning of the alias name
	aliasname	= aliasname:gsub('^/*', '')

	if not self.Aliases[aliasname] then
		self:warnUser(string.format('alias "/%s" does not exist', clralias(aliasname)))
		return false
	end

	local oldalias = self.Aliases[aliasname]

	self:warnUser(string.format('deleting alias "/%s" (previously aliased as "/%s")', clralias(aliasname), clrexpansion(oldalias)))

	self.Aliases[aliasname]			= nil
	self.db.profile.aliases[aliasname]	= nil

	return oldalias
end

function Prat_Alias:showAlias(aliasname)
	if not self:checkArgStr('showAlias', aliasname) then
		return false
	end

	-- check for undefined alias called aliasname
	if not self.Aliases[aliasname] then
		self:warnUser(string.format('tried to show value for alias "%s" but undefined in Prat_Alias.Aliases!', clralias(aliasname)))
		return false
	end

	-- everything OK; display value of alias "aliasname"
	self:warnUser(string.format('/%s aliased to "/%s"', clralias(aliasname), clrexpansion(self.Aliases[aliasname])))

	return true
end

function Prat_Alias:listAliases(q)
	if self.Aliases == {} then
		self:warnUser("No aliases have been defined")
		return false
	end

	local msg
	local count	= 0

	table.sort(self.Aliases)

	for name, alias in pairs(self.Aliases) do
		if not q or (name:match(q)) then
			self:showAlias(name)
			count = count + 1
		end
	end

	if q then
		msg	= 'matching aliases found: %d'
	else
		msg	= 'total aliases: %d'
	end

	self:tellUser(string.format(msg, count))
end


function Prat_Alias:reportUndefinedAlias(name)
	return self:warnUser(string.format('There is no alias current defined for "%s"', clralias(name)))
end

function Prat_Alias:tellUser(str)
	return Prat_Alias:warnUser(str)
end

function Prat_Alias:warnUser(str)
	if str == nil then
		str = "warnUser() called with nil argument!"
	elseif not str then
		str = "warnUser() called with zero length string!"
	end

	DEFAULT_CHAT_FRAME:AddMessage(string.format("%s: %s", clrmodname(self.moduleName), str))

	return true
end


local fake	= {}

function Prat_Alias:ChatEdit_HandleChatType(editBox, msg, command, send, dealiased)
	local command	= command or ""
	local alias	= self.Aliases[string.lower(strsub(command, 2))]
	local dealiased	= dealiased or {}
	local msg	= msg or ""

	if dealiased[command] then
		-- skip commands we've already dealiased
		self:warnUser(string.format('infinite loop detected for alias /%s - ignoring', clralias(alias)))
	elseif alias and alias ~= "" then
		if (send == 1) and self.db.profile.verbose then
			self:warnUser(string.format('dealiasing command /%s to /%s', clralias(strsub(command, 2)), clrexpansion(alias)))
		end

		dealiased[command] = true
		alias		= Prat:ReplaceMatches(alias, 'OUTBOUND')

		local newcmd	= strmatch(alias, "^/*([^%s]+)") or ""
		local premsg	= strsub(alias, strlen(newcmd) + 2) or ""

		if premsg ~= "" then
			msg	= premsg .. ' ' .. msg
		end

		command = '/' .. string.upper(newcmd)
		text	= string.lower(command)

		if msg ~= "" then
			fake.MESSAGE = msg

			Prat:ProcessUserEnteredChat(fake)

			msg	= fake.MESSAGE
			text	= text .. ' ' .. msg
		end

		if (send == 1) then
			editBox:SetText(text)
		elseif (self.db.profile.inline) then
			editBox:SetText(text .. ' ')
		end

		self:ChatEdit_HandleChatType(editBox, msg, command, send, dealiased)

		return true
	end

	if send == 1 then
		local text = editBox:GetText()
		-- ripped off Blizzard's slash command bits here; just changed return value
		if ( hash_SlashCmdList[command] ) then
			hash_SlashCmdList[command](strtrim(msg));
			editBox:AddHistoryLine(text);
			ChatEdit_OnEscapePressed(editBox);

			return true
		end

		for index, value in pairs(SlashCmdList) do
			local i = 1;
			local cmdString = getglobal("SLASH_"..index..i);
			while ( cmdString ) do
				cmdString = strupper(cmdString);
				if ( cmdString == command ) then
					hash_SlashCmdList[command] = value;	-- add to hash
					-- if the code in here changes - change the corresponding code above
					value(strtrim(msg));

					editBox:AddHistoryLine(text);
					ChatEdit_OnEscapePressed(editBox);
					return true
				end
				i = i + 1;
				cmdString = getglobal("SLASH_"..index..i);
			end
		end
	end

	return self.hooks["ChatEdit_HandleChatType"](editBox, msg, command, send)
end
