3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 21:19:31 +01:00

Initial PyLink-Relay stub, with CREATE and DESTROY commands using a pickle database.

ref #37.
This commit is contained in:
James Lu 2015-07-10 20:26:46 -07:00
parent 11e27b9f36
commit 8d4a06b60e

92
plugins/relay.py Normal file
View File

@ -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):
"""<channel>
Creates the channel <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):
"""<channel>
Destroys the channel <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()