3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-25 04:02:45 +01:00

Classify all our protocol modules - why didn't I do this earlier

This commit is contained in:
James Lu 2015-09-05 18:00:57 -07:00
parent 3b7d81d62a
commit 57da0aa3b3
8 changed files with 1393 additions and 1377 deletions

View File

@ -65,7 +65,8 @@ class Irc():
self.serverdata = conf['servers'][netname] self.serverdata = conf['servers'][netname]
self.sid = self.serverdata["sid"] self.sid = self.serverdata["sid"]
self.botdata = conf['bot'] self.botdata = conf['bot']
self.proto = proto self.protoname = proto.__name__
self.proto = proto.Class(self)
self.pingfreq = self.serverdata.get('pingfreq') or 30 self.pingfreq = self.serverdata.get('pingfreq') or 30
self.pingtimeout = self.pingfreq * 2 self.pingtimeout = self.pingfreq * 2
@ -142,7 +143,7 @@ class Irc():
sha1fp) sha1fp)
if checks_ok: if checks_ok:
self.proto.connect(self) self.proto.connect()
self.spawnMain() self.spawnMain()
log.info('(%s) Starting ping schedulers....', self.name) log.info('(%s) Starting ping schedulers....', self.name)
self.schedulePing() self.schedulePing()
@ -200,7 +201,7 @@ class Irc():
log.debug("(%s) <- %s", self.name, line) log.debug("(%s) <- %s", self.name, line)
hook_args = None hook_args = None
try: try:
hook_args = self.proto.handle_events(self, line) hook_args = self.proto.handle_events(line)
except Exception: except Exception:
log.exception('(%s) Caught error in handle_events, disconnecting!', self.name) log.exception('(%s) Caught error in handle_events, disconnecting!', self.name)
return return
@ -249,7 +250,7 @@ class Irc():
log.debug("(%s) Dropping message %r; network isn't connected!", self.name, stripped_data) log.debug("(%s) Dropping message %r; network isn't connected!", self.name, stripped_data)
def schedulePing(self): def schedulePing(self):
self.proto.pingServer(self) self.proto.pingServer()
self.pingTimer = threading.Timer(self.pingfreq, self.schedulePing) self.pingTimer = threading.Timer(self.pingfreq, self.schedulePing)
self.pingTimer.daemon = True self.pingTimer.daemon = True
self.pingTimer.start() self.pingTimer.start()
@ -261,9 +262,9 @@ class Irc():
host = self.serverdata["hostname"] host = self.serverdata["hostname"]
log.info('(%s) Connected! Spawning main client %s.', self.name, nick) log.info('(%s) Connected! Spawning main client %s.', self.name, nick)
olduserobj = self.pseudoclient olduserobj = self.pseudoclient
self.pseudoclient = self.proto.spawnClient(self, nick, ident, host, modes={("+o", None)}) self.pseudoclient = self.proto.spawnClient(nick, ident, host, modes={("+o", None)})
for chan in self.serverdata['channels']: for chan in self.serverdata['channels']:
self.proto.joinClient(self, self.pseudoclient.uid, chan) self.proto.joinClient(self.pseudoclient.uid, chan)
# PyLink internal hook called when spawnMain is called and the # PyLink internal hook called when spawnMain is called and the
# contents of Irc().pseudoclient change. # contents of Irc().pseudoclient change.
self.callHooks([self.sid, 'PYLINK_SPAWNMAIN', {'olduser': olduserobj}]) self.callHooks([self.sid, 'PYLINK_SPAWNMAIN', {'olduser': olduserobj}])
@ -339,7 +340,7 @@ class FakeIRC(Irc):
def run(self, data): def run(self, data):
"""Queues a message to the fake IRC server.""" """Queues a message to the fake IRC server."""
log.debug('<- ' + data) log.debug('<- ' + data)
hook_args = self.proto.handle_events(self, data) hook_args = self.proto.handle_events(data)
if hook_args is not None: if hook_args is not None:
self.hookmsgs.append(hook_args) self.hookmsgs.append(hook_args)
self.callHooks(hook_args) self.callHooks(hook_args)
@ -374,6 +375,13 @@ class FakeIRC(Irc):
self.hookmsgs = [] self.hookmsgs = []
return hookmsgs return hookmsgs
class Protocol():
# TODO: Future state-keeping things will go here
def __init__(self, irc):
self.irc = irc
self.casemapping = 'rfc1459'
self.hook_map = {}
class FakeProto(): class FakeProto():
"""Dummy protocol module for testing purposes.""" """Dummy protocol module for testing purposes."""
def __init__(self): def __init__(self):

View File

@ -15,7 +15,7 @@ def handle_kick(irc, source, command, args):
kicked = args['target'] kicked = args['target']
channel = args['channel'] channel = args['channel']
if kicked == irc.pseudoclient.uid: if kicked == irc.pseudoclient.uid:
irc.proto.joinClient(irc, irc.pseudoclient.uid, channel) irc.proto.joinClient(irc.pseudoclient.uid, channel)
utils.add_hook(handle_kick, 'KICK') utils.add_hook(handle_kick, 'KICK')
# Handle commands sent to the PyLink client (PRIVMSG) # Handle commands sent to the PyLink client (PRIVMSG)
@ -51,7 +51,7 @@ def handle_whois(irc, source, command, args):
sourceisOper = ('o', None) in irc.users[source].modes sourceisOper = ('o', None) in irc.users[source].modes
# https://www.alien.net.au/irc/irc2numerics.html # https://www.alien.net.au/irc/irc2numerics.html
# 311: sends nick!user@host information # 311: sends nick!user@host information
f(irc, server, 311, source, "%s %s %s * :%s" % (nick, user.ident, user.host, user.realname)) f(server, 311, source, "%s %s %s * :%s" % (nick, user.ident, user.host, user.realname))
# 319: RPL_WHOISCHANNELS, shows channel list # 319: RPL_WHOISCHANNELS, shows channel list
public_chans = [] public_chans = []
for chan in user.channels: for chan in user.channels:
@ -69,9 +69,9 @@ def handle_whois(irc, source, command, args):
chan = prefixchar + chan chan = prefixchar + chan
public_chans.append(chan) public_chans.append(chan)
if public_chans: if public_chans:
f(irc, server, 319, source, '%s :%s' % (nick, ' '.join(public_chans))) f(server, 319, source, '%s :%s' % (nick, ' '.join(public_chans)))
# 312: sends the server the target is on, and its server description. # 312: sends the server the target is on, and its server description.
f(irc, server, 312, source, "%s %s :%s" % (nick, irc.serverdata['hostname'], f(server, 312, source, "%s %s :%s" % (nick, irc.serverdata['hostname'],
irc.serverdata.get('serverdesc') or irc.botdata['serverdesc'])) irc.serverdata.get('serverdesc') or irc.botdata['serverdesc']))
# 313: sends a string denoting the target's operator privilege, # 313: sends a string denoting the target's operator privilege,
# only if they have umode +o. # only if they have umode +o.
@ -82,15 +82,15 @@ def handle_whois(irc, source, command, args):
opertype = "IRC Operator" opertype = "IRC Operator"
# Let's be gramatically correct. # Let's be gramatically correct.
n = 'n' if opertype[0].lower() in 'aeiou' else '' n = 'n' if opertype[0].lower() in 'aeiou' else ''
f(irc, server, 313, source, "%s :is a%s %s" % (nick, n, opertype)) f(server, 313, source, "%s :is a%s %s" % (nick, n, opertype))
# 379: RPL_WHOISMODES, used by UnrealIRCd and InspIRCd. # 379: RPL_WHOISMODES, used by UnrealIRCd and InspIRCd.
# Only show this to opers! # Only show this to opers!
if sourceisOper: if sourceisOper:
f(irc, server, 379, source, '%s :is using modes %s' % (nick, utils.joinModes(user.modes))) f(server, 379, source, '%s :is using modes %s' % (nick, utils.joinModes(user.modes)))
# 317: shows idle and signon time. However, we don't track the user's real # 317: shows idle and signon time. However, we don't track the user's real
# idle time, so we simply return 0. # idle time, so we simply return 0.
# <- 317 GL GL 15 1437632859 :seconds idle, signon time # <- 317 GL GL 15 1437632859 :seconds idle, signon time
f(irc, server, 317, source, "%s 0 %s :seconds idle, signon time" % (nick, user.ts)) f(server, 317, source, "%s 0 %s :seconds idle, signon time" % (nick, user.ts))
for func in world.whois_handlers: for func in world.whois_handlers:
# Iterate over custom plugin WHOIS handlers. They return a tuple # Iterate over custom plugin WHOIS handlers. They return a tuple
# or list with two arguments: the numeric, and the text to send. # or list with two arguments: the numeric, and the text to send.
@ -98,11 +98,11 @@ def handle_whois(irc, source, command, args):
res = func(irc, target) res = func(irc, target)
if res: if res:
num, text = res num, text = res
f(irc, server, num, source, text) f(server, num, source, text)
except Exception as e: except Exception as e:
# Again, we wouldn't want this to crash our service, in case # Again, we wouldn't want this to crash our service, in case
# something goes wrong! # something goes wrong!
log.exception('(%s) Error caught in WHOIS handler: %s', irc.name, e) log.exception('(%s) Error caught in WHOIS handler: %s', irc.name, e)
# 318: End of WHOIS. # 318: End of WHOIS.
f(irc, server, 318, source, "%s :End of /WHOIS list" % nick) f(server, 318, source, "%s :End of /WHOIS list" % nick)
utils.add_hook(handle_whois, 'WHOIS') utils.add_hook(handle_whois, 'WHOIS')

View File

@ -22,7 +22,7 @@ def spawnclient(irc, source, args):
except ValueError: except ValueError:
utils.msg(irc, source, "Error: Not enough arguments. Needs 3: nick, user, host.") utils.msg(irc, source, "Error: Not enough arguments. Needs 3: nick, user, host.")
return return
irc.proto.spawnClient(irc, nick, ident, host) irc.proto.spawnClient(nick, ident, host)
@utils.add_cmd @utils.add_cmd
def quit(irc, source, args): def quit(irc, source, args):
@ -40,7 +40,7 @@ def quit(irc, source, args):
return return
u = utils.nickToUid(irc, nick) u = utils.nickToUid(irc, nick)
quitmsg = ' '.join(args[1:]) or 'Client Quit' quitmsg = ' '.join(args[1:]) or 'Client Quit'
irc.proto.quitClient(irc, u, quitmsg) irc.proto.quitClient(u, quitmsg)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_QUIT', {'text': quitmsg, 'parse_as': 'QUIT'}]) irc.callHooks([u, 'PYLINK_BOTSPLUGIN_QUIT', {'text': quitmsg, 'parse_as': 'QUIT'}])
def joinclient(irc, source, args): def joinclient(irc, source, args):
@ -61,7 +61,7 @@ def joinclient(irc, source, args):
if not utils.isChannel(channel): if not utils.isChannel(channel):
utils.msg(irc, source, "Error: Invalid channel name %r." % channel) utils.msg(irc, source, "Error: Invalid channel name %r." % channel)
return return
irc.proto.joinClient(irc, u, channel) irc.proto.joinClient(u, channel)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_JOIN', {'channel': channel, 'users': [u], irc.callHooks([u, 'PYLINK_BOTSPLUGIN_JOIN', {'channel': channel, 'users': [u],
'modes': irc.channels[channel].modes, 'modes': irc.channels[channel].modes,
'parse_as': 'JOIN'}]) 'parse_as': 'JOIN'}])
@ -85,7 +85,7 @@ def nick(irc, source, args):
elif not utils.isNick(newnick): elif not utils.isNick(newnick):
utils.msg(irc, source, 'Error: Invalid nickname %r.' % newnick) utils.msg(irc, source, 'Error: Invalid nickname %r.' % newnick)
return return
irc.proto.nickClient(irc, u, newnick) irc.proto.nickClient(u, newnick)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_NICK', {'newnick': newnick, 'oldnick': nick, 'parse_as': 'NICK'}]) irc.callHooks([u, 'PYLINK_BOTSPLUGIN_NICK', {'newnick': newnick, 'oldnick': nick, 'parse_as': 'NICK'}])
@utils.add_cmd @utils.add_cmd
@ -106,7 +106,7 @@ def part(irc, source, args):
if not utils.isChannel(channel): if not utils.isChannel(channel):
utils.msg(irc, source, "Error: Invalid channel name %r." % channel) utils.msg(irc, source, "Error: Invalid channel name %r." % channel)
return return
irc.proto.partClient(irc, u, channel, reason) irc.proto.partClient(u, channel, reason)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}]) irc.callHooks([u, 'PYLINK_BOTSPLUGIN_PART', {'channels': clist, 'text': reason, 'parse_as': 'PART'}])
@utils.add_cmd @utils.add_cmd
@ -129,9 +129,9 @@ def kick(irc, source, args):
utils.msg(irc, source, "Error: Invalid channel name %r." % channel) utils.msg(irc, source, "Error: Invalid channel name %r." % channel)
return return
if utils.isInternalServer(irc, u): if utils.isInternalServer(irc, u):
irc.proto.kickServer(irc, u, channel, targetu, reason) irc.proto.kickServer(u, channel, targetu, reason)
else: else:
irc.proto.kickClient(irc, u, channel, targetu, reason) irc.proto.kickClient(u, channel, targetu, reason)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_KICK', {'channel': channel, 'target': targetu, 'text': reason, 'parse_as': 'KICK'}]) irc.callHooks([u, 'PYLINK_BOTSPLUGIN_KICK', {'channel': channel, 'target': targetu, 'text': reason, 'parse_as': 'KICK'}])
@utils.add_cmd @utils.add_cmd
@ -156,11 +156,11 @@ def mode(irc, source, args):
utils.msg(irc, source, "Error: Invalid channel or nick %r." % target) utils.msg(irc, source, "Error: Invalid channel or nick %r." % target)
return return
if utils.isInternalServer(irc, modesource): if utils.isInternalServer(irc, modesource):
irc.proto.modeServer(irc, modesource, target, parsedmodes) irc.proto.modeServer(modesource, target, parsedmodes)
irc.callHooks([modesource, 'PYLINK_BOTSPLUGIN_MODE', {'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}]) irc.callHooks([modesource, 'PYLINK_BOTSPLUGIN_MODE', {'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}])
else: else:
sourceuid = utils.nickToUid(irc, modesource) sourceuid = utils.nickToUid(irc, modesource)
irc.proto.modeClient(irc, sourceuid, target, parsedmodes) irc.proto.modeClient(sourceuid, target, parsedmodes)
irc.callHooks([sourceuid, 'PYLINK_BOTSPLUGIN_MODE', {'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}]) irc.callHooks([sourceuid, 'PYLINK_BOTSPLUGIN_MODE', {'target': target, 'modes': parsedmodes, 'parse_as': 'MODE'}])
@utils.add_cmd @utils.add_cmd
@ -188,5 +188,5 @@ def msg(irc, source, args):
if not text: if not text:
utils.msg(irc, source, 'Error: No text given.') utils.msg(irc, source, 'Error: No text given.')
return return
irc.proto.messageClient(irc, sourceuid, real_target, text) irc.proto.messageClient(sourceuid, real_target, text)
irc.callHooks([sourceuid, 'PYLINK_BOTSPLUGIN_MSG', {'target': real_target, 'text': text, 'parse_as': 'PRIVMSG'}]) irc.callHooks([sourceuid, 'PYLINK_BOTSPLUGIN_MSG', {'target': real_target, 'text': text, 'parse_as': 'PRIVMSG'}])

View File

@ -39,7 +39,7 @@ def normalizeNick(irc, netname, nick, separator=None, uid=''):
separator = separator or irc.serverdata.get('separator') or "/" separator = separator or irc.serverdata.get('separator') or "/"
log.debug('(%s) normalizeNick: using %r as separator.', irc.name, separator) log.debug('(%s) normalizeNick: using %r as separator.', irc.name, separator)
orig_nick = nick orig_nick = nick
protoname = irc.proto.__name__ protoname = irc.protoname
maxnicklen = irc.maxnicklen maxnicklen = irc.maxnicklen
if '/' not in separator or not protoname.startswith(('insp', 'unreal')): if '/' not in separator or not protoname.startswith(('insp', 'unreal')):
# Charybdis doesn't allow / in usernames, and will SQUIT with # Charybdis doesn't allow / in usernames, and will SQUIT with
@ -172,7 +172,7 @@ def getRemoteUser(irc, remoteirc, user, spawnIfMissing=True):
hideoper_mode = remoteirc.umodes.get('hideoper') hideoper_mode = remoteirc.umodes.get('hideoper')
if hideoper_mode: if hideoper_mode:
modes.append((hideoper_mode, None)) modes.append((hideoper_mode, None))
u = remoteirc.proto.spawnClient(remoteirc, nick, ident=ident, u = remoteirc.proto.spawnClient(nick, ident=ident,
host=host, realname=realname, host=host, realname=realname,
modes=modes, ts=userobj.ts, modes=modes, ts=userobj.ts,
opertype=opertype).uid opertype=opertype).uid
@ -180,7 +180,7 @@ def getRemoteUser(irc, remoteirc, user, spawnIfMissing=True):
remoteirc.users[u].opertype = opertype remoteirc.users[u].opertype = opertype
away = userobj.away away = userobj.away
if away: if away:
remoteirc.proto.awayClient(remoteirc, u, away) remoteirc.proto.awayClient(u, away)
relayusers[(irc.name, user)][remoteirc.name] = u relayusers[(irc.name, user)][remoteirc.name] = u
return u return u
@ -275,11 +275,11 @@ def initializeChannel(irc, channel):
# Only update the topic if it's different from what we already have, # Only update the topic if it's different from what we already have,
# and topic bursting is complete. # and topic bursting is complete.
if remoteirc.channels[remotechan].topicset and topic != irc.channels[channel].topic: if remoteirc.channels[remotechan].topicset and topic != irc.channels[channel].topic:
irc.proto.topicServer(irc, irc.sid, channel, topic) irc.proto.topicServer(irc.sid, channel, topic)
# Send our users and channel modes to the other nets # Send our users and channel modes to the other nets
log.debug('(%s) initializeChannel: joining our users: %s', irc.name, c.users) log.debug('(%s) initializeChannel: joining our users: %s', irc.name, c.users)
relayJoins(irc, channel, c.users, c.ts) relayJoins(irc, channel, c.users, c.ts)
irc.proto.joinClient(irc, irc.pseudoclient.uid, channel) irc.proto.joinClient(irc.pseudoclient.uid, channel)
def handle_join(irc, numeric, command, args): def handle_join(irc, numeric, command, args):
channel = args['channel'] channel = args['channel']
@ -294,7 +294,7 @@ utils.add_hook(handle_join, 'JOIN')
def handle_quit(irc, numeric, command, args): def handle_quit(irc, numeric, command, args):
for netname, user in relayusers[(irc.name, numeric)].copy().items(): for netname, user in relayusers[(irc.name, numeric)].copy().items():
remoteirc = world.networkobjects[netname] remoteirc = world.networkobjects[netname]
remoteirc.proto.quitClient(remoteirc, user, args['text']) remoteirc.proto.quitClient(user, args['text'])
del relayusers[(irc.name, numeric)] del relayusers[(irc.name, numeric)]
utils.add_hook(handle_quit, 'QUIT') utils.add_hook(handle_quit, 'QUIT')
@ -310,7 +310,7 @@ def handle_nick(irc, numeric, command, args):
remoteirc = world.networkobjects[netname] remoteirc = world.networkobjects[netname]
newnick = normalizeNick(remoteirc, irc.name, args['newnick'], uid=user) newnick = normalizeNick(remoteirc, irc.name, args['newnick'], uid=user)
if remoteirc.users[user].nick != newnick: if remoteirc.users[user].nick != newnick:
remoteirc.proto.nickClient(remoteirc, user, newnick) remoteirc.proto.nickClient(user, newnick)
utils.add_hook(handle_nick, 'NICK') utils.add_hook(handle_nick, 'NICK')
def handle_part(irc, numeric, command, args): def handle_part(irc, numeric, command, args):
@ -325,9 +325,9 @@ def handle_part(irc, numeric, command, args):
remotechan = findRemoteChan(irc, remoteirc, channel) remotechan = findRemoteChan(irc, remoteirc, channel)
if remotechan is None: if remotechan is None:
continue continue
remoteirc.proto.partClient(remoteirc, user, remotechan, text) remoteirc.proto.partClient(user, remotechan, text)
if not remoteirc.users[user].channels: if not remoteirc.users[user].channels:
remoteirc.proto.quitClient(remoteirc, user, 'Left all shared channels.') remoteirc.proto.quitClient(user, 'Left all shared channels.')
del relayusers[(irc.name, numeric)][remoteirc.name] del relayusers[(irc.name, numeric)][remoteirc.name]
utils.add_hook(handle_part, 'PART') utils.add_hook(handle_part, 'PART')
@ -363,9 +363,9 @@ def handle_privmsg(irc, numeric, command, args):
continue continue
real_target = prefix + real_target real_target = prefix + real_target
if notice: if notice:
remoteirc.proto.noticeClient(remoteirc, user, real_target, text) remoteirc.proto.noticeClient(user, real_target, text)
else: else:
remoteirc.proto.messageClient(remoteirc, user, real_target, text) remoteirc.proto.messageClient(user, real_target, text)
else: else:
remoteuser = getLocalUser(irc, target) remoteuser = getLocalUser(irc, target)
if remoteuser is None: if remoteuser is None:
@ -383,9 +383,9 @@ def handle_privmsg(irc, numeric, command, args):
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.noticeClient(remoteirc, user, real_target, text) remoteirc.proto.noticeClient(user, real_target, text)
else: else:
remoteirc.proto.messageClient(remoteirc, user, real_target, text) remoteirc.proto.messageClient(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')
@ -431,7 +431,7 @@ def handle_kick(irc, source, command, args):
# kick ops, admins can't kick owners, etc. # kick ops, admins can't kick owners, etc.
modes = getPrefixModes(remoteirc, irc, remotechan, real_target) modes = getPrefixModes(remoteirc, irc, remotechan, real_target)
# Join the kicked client back with its respective modes. # Join the kicked client back with its respective modes.
irc.proto.sjoinServer(irc, irc.sid, channel, [(modes, target)]) irc.proto.sjoinServer(irc.sid, channel, [(modes, target)])
if kicker in irc.users: if kicker in irc.users:
log.info('(%s) Relay claim: Blocked KICK (reason %r) from %s to relay client %s/%s on %s.', log.info('(%s) Relay claim: Blocked KICK (reason %r) from %s to relay client %s/%s on %s.',
irc.name, args['text'], irc.users[source].nick, irc.name, args['text'], irc.users[source].nick,
@ -450,7 +450,7 @@ def handle_kick(irc, source, command, args):
# Propogate the kick! # Propogate the kick!
if real_kicker: if real_kicker:
log.debug('(%s) Relay kick: Kicking %s from channel %s via %s on behalf of %s/%s', irc.name, real_target, remotechan,real_kicker, kicker, irc.name) log.debug('(%s) Relay kick: Kicking %s from channel %s via %s on behalf of %s/%s', irc.name, real_target, remotechan,real_kicker, kicker, irc.name)
remoteirc.proto.kickClient(remoteirc, real_kicker, remoteirc.proto.kickClient(real_kicker,
remotechan, real_target, text) remotechan, real_target, text)
else: else:
# Kick originated from a server, or the kicker isn't in any # Kick originated from a server, or the kicker isn't in any
@ -464,17 +464,17 @@ def handle_kick(irc, source, command, args):
text = "(%s/%s) %s" % (kname, irc.name, text) text = "(%s/%s) %s" % (kname, irc.name, text)
except AttributeError: except AttributeError:
text = "(<unknown kicker>@%s) %s" % (irc.name, text) text = "(<unknown kicker>@%s) %s" % (irc.name, text)
remoteirc.proto.kickServer(remoteirc, remoteirc.sid, remoteirc.proto.kickServer(remoteirc.sid,
remotechan, real_target, text) remotechan, real_target, text)
# If the target isn't on any channels, quit them. # If the target isn't on any channels, quit them.
if origuser and origuser[0] != remoteirc.name and not remoteirc.users[real_target].channels: if origuser and origuser[0] != remoteirc.name and not remoteirc.users[real_target].channels:
del relayusers[origuser][remoteirc.name] del relayusers[origuser][remoteirc.name]
remoteirc.proto.quitClient(remoteirc, real_target, 'Left all shared channels.') remoteirc.proto.quitClient(real_target, 'Left all shared channels.')
if origuser and not irc.users[target].channels: if origuser and not irc.users[target].channels:
del relayusers[origuser][irc.name] del relayusers[origuser][irc.name]
irc.proto.quitClient(irc, target, 'Left all shared channels.') irc.proto.quitClient(target, 'Left all shared channels.')
utils.add_hook(handle_kick, 'KICK') utils.add_hook(handle_kick, 'KICK')
@ -493,7 +493,7 @@ def handle_chgclient(irc, source, command, args):
for netname, user in relayusers[(irc.name, target)].items(): for netname, user in relayusers[(irc.name, target)].items():
remoteirc = world.networkobjects[netname] remoteirc = world.networkobjects[netname]
try: try:
remoteirc.proto.updateClient(remoteirc, user, field, text) remoteirc.proto.updateClient(user, field, text)
except NotImplementedError: # IRCd doesn't support changing the field we want except NotImplementedError: # IRCd doesn't support changing the field we want
log.debug('(%s) Ignoring changing field %r of %s on %s (for %s/%s);' log.debug('(%s) Ignoring changing field %r of %s on %s (for %s/%s);'
' remote IRCd doesn\'t support it', irc.name, field, ' remote IRCd doesn\'t support it', irc.name, field,
@ -584,9 +584,9 @@ def relayModes(irc, remoteirc, sender, channel, modes=None):
# Check if the sender is a user; remember servers are allowed to set modes too. # Check if the sender is a user; remember servers are allowed to set modes too.
u = getRemoteUser(irc, remoteirc, sender, spawnIfMissing=False) u = getRemoteUser(irc, remoteirc, sender, spawnIfMissing=False)
if u: if u:
remoteirc.proto.modeClient(remoteirc, u, remotechan, supported_modes) remoteirc.proto.modeClient(u, remotechan, supported_modes)
else: else:
remoteirc.proto.modeServer(remoteirc, remoteirc.sid, remotechan, supported_modes) remoteirc.proto.modeServer(remoteirc.sid, remotechan, supported_modes)
def getSupportedUmodes(irc, remoteirc, modes): def getSupportedUmodes(irc, remoteirc, modes):
supported_modes = [] supported_modes = []
@ -613,7 +613,7 @@ def getSupportedUmodes(irc, remoteirc, modes):
log.debug("(%s) getSupportedUmodes: skipping mode (%r, %r) because " log.debug("(%s) getSupportedUmodes: skipping mode (%r, %r) because "
"the remote network (%s)'s IRCd (%s) doesn't support it.", "the remote network (%s)'s IRCd (%s) doesn't support it.",
irc.name, modechar, arg, remoteirc.name, irc.name, modechar, arg, remoteirc.name,
remoteirc.proto.__name__) remoteirc.protoname)
return supported_modes return supported_modes
def handle_mode(irc, numeric, command, args): def handle_mode(irc, numeric, command, args):
@ -636,7 +636,7 @@ def handle_mode(irc, numeric, command, args):
modes.append(('-%s' % hideoper_mode, None)) modes.append(('-%s' % hideoper_mode, None))
remoteuser = getRemoteUser(irc, remoteirc, target, spawnIfMissing=False) remoteuser = getRemoteUser(irc, remoteirc, target, spawnIfMissing=False)
if remoteuser and modes: if remoteuser and modes:
remoteirc.proto.modeClient(remoteirc, remoteuser, remoteuser, modes) remoteirc.proto.modeClient(remoteuser, remoteuser, modes)
utils.add_hook(handle_mode, 'MODE') utils.add_hook(handle_mode, 'MODE')
@ -654,9 +654,9 @@ def handle_topic(irc, numeric, command, args):
# This might originate from a server too. # This might originate from a server too.
remoteuser = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False) remoteuser = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False)
if remoteuser: if remoteuser:
remoteirc.proto.topicClient(remoteirc, remoteuser, remotechan, topic) remoteirc.proto.topicClient(remoteuser, remotechan, topic)
else: else:
remoteirc.proto.topicServer(remoteirc, remoteirc.sid, remotechan, topic) remoteirc.proto.topicServer(remoteirc.sid, remotechan, topic)
utils.add_hook(handle_topic, 'TOPIC') utils.add_hook(handle_topic, 'TOPIC')
def handle_kill(irc, numeric, command, args): def handle_kill(irc, numeric, command, args):
@ -676,7 +676,7 @@ def handle_kill(irc, numeric, command, args):
modes = getPrefixModes(remoteirc, irc, localchan, realuser[1]) modes = getPrefixModes(remoteirc, irc, localchan, realuser[1])
log.debug('(%s) relay handle_kill: userpair: %s, %s', irc.name, modes, realuser) log.debug('(%s) relay handle_kill: userpair: %s, %s', irc.name, modes, realuser)
client = getRemoteUser(remoteirc, irc, realuser[1]) client = getRemoteUser(remoteirc, irc, realuser[1])
irc.proto.sjoinServer(irc, irc.sid, localchan, [(modes, client)]) irc.proto.sjoinServer(irc.sid, localchan, [(modes, client)])
if userdata and numeric in irc.users: if userdata and numeric in irc.users:
log.info('(%s) Relay claim: Blocked KILL (reason %r) from %s to relay client %s/%s.', log.info('(%s) Relay claim: Blocked KILL (reason %r) from %s to relay client %s/%s.',
irc.name, args['text'], irc.users[numeric].nick, irc.name, args['text'], irc.users[numeric].nick,
@ -745,10 +745,10 @@ def relayJoins(irc, channel, users, ts, burst=True):
# Burst was explicitly given, or we're trying to join multiple # Burst was explicitly given, or we're trying to join multiple
# users/someone with a prefix. # users/someone with a prefix.
if burst or len(queued_users) > 1 or queued_users[0][0]: if burst or len(queued_users) > 1 or queued_users[0][0]:
remoteirc.proto.sjoinServer(remoteirc, remoteirc.sid, remotechan, queued_users, ts=ts) remoteirc.proto.sjoinServer(remoteirc.sid, remotechan, queued_users, ts=ts)
relayModes(irc, remoteirc, irc.sid, channel, irc.channels[channel].modes) relayModes(irc, remoteirc, irc.sid, channel, irc.channels[channel].modes)
else: else:
remoteirc.proto.joinClient(remoteirc, queued_users[0][1], remotechan) remoteirc.proto.joinClient(queued_users[0][1], remotechan)
def relayPart(irc, channel, user): def relayPart(irc, channel, user):
for name, remoteirc in world.networkobjects.items(): for name, remoteirc in world.networkobjects.items():
@ -762,16 +762,16 @@ def relayPart(irc, channel, user):
log.debug('(%s) relayPart: remoteuser for %s/%s found as %s', irc.name, user, irc.name, remoteuser) log.debug('(%s) relayPart: remoteuser for %s/%s found as %s', irc.name, user, irc.name, remoteuser)
if remotechan is None or remoteuser is None: if remotechan is None or remoteuser is None:
continue continue
remoteirc.proto.partClient(remoteirc, remoteuser, remotechan, 'Channel delinked.') remoteirc.proto.partClient(remoteuser, remotechan, 'Channel delinked.')
if isRelayClient(remoteirc, remoteuser) and not remoteirc.users[remoteuser].channels: if isRelayClient(remoteirc, remoteuser) and not remoteirc.users[remoteuser].channels:
remoteirc.proto.quitClient(remoteirc, remoteuser, 'Left all shared channels.') remoteirc.proto.quitClient(remoteuser, 'Left all shared channels.')
del relayusers[(irc.name, user)][remoteirc.name] del relayusers[(irc.name, user)][remoteirc.name]
def removeChannel(irc, channel): def removeChannel(irc, channel):
if irc is None: if irc is None:
return return
if channel not in map(str.lower, irc.serverdata['channels']): if channel not in map(str.lower, irc.serverdata['channels']):
irc.proto.partClient(irc, irc.pseudoclient.uid, channel, 'Channel delinked.') irc.proto.partClient(irc.pseudoclient.uid, channel, 'Channel delinked.')
relay = findRelay((irc.name, channel)) relay = findRelay((irc.name, channel))
if relay: if relay:
for user in irc.channels[channel].users.copy(): for user in irc.channels[channel].users.copy():
@ -782,12 +782,12 @@ def removeChannel(irc, channel):
if user == irc.pseudoclient.uid and channel in \ if user == irc.pseudoclient.uid and channel in \
irc.serverdata['channels']: irc.serverdata['channels']:
continue continue
irc.proto.partClient(irc, user, channel, 'Channel delinked.') irc.proto.partClient(user, channel, 'Channel delinked.')
# Don't ever quit it either... # Don't ever quit it either...
if user != irc.pseudoclient.uid and not irc.users[user].channels: if user != irc.pseudoclient.uid and not irc.users[user].channels:
remoteuser = getLocalUser(irc, user) remoteuser = getLocalUser(irc, user)
del relayusers[remoteuser][irc.name] del relayusers[remoteuser][irc.name]
irc.proto.quitClient(irc, user, 'Left all shared channels.') irc.proto.quitClient(user, 'Left all shared channels.')
@utils.add_cmd @utils.add_cmd
def create(irc, source, args): def create(irc, source, args):
@ -986,7 +986,7 @@ def handle_save(irc, numeric, command, args):
newnick = normalizeNick(irc, remotenet, nick) newnick = normalizeNick(irc, remotenet, nick)
log.info('(%s) SAVE received for relay client %r (%s), fixing nick to %s', log.info('(%s) SAVE received for relay client %r (%s), fixing nick to %s',
irc.name, target, nick, newnick) irc.name, target, nick, newnick)
irc.proto.nickClient(irc, target, newnick) irc.proto.nickClient(target, newnick)
else: else:
log.warning('(%s) SAVE received for relay client %r (%s), not ' log.warning('(%s) SAVE received for relay client %r (%s), not '
'fixing nick again due to 5 failed attempts in ' 'fixing nick again due to 5 failed attempts in '
@ -1020,7 +1020,7 @@ def linked(irc, source, args):
def handle_away(irc, numeric, command, args): def handle_away(irc, numeric, command, args):
for netname, user in relayusers[(irc.name, numeric)].items(): for netname, user in relayusers[(irc.name, numeric)].items():
remoteirc = world.networkobjects[netname] remoteirc = world.networkobjects[netname]
remoteirc.proto.awayClient(remoteirc, user, args['text']) remoteirc.proto.awayClient(user, args['text'])
utils.add_hook(handle_away, 'AWAY') utils.add_hook(handle_away, 'AWAY')
def handle_spawnmain(irc, numeric, command, args): def handle_spawnmain(irc, numeric, command, args):
@ -1048,7 +1048,7 @@ def handle_invite(irc, source, command, args):
'channel not on their network!', 'channel not on their network!',
notice=True) notice=True)
else: else:
remoteirc.proto.inviteClient(remoteirc, remotesource, remoteuser, remoteirc.proto.inviteClient(remotesource, remoteuser,
remotechan) remotechan)
utils.add_hook(handle_invite, 'INVITE') utils.add_hook(handle_invite, 'INVITE')

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -7,251 +7,252 @@ import utils
from log import log from log import log
from classes import * from classes import *
def _send(irc, source, msg): class TS6BaseProtocol(Protocol):
"""Sends a TS6-style raw command from a source numeric to the IRC connection given.""" def _send(self, source, msg):
irc.send(':%s %s' % (source, msg)) """Sends a TS6-style raw command from a source numeric to the self.irc connection given."""
self.irc.send(':%s %s' % (source, msg))
def parseArgs(args): def parseArgs(self, args):
"""Parses a string of RFC1459-style arguments split into a list, where ":" may """Parses a string of RFC1459-style arguments split into a list, where ":" may
be used for multi-word arguments that last until the end of a line. be used for multi-word arguments that last until the end of a line.
""" """
real_args = [] real_args = []
for idx, arg in enumerate(args): for idx, arg in enumerate(args):
real_args.append(arg)
# If the argument starts with ':' and ISN'T the first argument.
# The first argument is used for denoting the source UID/SID.
if arg.startswith(':') and idx != 0:
# : is used for multi-word arguments that last until the end
# of the message. We can use list splicing here to turn them all
# into one argument.
# Set the last arg to a joined version of the remaining args
arg = args[idx:]
arg = ' '.join(arg)[1:]
# Cut the original argument list right before the multi-word arg,
# and then append the multi-word arg.
real_args = args[:idx]
real_args.append(arg) real_args.append(arg)
break # If the argument starts with ':' and ISN'T the first argument.
return real_args # The first argument is used for denoting the source UID/SID.
if arg.startswith(':') and idx != 0:
# : is used for multi-word arguments that last until the end
# of the message. We can use list splicing here to turn them all
# into one argument.
# Set the last arg to a joined version of the remaining args
arg = args[idx:]
arg = ' '.join(arg)[1:]
# Cut the original argument list right before the multi-word arg,
# and then append the multi-word arg.
real_args = args[:idx]
real_args.append(arg)
break
return real_args
def parseTS6Args(args): def parseTS6Args(self, args):
"""Similar to parseArgs(), but stripping leading colons from the first argument """Similar to parseArgs(), but stripping leading colons from the first argument
of a line (usually the sender field).""" of a line (usually the sender field)."""
args = parseArgs(args) args = self.parseArgs(args)
args[0] = args[0].split(':', 1)[1] args[0] = args[0].split(':', 1)[1]
return args return args
### OUTGOING COMMANDS ### OUTGOING COMMANDS
def _sendKick(irc, numeric, channel, target, reason=None): def _sendKick(self, numeric, channel, target, reason=None):
"""Internal function to send kicks from a PyLink client/server.""" """Internal function to send kicks from a PyLink client/server."""
channel = utils.toLower(irc, channel) channel = utils.toLower(self.irc, channel)
if not reason: if not reason:
reason = 'No reason given' reason = 'No reason given'
_send(irc, numeric, 'KICK %s %s :%s' % (channel, target, reason)) self._send(numeric, 'KICK %s %s :%s' % (channel, target, reason))
# We can pretend the target left by its own will; all we really care about # We can pretend the target left by its own will; all we really care about
# is that the target gets removed from the channel userlist, and calling # is that the target gets removed from the channel userlist, and calling
# handle_part() does that just fine. # handle_part() does that just fine.
handle_part(irc, target, 'KICK', [channel]) self.handle_part(target, 'KICK', [channel])
def kickClient(irc, numeric, channel, target, reason=None): def kickClient(self, numeric, channel, target, reason=None):
"""Sends a kick from a PyLink client.""" """Sends a kick from a PyLink client."""
if not utils.isInternalClient(irc, numeric): if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.') raise LookupError('No such PyLink PseudoClient exists.')
_sendKick(irc, numeric, channel, target, reason=reason) self._sendKick(numeric, channel, target, reason=reason)
def kickServer(irc, numeric, channel, target, reason=None): def kickServer(self, numeric, channel, target, reason=None):
"""Sends a kick from a PyLink server.""" """Sends a kick from a PyLink server."""
if not utils.isInternalServer(irc, numeric): if not utils.isInternalServer(self.irc, numeric):
raise LookupError('No such PyLink PseudoServer exists.') raise LookupError('No such PyLink PseudoServer exists.')
_sendKick(irc, numeric, channel, target, reason=reason) self._sendKick(numeric, channel, target, reason=reason)
def nickClient(irc, numeric, newnick): def nickClient(self, numeric, newnick):
"""Changes the nick of a PyLink client.""" """Changes the nick of a PyLink client."""
if not utils.isInternalClient(irc, numeric): if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.') raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'NICK %s %s' % (newnick, int(time.time()))) self._send(numeric, 'NICK %s %s' % (newnick, int(time.time())))
irc.users[numeric].nick = newnick self.irc.users[numeric].nick = newnick
def removeClient(irc, numeric): def removeClient(self, numeric):
"""Internal function to remove a client from our internal state.""" """Internal function to remove a client from our internal state."""
for c, v in irc.channels.copy().items(): for c, v in self.irc.channels.copy().items():
v.removeuser(numeric) v.removeuser(numeric)
# Clear empty non-permanent channels. # Clear empty non-permanent channels.
if not (irc.channels[c].users or ((irc.cmodes.get('permanent'), None) in irc.channels[c].modes)): if not (self.irc.channels[c].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[c].modes)):
del irc.channels[c] del self.irc.channels[c]
sid = numeric[:3] sid = numeric[:3]
log.debug('Removing client %s from irc.users', numeric) log.debug('Removing client %s from self.irc.users', numeric)
del irc.users[numeric] del self.irc.users[numeric]
log.debug('Removing client %s from irc.servers[%s]', numeric, sid) log.debug('Removing client %s from self.irc.servers[%s]', numeric, sid)
irc.servers[sid].users.discard(numeric) self.irc.servers[sid].users.discard(numeric)
def partClient(irc, client, channel, reason=None): def partClient(self, client, channel, reason=None):
"""Sends a part from a PyLink client.""" """Sends a part from a PyLink client."""
channel = utils.toLower(irc, channel) channel = utils.toLower(self.irc, channel)
if not utils.isInternalClient(irc, client): if not utils.isInternalClient(self.irc, client):
log.error('(%s) Error trying to part client %r to %r (no such pseudoclient exists)', irc.name, client, channel) log.error('(%s) Error trying to part client %r to %r (no such pseudoclient exists)', self.irc.name, client, channel)
raise LookupError('No such PyLink PseudoClient exists.') raise LookupError('No such PyLink PseudoClient exists.')
msg = "PART %s" % channel msg = "PART %s" % channel
if reason: if reason:
msg += " :%s" % reason msg += " :%s" % reason
_send(irc, client, msg) self._send(client, msg)
handle_part(irc, client, 'PART', [channel]) self.handle_part(client, 'PART', [channel])
def quitClient(irc, numeric, reason): def quitClient(self, numeric, reason):
"""Quits a PyLink client.""" """Quits a PyLink client."""
if utils.isInternalClient(irc, numeric): if utils.isInternalClient(self.irc, numeric):
_send(irc, numeric, "QUIT :%s" % reason) self._send(numeric, "QUIT :%s" % reason)
removeClient(irc, numeric) self.removeClient(numeric)
else: else:
raise LookupError("No such PyLink PseudoClient exists.") raise LookupError("No such PyLink PseudoClient exists.")
def messageClient(irc, numeric, target, text): def messageClient(self, numeric, target, text):
"""Sends a PRIVMSG from a PyLink client.""" """Sends a PRIVMSG from a PyLink client."""
if not utils.isInternalClient(irc, numeric): if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.') raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'PRIVMSG %s :%s' % (target, text)) self._send(numeric, 'PRIVMSG %s :%s' % (target, text))
def noticeClient(irc, numeric, target, text): def noticeClient(self, numeric, target, text):
"""Sends a NOTICE from a PyLink client.""" """Sends a NOTICE from a PyLink client."""
if not utils.isInternalClient(irc, numeric): if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.') raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'NOTICE %s :%s' % (target, text)) self._send(numeric, 'NOTICE %s :%s' % (target, text))
def topicClient(irc, numeric, target, text): def topicClient(self, numeric, target, text):
"""Sends a ROPIC from a PyLink client.""" """Sends a ROPIC from a PyLink client."""
if not utils.isInternalClient(irc, numeric): if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.') raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'TOPIC %s :%s' % (target, text)) self._send(numeric, 'TOPIC %s :%s' % (target, text))
irc.channels[target].topic = text self.irc.channels[target].topic = text
irc.channels[target].topicset = True self.irc.channels[target].topicset = True
### HANDLERS ### HANDLERS
def handle_privmsg(irc, source, command, args): def handle_privmsg(self, source, command, args):
"""Handles incoming PRIVMSG/NOTICE.""" """Handles incoming PRIVMSG/NOTICE."""
# <- :70MAAAAAA PRIVMSG #dev :afasfsa # <- :70MAAAAAA PRIVMSG #dev :afasfsa
# <- :70MAAAAAA NOTICE 0ALAAAAAA :afasfsa # <- :70MAAAAAA NOTICE 0ALAAAAAA :afasfsa
target = args[0] target = args[0]
# We use lowercase channels internally, but uppercase UIDs. # We use lowercase channels internally, but uppercase UIDs.
if utils.isChannel(target): if utils.isChannel(target):
target = utils.toLower(irc, target) target = utils.toLower(self.irc, target)
return {'target': target, 'text': args[1]} return {'target': target, 'text': args[1]}
handle_notice = handle_privmsg handle_notice = handle_privmsg
def handle_kill(irc, source, command, args): def handle_kill(self, source, command, args):
"""Handles incoming KILLs.""" """Handles incoming KILLs."""
killed = args[0] killed = args[0]
# Depending on whether the IRCd sends explicit QUIT messages for # Depending on whether the self.ircd sends explicit QUIT messages for
# KILLed clients, the user may or may not have automatically been removed. # KILLed clients, the user may or may not have automatically been removed.
# If not, we have to assume that KILL = QUIT and remove them ourselves. # If not, we have to assume that KILL = QUIT and remove them ourselves.
data = irc.users.get(killed) data = self.irc.users.get(killed)
if data: if data:
removeClient(irc, killed) self.removeClient(killed)
return {'target': killed, 'text': args[1], 'userdata': data} return {'target': killed, 'text': args[1], 'userdata': data}
def handle_kick(irc, source, command, args): def handle_kick(self, source, command, args):
"""Handles incoming KICKs.""" """Handles incoming KICKs."""
# :70MAAAAAA KICK #endlessvoid 70MAAAAAA :some reason # :70MAAAAAA KICK #endlessvoid 70MAAAAAA :some reason
channel = utils.toLower(irc, args[0]) channel = utils.toLower(self.irc, args[0])
kicked = args[1] kicked = args[1]
handle_part(irc, kicked, 'KICK', [channel, args[2]]) self.handle_part(kicked, 'KICK', [channel, args[2]])
return {'channel': channel, 'target': kicked, 'text': args[2]} return {'channel': channel, 'target': kicked, 'text': args[2]}
def handle_error(irc, numeric, command, args): def handle_error(self, numeric, command, args):
"""Handles ERROR messages - these mean that our uplink has disconnected us!""" """Handles ERROR messages - these mean that our uplink has disconnected us!"""
irc.connected.clear() self.irc.connected.clear()
raise ProtocolError('Received an ERROR, disconnecting!') raise ProtocolError('Received an ERROR, disconnecting!')
def handle_nick(irc, numeric, command, args): def handle_nick(self, numeric, command, args):
"""Handles incoming NICK changes.""" """Handles incoming NICK changes."""
# <- :70MAAAAAA NICK GL-devel 1434744242 # <- :70MAAAAAA NICK GL-devel 1434744242
oldnick = irc.users[numeric].nick oldnick = self.irc.users[numeric].nick
newnick = irc.users[numeric].nick = args[0] newnick = self.irc.users[numeric].nick = args[0]
return {'newnick': newnick, 'oldnick': oldnick, 'ts': int(args[1])} return {'newnick': newnick, 'oldnick': oldnick, 'ts': int(args[1])}
def handle_quit(irc, numeric, command, args): def handle_quit(self, numeric, command, args):
"""Handles incoming QUITs.""" """Handles incoming QUITs."""
# <- :1SRAAGB4T QUIT :Quit: quit message goes here # <- :1SRAAGB4T QUIT :Quit: quit message goes here
removeClient(irc, numeric) self.removeClient(numeric)
return {'text': args[0]} return {'text': args[0]}
def handle_save(irc, numeric, command, args): def handle_save(self, numeric, command, args):
"""Handles incoming SAVE messages, used to handle nick collisions.""" """Handles incoming SAVE messages, used to handle nick collisions."""
# In this below example, the client Derp_ already exists, # In this below example, the client Derp_ already exists,
# and trying to change someone's nick to it will cause a nick # and trying to change someone's nick to it will cause a nick
# collision. On TS6 IRCds, this will simply set the collided user's # collision. On TS6 self.ircds, this will simply set the collided user's
# nick to its UID. # nick to its UID.
# <- :70MAAAAAA PRIVMSG 0AL000001 :nickclient PyLink Derp_ # <- :70MAAAAAA PRIVMSG 0AL000001 :nickclient PyLink Derp_
# -> :0AL000001 NICK Derp_ 1433728673 # -> :0AL000001 NICK Derp_ 1433728673
# <- :70M SAVE 0AL000001 1433728673 # <- :70M SAVE 0AL000001 1433728673
user = args[0] user = args[0]
oldnick = irc.users[user].nick oldnick = self.irc.users[user].nick
irc.users[user].nick = user self.irc.users[user].nick = user
return {'target': user, 'ts': int(args[1]), 'oldnick': oldnick} return {'target': user, 'ts': int(args[1]), 'oldnick': oldnick}
def handle_squit(irc, numeric, command, args): def handle_squit(self, numeric, command, args):
"""Handles incoming SQUITs (netsplits).""" """Handles incoming SQUITs (netsplits)."""
# :70M SQUIT 1ML :Server quit by GL!gl@0::1 # :70M SQUIT 1ML :Server quit by GL!gl@0::1
split_server = args[0] split_server = args[0]
affected_users = [] affected_users = []
log.info('(%s) Netsplit on server %s', irc.name, split_server) log.info('(%s) Netsplit on server %s', self.irc.name, split_server)
# Prevent RuntimeError: dictionary changed size during iteration # Prevent RuntimeError: dictionary changed size during iteration
old_servers = irc.servers.copy() old_servers = self.irc.servers.copy()
for sid, data in old_servers.items(): for sid, data in old_servers.items():
if data.uplink == split_server: if data.uplink == split_server:
log.debug('Server %s also hosts server %s, removing those users too...', split_server, sid) log.debug('Server %s also hosts server %s, removing those users too...', split_server, sid)
args = handle_squit(irc, sid, 'SQUIT', [sid, "PyLink: Automatically splitting leaf servers of %s" % sid]) args = self.handle_squit(sid, 'SQUIT', [sid, "PyLink: Automatically splitting leaf servers of %s" % sid])
affected_users += args['users'] affected_users += args['users']
for user in irc.servers[split_server].users.copy(): for user in self.irc.servers[split_server].users.copy():
affected_users.append(user) affected_users.append(user)
log.debug('Removing client %s (%s)', user, irc.users[user].nick) log.debug('Removing client %s (%s)', user, self.irc.users[user].nick)
removeClient(irc, user) self.removeClient(user)
del irc.servers[split_server] del self.irc.servers[split_server]
log.debug('(%s) Netsplit affected users: %s', irc.name, affected_users) log.debug('(%s) Netsplit affected users: %s', self.irc.name, affected_users)
return {'target': split_server, 'users': affected_users} return {'target': split_server, 'users': affected_users}
def handle_mode(irc, numeric, command, args): def handle_mode(self, numeric, command, args):
"""Handles incoming user mode changes. For channel mode changes, """Handles incoming user mode changes. For channel mode changes,
TMODE (TS6/charybdis) and FMODE (InspIRCd) are used instead.""" TMODE (TS6/charybdis) and FMODE (Inspself.ircd) are used instead."""
# In InspIRCd, MODE is used for setting user modes and # In Inspself.ircd, MODE is used for setting user modes and
# FMODE is used for channel modes: # FMODE is used for channel modes:
# <- :70MAAAAAA MODE 70MAAAAAA -i+xc # <- :70MAAAAAA MODE 70MAAAAAA -i+xc
target = args[0] target = args[0]
modestrings = args[1:] modestrings = args[1:]
changedmodes = utils.parseModes(irc, numeric, modestrings) changedmodes = utils.parseModes(self.irc, numeric, modestrings)
utils.applyModes(irc, target, changedmodes) utils.applyModes(self.irc, target, changedmodes)
return {'target': target, 'modes': changedmodes} return {'target': target, 'modes': changedmodes}
def handle_topic(irc, numeric, command, args): def handle_topic(self, numeric, command, args):
"""Handles incoming TOPIC changes from clients. For topic bursts, """Handles incoming TOPIC changes from clients. For topic bursts,
TB (TS6/charybdis) and FTOPIC (InspIRCd) are used instead.""" TB (TS6/charybdis) and FTOPIC (Inspself.ircd) are used instead."""
# <- :70MAAAAAA TOPIC #test :test # <- :70MAAAAAA TOPIC #test :test
channel = utils.toLower(irc, args[0]) channel = utils.toLower(self.irc, args[0])
topic = args[1] topic = args[1]
ts = int(time.time()) ts = int(time.time())
irc.channels[channel].topic = topic self.irc.channels[channel].topic = topic
irc.channels[channel].topicset = True self.irc.channels[channel].topicset = True
return {'channel': channel, 'setter': numeric, 'ts': ts, 'topic': topic} return {'channel': channel, 'setter': numeric, 'ts': ts, 'topic': topic}
def handle_part(irc, source, command, args): def handle_part(self, source, command, args):
"""Handles incoming PART commands.""" """Handles incoming PART commands."""
channels = utils.toLower(irc, args[0]).split(',') channels = utils.toLower(self.irc, args[0]).split(',')
for channel in channels: for channel in channels:
# We should only get PART commands for channels that exist, right?? # We should only get PART commands for channels that exist, right??
irc.channels[channel].removeuser(source) self.irc.channels[channel].removeuser(source)
try: try:
irc.users[source].channels.discard(channel) self.irc.users[source].channels.discard(channel)
except KeyError: except KeyError:
log.debug("(%s) handle_part: KeyError trying to remove %r from %r's channel list?", irc.name, channel, source) log.debug("(%s) handle_part: KeyError trying to remove %r from %r's channel list?", self.irc.name, channel, source)
try: try:
reason = args[1] reason = args[1]
except IndexError: except IndexError:
reason = '' reason = ''
# Clear empty non-permanent channels. # Clear empty non-permanent channels.
if not (irc.channels[channel].users or ((irc.cmodes.get('permanent'), None) in irc.channels[channel].modes)): if not (self.irc.channels[channel].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[channel].modes)):
del irc.channels[channel] del self.irc.channels[channel]
return {'channels': channels, 'text': reason} return {'channels': channels, 'text': reason}

View File

@ -100,9 +100,9 @@ class TS6SIDGenerator():
def msg(irc, target, text, notice=False): def msg(irc, target, text, notice=False):
if notice: if notice:
irc.proto.noticeClient(irc, irc.pseudoclient.uid, target, text) irc.proto.noticeClient(irc.pseudoclient.uid, target, text)
else: else:
irc.proto.messageClient(irc, irc.pseudoclient.uid, target, text) irc.proto.messageClient(irc.pseudoclient.uid, target, text)
def add_cmd(func, name=None): def add_cmd(func, name=None):
if name is None: if name is None: