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

relay/protocols: use utils.toLower() for channel names, respecting IRCd casemappings

Closes #102.
This commit is contained in:
James Lu 2015-09-01 22:01:22 -07:00
parent 4525b8117e
commit 0b590d6ab3
4 changed files with 34 additions and 27 deletions

View File

@ -792,7 +792,7 @@ def create(irc, source, args):
Creates the channel <channel> over the relay."""
try:
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
except IndexError:
utils.msg(irc, source, "Error: Not enough arguments. Needs 1: channel.")
return
@ -815,7 +815,7 @@ def destroy(irc, source, args):
Removes <channel> from the relay, delinking all networks linked to it."""
try:
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
except IndexError:
utils.msg(irc, source, "Error: Not enough arguments. Needs 1: channel.")
return
@ -844,13 +844,13 @@ def link(irc, source, args):
Links channel <channel> on <remotenet> over the relay to <local channel>.
If <local channel> is not specified, it defaults to the same name as <channel>."""
try:
channel = args[1].lower()
channel = utils.toLower(irc, args[1])
remotenet = args[0].lower()
except IndexError:
utils.msg(irc, source, "Error: Not enough arguments. Needs 2-3: remote netname, channel, local channel name (optional).")
return
try:
localchan = args[2].lower()
localchan = utils.toLower(irc, args[2])
except IndexError:
localchan = channel
for c in (channel, localchan):
@ -896,7 +896,7 @@ def delink(irc, source, args):
Delinks channel <local channel>. <network> must and can only be specified if you are on the host network for <local channel>, and allows you to pick which network to delink.
To remove a relay entirely, use the 'destroy' command instead."""
try:
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
except IndexError:
utils.msg(irc, source, "Error: Not enough arguments. Needs 1-2: channel, remote netname (optional).")
return
@ -1039,7 +1039,7 @@ def linkacl(irc, source, args):
return
try:
cmd = args[0].lower()
channel = args[1].lower()
channel = utils.toLower(irc, args[1])
except IndexError:
utils.msg(irc, source, missingargs)
return

View File

@ -53,7 +53,7 @@ def joinClient(irc, client, channel):
# InspIRCd doesn't distinguish between burst joins and regular joins,
# so what we're actually doing here is sending FJOIN from the server,
# on behalf of the clients that call it.
channel = channel.lower()
channel = utils.toLower(irc, channel)
server = utils.isInternalClient(irc, client)
if not server:
log.error('(%s) Error trying to join client %r to %r (no such pseudoclient exists)', irc.name, client, channel)
@ -67,7 +67,7 @@ def joinClient(irc, client, channel):
irc.users[client].channels.add(channel)
def sjoinServer(irc, server, channel, users, ts=None):
channel = channel.lower()
channel = utils.toLower(irc, channel)
server = server or irc.sid
assert users, "sjoinServer: No users sent?"
log.debug('(%s) sjoinServer: got %r for users', irc.name, users)
@ -113,7 +113,7 @@ def sjoinServer(irc, server, channel, users, ts=None):
irc.channels[channel].users.update(uids)
def partClient(irc, client, channel, reason=None):
channel = channel.lower()
channel = utils.toLower(irc, channel)
if not utils.isInternalClient(irc, client):
log.error('(%s) Error trying to part client %r to %r (no such pseudoclient exists)', irc.name, client, channel)
raise LookupError('No such PyLink PseudoClient exists.')
@ -156,7 +156,7 @@ def _sendKick(irc, numeric, channel, target, reason=None):
"""<irc object> <kicker client numeric>
Sends a kick from a PyLink PseudoClient."""
channel = channel.lower()
channel = utils.toLower(irc, channel)
if not reason:
reason = 'No reason given'
_send(irc, numeric, 'KICK %s %s :%s' % (channel, target, reason))
@ -210,7 +210,7 @@ def _sendModes(irc, numeric, target, modes, ts=None):
utils.applyModes(irc, target, modes)
joinedmodes = utils.joinModes(modes)
if utils.isChannel(target):
ts = ts or irc.channels[target.lower()].ts
ts = ts or irc.channels[utils.toLower(irc, target)].ts
_send(irc, numeric, 'FMODE %s %s %s' % (target, ts, joinedmodes))
else:
_send(irc, numeric, 'MODE %s %s' % (target, joinedmodes))
@ -373,7 +373,7 @@ def handle_privmsg(irc, source, command, args):
target = args[0]
# We use lowercase channels internally, but uppercase UIDs.
if utils.isChannel(target):
target = target.lower()
target = utils.toLower(irc, target)
return {'target': target, 'text': args[1]}
handle_notice = handle_privmsg
@ -387,13 +387,13 @@ def handle_kill(irc, source, command, args):
def handle_kick(irc, source, command, args):
# :70MAAAAAA KICK #endlessvoid 70MAAAAAA :some reason
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
kicked = args[1]
handle_part(irc, kicked, 'KICK', [channel, args[2]])
return {'channel': channel, 'target': kicked, 'text': args[2]}
def handle_part(irc, source, command, args):
channels = args[0].lower().split(',')
channels = utils.toLower(irc, args[0]).split(',')
for channel in channels:
# We should only get PART commands for channels that exist, right??
irc.channels[channel].removeuser(source)
@ -416,7 +416,7 @@ def handle_error(irc, numeric, command, args):
def handle_fjoin(irc, servernumeric, command, args):
# :70M FJOIN #chat 1423790411 +AFPfjnt 6:5 7:5 9:5 :o,1SRAABIT4 v,1IOAAF53R <...>
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
# InspIRCd sends each user's channel data in the form of 'modeprefix(es),UID'
userlist = args[-1].split()
our_ts = irc.channels[channel].ts
@ -489,7 +489,7 @@ def handle_save(irc, numeric, command, args):
def handle_fmode(irc, numeric, command, args):
# <- :70MAAAAAA FMODE #chat 1433653462 +hhT 70MAAAAAA 70MAAAAAD
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
modes = args[2:]
changedmodes = utils.parseModes(irc, channel, modes)
utils.applyModes(irc, channel, changedmodes)
@ -657,7 +657,7 @@ def spawnServer(irc, name, sid=None, uplink=None, desc='PyLink Server'):
def handle_ftopic(irc, numeric, command, args):
# <- :70M FTOPIC #channel 1434510754 GLo|o|!GLolol@escape.the.dreamland.ca :Some channel topic
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
ts = args[1]
setter = args[2]
topic = args[-1]
@ -667,7 +667,7 @@ def handle_ftopic(irc, numeric, command, args):
def handle_topic(irc, numeric, command, args):
# <- :70MAAAAAA TOPIC #test :test
channel = args[0].lower()
channel = utils.toLower(irc, args[0])
topic = args[1]
ts = int(time.time())
irc.channels[channel].topic = topic
@ -677,7 +677,7 @@ def handle_topic(irc, numeric, command, args):
def handle_invite(irc, numeric, command, args):
# <- :70MAAAAAC INVITE 0ALAAAAAA #blah 0
target = args[0]
channel = args[1].lower()
channel = utils.toLower(irc, args[1])
# We don't actually need to process this; it's just something plugins/hooks can use
return {'target': target, 'channel': channel}
@ -695,7 +695,7 @@ def handle_encap(irc, numeric, command, args):
targetmask = args[0]
real_command = args[1]
if targetmask == '*' and real_command == 'KNOCK':
channel = args[2].lower()
channel = utils.toLower(irc, args[2])
text = args[3]
return {'parse_as': real_command, 'channel': channel,
'text': text}

View File

@ -51,7 +51,7 @@ def spawnClient(irc, nick, ident='null', host='null', realhost=None, modes=set()
return u
def joinClient(irc, client, channel):
channel = channel.lower()
channel = utils.toLower(irc, channel)
# JOIN:
# parameters: channelTS, channel, '+' (a plus sign)
if not utils.isInternalClient(irc, client):
@ -71,7 +71,7 @@ def sjoinServer(irc, server, channel, users, ts=None):
# their status ('@+', '@', '+' or ''), for example:
# '@+1JJAAAAAB +2JJAAAA4C 1JJAAAADS'. All users must be behind the source server
# so it is not possible to use this message to force users to join a channel.
channel = channel.lower()
channel = utils.toLower(irc, channel)
server = server or irc.sid
assert users, "sjoinServer: No users sent?"
log.debug('(%s) sjoinServer: got %r for users', irc.name, users)
@ -124,7 +124,7 @@ def sjoinServer(irc, server, channel, users, ts=None):
def _sendModes(irc, numeric, target, modes, ts=None):
utils.applyModes(irc, target, modes)
if utils.isChannel(target):
ts = ts or irc.channels[target.lower()].ts
ts = ts or irc.channels[utils.toLower(irc, target)].ts
# TMODE:
# parameters: channelTS, channel, cmode changes, opt. cmode parameters...
@ -371,7 +371,7 @@ def handle_pong(irc, source, command, args):
irc.lastping = time.time()
def handle_part(irc, source, command, args):
channels = args[0].lower().split(',')
channels = utils.toLower(irc, args[0]).split(',')
# We should only get PART commands for channels that exist, right??
for channel in channels:
irc.channels[channel].removeuser(source)
@ -389,7 +389,7 @@ def handle_part(irc, source, command, args):
def handle_sjoin(irc, servernumeric, command, args):
# parameters: channelTS, channel, simple modes, opt. mode parameters..., nicklist
channel = args[1].lower()
channel = utils.toLower(irc, args[1])
userlist = args[-1].split()
our_ts = irc.channels[channel].ts
their_ts = int(args[0])
@ -440,7 +440,7 @@ def handle_join(irc, numeric, command, args):
irc.users[numeric].channels.discard(channel)
return {'channels': oldchans, 'text': 'Left all channels.', 'parse_as': 'PART'}
else:
channel = args[1].lower()
channel = utils.toLower(irc, args[1])
our_ts = irc.channels[channel].ts
if ts < our_ts:
# Channel timestamp was reset on burst
@ -498,7 +498,7 @@ handle_sid = handle_server
def handle_tmode(irc, numeric, command, args):
# <- :42XAAAAAB TMODE 1437450768 #endlessvoid -c+lkC 3 agte4
channel = args[1].lower()
channel = utils.toLower(irc, args[1])
modes = args[2:]
changedmodes = utils.parseModes(irc, channel, modes)
utils.applyModes(irc, channel, changedmodes)

View File

@ -116,6 +116,10 @@ def add_hook(func, command):
world.command_hooks[command].append(func)
def toLower(irc, text):
"""<irc object> <text>
Returns a lowercase representation of <text> based on <irc object>'s
casemapping (rfc1459 vs ascii)."""
if irc.proto.casemapping == 'rfc1459':
text = text.replace('{', '[')
text = text.replace('}', ']')
@ -124,6 +128,9 @@ def toLower(irc, text):
return text.lower()
def nickToUid(irc, nick):
"""<irc object> <nick>
Returns the UID of a user named <nick>, if present."""
nick = toLower(irc, nick)
for k, v in irc.users.items():
if toLower(irc, v.nick) == nick: