3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-18 08:02:51 +01:00
PyLink/protocols/hybrid.py

276 lines
12 KiB
Python
Raw Normal View History

2015-12-26 07:49:55 +01:00
import time
import sys
import os
import re
curdir = os.path.dirname(__file__)
sys.path += [curdir, os.path.dirname(curdir)]
import utils
from log import log
from classes import *
from ts6 import TS6Protocol
2015-12-26 07:49:55 +01:00
class HybridProtocol(TS6Protocol):
2015-12-26 07:49:55 +01:00
def __init__(self, irc):
# This protocol module inherits from the TS6 protocol.
super().__init__(irc)
2015-12-26 07:49:55 +01:00
self.casemapping = 'ascii'
2015-12-26 07:49:55 +01:00
self.caps = {}
2016-03-30 05:59:04 +02:00
self.hook_map = {'EOB': 'ENDBURST', 'TBURST': 'TOPIC'}
2015-12-26 07:49:55 +01:00
def connect(self):
"""Initializes a connection to a server."""
ts = self.irc.start_ts
f = self.irc.send
# Valid keywords (from mostly InspIRCd's named modes):
# admin allowinvite autoop ban banexception blockcolor
# c_registered exemptchanops filter forward flood halfop history invex
# inviteonly joinflood key kicknorejoin limit moderated nickflood
# noctcp noextmsg nokick noknock nonick nonotice official-join op
# operonly opmoderated owner permanent private redirect regonly
# regmoderated secret sslonly stripcolor topiclock voice
# https://github.com/grawity/irc-docs/blob/master/server/ts6.txt#L80
cmodes = {
# TS6 generic modes:
'op': 'o', 'halfop': 'h', 'voice': 'v', 'ban': 'b', 'key': 'k',
'limit': 'l', 'moderated': 'm', 'noextmsg': 'n',
2015-12-26 07:49:55 +01:00
'secret': 's', 'topiclock': 't',
# hybrid-specific modes:
'blockcolor': 'c', 'inviteonly': 'i', 'noctcp': 'C',
'regmoderated': 'M', 'operonly': 'O', 'regonly': 'R',
'sslonly': 'S', 'banexception': 'e', 'paranoia': 'p',
'registered': 'r', 'invex': 'I',
2015-12-26 07:49:55 +01:00
# Now, map all the ABCD type modes:
'*A': 'beI', '*B': 'k', '*C': 'l', '*D': 'cimnprstCMORS'
}
self.irc.cmodes.update(cmodes)
# Same thing with umodes:
# bot callerid cloak deaf_commonchan helpop hidechans hideoper invisible oper
# regdeaf servprotect showwhois snomask u_registered u_stripcolor wallops
umodes = {
'oper': 'o', 'invisible': 'i', 'wallops': 'w', 'chary_locops': 'l',
'cloak_hybrid': 'x', 'hidechans': 'p', 'regdeaf': 'R', 'deaf': 'D',
'callerid': 'g', 'showadmin': 'a', 'softcallerid': 'G', 'hideops': 'H',
'webirc': 'W', 'client_connections': 'c', 'bad_client_connections': 'u',
'rejected_clients': 'j', 'skill_notices': 'k', 'fullauthblock': 'f',
'remote_client_connections': 'F', 'admin_requests': 'y', 'debug': 'd',
'nickchange_notices': 'n', 'hideidle': 'q', 'registered': 'r',
'smessages': 's', 'ssl': 'S', 'sjoins': 'e', 'botfloods': 'b',
2015-12-26 07:49:55 +01:00
# Now, map all the ABCD type modes:
'*A': '', '*B': '', '*C': '', '*D': 'oiwlpRDgdx'
2015-12-26 07:49:55 +01:00
}
self.irc.umodes.update(umodes)
# halfops is mandatory on Hybrid
self.irc.prefixmodes = {'o': '@', 'h': '%', 'v': '+'}
2015-12-26 07:49:55 +01:00
# https://github.com/grawity/irc-docs/blob/master/server/ts6.txt#L55
f('PASS %s TS 6 %s' % (self.irc.serverdata["sendpass"], self.irc.sid))
# We request the following capabilities (for hybrid):
# ENCAP: message encapsulation for certain commands
# EX: Support for ban exemptions (+e)
# IE: Support for invite exemptions (+e)
# CHW: Allow sending messages to @#channel and the like.
# KNOCK: Support for /knock
# SVS: Deal with extended NICK/UID messages that contain service IDs/stamps
# TBURST: Topic Burst command; we send this in topicBurst
2015-12-26 07:49:55 +01:00
# DLN: DLINE command
# UNDLN: UNDLINE command
# KLN: KLINE command
# UNKLN: UNKLINE command
# HOPS: Supports HALFOPS
# CHW: Can do channel wall (@#)
# CLUSTER: Supports server clustering
# EOB: Supports EOB (end of burst) command
f('CAPAB :TBURST DLN KNOCK UNDLN UNKLN KLN ENCAP IE EX HOPS CHW SVS CLUSTER EOB QS')
f('SERVER %s 0 :%s' % (self.irc.serverdata["hostname"],
self.irc.serverdata.get('serverdesc') or self.irc.botdata['serverdesc']))
# send endburst now
self.irc.send(':%s EOB' % (self.irc.sid,))
2015-12-26 07:49:55 +01:00
def spawnClient(self, nick, ident='null', host='null', realhost=None, modes=set(),
server=None, ip='0.0.0.0', realname=None, ts=None, opertype=None,
manipulatable=False):
"""Spawns a client with nick <nick> on the given IRC connection.
Note: No nick collision / valid nickname checks are done here; it is
up to plugins to make sure they don't introduce anything invalid."""
server = server or self.irc.sid
2016-03-29 10:35:29 +02:00
if not self.irc.isInternalServer(server):
2015-12-26 07:49:55 +01:00
raise ValueError('Server %r is not a PyLink internal PseudoServer!' % server)
# Create an UIDGenerator instance for every SID, so that each gets
# distinct values.
uid = self.uidgen.setdefault(server, utils.TS6UIDGenerator(server)).next_uid()
# EUID:
# parameters: nickname, hopcount, nickTS, umodes, username,
# visible hostname, IP address, UID, real hostname, account name, gecos
ts = ts or int(time.time())
realname = realname or self.irc.botdata['realname']
realhost = realhost or host
raw_modes = utils.joinModes(modes)
u = self.irc.users[uid] = IrcUser(nick, ts, uid, ident=ident, host=host, realname=realname,
realhost=realhost, ip=ip, manipulatable=manipulatable)
utils.applyModes(self.irc, uid, modes)
self.irc.servers[server].users.add(uid)
self._send(server, "UID {nick} 1 {ts} {modes} {ident} {host} {ip} {uid} "
"* :{realname}".format(ts=ts, host=host,
nick=nick, ident=ident, uid=uid,
modes=raw_modes, ip=ip, realname=realname))
return u
def updateClient(self, target, field, text):
2015-12-26 07:49:55 +01:00
"""Updates the ident, host, or realname of a PyLink client."""
# https://github.com/ircd-hybrid/ircd-hybrid/blob/58323b8/modules/m_svsmode.c#L40-L103
# parv[0] = command
# parv[1] = nickname <-- UID works too -GLolol
# parv[2] = TS <-- Of the user, not the current time. -GLolol
# parv[3] = mode
# parv[4] = optional argument (services account, vhost)
2015-12-26 07:49:55 +01:00
field = field.upper()
2016-04-09 05:57:03 +02:00
ts = self.irc.users[target].ts
if field == 'HOST':
self.irc.users[target].host = text
# On Hybrid, it appears that host changing is actually just forcing umode
# "+x <hostname>" on the target. -GLolol
self._send(self.irc.sid, 'SVSMODE %s %s +x %s' % (target, ts, text))
2015-12-26 07:49:55 +01:00
else:
raise NotImplementedError("Changing field %r of a client is unsupported by this protocol." % field)
2016-04-09 05:01:45 +02:00
def topicBurst(self, numeric, target, text):
"""Sends a topic change from a PyLink server. This is usually used on burst."""
# <- :0UY TBURST 1459308205 #testchan 1459309379 dan!~d@localhost :sdf
if not self.irc.isInternalServer(numeric):
raise LookupError('No such PyLink server exists.')
ts = self.irc.channels[target].ts
servername = self.irc.servers[numeric].name
self._send(numeric, 'TBURST %s %s %s %s :%s' % (ts, target, int(time.time()), servername, text))
self.irc.channels[target].topic = text
self.irc.channels[target].topicset = True
2015-12-26 07:49:55 +01:00
# command handlers
def handle_capab(self, numeric, command, args):
# We only get a list of keywords here. Hybrid obviously assumes that
2015-12-26 07:49:55 +01:00
# we know what modes it supports (indeed, this is a standard list).
# <- CAPAB :UNDLN UNKLN KLN TBURST KNOCK ENCAP DLN IE EX HOPS CHW SVS CLUSTER EOB QS
2015-12-26 07:49:55 +01:00
self.irc.caps = caps = args[0].split()
for required_cap in ('EX', 'IE', 'SVS', 'EOB', 'HOPS', 'QS', 'TBURST', 'SVS'):
if required_cap not in caps:
raise ProtocolError('%s not found in TS6 capabilities list; this is required! (got %r)' % (required_cap, caps))
2015-12-26 07:49:55 +01:00
log.debug('(%s) self.irc.connected set!', self.irc.name)
self.irc.connected.set()
2015-12-26 07:49:55 +01:00
def handle_uid(self, numeric, command, args):
"""
Handles Hybrid-style UID commands (user introduction). This is INCOMPATIBLE
with standard TS6 implementations, as the arguments are slightly different.
"""
2015-12-26 07:49:55 +01:00
# <- :0UY UID dan 1 1451041551 +Facdeiklosuw ~ident localhost 127.0.0.1 0UYAAAAAB * :realname
nick = args[0]
ts, modes, ident, host, ip, uid, account, realname = args[2:10]
if account == '*':
account = None
log.debug('(%s) handle_uid: got args nick=%s ts=%s uid=%s ident=%s '
2015-12-26 07:49:55 +01:00
'host=%s realname=%s ip=%s', self.irc.name, nick, ts, uid,
ident, host, realname, ip)
self.irc.users[uid] = IrcUser(nick, ts, uid, ident, host, realname, host, ip)
2015-12-26 07:49:55 +01:00
parsedmodes = utils.parseModes(self.irc, uid, [modes])
log.debug('(%s) handle_uid: Applying modes %s for %s', self.irc.name, parsedmodes, uid)
2015-12-26 07:49:55 +01:00
utils.applyModes(self.irc, uid, parsedmodes)
self.irc.servers[numeric].users.add(uid)
2015-12-26 07:49:55 +01:00
# Call the OPERED UP hook if +o is being added to the mode list.
if ('+o', None) in parsedmodes:
2015-12-27 00:49:32 +01:00
self.irc.callHooks([uid, 'CLIENT_OPERED', {'text': 'IRC_Operator'}])
# Set the account name if present
if account:
self.irc.callHooks([uid, 'CLIENT_SERVICES_LOGIN', {'text': account}])
2015-12-26 07:49:55 +01:00
return {'uid': uid, 'ts': ts, 'nick': nick, 'realname': realname, 'host': host, 'ident': ident, 'ip': ip}
2016-03-30 05:51:53 +02:00
def handle_tburst(self, numeric, command, args):
"""Handles incoming topic burst (TBURST) commands."""
# <- :0UY TBURST 1459308205 #testchan 1459309379 dan!~d@localhost :sdf
channel = args[1].lower()
ts = args[2]
setter = args[3]
topic = args[-1]
self.irc.channels[channel].topic = topic
self.irc.channels[channel].topicset = True
return {'channel': channel, 'setter': setter, 'ts': ts, 'text': topic}
2016-04-09 05:36:14 +02:00
def handle_eob(self, numeric, command, args):
log.debug('(%s) end of burst received', self.irc.name)
return {}
def handle_svsmode(self, numeric, command, args):
"""
Handles SVSMODE, which is used for sending services metadata
(vhosts, account logins), and other forced usermode changes.
"""
target = args[0]
ts = args[1]
modes = args[2:]
parsedmodes = utils.parseModes(self.irc, target, modes)
for modepair in parsedmodes:
if modepair[0] == '+d':
# Login sequence (tested with Anope 2.0.4-git):
# A mode change +d accountname is used to propagate logins,
# before setting umode +r on the target.
# <- :5ANAAAAAG SVSMODE 5HYAAAAAA 1460175209 +d GL
# <- :5ANAAAAAG SVSMODE 5HYAAAAAA 1460175209 +r
# Logout sequence:
# <- :5ANAAAAAG SVSMODE 5HYAAAAAA 1460175209 +d *
# <- :5ANAAAAAG SVSMODE 5HYAAAAAA 1460175209 -r
account = args[-1]
if account == '*':
account = '' # Logout
# Send the login hook, and remove this mode from the mode
# list, as it shouldn't be parsed literally.
self.irc.callHooks([target, 'CLIENT_SERVICES_LOGIN', {'text': account}])
parsedmodes.remove(modepair)
2016-04-09 07:02:08 +02:00
elif modepair[0] == '+x':
# SVSMODE is also used to set cloaks on Hybrid.
# "SVSMODE 001TARGET +x some.host" would change 001TARGET's host
# to some.host, for example.
host = args[-1]
2016-04-09 07:02:08 +02:00
self.irc.users[target].host = host
# Propagate the hostmask change as a hook.
self.irc.callHooks([numeric, 'CHGHOST',
{'target': target, 'newhost': host}])
parsedmodes.remove(modepair)
if parsedmodes:
utils.applyModes(self.irc, target, parsedmodes)
return {'target': target, 'modes': parsedmodes}
2015-12-26 07:49:55 +01:00
Class = HybridProtocol