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

relay: various cleanup

This commit is contained in:
James Lu 2016-02-20 19:29:52 -08:00
parent eec8e0dca4
commit de1a9a7995

View File

@ -717,18 +717,22 @@ def handle_messages(irc, numeric, command, args):
# Drop attempted PMs between internal clients (this shouldn't happen, # Drop attempted PMs between internal clients (this shouldn't happen,
# but whatever). # but whatever).
return return
elif numeric in irc.servers: elif numeric in irc.servers:
# Sender is a server? This shouldn't be allowed, except for some truly # Sender is a server? This shouldn't be allowed, except for some truly
# special cases... We'll route these through the main PyLink client, # special cases... We'll route these through the main PyLink client,
# tagging the message with the sender name. # tagging the message with the sender name.
text = '[from %s] %s' % (irc.servers[numeric].name, text) text = '[from %s] %s' % (irc.servers[numeric].name, text)
numeric = irc.pseudoclient.uid numeric = irc.pseudoclient.uid
elif numeric not in irc.users: elif numeric not in irc.users:
# Sender didn't pass the check above, AND isn't a user. # Sender didn't pass the check above, AND isn't a user.
log.debug('(%s) relay: Unknown message sender %s.', irc.name, numeric) log.debug('(%s) relay: Unknown message sender %s.', irc.name, numeric)
return return
relay = getRelay((irc.name, target)) relay = getRelay((irc.name, target))
remoteusers = relayusers[(irc.name, numeric)] remoteusers = relayusers[(irc.name, numeric)]
# HACK: Don't break on sending to @#channel or similar. # HACK: Don't break on sending to @#channel or similar.
try: try:
prefix, target = target.split('#', 1) prefix, target = target.split('#', 1)
@ -736,6 +740,7 @@ def handle_messages(irc, numeric, command, args):
prefix = '' prefix = ''
else: else:
target = '#' + target target = '#' + target
log.debug('(%s) relay privmsg: prefix is %r, target is %r', irc.name, prefix, target) log.debug('(%s) relay privmsg: prefix is %r, target is %r', irc.name, prefix, target)
if utils.isChannel(target) and relay and numeric not in irc.channels[target].users: if utils.isChannel(target) and relay and numeric not in irc.channels[target].users:
# The sender must be in the target channel to send messages over the relay; # The sender must be in the target channel to send messages over the relay;
@ -745,22 +750,33 @@ def handle_messages(irc, numeric, command, args):
irc.msg(numeric, 'Error: You must be in %r in order to send ' irc.msg(numeric, 'Error: You must be in %r in order to send '
'messages over the relay.' % target, notice=True) 'messages over the relay.' % target, notice=True)
return return
if utils.isChannel(target): if utils.isChannel(target):
for name, remoteirc in world.networkobjects.items(): for name, remoteirc in world.networkobjects.items():
real_target = getRemoteChan(irc, remoteirc, target) real_target = getRemoteChan(irc, remoteirc, target)
if irc.name == name or not remoteirc.connected.is_set() or not real_target:
# Don't relay anything back to the source net, or to disconnected networks
# and networks without a relay for this channel
if irc.name == name or (not remoteirc.connected.is_set()) or (not real_target) \
or (not irc.connected.is_set()):
continue continue
user = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False) user = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False)
real_target = prefix + real_target real_target = prefix + real_target
log.debug('(%s) relay: sending message to %s from %s on behalf of %s',
irc.name, real_target, user, numeric)
if notice: if notice:
remoteirc.proto.notice(user, real_target, text) remoteirc.proto.notice(user, real_target, text)
else: else:
remoteirc.proto.message(user, real_target, text) remoteirc.proto.message(user, real_target, text)
else: else:
remoteuser = getOrigUser(irc, target) # Get the real user that the PM was meant for
if remoteuser is None: origuser = getOrigUser(irc, target)
if origuser is None: # Not a relay client, return
return return
homenet, real_target = remoteuser homenet, real_target = origuser
# For PMs, we must be on a common channel with the target. # For PMs, we must be on a common channel with the target.
# Otherwise, the sender doesn't have a client representing them # Otherwise, the sender doesn't have a client representing them
# on the remote network, and we won't have anything to send our # on the remote network, and we won't have anything to send our
@ -772,10 +788,12 @@ def handle_messages(irc, numeric, command, args):
return return
remoteirc = world.networkobjects[homenet] remoteirc = world.networkobjects[homenet]
user = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False) user = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False)
if notice: if notice:
remoteirc.proto.notice(user, real_target, text) remoteirc.proto.notice(user, real_target, text)
else: else:
remoteirc.proto.message(user, real_target, text) remoteirc.proto.message(user, real_target, text)
for cmd in ('PRIVMSG', 'NOTICE', 'PYLINK_SELF_NOTICE', 'PYLINK_SELF_PRIVMSG'): for cmd in ('PRIVMSG', 'NOTICE', 'PYLINK_SELF_NOTICE', 'PYLINK_SELF_PRIVMSG'):
utils.add_hook(handle_messages, cmd) utils.add_hook(handle_messages, cmd)