3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 01:09:22 +01:00

Move MODE from bots.py into a new plugin, opercmds.py

This commit is contained in:
James Lu 2015-12-18 21:42:46 -08:00
parent 1f2b99ca26
commit 775933420a
2 changed files with 50 additions and 34 deletions

View File

@ -121,40 +121,6 @@ def part(irc, source, args):
irc.proto.partClient(u, channel, reason) irc.proto.partClient(u, channel, reason)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}]) irc.callHooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}])
@utils.add_cmd
def mode(irc, source, args):
"""<source> <target> <modes>
Admin-only. Sets modes <modes> on <target> from <source>, where <source> is either the nick of a PyLink client, or the SID of a PyLink server. <target> can be either a nick or a channel."""
utils.checkAuthenticated(irc, source, allowOper=False)
try:
modesource, target, modes = args[0], args[1], args[2:]
except IndexError:
irc.reply('Error: Not enough arguments. Needs 3: source nick, target, modes to set.')
return
target = utils.nickToUid(irc, target) or target
extclient = target in irc.users and not utils.isInternalClient(irc, target)
parsedmodes = utils.parseModes(irc, target, modes)
ischannel = target in irc.channels
if not (target in irc.users or ischannel):
irc.reply("Error: Invalid channel or nick %r." % target)
return
elif not parsedmodes:
irc.reply("Error: No valid modes were given.")
return
elif not (ischannel or utils.isManipulatableClient(irc, target)):
irc.reply("Error: Can only set modes on channels or non-protected PyLink clients.")
return
if utils.isInternalServer(irc, modesource):
# Setting modes from a server.
irc.proto.modeServer(modesource, target, parsedmodes)
else:
# Setting modes from a client.
modesource = utils.nickToUid(irc, modesource)
irc.proto.modeClient(modesource, target, parsedmodes)
irc.callHooks([modesource, 'PYLINK_BOTSPLUGIN_MODE',
{'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}])
@utils.add_cmd @utils.add_cmd
def msg(irc, source, args): def msg(irc, source, args):
"""<source> <target> <text> """<source> <target> <text>

50
plugins/opercmds.py Normal file
View File

@ -0,0 +1,50 @@
"""
opercmds.py: Provides a subset of network management commands.
"""
import sys
import os
# Add the base PyLink folder to path, so we can import utils and log.
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import utils
from log import log
@utils.add_cmd
def mode(irc, source, args):
"""<channel> <modes>
Oper-only, sets modes <modes> on the target channel."""
# Check that the caller is either opered or logged in as admin.
utils.checkAuthenticated(irc, source)
try:
target, modes = args[0], args[1:]
except IndexError:
irc.reply('Error: Not enough arguments. Needs 2: target, modes to set.')
return
if target not in irc.channels:
irc.reply("Error: Unknown channel '%s'." % target)
return
elif not modes:
# No modes were given before parsing (i.e. mode list was blank).
irc.reply("Error: No valid modes were given.")
return
parsedmodes = utils.parseModes(irc, target, modes)
if not parsedmodes:
# Modes were given but they failed to parse into anything meaningful.
# For example, "mode #somechan +o" would be erroneous because +o
# requires an argument!
irc.reply("Error: No valid modes were given.")
return
irc.proto.modeClient(irc.pseudoclient.uid, target, parsedmodes)
# Call the appropriate hooks for plugin like relay.
irc.callHooks([irc.pseudoclient.uid, 'OPERCMDS_MODEOVERRIDE',
{'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}])