2015-09-03 08:24:58 +02:00
|
|
|
"""
|
|
|
|
bots.py: Spawn virtual users/bots on a PyLink server and make them interact
|
|
|
|
with things.
|
|
|
|
"""
|
|
|
|
|
2015-07-19 23:59:51 +02:00
|
|
|
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-07-19 23:59:51 +02:00
|
|
|
|
2015-06-07 22:40:18 +02:00
|
|
|
import utils
|
2015-07-19 23:59:51 +02:00
|
|
|
from log import log
|
2015-06-07 22:40:18 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
|
|
|
def spawnclient(irc, source, args):
|
2015-07-18 07:35:34 +02:00
|
|
|
"""<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!"""
|
2015-08-26 05:38:32 +02:00
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
2015-06-07 22:40:18 +02:00
|
|
|
try:
|
|
|
|
nick, ident, host = args[:3]
|
|
|
|
except ValueError:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Not enough arguments. Needs 3: nick, user, host.")
|
2015-06-07 22:40:18 +02:00
|
|
|
return
|
2015-09-18 04:07:16 +02:00
|
|
|
irc.proto.spawnClient(nick, ident, host, manipulatable=True)
|
2015-06-07 22:40:18 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
2015-07-11 01:43:03 +02:00
|
|
|
def quit(irc, source, args):
|
2015-07-18 07:35:34 +02:00
|
|
|
"""<target> [<reason>]
|
|
|
|
|
2015-07-22 22:18:11 +02:00
|
|
|
Admin-only. Quits the PyLink client with nick <target>, if one exists."""
|
2015-08-26 05:38:32 +02:00
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-06-07 22:40:18 +02:00
|
|
|
try:
|
2015-06-08 04:31:56 +02:00
|
|
|
nick = args[0]
|
2015-06-07 22:40:18 +02:00
|
|
|
except IndexError:
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Not enough arguments. Needs 1-2: nick, reason (optional).")
|
2015-06-07 22:40:18 +02:00
|
|
|
return
|
2016-01-01 02:28:47 +01:00
|
|
|
if irc.pseudoclient.uid == irc.nickToUid(nick):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Cannot quit the main PyLink PseudoClient!")
|
2015-06-07 22:40:18 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2016-01-01 02:28:47 +01:00
|
|
|
u = irc.nickToUid(nick)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-08-26 05:55:39 +02:00
|
|
|
quitmsg = ' '.join(args[1:]) or 'Client Quit'
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-09-18 04:07:16 +02:00
|
|
|
if not utils.isManipulatableClient(irc, u):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Cannot force quit a protected PyLink services client.")
|
2015-09-18 04:07:16 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2016-01-17 01:51:54 +01:00
|
|
|
irc.proto.quit(u, quitmsg)
|
2015-09-03 08:24:58 +02:00
|
|
|
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_QUIT', {'text': quitmsg, 'parse_as': 'QUIT'}])
|
2015-06-07 22:40:18 +02:00
|
|
|
|
|
|
|
def joinclient(irc, source, args):
|
2016-02-28 04:05:06 +01:00
|
|
|
"""[<target>] <channel1>,[<channel2>], etc.
|
2015-07-18 07:35:34 +02:00
|
|
|
|
2016-02-28 04:05:06 +01:00
|
|
|
Admin-only. Joins <target>, the nick of a PyLink client, to a comma-separated list of channels. If <target> is not given, it defaults to the main PyLink client."""
|
2015-08-26 05:38:32 +02:00
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-06-07 22:40:18 +02:00
|
|
|
try:
|
2016-02-28 04:05:06 +01:00
|
|
|
# Check if the first argument is an existing PyLink client. If it is not,
|
|
|
|
# then assume that the first argument was actually the channels being joined.
|
|
|
|
u = irc.nickToUid(args[0])
|
|
|
|
|
|
|
|
if not irc.isInternalClient(u): # First argument isn't one of our clients
|
2015-06-07 22:40:18 +02:00
|
|
|
raise IndexError
|
2016-02-28 04:05:06 +01:00
|
|
|
|
|
|
|
clist = args[1]
|
|
|
|
except IndexError: # No nick was given; shift arguments one to the left.
|
|
|
|
u = irc.pseudoclient.uid
|
|
|
|
try:
|
|
|
|
clist = args[0]
|
|
|
|
except IndexError:
|
|
|
|
irc.reply("Error: Not enough arguments. Needs 1-2: nick (optional), comma separated list of channels.")
|
|
|
|
return
|
|
|
|
|
|
|
|
clist = clist.split(',')
|
|
|
|
if not clist:
|
|
|
|
irc.reply("Error: No valid channels given.")
|
2015-06-07 22:40:18 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-09-18 04:07:16 +02:00
|
|
|
if not utils.isManipulatableClient(irc, u):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Cannot force join a protected PyLink services client.")
|
2015-09-18 04:07:16 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-06-08 04:31:56 +02:00
|
|
|
for channel in clist:
|
2015-07-09 07:50:19 +02:00
|
|
|
if not utils.isChannel(channel):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Invalid channel name %r." % channel)
|
2015-06-08 04:31:56 +02:00
|
|
|
return
|
2016-01-17 01:36:45 +01:00
|
|
|
irc.proto.join(u, channel)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
|
|
|
# Call a join hook manually so other plugins like relay can understand it.
|
2015-09-03 08:24:58 +02:00
|
|
|
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_JOIN', {'channel': channel, 'users': [u],
|
2015-08-15 14:12:49 +02:00
|
|
|
'modes': irc.channels[channel].modes,
|
|
|
|
'parse_as': 'JOIN'}])
|
2015-07-09 07:50:19 +02:00
|
|
|
utils.add_cmd(joinclient, name='join')
|
2015-06-08 04:31:56 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
2015-07-09 07:50:19 +02:00
|
|
|
def nick(irc, source, args):
|
2016-02-28 04:05:06 +01:00
|
|
|
"""[<target>] <newnick>
|
2015-07-18 07:35:34 +02:00
|
|
|
|
2016-02-28 04:05:06 +01:00
|
|
|
Admin-only. Changes the nick of <target>, a PyLink client, to <newnick>. If <target> is not given, it defaults to the main PyLink client."""
|
2015-08-26 05:38:32 +02:00
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-06-08 04:31:56 +02:00
|
|
|
try:
|
|
|
|
nick = args[0]
|
|
|
|
newnick = args[1]
|
|
|
|
except IndexError:
|
2016-02-28 04:05:06 +01:00
|
|
|
try:
|
|
|
|
nick = irc.pseudoclient.nick
|
|
|
|
newnick = args[0]
|
|
|
|
except IndexError:
|
|
|
|
irc.reply("Error: Not enough arguments. Needs 1-2: nick (optional), newnick.")
|
|
|
|
return
|
2016-01-01 02:28:47 +01:00
|
|
|
u = irc.nickToUid(nick)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
|
|
|
if newnick in ('0', u): # Allow /nick 0 to work
|
2015-07-09 07:50:19 +02:00
|
|
|
newnick = u
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-07-09 07:50:19 +02:00
|
|
|
elif not utils.isNick(newnick):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: Invalid nickname %r.' % newnick)
|
2015-07-09 07:50:19 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-09-18 04:07:16 +02:00
|
|
|
elif not utils.isManipulatableClient(irc, u):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Cannot force nick changes for a protected PyLink services client.")
|
2015-09-18 04:07:16 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2016-01-17 01:51:04 +01:00
|
|
|
irc.proto.nick(u, newnick)
|
2016-02-28 04:05:06 +01:00
|
|
|
# Ditto above: manually send a NICK change hook payload to other plugins.
|
2015-09-03 08:24:58 +02:00
|
|
|
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_NICK', {'newnick': newnick, 'oldnick': nick, 'parse_as': 'NICK'}])
|
2015-06-08 04:31:56 +02:00
|
|
|
|
|
|
|
@utils.add_cmd
|
2015-07-09 07:50:19 +02:00
|
|
|
def part(irc, source, args):
|
2016-02-28 04:05:06 +01:00
|
|
|
"""[<target>] <channel1>,[<channel2>],... [<reason>]
|
2015-07-18 07:35:34 +02:00
|
|
|
|
2016-02-28 04:05:06 +01:00
|
|
|
Admin-only. Parts <target>, the nick of a PyLink client, from a comma-separated list of channels. If <target> is not given, it defaults to the main PyLink client."""
|
2015-08-26 05:38:32 +02:00
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-06-08 04:31:56 +02:00
|
|
|
try:
|
|
|
|
nick = args[0]
|
2016-02-28 04:05:06 +01:00
|
|
|
clist = args[1]
|
|
|
|
# For the part message, join all remaining arguments into one text string
|
2015-06-08 04:31:56 +02:00
|
|
|
reason = ' '.join(args[2:])
|
2016-02-28 04:05:06 +01:00
|
|
|
|
|
|
|
# First, check if the first argument is an existing PyLink client. If it is not,
|
|
|
|
# then assume that the first argument was actually the channels being parted.
|
|
|
|
u = irc.nickToUid(nick)
|
|
|
|
if not irc.isInternalClient(u): # First argument isn't one of our clients
|
|
|
|
raise IndexError
|
|
|
|
|
|
|
|
except IndexError: # No nick was given; shift arguments one to the left.
|
|
|
|
u = irc.pseudoclient.uid
|
|
|
|
|
|
|
|
try:
|
|
|
|
clist = args[0]
|
|
|
|
except IndexError:
|
|
|
|
irc.reply("Error: Not enough arguments. Needs 1-2: nick (optional), comma separated list of channels.")
|
|
|
|
return
|
|
|
|
reason = ' '.join(args[1:])
|
|
|
|
|
|
|
|
clist = clist.split(',')
|
|
|
|
if not clist:
|
|
|
|
irc.reply("Error: No valid channels given.")
|
2015-06-07 22:40:18 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-09-18 04:07:16 +02:00
|
|
|
if not utils.isManipulatableClient(irc, u):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Cannot force part a protected PyLink services client.")
|
2015-09-18 04:07:16 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-06-07 22:40:18 +02:00
|
|
|
for channel in clist:
|
2015-07-09 07:50:19 +02:00
|
|
|
if not utils.isChannel(channel):
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply("Error: Invalid channel name %r." % channel)
|
2015-06-07 22:40:18 +02:00
|
|
|
return
|
2016-01-17 01:51:54 +01:00
|
|
|
irc.proto.part(u, channel, reason)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-09-03 08:24:58 +02:00
|
|
|
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}])
|
2015-06-08 04:31:56 +02:00
|
|
|
|
2015-08-15 13:51:32 +02:00
|
|
|
@utils.add_cmd
|
|
|
|
def msg(irc, source, args):
|
2016-02-28 04:05:06 +01:00
|
|
|
"""[<source>] <target> <text>
|
2015-08-15 13:51:32 +02:00
|
|
|
|
2016-02-28 04:05:06 +01:00
|
|
|
Admin-only. Sends message <text> from <source>, where <source> is the nick of a PyLink client. If <source> is not given, it defaults to the main PyLink client."""
|
2015-08-26 05:38:32 +02:00
|
|
|
utils.checkAuthenticated(irc, source, allowOper=False)
|
2016-02-28 04:05:06 +01:00
|
|
|
|
|
|
|
# Because we want the source nick to be optional, this argument parsing gets a bit tricky.
|
2015-08-15 13:51:32 +02:00
|
|
|
try:
|
2016-02-28 04:05:06 +01:00
|
|
|
msgsource = args[0]
|
|
|
|
target = args[1]
|
|
|
|
text = ' '.join(args[2:])
|
|
|
|
|
|
|
|
# First, check if the first argument is an existing PyLink client. If it is not,
|
|
|
|
# then assume that the first argument was actually the message TARGET.
|
|
|
|
sourceuid = irc.nickToUid(msgsource)
|
|
|
|
if not irc.isInternalClient(sourceuid): # First argument isn't one of our clients
|
|
|
|
raise IndexError
|
|
|
|
|
|
|
|
if not text:
|
|
|
|
raise IndexError
|
2015-08-15 13:51:32 +02:00
|
|
|
except IndexError:
|
2016-02-28 04:05:06 +01:00
|
|
|
try:
|
|
|
|
sourceuid = irc.pseudoclient.uid
|
|
|
|
target = args[0]
|
|
|
|
text = ' '.join(args[1:])
|
|
|
|
except IndexError:
|
2016-02-28 04:07:03 +01:00
|
|
|
irc.reply('Error: Not enough arguments. Needs 2-3: source nick (optional), target, text.')
|
2016-02-28 04:05:06 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
if not text:
|
|
|
|
irc.reply('Error: No text given.')
|
2015-08-15 13:51:32 +02:00
|
|
|
return
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2015-08-15 13:51:32 +02:00
|
|
|
if not utils.isChannel(target):
|
2016-02-28 04:05:06 +01:00
|
|
|
# Convert nick of the message target to a UID, if the target isn't a channel
|
2016-01-01 02:28:47 +01:00
|
|
|
real_target = irc.nickToUid(target)
|
2016-02-28 04:05:06 +01:00
|
|
|
if real_target is None: # Unknown target user, if target isn't a valid channel name
|
2015-10-24 03:29:10 +02:00
|
|
|
irc.reply('Error: Unknown user %r.' % target)
|
2015-08-15 13:51:32 +02:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
real_target = target
|
2016-02-28 04:05:06 +01:00
|
|
|
|
2016-01-17 01:44:23 +01:00
|
|
|
irc.proto.message(sourceuid, real_target, text)
|
2015-09-03 08:24:58 +02:00
|
|
|
irc.callHooks([sourceuid, 'PYLINK_BOTSPLUGIN_MSG', {'target': real_target, 'text': text, 'parse_as': 'PRIVMSG'}])
|
2016-03-08 06:37:08 +01:00
|
|
|
utils.add_cmd(msg, 'say')
|