2015-07-17 06:30:25 +02:00
|
|
|
## coreplugin.py - Core PyLink plugin
|
|
|
|
|
|
|
|
import utils
|
|
|
|
from log import log
|
2015-08-29 18:39:33 +02:00
|
|
|
import world
|
2015-07-17 06:30:25 +02:00
|
|
|
|
|
|
|
# Handle KILLs sent to the PyLink client and respawn
|
|
|
|
def handle_kill(irc, source, command, args):
|
|
|
|
if args['target'] == irc.pseudoclient.uid:
|
|
|
|
irc.spawnMain()
|
2015-08-18 14:44:36 +02:00
|
|
|
utils.add_hook(handle_kill, 'KILL')
|
2015-07-17 06:30:25 +02:00
|
|
|
|
|
|
|
# Handle KICKs to the PyLink client, rejoining the channels
|
|
|
|
def handle_kick(irc, source, command, args):
|
|
|
|
kicked = args['target']
|
|
|
|
channel = args['channel']
|
|
|
|
if kicked == irc.pseudoclient.uid:
|
2015-09-06 03:00:57 +02:00
|
|
|
irc.proto.joinClient(irc.pseudoclient.uid, channel)
|
2015-08-18 14:44:36 +02:00
|
|
|
utils.add_hook(handle_kick, 'KICK')
|
2015-07-17 06:30:25 +02:00
|
|
|
|
|
|
|
# Handle commands sent to the PyLink client (PRIVMSG)
|
|
|
|
def handle_commands(irc, source, command, args):
|
|
|
|
if args['target'] == irc.pseudoclient.uid:
|
2015-08-18 14:49:27 +02:00
|
|
|
text = args['text'].strip()
|
|
|
|
cmd_args = text.split(' ')
|
2015-07-17 06:30:25 +02:00
|
|
|
cmd = cmd_args[0].lower()
|
|
|
|
cmd_args = cmd_args[1:]
|
2015-08-30 04:24:32 +02:00
|
|
|
if cmd not in world.bot_commands:
|
2015-09-07 07:23:44 +02:00
|
|
|
irc.msg(source, 'Error: Unknown command %r.' % cmd)
|
2015-07-17 06:30:25 +02:00
|
|
|
return
|
2015-08-30 04:24:32 +02:00
|
|
|
log.info('(%s) Calling command %r for %s', irc.name, cmd, utils.getHostmask(irc, source))
|
|
|
|
for func in world.bot_commands[cmd]:
|
|
|
|
try:
|
|
|
|
func(irc, source, cmd_args)
|
2015-09-03 08:37:54 +02:00
|
|
|
except utils.NotAuthenticatedError:
|
2015-09-07 07:23:44 +02:00
|
|
|
irc.msg(source, 'Error: You are not authorized to perform this operation.')
|
2015-08-30 04:24:32 +02:00
|
|
|
except Exception as e:
|
|
|
|
log.exception('Unhandled exception caught in command %r', cmd)
|
2015-09-07 07:23:44 +02:00
|
|
|
irc.msg(source, 'Uncaught exception in command %r: %s: %s' % (cmd, type(e).__name__, str(e)))
|
2015-07-17 06:30:25 +02:00
|
|
|
utils.add_hook(handle_commands, 'PRIVMSG')
|
2015-07-23 06:15:34 +02:00
|
|
|
|
2015-08-25 03:26:02 +02:00
|
|
|
# Handle WHOIS queries, for IRCds that send them across servers (charybdis, UnrealIRCd; NOT InspIRCd).
|
2015-07-23 06:15:34 +02:00
|
|
|
def handle_whois(irc, source, command, args):
|
|
|
|
target = args['target']
|
|
|
|
user = irc.users.get(target)
|
|
|
|
if user is None:
|
|
|
|
log.warning('(%s) Got a WHOIS request for %r from %r, but the target doesn\'t exist in irc.users!', irc.name, target, source)
|
|
|
|
f = irc.proto.numericServer
|
|
|
|
server = utils.clientToServer(irc, target) or irc.sid
|
|
|
|
nick = user.nick
|
2015-07-23 09:05:05 +02:00
|
|
|
sourceisOper = ('o', None) in irc.users[source].modes
|
2015-07-23 06:15:34 +02:00
|
|
|
# https://www.alien.net.au/irc/irc2numerics.html
|
|
|
|
# 311: sends nick!user@host information
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 311, source, "%s %s %s * :%s" % (nick, user.ident, user.host, user.realname))
|
2015-07-23 09:01:51 +02:00
|
|
|
# 319: RPL_WHOISCHANNELS, shows channel list
|
|
|
|
public_chans = []
|
|
|
|
for chan in user.channels:
|
|
|
|
# Here, we'll want to hide secret/private channels from non-opers
|
|
|
|
# who are not in them.
|
|
|
|
c = irc.channels[chan]
|
|
|
|
if ((irc.cmodes.get('secret'), None) in c.modes or \
|
|
|
|
(irc.cmodes.get('private'), None) in c.modes) \
|
2015-07-23 09:05:05 +02:00
|
|
|
and not (sourceisOper or source in c.users):
|
2015-07-23 09:01:51 +02:00
|
|
|
continue
|
2015-08-25 03:37:46 +02:00
|
|
|
# Show prefix modes like a regular IRCd does.
|
|
|
|
for prefixmode, prefixchar in irc.prefixmodes.items():
|
|
|
|
modename = [mname for mname, char in irc.cmodes.items() if char == prefixmode]
|
|
|
|
if modename and target in c.prefixmodes[modename[0]+'s']:
|
|
|
|
chan = prefixchar + chan
|
2015-07-23 09:01:51 +02:00
|
|
|
public_chans.append(chan)
|
|
|
|
if public_chans:
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 319, source, '%s :%s' % (nick, ' '.join(public_chans)))
|
2015-08-25 03:26:02 +02:00
|
|
|
# 312: sends the server the target is on, and its server description.
|
2015-09-07 07:40:22 +02:00
|
|
|
f(server, 312, source, "%s %s :%s" % (nick, irc.servers[server].name,
|
2015-08-25 03:26:02 +02:00
|
|
|
irc.serverdata.get('serverdesc') or irc.botdata['serverdesc']))
|
|
|
|
# 313: sends a string denoting the target's operator privilege,
|
|
|
|
# only if they have umode +o.
|
|
|
|
if ('o', None) in user.modes:
|
2015-08-31 23:52:56 +02:00
|
|
|
if hasattr(user, 'opertype'):
|
|
|
|
opertype = user.opertype.replace("_", " ")
|
|
|
|
else:
|
|
|
|
opertype = "IRC Operator"
|
|
|
|
# Let's be gramatically correct.
|
|
|
|
n = 'n' if opertype[0].lower() in 'aeiou' else ''
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 313, source, "%s :is a%s %s" % (nick, n, opertype))
|
2015-08-25 03:26:02 +02:00
|
|
|
# 379: RPL_WHOISMODES, used by UnrealIRCd and InspIRCd.
|
|
|
|
# Only show this to opers!
|
|
|
|
if sourceisOper:
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 379, source, '%s :is using modes %s' % (nick, utils.joinModes(user.modes)))
|
2015-08-25 03:26:02 +02:00
|
|
|
# 317: shows idle and signon time. However, we don't track the user's real
|
|
|
|
# idle time, so we simply return 0.
|
|
|
|
# <- 317 GL GL 15 1437632859 :seconds idle, signon time
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 317, source, "%s 0 %s :seconds idle, signon time" % (nick, user.ts))
|
2015-08-29 18:39:33 +02:00
|
|
|
for func in world.whois_handlers:
|
2015-08-25 03:38:58 +02:00
|
|
|
# Iterate over custom plugin WHOIS handlers. They return a tuple
|
|
|
|
# or list with two arguments: the numeric, and the text to send.
|
|
|
|
try:
|
2015-07-23 09:10:54 +02:00
|
|
|
res = func(irc, target)
|
|
|
|
if res:
|
|
|
|
num, text = res
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, num, source, text)
|
2015-08-25 03:38:58 +02:00
|
|
|
except Exception as e:
|
|
|
|
# Again, we wouldn't want this to crash our service, in case
|
|
|
|
# something goes wrong!
|
|
|
|
log.exception('(%s) Error caught in WHOIS handler: %s', irc.name, e)
|
|
|
|
# 318: End of WHOIS.
|
2015-09-06 03:00:57 +02:00
|
|
|
f(server, 318, source, "%s :End of /WHOIS list" % nick)
|
2015-07-23 06:15:34 +02:00
|
|
|
utils.add_hook(handle_whois, 'WHOIS')
|