3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-24 03:33:10 +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.sid = self.serverdata["sid"]
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.pingtimeout = self.pingfreq * 2
@ -142,7 +143,7 @@ class Irc():
sha1fp)
if checks_ok:
self.proto.connect(self)
self.proto.connect()
self.spawnMain()
log.info('(%s) Starting ping schedulers....', self.name)
self.schedulePing()
@ -200,7 +201,7 @@ class Irc():
log.debug("(%s) <- %s", self.name, line)
hook_args = None
try:
hook_args = self.proto.handle_events(self, line)
hook_args = self.proto.handle_events(line)
except Exception:
log.exception('(%s) Caught error in handle_events, disconnecting!', self.name)
return
@ -249,7 +250,7 @@ class Irc():
log.debug("(%s) Dropping message %r; network isn't connected!", self.name, stripped_data)
def schedulePing(self):
self.proto.pingServer(self)
self.proto.pingServer()
self.pingTimer = threading.Timer(self.pingfreq, self.schedulePing)
self.pingTimer.daemon = True
self.pingTimer.start()
@ -261,9 +262,9 @@ class Irc():
host = self.serverdata["hostname"]
log.info('(%s) Connected! Spawning main client %s.', self.name, nick)
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']:
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
# contents of Irc().pseudoclient change.
self.callHooks([self.sid, 'PYLINK_SPAWNMAIN', {'olduser': olduserobj}])
@ -339,7 +340,7 @@ class FakeIRC(Irc):
def run(self, data):
"""Queues a message to the fake IRC server."""
log.debug('<- ' + data)
hook_args = self.proto.handle_events(self, data)
hook_args = self.proto.handle_events(data)
if hook_args is not None:
self.hookmsgs.append(hook_args)
self.callHooks(hook_args)
@ -374,6 +375,13 @@ class FakeIRC(Irc):
self.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():
"""Dummy protocol module for testing purposes."""
def __init__(self):

View File

@ -15,7 +15,7 @@ def handle_kick(irc, source, command, args):
kicked = args['target']
channel = args['channel']
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')
# 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
# https://www.alien.net.au/irc/irc2numerics.html
# 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
public_chans = []
for chan in user.channels:
@ -69,9 +69,9 @@ def handle_whois(irc, source, command, args):
chan = prefixchar + chan
public_chans.append(chan)
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.
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']))
# 313: sends a string denoting the target's operator privilege,
# only if they have umode +o.
@ -82,15 +82,15 @@ def handle_whois(irc, source, command, args):
opertype = "IRC Operator"
# Let's be gramatically correct.
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.
# Only show this to opers!
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
# idle time, so we simply return 0.
# <- 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:
# Iterate over custom plugin WHOIS handlers. They return a tuple
# 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)
if res:
num, text = res
f(irc, server, num, source, text)
f(server, num, source, text)
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.
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')

View File

@ -22,7 +22,7 @@ def spawnclient(irc, source, args):
except ValueError:
utils.msg(irc, source, "Error: Not enough arguments. Needs 3: nick, user, host.")
return
irc.proto.spawnClient(irc, nick, ident, host)
irc.proto.spawnClient(nick, ident, host)
@utils.add_cmd
def quit(irc, source, args):
@ -40,7 +40,7 @@ def quit(irc, source, args):
return
u = utils.nickToUid(irc, nick)
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'}])
def joinclient(irc, source, args):
@ -61,7 +61,7 @@ def joinclient(irc, source, args):
if not utils.isChannel(channel):
utils.msg(irc, source, "Error: Invalid channel name %r." % channel)
return
irc.proto.joinClient(irc, u, channel)
irc.proto.joinClient(u, channel)
irc.callHooks([u, 'PYLINK_BOTSPLUGIN_JOIN', {'channel': channel, 'users': [u],
'modes': irc.channels[channel].modes,
'parse_as': 'JOIN'}])
@ -85,7 +85,7 @@ def nick(irc, source, args):
elif not utils.isNick(newnick):
utils.msg(irc, source, 'Error: Invalid nickname %r.' % newnick)
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'}])
@utils.add_cmd
@ -106,7 +106,7 @@ def part(irc, source, args):
if not utils.isChannel(channel):
utils.msg(irc, source, "Error: Invalid channel name %r." % channel)
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'}])
@utils.add_cmd
@ -129,9 +129,9 @@ def kick(irc, source, args):
utils.msg(irc, source, "Error: Invalid channel name %r." % channel)
return
if utils.isInternalServer(irc, u):
irc.proto.kickServer(irc, u, channel, targetu, reason)
irc.proto.kickServer(u, channel, targetu, reason)
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'}])
@utils.add_cmd
@ -156,11 +156,11 @@ def mode(irc, source, args):
utils.msg(irc, source, "Error: Invalid channel or nick %r." % target)
return
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'}])
else:
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'}])
@utils.add_cmd
@ -188,5 +188,5 @@ def msg(irc, source, args):
if not text:
utils.msg(irc, source, 'Error: No text given.')
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'}])

View File

@ -39,7 +39,7 @@ def normalizeNick(irc, netname, nick, separator=None, uid=''):
separator = separator or irc.serverdata.get('separator') or "/"
log.debug('(%s) normalizeNick: using %r as separator.', irc.name, separator)
orig_nick = nick
protoname = irc.proto.__name__
protoname = irc.protoname
maxnicklen = irc.maxnicklen
if '/' not in separator or not protoname.startswith(('insp', 'unreal')):
# 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')
if hideoper_mode:
modes.append((hideoper_mode, None))
u = remoteirc.proto.spawnClient(remoteirc, nick, ident=ident,
u = remoteirc.proto.spawnClient(nick, ident=ident,
host=host, realname=realname,
modes=modes, ts=userobj.ts,
opertype=opertype).uid
@ -180,7 +180,7 @@ def getRemoteUser(irc, remoteirc, user, spawnIfMissing=True):
remoteirc.users[u].opertype = opertype
away = userobj.away
if away:
remoteirc.proto.awayClient(remoteirc, u, away)
remoteirc.proto.awayClient(u, away)
relayusers[(irc.name, user)][remoteirc.name] = u
return u
@ -275,11 +275,11 @@ def initializeChannel(irc, channel):
# Only update the topic if it's different from what we already have,
# and topic bursting is complete.
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
log.debug('(%s) initializeChannel: joining our users: %s', irc.name, c.users)
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):
channel = args['channel']
@ -294,7 +294,7 @@ utils.add_hook(handle_join, 'JOIN')
def handle_quit(irc, numeric, command, args):
for netname, user in relayusers[(irc.name, numeric)].copy().items():
remoteirc = world.networkobjects[netname]
remoteirc.proto.quitClient(remoteirc, user, args['text'])
remoteirc.proto.quitClient(user, args['text'])
del relayusers[(irc.name, numeric)]
utils.add_hook(handle_quit, 'QUIT')
@ -310,7 +310,7 @@ def handle_nick(irc, numeric, command, args):
remoteirc = world.networkobjects[netname]
newnick = normalizeNick(remoteirc, irc.name, args['newnick'], uid=user)
if remoteirc.users[user].nick != newnick:
remoteirc.proto.nickClient(remoteirc, user, newnick)
remoteirc.proto.nickClient(user, newnick)
utils.add_hook(handle_nick, 'NICK')
def handle_part(irc, numeric, command, args):
@ -325,9 +325,9 @@ def handle_part(irc, numeric, command, args):
remotechan = findRemoteChan(irc, remoteirc, channel)
if remotechan is None:
continue
remoteirc.proto.partClient(remoteirc, user, remotechan, text)
remoteirc.proto.partClient(user, remotechan, text)
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]
utils.add_hook(handle_part, 'PART')
@ -363,9 +363,9 @@ def handle_privmsg(irc, numeric, command, args):
continue
real_target = prefix + real_target
if notice:
remoteirc.proto.noticeClient(remoteirc, user, real_target, text)
remoteirc.proto.noticeClient(user, real_target, text)
else:
remoteirc.proto.messageClient(remoteirc, user, real_target, text)
remoteirc.proto.messageClient(user, real_target, text)
else:
remoteuser = getLocalUser(irc, target)
if remoteuser is None:
@ -383,9 +383,9 @@ def handle_privmsg(irc, numeric, command, args):
remoteirc = world.networkobjects[homenet]
user = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False)
if notice:
remoteirc.proto.noticeClient(remoteirc, user, real_target, text)
remoteirc.proto.noticeClient(user, real_target, text)
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, 'NOTICE')
@ -431,7 +431,7 @@ def handle_kick(irc, source, command, args):
# kick ops, admins can't kick owners, etc.
modes = getPrefixModes(remoteirc, irc, remotechan, real_target)
# 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:
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,
@ -450,7 +450,7 @@ def handle_kick(irc, source, command, args):
# Propogate the kick!
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)
remoteirc.proto.kickClient(remoteirc, real_kicker,
remoteirc.proto.kickClient(real_kicker,
remotechan, real_target, text)
else:
# 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)
except AttributeError:
text = "(<unknown kicker>@%s) %s" % (irc.name, text)
remoteirc.proto.kickServer(remoteirc, remoteirc.sid,
remoteirc.proto.kickServer(remoteirc.sid,
remotechan, real_target, text)
# If the target isn't on any channels, quit them.
if origuser and origuser[0] != remoteirc.name and not remoteirc.users[real_target].channels:
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:
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')
@ -493,7 +493,7 @@ def handle_chgclient(irc, source, command, args):
for netname, user in relayusers[(irc.name, target)].items():
remoteirc = world.networkobjects[netname]
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
log.debug('(%s) Ignoring changing field %r of %s on %s (for %s/%s);'
' 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.
u = getRemoteUser(irc, remoteirc, sender, spawnIfMissing=False)
if u:
remoteirc.proto.modeClient(remoteirc, u, remotechan, supported_modes)
remoteirc.proto.modeClient(u, remotechan, supported_modes)
else:
remoteirc.proto.modeServer(remoteirc, remoteirc.sid, remotechan, supported_modes)
remoteirc.proto.modeServer(remoteirc.sid, remotechan, supported_modes)
def getSupportedUmodes(irc, remoteirc, modes):
supported_modes = []
@ -613,7 +613,7 @@ def getSupportedUmodes(irc, remoteirc, modes):
log.debug("(%s) getSupportedUmodes: skipping mode (%r, %r) because "
"the remote network (%s)'s IRCd (%s) doesn't support it.",
irc.name, modechar, arg, remoteirc.name,
remoteirc.proto.__name__)
remoteirc.protoname)
return supported_modes
def handle_mode(irc, numeric, command, args):
@ -636,7 +636,7 @@ def handle_mode(irc, numeric, command, args):
modes.append(('-%s' % hideoper_mode, None))
remoteuser = getRemoteUser(irc, remoteirc, target, spawnIfMissing=False)
if remoteuser and modes:
remoteirc.proto.modeClient(remoteirc, remoteuser, remoteuser, modes)
remoteirc.proto.modeClient(remoteuser, remoteuser, modes)
utils.add_hook(handle_mode, 'MODE')
@ -654,9 +654,9 @@ def handle_topic(irc, numeric, command, args):
# This might originate from a server too.
remoteuser = getRemoteUser(irc, remoteirc, numeric, spawnIfMissing=False)
if remoteuser:
remoteirc.proto.topicClient(remoteirc, remoteuser, remotechan, topic)
remoteirc.proto.topicClient(remoteuser, remotechan, topic)
else:
remoteirc.proto.topicServer(remoteirc, remoteirc.sid, remotechan, topic)
remoteirc.proto.topicServer(remoteirc.sid, remotechan, topic)
utils.add_hook(handle_topic, 'TOPIC')
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])
log.debug('(%s) relay handle_kill: userpair: %s, %s', irc.name, modes, realuser)
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:
log.info('(%s) Relay claim: Blocked KILL (reason %r) from %s to relay client %s/%s.',
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
# users/someone with a prefix.
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)
else:
remoteirc.proto.joinClient(remoteirc, queued_users[0][1], remotechan)
remoteirc.proto.joinClient(queued_users[0][1], remotechan)
def relayPart(irc, channel, user):
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)
if remotechan is None or remoteuser is None:
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:
remoteirc.proto.quitClient(remoteirc, remoteuser, 'Left all shared channels.')
remoteirc.proto.quitClient(remoteuser, 'Left all shared channels.')
del relayusers[(irc.name, user)][remoteirc.name]
def removeChannel(irc, channel):
if irc is None:
return
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))
if relay:
for user in irc.channels[channel].users.copy():
@ -782,12 +782,12 @@ def removeChannel(irc, channel):
if user == irc.pseudoclient.uid and channel in \
irc.serverdata['channels']:
continue
irc.proto.partClient(irc, user, channel, 'Channel delinked.')
irc.proto.partClient(user, channel, 'Channel delinked.')
# Don't ever quit it either...
if user != irc.pseudoclient.uid and not irc.users[user].channels:
remoteuser = getLocalUser(irc, user)
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
def create(irc, source, args):
@ -986,7 +986,7 @@ def handle_save(irc, numeric, command, args):
newnick = normalizeNick(irc, remotenet, nick)
log.info('(%s) SAVE received for relay client %r (%s), fixing nick to %s',
irc.name, target, nick, newnick)
irc.proto.nickClient(irc, target, newnick)
irc.proto.nickClient(target, newnick)
else:
log.warning('(%s) SAVE received for relay client %r (%s), not '
'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):
for netname, user in relayusers[(irc.name, numeric)].items():
remoteirc = world.networkobjects[netname]
remoteirc.proto.awayClient(remoteirc, user, args['text'])
remoteirc.proto.awayClient(user, args['text'])
utils.add_hook(handle_away, 'AWAY')
def handle_spawnmain(irc, numeric, command, args):
@ -1048,7 +1048,7 @@ def handle_invite(irc, source, command, args):
'channel not on their network!',
notice=True)
else:
remoteirc.proto.inviteClient(remoteirc, remotesource, remoteuser,
remoteirc.proto.inviteClient(remotesource, remoteuser,
remotechan)
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 classes import *
def _send(irc, source, msg):
"""Sends a TS6-style raw command from a source numeric to the IRC connection given."""
irc.send(':%s %s' % (source, msg))
class TS6BaseProtocol(Protocol):
def _send(self, 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):
"""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.
"""
real_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]
def parseArgs(self, args):
"""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.
"""
real_args = []
for idx, arg in enumerate(args):
real_args.append(arg)
break
return real_args
# 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)
break
return real_args
def parseTS6Args(args):
"""Similar to parseArgs(), but stripping leading colons from the first argument
of a line (usually the sender field)."""
args = parseArgs(args)
args[0] = args[0].split(':', 1)[1]
return args
def parseTS6Args(self, args):
"""Similar to parseArgs(), but stripping leading colons from the first argument
of a line (usually the sender field)."""
args = self.parseArgs(args)
args[0] = args[0].split(':', 1)[1]
return args
### OUTGOING COMMANDS
### OUTGOING COMMANDS
def _sendKick(irc, numeric, channel, target, reason=None):
"""Internal function to send kicks from a PyLink client/server."""
channel = utils.toLower(irc, channel)
if not reason:
reason = 'No reason given'
_send(irc, numeric, 'KICK %s %s :%s' % (channel, target, reason))
# 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
# handle_part() does that just fine.
handle_part(irc, target, 'KICK', [channel])
def _sendKick(self, numeric, channel, target, reason=None):
"""Internal function to send kicks from a PyLink client/server."""
channel = utils.toLower(self.irc, channel)
if not reason:
reason = 'No reason given'
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
# is that the target gets removed from the channel userlist, and calling
# handle_part() does that just fine.
self.handle_part(target, 'KICK', [channel])
def kickClient(irc, numeric, channel, target, reason=None):
"""Sends a kick from a PyLink client."""
if not utils.isInternalClient(irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
_sendKick(irc, numeric, channel, target, reason=reason)
def kickClient(self, numeric, channel, target, reason=None):
"""Sends a kick from a PyLink client."""
if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
self._sendKick(numeric, channel, target, reason=reason)
def kickServer(irc, numeric, channel, target, reason=None):
"""Sends a kick from a PyLink server."""
if not utils.isInternalServer(irc, numeric):
raise LookupError('No such PyLink PseudoServer exists.')
_sendKick(irc, numeric, channel, target, reason=reason)
def kickServer(self, numeric, channel, target, reason=None):
"""Sends a kick from a PyLink server."""
if not utils.isInternalServer(self.irc, numeric):
raise LookupError('No such PyLink PseudoServer exists.')
self._sendKick(numeric, channel, target, reason=reason)
def nickClient(irc, numeric, newnick):
"""Changes the nick of a PyLink client."""
if not utils.isInternalClient(irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'NICK %s %s' % (newnick, int(time.time())))
irc.users[numeric].nick = newnick
def nickClient(self, numeric, newnick):
"""Changes the nick of a PyLink client."""
if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
self._send(numeric, 'NICK %s %s' % (newnick, int(time.time())))
self.irc.users[numeric].nick = newnick
def removeClient(irc, numeric):
"""Internal function to remove a client from our internal state."""
for c, v in irc.channels.copy().items():
v.removeuser(numeric)
# Clear empty non-permanent channels.
if not (irc.channels[c].users or ((irc.cmodes.get('permanent'), None) in irc.channels[c].modes)):
del irc.channels[c]
def removeClient(self, numeric):
"""Internal function to remove a client from our internal state."""
for c, v in self.irc.channels.copy().items():
v.removeuser(numeric)
# Clear empty non-permanent channels.
if not (self.irc.channels[c].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[c].modes)):
del self.irc.channels[c]
sid = numeric[:3]
log.debug('Removing client %s from irc.users', numeric)
del irc.users[numeric]
log.debug('Removing client %s from irc.servers[%s]', numeric, sid)
irc.servers[sid].users.discard(numeric)
sid = numeric[:3]
log.debug('Removing client %s from self.irc.users', numeric)
del self.irc.users[numeric]
log.debug('Removing client %s from self.irc.servers[%s]', numeric, sid)
self.irc.servers[sid].users.discard(numeric)
def partClient(irc, client, channel, reason=None):
"""Sends a part from a PyLink client."""
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.')
msg = "PART %s" % channel
if reason:
msg += " :%s" % reason
_send(irc, client, msg)
handle_part(irc, client, 'PART', [channel])
def partClient(self, client, channel, reason=None):
"""Sends a part from a PyLink client."""
channel = utils.toLower(self.irc, channel)
if not utils.isInternalClient(self.irc, client):
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.')
msg = "PART %s" % channel
if reason:
msg += " :%s" % reason
self._send(client, msg)
self.handle_part(client, 'PART', [channel])
def quitClient(irc, numeric, reason):
"""Quits a PyLink client."""
if utils.isInternalClient(irc, numeric):
_send(irc, numeric, "QUIT :%s" % reason)
removeClient(irc, numeric)
else:
raise LookupError("No such PyLink PseudoClient exists.")
def quitClient(self, numeric, reason):
"""Quits a PyLink client."""
if utils.isInternalClient(self.irc, numeric):
self._send(numeric, "QUIT :%s" % reason)
self.removeClient(numeric)
else:
raise LookupError("No such PyLink PseudoClient exists.")
def messageClient(irc, numeric, target, text):
"""Sends a PRIVMSG from a PyLink client."""
if not utils.isInternalClient(irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'PRIVMSG %s :%s' % (target, text))
def messageClient(self, numeric, target, text):
"""Sends a PRIVMSG from a PyLink client."""
if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
self._send(numeric, 'PRIVMSG %s :%s' % (target, text))
def noticeClient(irc, numeric, target, text):
"""Sends a NOTICE from a PyLink client."""
if not utils.isInternalClient(irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'NOTICE %s :%s' % (target, text))
def noticeClient(self, numeric, target, text):
"""Sends a NOTICE from a PyLink client."""
if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
self._send(numeric, 'NOTICE %s :%s' % (target, text))
def topicClient(irc, numeric, target, text):
"""Sends a ROPIC from a PyLink client."""
if not utils.isInternalClient(irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
_send(irc, numeric, 'TOPIC %s :%s' % (target, text))
irc.channels[target].topic = text
irc.channels[target].topicset = True
def topicClient(self, numeric, target, text):
"""Sends a ROPIC from a PyLink client."""
if not utils.isInternalClient(self.irc, numeric):
raise LookupError('No such PyLink PseudoClient exists.')
self._send(numeric, 'TOPIC %s :%s' % (target, text))
self.irc.channels[target].topic = text
self.irc.channels[target].topicset = True
### HANDLERS
### HANDLERS
def handle_privmsg(irc, source, command, args):
"""Handles incoming PRIVMSG/NOTICE."""
# <- :70MAAAAAA PRIVMSG #dev :afasfsa
# <- :70MAAAAAA NOTICE 0ALAAAAAA :afasfsa
target = args[0]
# We use lowercase channels internally, but uppercase UIDs.
if utils.isChannel(target):
target = utils.toLower(irc, target)
return {'target': target, 'text': args[1]}
def handle_privmsg(self, source, command, args):
"""Handles incoming PRIVMSG/NOTICE."""
# <- :70MAAAAAA PRIVMSG #dev :afasfsa
# <- :70MAAAAAA NOTICE 0ALAAAAAA :afasfsa
target = args[0]
# We use lowercase channels internally, but uppercase UIDs.
if utils.isChannel(target):
target = utils.toLower(self.irc, target)
return {'target': target, 'text': args[1]}
handle_notice = handle_privmsg
handle_notice = handle_privmsg
def handle_kill(irc, source, command, args):
"""Handles incoming KILLs."""
killed = args[0]
# Depending on whether the IRCd sends explicit QUIT messages for
# 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.
data = irc.users.get(killed)
if data:
removeClient(irc, killed)
return {'target': killed, 'text': args[1], 'userdata': data}
def handle_kill(self, source, command, args):
"""Handles incoming KILLs."""
killed = args[0]
# Depending on whether the self.ircd sends explicit QUIT messages for
# 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.
data = self.irc.users.get(killed)
if data:
self.removeClient(killed)
return {'target': killed, 'text': args[1], 'userdata': data}
def handle_kick(irc, source, command, args):
"""Handles incoming KICKs."""
# :70MAAAAAA KICK #endlessvoid 70MAAAAAA :some reason
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_kick(self, source, command, args):
"""Handles incoming KICKs."""
# :70MAAAAAA KICK #endlessvoid 70MAAAAAA :some reason
channel = utils.toLower(self.irc, args[0])
kicked = args[1]
self.handle_part(kicked, 'KICK', [channel, args[2]])
return {'channel': channel, 'target': kicked, 'text': args[2]}
def handle_error(irc, numeric, command, args):
"""Handles ERROR messages - these mean that our uplink has disconnected us!"""
irc.connected.clear()
raise ProtocolError('Received an ERROR, disconnecting!')
def handle_error(self, numeric, command, args):
"""Handles ERROR messages - these mean that our uplink has disconnected us!"""
self.irc.connected.clear()
raise ProtocolError('Received an ERROR, disconnecting!')
def handle_nick(irc, numeric, command, args):
"""Handles incoming NICK changes."""
# <- :70MAAAAAA NICK GL-devel 1434744242
oldnick = irc.users[numeric].nick
newnick = irc.users[numeric].nick = args[0]
return {'newnick': newnick, 'oldnick': oldnick, 'ts': int(args[1])}
def handle_nick(self, numeric, command, args):
"""Handles incoming NICK changes."""
# <- :70MAAAAAA NICK GL-devel 1434744242
oldnick = self.irc.users[numeric].nick
newnick = self.irc.users[numeric].nick = args[0]
return {'newnick': newnick, 'oldnick': oldnick, 'ts': int(args[1])}
def handle_quit(irc, numeric, command, args):
"""Handles incoming QUITs."""
# <- :1SRAAGB4T QUIT :Quit: quit message goes here
removeClient(irc, numeric)
return {'text': args[0]}
def handle_quit(self, numeric, command, args):
"""Handles incoming QUITs."""
# <- :1SRAAGB4T QUIT :Quit: quit message goes here
self.removeClient(numeric)
return {'text': args[0]}
def handle_save(irc, numeric, command, args):
"""Handles incoming SAVE messages, used to handle nick collisions."""
# In this below example, the client Derp_ already exists,
# 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
# nick to its UID.
def handle_save(self, numeric, command, args):
"""Handles incoming SAVE messages, used to handle nick collisions."""
# In this below example, the client Derp_ already exists,
# and trying to change someone's nick to it will cause a nick
# collision. On TS6 self.ircds, this will simply set the collided user's
# nick to its UID.
# <- :70MAAAAAA PRIVMSG 0AL000001 :nickclient PyLink Derp_
# -> :0AL000001 NICK Derp_ 1433728673
# <- :70M SAVE 0AL000001 1433728673
user = args[0]
oldnick = irc.users[user].nick
irc.users[user].nick = user
return {'target': user, 'ts': int(args[1]), 'oldnick': oldnick}
# <- :70MAAAAAA PRIVMSG 0AL000001 :nickclient PyLink Derp_
# -> :0AL000001 NICK Derp_ 1433728673
# <- :70M SAVE 0AL000001 1433728673
user = args[0]
oldnick = self.irc.users[user].nick
self.irc.users[user].nick = user
return {'target': user, 'ts': int(args[1]), 'oldnick': oldnick}
def handle_squit(irc, numeric, command, args):
"""Handles incoming SQUITs (netsplits)."""
# :70M SQUIT 1ML :Server quit by GL!gl@0::1
split_server = args[0]
affected_users = []
log.info('(%s) Netsplit on server %s', irc.name, split_server)
# Prevent RuntimeError: dictionary changed size during iteration
old_servers = irc.servers.copy()
for sid, data in old_servers.items():
if data.uplink == split_server:
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])
affected_users += args['users']
for user in irc.servers[split_server].users.copy():
affected_users.append(user)
log.debug('Removing client %s (%s)', user, irc.users[user].nick)
removeClient(irc, user)
del irc.servers[split_server]
log.debug('(%s) Netsplit affected users: %s', irc.name, affected_users)
return {'target': split_server, 'users': affected_users}
def handle_squit(self, numeric, command, args):
"""Handles incoming SQUITs (netsplits)."""
# :70M SQUIT 1ML :Server quit by GL!gl@0::1
split_server = args[0]
affected_users = []
log.info('(%s) Netsplit on server %s', self.irc.name, split_server)
# Prevent RuntimeError: dictionary changed size during iteration
old_servers = self.irc.servers.copy()
for sid, data in old_servers.items():
if data.uplink == split_server:
log.debug('Server %s also hosts server %s, removing those users too...', split_server, sid)
args = self.handle_squit(sid, 'SQUIT', [sid, "PyLink: Automatically splitting leaf servers of %s" % sid])
affected_users += args['users']
for user in self.irc.servers[split_server].users.copy():
affected_users.append(user)
log.debug('Removing client %s (%s)', user, self.irc.users[user].nick)
self.removeClient(user)
del self.irc.servers[split_server]
log.debug('(%s) Netsplit affected users: %s', self.irc.name, affected_users)
return {'target': split_server, 'users': affected_users}
def handle_mode(irc, numeric, command, args):
"""Handles incoming user mode changes. For channel mode changes,
TMODE (TS6/charybdis) and FMODE (InspIRCd) are used instead."""
# In InspIRCd, MODE is used for setting user modes and
# FMODE is used for channel modes:
# <- :70MAAAAAA MODE 70MAAAAAA -i+xc
target = args[0]
modestrings = args[1:]
changedmodes = utils.parseModes(irc, numeric, modestrings)
utils.applyModes(irc, target, changedmodes)
return {'target': target, 'modes': changedmodes}
def handle_mode(self, numeric, command, args):
"""Handles incoming user mode changes. For channel mode changes,
TMODE (TS6/charybdis) and FMODE (Inspself.ircd) are used instead."""
# In Inspself.ircd, MODE is used for setting user modes and
# FMODE is used for channel modes:
# <- :70MAAAAAA MODE 70MAAAAAA -i+xc
target = args[0]
modestrings = args[1:]
changedmodes = utils.parseModes(self.irc, numeric, modestrings)
utils.applyModes(self.irc, target, changedmodes)
return {'target': target, 'modes': changedmodes}
def handle_topic(irc, numeric, command, args):
"""Handles incoming TOPIC changes from clients. For topic bursts,
TB (TS6/charybdis) and FTOPIC (InspIRCd) are used instead."""
# <- :70MAAAAAA TOPIC #test :test
channel = utils.toLower(irc, args[0])
topic = args[1]
ts = int(time.time())
irc.channels[channel].topic = topic
irc.channels[channel].topicset = True
return {'channel': channel, 'setter': numeric, 'ts': ts, 'topic': topic}
def handle_topic(self, numeric, command, args):
"""Handles incoming TOPIC changes from clients. For topic bursts,
TB (TS6/charybdis) and FTOPIC (Inspself.ircd) are used instead."""
# <- :70MAAAAAA TOPIC #test :test
channel = utils.toLower(self.irc, args[0])
topic = args[1]
ts = int(time.time())
self.irc.channels[channel].topic = topic
self.irc.channels[channel].topicset = True
return {'channel': channel, 'setter': numeric, 'ts': ts, 'topic': topic}
def handle_part(irc, source, command, args):
"""Handles incoming PART commands."""
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)
try:
irc.users[source].channels.discard(channel)
except KeyError:
log.debug("(%s) handle_part: KeyError trying to remove %r from %r's channel list?", irc.name, channel, source)
try:
reason = args[1]
except IndexError:
reason = ''
# Clear empty non-permanent channels.
if not (irc.channels[channel].users or ((irc.cmodes.get('permanent'), None) in irc.channels[channel].modes)):
del irc.channels[channel]
return {'channels': channels, 'text': reason}
def handle_part(self, source, command, args):
"""Handles incoming PART commands."""
channels = utils.toLower(self.irc, args[0]).split(',')
for channel in channels:
# We should only get PART commands for channels that exist, right??
self.irc.channels[channel].removeuser(source)
try:
self.irc.users[source].channels.discard(channel)
except KeyError:
log.debug("(%s) handle_part: KeyError trying to remove %r from %r's channel list?", self.irc.name, channel, source)
try:
reason = args[1]
except IndexError:
reason = ''
# Clear empty non-permanent channels.
if not (self.irc.channels[channel].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[channel].modes)):
del self.irc.channels[channel]
return {'channels': channels, 'text': reason}

View File

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