3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-25 12:12:53 +01:00
PyLink/plugins/bots.py

151 lines
5.5 KiB
Python
Raw Normal View History

"""
bots.py: Spawn virtual users/bots on a PyLink server and make them interact
with things.
"""
import sys
import os
2015-06-07 22:40:18 +02:00
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
2015-06-07 22:40:18 +02:00
import utils
from log import log
2015-06-07 22:40:18 +02:00
@utils.add_cmd
def spawnclient(irc, source, args):
"""<nick> <ident> <host>
Admin-only. Spawns the specified PseudoClient on the PyLink server.
Note: this doesn't check the validity of any fields you give it!"""
utils.checkAuthenticated(irc, source, allowOper=False)
2015-06-07 22:40:18 +02:00
try:
nick, ident, host = args[:3]
except ValueError:
irc.reply("Error: Not enough arguments. Needs 3: nick, user, host.")
2015-06-07 22:40:18 +02:00
return
irc.proto.spawnClient(nick, ident, host, manipulatable=True)
2015-06-07 22:40:18 +02:00
@utils.add_cmd
def quit(irc, source, args):
"""<target> [<reason>]
2015-07-22 22:18:11 +02:00
Admin-only. Quits the PyLink client with nick <target>, if one exists."""
utils.checkAuthenticated(irc, source, allowOper=False)
2015-06-07 22:40:18 +02:00
try:
nick = args[0]
2015-06-07 22:40:18 +02:00
except IndexError:
irc.reply("Error: Not enough arguments. Needs 1-2: nick, reason (optional).")
2015-06-07 22:40:18 +02:00
return
if irc.pseudoclient.uid == irc.nickToUid(nick):
irc.reply("Error: Cannot quit the main PyLink PseudoClient!")
2015-06-07 22:40:18 +02:00
return
u = irc.nickToUid(nick)
quitmsg = ' '.join(args[1:]) or 'Client Quit'
if not utils.isManipulatableClient(irc, u):
irc.reply("Error: Cannot force quit a protected PyLink services client.")
return
irc.proto.quit(u, quitmsg)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_QUIT', {'text': quitmsg, 'parse_as': 'QUIT'}])
2015-06-07 22:40:18 +02:00
def joinclient(irc, source, args):
"""<target> <channel1>,[<channel2>], etc.
2015-07-22 22:18:11 +02:00
Admin-only. Joins <target>, the nick of a PyLink client, to a comma-separated list of channels."""
utils.checkAuthenticated(irc, source, allowOper=False)
2015-06-07 22:40:18 +02:00
try:
nick = args[0]
clist = args[1].split(',')
if not clist:
raise IndexError
except IndexError:
irc.reply("Error: Not enough arguments. Needs 2: nick, comma separated list of channels.")
2015-06-07 22:40:18 +02:00
return
u = irc.nickToUid(nick)
if not utils.isManipulatableClient(irc, u):
irc.reply("Error: Cannot force join a protected PyLink services client.")
return
for channel in clist:
if not utils.isChannel(channel):
irc.reply("Error: Invalid channel name %r." % channel)
return
irc.proto.join(u, channel)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_JOIN', {'channel': channel, 'users': [u],
'modes': irc.channels[channel].modes,
'parse_as': 'JOIN'}])
utils.add_cmd(joinclient, name='join')
@utils.add_cmd
def nick(irc, source, args):
"""<target> <newnick>
Admin-only. Changes the nick of <target>, a PyLink client, to <newnick>."""
utils.checkAuthenticated(irc, source, allowOper=False)
try:
nick = args[0]
newnick = args[1]
except IndexError:
irc.reply("Error: Not enough arguments. Needs 2: nick, newnick.")
return
u = irc.nickToUid(nick)
if newnick in ('0', u):
newnick = u
elif not utils.isNick(newnick):
irc.reply('Error: Invalid nickname %r.' % newnick)
return
elif not utils.isManipulatableClient(irc, u):
irc.reply("Error: Cannot force nick changes for a protected PyLink services client.")
return
irc.proto.nick(u, newnick)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_NICK', {'newnick': newnick, 'oldnick': nick, 'parse_as': 'NICK'}])
@utils.add_cmd
def part(irc, source, args):
"""<target> <channel1>,[<channel2>],... [<reason>]
2015-07-22 22:18:11 +02:00
Admin-only. Parts <target>, the nick of a PyLink client, from a comma-separated list of channels."""
utils.checkAuthenticated(irc, source, allowOper=False)
try:
nick = args[0]
clist = args[1].split(',')
reason = ' '.join(args[2:])
except IndexError:
irc.reply("Error: Not enough arguments. Needs 2: nick, comma separated list of channels.")
2015-06-07 22:40:18 +02:00
return
u = irc.nickToUid(nick)
if not utils.isManipulatableClient(irc, u):
irc.reply("Error: Cannot force part a protected PyLink services client.")
return
2015-06-07 22:40:18 +02:00
for channel in clist:
if not utils.isChannel(channel):
irc.reply("Error: Invalid channel name %r." % channel)
2015-06-07 22:40:18 +02:00
return
irc.proto.part(u, channel, reason)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}])
2015-08-15 13:51:32 +02:00
@utils.add_cmd
def msg(irc, source, args):
"""<source> <target> <text>
Admin-only. Sends message <text> from <source>, where <source> is the nick of a PyLink client."""
utils.checkAuthenticated(irc, source, allowOper=False)
2015-08-15 13:51:32 +02:00
try:
msgsource, target, text = args[0], args[1], ' '.join(args[2:])
except IndexError:
irc.reply('Error: Not enough arguments. Needs 3: source nick, target, text.')
2015-08-15 13:51:32 +02:00
return
sourceuid = irc.nickToUid(msgsource)
2015-08-15 13:51:32 +02:00
if not sourceuid:
irc.reply('Error: Unknown user %r.' % msgsource)
2015-08-15 13:51:32 +02:00
return
if not utils.isChannel(target):
real_target = irc.nickToUid(target)
2015-08-15 13:51:32 +02:00
if real_target is None:
irc.reply('Error: Unknown user %r.' % target)
2015-08-15 13:51:32 +02:00
return
else:
real_target = target
if not text:
irc.reply('Error: No text given.')
2015-08-15 13:51:32 +02:00
return
irc.proto.message(sourceuid, real_target, text)
irc.callHooks([sourceuid, 'PYLINK_BOTSPLUGIN_MSG', {'target': real_target, 'text': text, 'parse_as': 'PRIVMSG'}])