|
import hexchat |
|
import imp |
|
import sys |
|
import os |
|
|
|
__module_name__ = "Pyld script manager" |
|
__module_version__ = "1.1" |
|
__module_description__ = "Replacement for the broken Hexchat script manager." |
|
|
|
basedir = "Wherever/you/put/your/stuff" |
|
|
|
scripts = {} |
|
hooks = {} |
|
autolist = os.path.join(basedir, "autorun.txt") |
|
|
|
def load_autorun(): |
|
global autorun |
|
if os.path.exists(autolist): |
|
with open(autolist, "rt") as f: |
|
autorun = f.read().split("\n") |
|
else: |
|
autorun = [] |
|
|
|
def save_autorun(): |
|
with open(autolist, "wt") as f: |
|
f.write("\n".join(autorun)) |
|
|
|
# load a script, first unloading if it's already loaded |
|
def script_load(name): |
|
if name in scripts: |
|
script_unload(name) |
|
try: |
|
scripts[name] = imp.load_source(name, os.path.join(basedir, name + ".py")) |
|
hooks[name] = scripts[name].hooks |
|
print "Loaded script \"%s\"" % name |
|
except: |
|
print "Error loading script \"%s\": %s" % (name, sys.exc_info()[1]) |
|
pass |
|
|
|
# unload a script |
|
def script_unload(name): |
|
try: |
|
for i in hooks[name]: |
|
hexchat.unhook(i) |
|
hooks.pop(name) |
|
scripts.pop(name) |
|
print "Unloaded script \"%s\"" % name |
|
except: |
|
print "Error unloading script \"%s\": %s" % (name, sys.exc_info()[1]) |
|
pass |
|
|
|
# hook for commands |
|
def script_manager_hook(word, word_eol, userdata): |
|
argnum = len(word) - 1 |
|
if argnum == 0 or (argnum == 1 and word[1] == "help"): |
|
print "Available commands: list, load [name], unload [name], reload [name], auto [name]" |
|
elif argnum == 1 and word[1] == "info": |
|
print "PyLd script manager by nucular, based on a script by jacob1" |
|
elif argnum == 1 and word[1] == "list": |
|
print "Loaded scripts: " + ", ".join(scripts) |
|
|
|
elif argnum == 2 and word[1] == "unload": |
|
if not word[2] in scripts: |
|
print "Script \"%s\" is not loaded." % word[2] |
|
else: |
|
script_unload(word[2]) |
|
elif argnum == 2 and word[1] == "load": |
|
if word[2] in scripts: |
|
print "Script \"%s\" is already loaded." % word[2] |
|
else: |
|
script_load(word[2]) |
|
elif argnum == 2 and word[1] == "reload": |
|
if not word[2] in scripts: |
|
print "Script \"%s\" is not loaded." % word[2] |
|
else: |
|
script_load(word[2]) |
|
|
|
elif argnum == 2 and word[1] == "auto": |
|
if word[2] in autorun: |
|
autorun.remove(word[2]) |
|
print "Script \"%s\" removed from autorun." % word[2] |
|
else: |
|
autorun.append(word[2]) |
|
print "Script \"%s\" added to autorun." % word[2] |
|
save_autorun() |
|
|
|
else: |
|
print "Unknown command or wrong number of arguments. Try /pyld help." |
|
return hexchat.EAT_HEXCHAT |
|
|
|
hexchat.hook_command("pyld", script_manager_hook) |
|
|
|
load_autorun() |
|
for i in autorun: |
|
script_load(i) |