3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-24 03:29:28 +01:00

relay: warn users when messaging channels they aren't in / remote users w/o sharing a channel

Some refactoring of relay's PRIVMSG handling is done here:
- Messages to channels the sender isn't in are dropped, with a notice warning sent.
- Messaging a remote user without sharing a channel with them raises an error, and is dropped.

Closes #91.
This commit is contained in:
James Lu 2015-08-15 00:02:46 -07:00
parent e5c7d438b1
commit 4382b22138

View File

@ -311,57 +311,51 @@ def handle_privmsg(irc, numeric, command, args):
text = args['text'] text = args['text']
if target == irc.pseudoclient.uid: if target == irc.pseudoclient.uid:
return return
sent = 0
relay = findRelay((irc.name, target)) relay = findRelay((irc.name, target))
# Don't send any "you must be in common channels" if we're not part remoteusers = relayusers[(irc.name, numeric)]
# of a relay, or we are but there are no links! if utils.isChannel(target) and relay and numeric not in irc.channels[target].users:
remoteusers = relayusers[(irc.name, numeric)].items() # The sender must be in the target channel to send messages over the relay;
''' # it's the only way we can make sure they have a spawned client on ALL
if utils.isChannel(target) and ((relay and not db[relay]['links']) or \ # of the linked networks. This affects -n channels too; see
relay is None): # https://github.com/GLolol/PyLink/issues/91 for an explanation of why.
return utils.msg(irc, numeric, 'Error: You must be in %r in order to send '
''' 'messages over the relay.' % target, notice=True)
if not remoteusers:
return return
if utils.isChannel(target):
for netname, user in relayusers[(irc.name, numeric)].items(): for netname, user in relayusers[(irc.name, numeric)].items():
remoteirc = utils.networkobjects[netname] remoteirc = utils.networkobjects[netname]
# HACK: Don't break on sending to @#channel or similar. # HACK: Don't break on sending to @#channel or similar.
try:
prefix, target = target.split('#', 1) prefix, target = target.split('#', 1)
except ValueError:
prefix = ''
else:
target = '#' + target target = '#' + target
if utils.isChannel(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)
real_target = findRemoteChan(irc, remoteirc, target) real_target = findRemoteChan(irc, remoteirc, target)
if not real_target: if not real_target:
continue continue
real_target = prefix + real_target real_target = prefix + real_target
else:
remoteuser = getLocalUser(irc, target)
if remoteuser is None:
continue
real_target = remoteuser[1]
if notice: if notice:
remoteirc.proto.noticeClient(remoteirc, user, real_target, text) remoteirc.proto.noticeClient(remoteirc, user, real_target, text)
else: else:
remoteirc.proto.messageClient(remoteirc, user, real_target, text) remoteirc.proto.messageClient(remoteirc, user, real_target, text)
sent += 1
'''
if not sent:
# We must be on a common channel with the target. Otherwise, the sender
# doesn't have a client representing them on the remote network,
# and we won't have anywhere to send our messages from.
# In this case, we've iterated over all networks where the sender
# has pseudoclients, and found no suitable targets to send to.
if target in irc.users:
target_s = 'a common channel with %r' % irc.users[target].nick
else: else:
target_s = repr(target) remoteuser = getLocalUser(irc, target)
utils.msg(irc, numeric, 'Error: You must be in %s in order to send messages.' % \ if remoteuser is None:
target_s, notice=True) return
''' homenet, real_target = remoteuser
# For PMs, we must be on a common channel with the target.
# Otherwise, the sender doesn't have a client representing them
# on the remote network, and we won't have anything to send our
# messages from.
if homenet not in remoteusers.keys():
utils.msg(irc, numeric, 'Error: you must be in a common channel '
'with %r in order to send messages.' % \
irc.users[target].nick, notice=True)
return
remoteirc = utils.networkobjects[homenet]
user = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False)
if notice:
remoteirc.proto.noticeClient(remoteirc, user, real_target, text)
else:
remoteirc.proto.messageClient(remoteirc, user, real_target, text)
utils.add_hook(handle_privmsg, 'PRIVMSG') utils.add_hook(handle_privmsg, 'PRIVMSG')
utils.add_hook(handle_privmsg, 'NOTICE') utils.add_hook(handle_privmsg, 'NOTICE')