import hexchat

__module_name__ = "Alias"
__module_author__ = "TingPing"
__module_version__ = "1"
__module_description__ = "Create aliases for commands"

alias_hooks = {}
help_msg = 'Alias: Valid commands are:\n \
					ALIAS name command\n \
					UNALIAS name\n \
					ALIASES\n \
					HELP name'

def load_aliases ():
	for pref in hexchat.list_pluginpref():
		if pref[:6] == 'alias_':
			name = pref[6:]
			hook = hexchat.hook_command(name, alias_cmd_cb, help='Alias: "%s" is an alias for "%s"' %(name, get_alias(name)))
			alias_hooks[name] = hook

def get_alias(name):
	cmd = hexchat.get_pluginpref('alias_' + name)
	return cmd

def remove_alias(name, quiet=False):
	hexchat.del_pluginpref('alias_' + name)
	if name in alias_hooks:
		hook = alias_hooks.get(name)
		hexchat.unhook(hook)
		del alias_hooks[name]
		return True
	return False


def add_alias(name, cmd):
	hexchat.set_pluginpref('alias_' + name, cmd)
	hook = hexchat.hook_command(name, alias_cmd_cb, help='Alias: "%s" is an alias for "%s"' %(name, get_alias(name)))
	alias_hooks[name] = hook
	if hook:
		return True
	return False


def alias_cmd_cb(word, word_eol, userdata):
	hexchat.command('%s %s' %(get_alias(word[0]), word_eol[1]))
	return hexchat.EAT_HEXCHAT

def unalias_cb(word, word_eol, userdata):
	if len(word) > 1:
		if remove_alias(word[1]):
			print('Alias: %s removed' %word[1])
		else:
			print('Alias: %s not found' %word[1])
	else:
		hexchat.prnt('Alias: Not enough arguements')
	return hexchat.EAT_ALL
	
def alias_cb(word, word_eol, userdata):
	if len(word) > 3:
		edited = False
		if remove_alias(word[1], True):
			edited = True
		if add_alias(word[1], word_eol[2]):
			if edited:
				print('Alias: %s edited' %word[1])
			else:
				print('Alias: %s added' %word[1])
		else:
			print('Alias: %s failed to hook' %word[1])
	else:		
		print(help_msg)
	return hexchat.EAT_ALL

# FIXME: Formatting
def aliases_cb(word, word_eol, userdata):
	print('\026NAME\t\t\t\tCMD                                                     ')
	for pref in hexchat.list_pluginpref():
		if pref[:6] == 'alias_':
			print('%s\t\t\t%s' %(pref[6:], get_alias(pref[6:])))
	return hexchat.EAT_ALL
		
def unload_callback(userdata):
	print(__module_name__ + ' version ' + __module_version__ + ' unloaded.')

hexchat.hook_command("alias", alias_cb, help=help_msg)
hexchat.hook_command("aliases", aliases_cb, help=help_msg)
hexchat.hook_command("unalias", unalias_cb, help=help_msg)
hexchat.hook_unload(unload_callback)
load_aliases()
print(__module_name__ + ' version ' + __module_version__ + ' loaded.')

@@ -151,25 +151,8 @@
 s%22%0A%0A
-alias_hooks = %7B%7D%0A
 help
@@ -296,28 +296,28 @@
 es ():%0A%09for 
-pref
+name
  in hexchat.
@@ -339,55 +339,8 @@
 ():%0A
-%09%09if pref%5B:6%5D == 'alias_':%0A%09%09%09name = pref%5B6:%5D%0A%09
 %09%09ho
@@ -454,36 +454,8 @@
 e)))
-%0A%09%09%09alias_hooks%5Bname%5D = hook
 %0A%0Ade
@@ -499,35 +499,24 @@
 _pluginpref(
-'alias_' + 
 name)%0A%09retur
@@ -587,129 +587,36 @@
 ref(
-'alias_' + name)%0A%09if name in alias_hooks:%0A%09%09hook = alias_hooks.get(name)%0A%09%09hexchat.unhook(hook)%0A%09%09del alias_hooks%5B
+name)%0A%09hexchat.unhook(
 name
-%5D%0A%09
+)%0A
 %09ret
@@ -620,38 +620,24 @@
 return True%0A
-%09return False%0A
 %0A%0Adef add_al
@@ -680,19 +680,8 @@
 ref(
-'alias_' + 
 name
@@ -1819,36 +1819,8 @@
 ():%0A
-%09%09if pref%5B:6%5D == 'alias_':%0A%09
 %09%09pr
@@ -1842,20 +1842,16 @@
 ' %25(pref
-%5B6:%5D
 , get_al
@@ -1862,12 +1862,8 @@
 pref
-%5B6:%5D
 )))%0A

Update alias.py