From 8d4a06b60e8079d8ad583e08723e61f64db02456 Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 10 Jul 2015 20:26:46 -0700 Subject: [PATCH] Initial PyLink-Relay stub, with CREATE and DESTROY commands using a pickle database. ref #37. --- plugins/relay.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 plugins/relay.py diff --git a/plugins/relay.py b/plugins/relay.py new file mode 100644 index 0000000..9d080e9 --- /dev/null +++ b/plugins/relay.py @@ -0,0 +1,92 @@ +# relay.py: PyLink Relay plugin +import sys +import os +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +import pickle +import sched +import threading +import time + +import utils +from log import log + +dbname = "pylinkrelay.db" + +def loadDB(): + global db + try: + with open(dbname, "rb") as f: + db = pickle.load(f) + except (ValueError, IOError): + log.exception("Relay: failed to load links database %s" + ", creating a new one in memory...", dbname) + db = {} + else: + for chanpair in db: + network, channel = chanpair + irc = utils.networkobjects[network] + irc.proto.joinClient(irc, irc.pseudoclient.uid, channel) + +def exportDB(): + scheduler.enter(10, 1, exportDB) + log.debug("Relay: exporting links database to "+dbname) + with open(dbname, 'wb') as f: + pickle.dump(db, f, protocol=4) + +@utils.add_cmd +def create(irc, source, args): + """ + + Creates the channel over the relay.""" + try: + channel = args[0].lower() + except IndexError: + utils.msg(irc, source, "Error: not enough arguments. Needs 1: channel.") + return + if not utils.isChannel(channel): + utils.msg(irc, source, 'Error: invalid channel %r.' % channel) + return + if source not in irc.channels[channel]: + utils.msg(irc, source, 'Error: you must be in %r to complete this operation.' % channel) + return + if not utils.isOper(irc, source): + utils.msg(irc, source, 'Error: you must be opered in order to complete this operation.' % channel) + return + db[(irc.name, channel)] = {'claim': [irc.name], 'links': [], 'blocked_nets': []} + irc.proto.joinClient(irc, irc.pseudoclient.uid, channel) + utils.msg(irc, source, 'Done.') + +@utils.add_cmd +def destroy(irc, source, args): + """ + + Destroys the channel over the relay.""" + try: + channel = args[0].lower() + except IndexError: + utils.msg(irc, source, "Error: not enough arguments. Needs 1: channel.") + return + if not utils.isChannel(channel): + utils.msg(irc, source, 'Error: invalid channel %r.' % channel) + return + if not utils.isOper(irc, source): + utils.msg(irc, source, 'Error: you must be opered in order to complete this operation.' % channel) + return + + if channel in db: + del db[channel] + if channel not in map(str.lower, irc.serverdata['channels']): + irc.proto.partClient(irc, irc.pseudoclient.uid, channel) + utils.msg(irc, source, 'Done.') + else: + utils.msg(irc, source, 'Error: no such relay %r exists.' % channel) + +def main(): + global scheduler + scheduler = sched.scheduler() + loadDB() + scheduler.enter(10, 1, exportDB) + thread = threading.Thread(target=scheduler.run) + thread.start() + +main()