2015-03-20 00:21:49 +01:00
|
|
|
import time
|
2015-04-18 07:11:49 +02:00
|
|
|
import sys
|
2015-06-17 05:05:41 +02:00
|
|
|
import os
|
2015-07-05 22:22:17 +02:00
|
|
|
import re
|
2015-07-07 04:42:09 +02:00
|
|
|
from copy import copy
|
2015-07-05 22:22:17 +02:00
|
|
|
|
2015-09-05 23:25:11 +02:00
|
|
|
# Import hacks to access utils and classes...
|
2015-09-03 21:57:57 +02:00
|
|
|
curdir = os.path.dirname(__file__)
|
|
|
|
sys.path += [curdir, os.path.dirname(curdir)]
|
2015-06-21 05:36:35 +02:00
|
|
|
import utils
|
2015-07-05 22:22:17 +02:00
|
|
|
from log import log
|
2015-06-07 07:17:45 +02:00
|
|
|
from classes import *
|
2015-04-04 03:45:18 +02:00
|
|
|
|
2015-09-05 23:28:18 +02:00
|
|
|
# Some functions are shared with the charybdis module (ts6_common)
|
2015-09-04 20:24:40 +02:00
|
|
|
from ts6_common import nickClient, kickServer, kickClient, _sendKick, quitClient, \
|
|
|
|
removeClient, partClient, messageClient, noticeClient, topicClient, parseTS6Args
|
|
|
|
from ts6_common import handle_privmsg, handle_kill, handle_kick, handle_error, \
|
|
|
|
handle_quit, handle_nick, handle_save, handle_squit, handle_mode, handle_topic, \
|
|
|
|
handle_notice, _send, handle_part
|
|
|
|
|
2015-09-05 23:25:11 +02:00
|
|
|
# Set our case mapping (rfc1459 maps "\" and "|" together, for example".
|
2015-07-24 19:59:04 +02:00
|
|
|
casemapping = 'rfc1459'
|
2015-07-23 05:31:45 +02:00
|
|
|
|
2015-07-05 04:00:29 +02:00
|
|
|
# Raw commands sent from servers vary from protocol to protocol. Here, we map
|
2015-09-05 23:25:11 +02:00
|
|
|
# non-standard names to our hook handlers, so command handlers' outputs
|
|
|
|
# are called with the right hooks.
|
2015-07-08 22:54:45 +02:00
|
|
|
hook_map = {'FJOIN': 'JOIN', 'RSQUIT': 'SQUIT', 'FMODE': 'MODE',
|
2015-08-03 14:00:32 +02:00
|
|
|
'FTOPIC': 'TOPIC', 'OPERTYPE': 'MODE', 'FHOST': 'CHGHOST',
|
|
|
|
'FIDENT': 'CHGIDENT', 'FNAME': 'CHGNAME'}
|
2015-06-19 19:43:42 +02:00
|
|
|
|
2015-07-08 02:46:56 +02:00
|
|
|
def spawnClient(irc, nick, ident='null', host='null', realhost=None, modes=set(),
|
2015-08-31 19:31:50 +02:00
|
|
|
server=None, ip='0.0.0.0', realname=None, ts=None, opertype=None):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""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."""
|
2015-06-22 00:00:33 +02:00
|
|
|
server = server or irc.sid
|
2015-06-24 04:29:53 +02:00
|
|
|
if not utils.isInternalServer(irc, server):
|
2015-06-22 00:00:33 +02:00
|
|
|
raise ValueError('Server %r is not a PyLink internal PseudoServer!' % server)
|
|
|
|
# We need a separate UID generator instance for every PseudoServer
|
|
|
|
# we spawn. Otherwise, things won't wrap around properly.
|
2015-07-04 08:30:38 +02:00
|
|
|
if server not in irc.uidgen:
|
|
|
|
irc.uidgen[server] = utils.TS6UIDGenerator(server)
|
|
|
|
uid = irc.uidgen[server].next_uid()
|
2015-07-23 09:11:24 +02:00
|
|
|
ts = ts or int(time.time())
|
2015-07-08 22:54:45 +02:00
|
|
|
realname = realname or irc.botdata['realname']
|
2015-07-07 04:42:09 +02:00
|
|
|
realhost = realhost or host
|
2015-07-08 02:46:56 +02:00
|
|
|
raw_modes = utils.joinModes(modes)
|
2015-07-18 00:08:24 +02:00
|
|
|
u = irc.users[uid] = IrcUser(nick, ts, uid, ident=ident, host=host, realname=realname,
|
2015-07-23 06:14:22 +02:00
|
|
|
realhost=realhost, ip=ip)
|
|
|
|
utils.applyModes(irc, uid, modes)
|
2015-08-29 21:35:06 +02:00
|
|
|
irc.servers[server].users.add(uid)
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, server, "UID {uid} {ts} {nick} {realhost} {host} {ident} {ip}"
|
2015-07-07 04:42:09 +02:00
|
|
|
" {ts} {modes} + :{realname}".format(ts=ts, host=host,
|
2015-06-21 05:54:01 +02:00
|
|
|
nick=nick, ident=ident, uid=uid,
|
2015-07-07 04:42:09 +02:00
|
|
|
modes=raw_modes, ip=ip, realname=realname,
|
|
|
|
realhost=realhost))
|
2015-08-31 19:31:50 +02:00
|
|
|
if ('o', None) in modes or ('+o', None) in modes:
|
|
|
|
_operUp(irc, uid, opertype=opertype or 'IRC_Operator')
|
2015-06-07 07:17:45 +02:00
|
|
|
return u
|
|
|
|
|
|
|
|
def joinClient(irc, client, channel):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Joins an internal spawned client <client> to a channel."""
|
2015-07-14 01:06:58 +02:00
|
|
|
# InspIRCd doesn't distinguish between burst joins and regular joins,
|
|
|
|
# so what we're actually doing here is sending FJOIN from the server,
|
2015-09-05 23:25:11 +02:00
|
|
|
# on behalf of the clients that are joining.
|
2015-09-02 07:01:22 +02:00
|
|
|
channel = utils.toLower(irc, channel)
|
2015-06-24 04:29:53 +02:00
|
|
|
server = utils.isInternalClient(irc, client)
|
2015-06-22 00:00:33 +02:00
|
|
|
if not server:
|
2015-07-13 08:28:34 +02:00
|
|
|
log.error('(%s) Error trying to join client %r to %r (no such pseudoclient exists)', irc.name, client, channel)
|
2015-06-08 04:31:56 +02:00
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-08-14 17:47:23 +02:00
|
|
|
# Strip out list-modes, they shouldn't be ever sent in FJOIN.
|
|
|
|
modes = [m for m in irc.channels[channel].modes if m[0] not in irc.cmodes['*A']]
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, server, "FJOIN {channel} {ts} {modes} :,{uid}".format(
|
2015-07-07 04:00:20 +02:00
|
|
|
ts=irc.channels[channel].ts, uid=client, channel=channel,
|
2015-08-14 17:47:23 +02:00
|
|
|
modes=utils.joinModes(modes)))
|
2015-06-24 04:29:53 +02:00
|
|
|
irc.channels[channel].users.add(client)
|
2015-07-16 21:20:40 +02:00
|
|
|
irc.users[client].channels.add(channel)
|
2015-06-08 04:31:56 +02:00
|
|
|
|
2015-07-15 03:20:10 +02:00
|
|
|
def sjoinServer(irc, server, channel, users, ts=None):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Sends an SJOIN for a group of users to a channel.
|
|
|
|
|
|
|
|
The sender should always be a Server ID (SID). TS is optional, and defaults
|
|
|
|
to the one we've stored in the channel state if not given.
|
|
|
|
<users> is a list of (prefix mode, UID) pairs:
|
|
|
|
|
|
|
|
Example uses:
|
|
|
|
sjoinServer(irc, '100', '#test', [('', '100AAABBC'), ('qo', 100AAABBB'), ('h', '100AAADDD')])
|
|
|
|
sjoinServer(irc, irc.sid, '#test', [('o', irc.pseudoclient.uid)])
|
|
|
|
"""
|
2015-09-02 07:01:22 +02:00
|
|
|
channel = utils.toLower(irc, channel)
|
2015-07-14 01:06:58 +02:00
|
|
|
server = server or irc.sid
|
|
|
|
assert users, "sjoinServer: No users sent?"
|
2015-07-21 21:44:01 +02:00
|
|
|
log.debug('(%s) sjoinServer: got %r for users', irc.name, users)
|
2015-07-14 01:06:58 +02:00
|
|
|
if not server:
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-08-16 04:53:09 +02:00
|
|
|
orig_ts = irc.channels[channel].ts
|
|
|
|
ts = ts or orig_ts
|
|
|
|
if ts < orig_ts:
|
2015-08-20 10:32:30 +02:00
|
|
|
# If the TS we're sending is lower than the one that existing, clear the
|
|
|
|
# mode lists from our channel state and reset the timestamp.
|
2015-08-16 04:53:09 +02:00
|
|
|
log.debug('(%s) sjoinServer: resetting TS of %r from %s to %s (clearing modes)',
|
|
|
|
irc.name, channel, orig_ts, ts)
|
|
|
|
irc.channels[channel].ts = ts
|
|
|
|
irc.channels[channel].modes.clear()
|
|
|
|
for p in irc.channels[channel].prefixmodes.values():
|
|
|
|
p.clear()
|
|
|
|
log.debug("sending SJOIN to %s%s with ts %s (that's %r)", channel, irc.name, ts,
|
2015-07-14 21:03:22 +02:00
|
|
|
time.strftime("%c", time.localtime(ts)))
|
2015-09-05 23:25:11 +02:00
|
|
|
# Strip out list-modes, they shouldn't ever be sent in FJOIN (protocol rules).
|
2015-08-12 16:05:05 +02:00
|
|
|
modes = [m for m in irc.channels[channel].modes if m[0] not in irc.cmodes['*A']]
|
2015-07-14 01:06:58 +02:00
|
|
|
uids = []
|
2015-07-15 03:20:10 +02:00
|
|
|
changedmodes = []
|
2015-07-14 01:06:58 +02:00
|
|
|
namelist = []
|
|
|
|
# We take <users> as a list of (prefixmodes, uid) pairs.
|
|
|
|
for userpair in users:
|
|
|
|
assert len(userpair) == 2, "Incorrect format of userpair: %r" % userpair
|
2015-07-15 03:20:10 +02:00
|
|
|
prefixes, user = userpair
|
2015-07-14 01:06:58 +02:00
|
|
|
namelist.append(','.join(userpair))
|
2015-07-15 03:20:10 +02:00
|
|
|
uids.append(user)
|
|
|
|
for m in prefixes:
|
|
|
|
changedmodes.append(('+%s' % m, user))
|
2015-07-20 08:45:52 +02:00
|
|
|
try:
|
|
|
|
irc.users[user].channels.add(channel)
|
|
|
|
except KeyError: # Not initialized yet?
|
|
|
|
log.debug("(%s) sjoinServer: KeyError trying to add %r to %r's channel list?", irc.name, channel, user)
|
2015-08-25 03:37:39 +02:00
|
|
|
if ts <= orig_ts:
|
|
|
|
# Only save our prefix modes in the channel state if our TS is lower than or equal to theirs.
|
2015-08-20 10:32:30 +02:00
|
|
|
utils.applyModes(irc, channel, changedmodes)
|
2015-07-14 01:06:58 +02:00
|
|
|
namelist = ' '.join(namelist)
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, server, "FJOIN {channel} {ts} {modes} :{users}".format(
|
2015-07-14 01:06:58 +02:00
|
|
|
ts=ts, users=namelist, channel=channel,
|
|
|
|
modes=utils.joinModes(modes)))
|
|
|
|
irc.channels[channel].users.update(uids)
|
|
|
|
|
2015-08-31 19:31:50 +02:00
|
|
|
def _operUp(irc, target, opertype=None):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Opers a client up (internal function specific to InspIRCd).
|
|
|
|
|
|
|
|
This should be called whenever user mode +o is set on anyone, because
|
|
|
|
InspIRCd requires a special command (OPERTYPE) to be sent in order to
|
|
|
|
recognize ANY non-burst oper ups.
|
|
|
|
|
|
|
|
Plugins don't have to call this function themselves, but they can
|
|
|
|
set the opertype attribute of an IrcUser object (in irc.users),
|
|
|
|
and the change will be reflected here."""
|
2015-08-31 19:31:50 +02:00
|
|
|
userobj = irc.users[target]
|
|
|
|
try:
|
|
|
|
otype = opertype or userobj.opertype
|
|
|
|
except AttributeError:
|
|
|
|
log.debug('(%s) opertype field for %s (%s) isn\'t filled yet!',
|
|
|
|
irc.name, target, userobj.nick)
|
|
|
|
# whatever, this is non-standard anyways.
|
|
|
|
otype = 'IRC_Operator'
|
|
|
|
log.debug('(%s) Sending OPERTYPE from %s to oper them up.',
|
|
|
|
irc.name, target)
|
2015-08-31 23:52:56 +02:00
|
|
|
userobj.opertype = otype
|
2015-08-31 19:31:50 +02:00
|
|
|
_send(irc, target, 'OPERTYPE %s' % otype)
|
|
|
|
|
2015-07-14 07:42:33 +02:00
|
|
|
def _sendModes(irc, numeric, target, modes, ts=None):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Internal function to send modes from a PyLink client/server."""
|
2015-07-09 03:23:32 +02:00
|
|
|
# -> :9PYAAAAAA FMODE #pylink 1433653951 +os 9PYAAAAAA
|
|
|
|
# -> :9PYAAAAAA MODE 9PYAAAAAA -i+w
|
2015-08-31 19:31:50 +02:00
|
|
|
log.debug('(%s) inspircd._sendModes: received %r for mode list', irc.name, modes)
|
|
|
|
if ('+o', None) in modes and not utils.isChannel(target):
|
|
|
|
# https://github.com/inspircd/inspircd/blob/master/src/modules/m_spanningtree/opertype.cpp#L26-L28
|
|
|
|
# Servers need a special command to set umode +o on people.
|
|
|
|
# Why isn't this documented anywhere, InspIRCd?
|
|
|
|
_operUp(irc, target)
|
2015-07-09 03:23:32 +02:00
|
|
|
utils.applyModes(irc, target, modes)
|
2015-08-31 19:31:50 +02:00
|
|
|
joinedmodes = utils.joinModes(modes)
|
2015-07-09 03:23:32 +02:00
|
|
|
if utils.isChannel(target):
|
2015-09-02 07:01:22 +02:00
|
|
|
ts = ts or irc.channels[utils.toLower(irc, target)].ts
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'FMODE %s %s %s' % (target, ts, joinedmodes))
|
2015-07-09 03:23:32 +02:00
|
|
|
else:
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'MODE %s %s' % (target, joinedmodes))
|
2015-07-09 03:23:32 +02:00
|
|
|
|
2015-07-14 07:42:33 +02:00
|
|
|
def modeClient(irc, numeric, target, modes, ts=None):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""
|
|
|
|
Sends modes from a PyLink client. <modes> should be
|
|
|
|
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
|
2015-07-09 03:23:32 +02:00
|
|
|
"""
|
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-14 07:42:33 +02:00
|
|
|
_sendModes(irc, numeric, target, modes, ts=ts)
|
2015-07-09 03:23:32 +02:00
|
|
|
|
2015-07-14 07:42:33 +02:00
|
|
|
def modeServer(irc, numeric, target, modes, ts=None):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""
|
|
|
|
Sends modes from a PyLink server. <list of modes> should be
|
|
|
|
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
|
2015-07-09 03:23:32 +02:00
|
|
|
"""
|
|
|
|
if not utils.isInternalServer(irc, numeric):
|
2015-07-10 04:05:37 +02:00
|
|
|
raise LookupError('No such PyLink PseudoServer exists.')
|
2015-07-14 07:42:33 +02:00
|
|
|
_sendModes(irc, numeric, target, modes, ts=ts)
|
2015-07-09 03:23:32 +02:00
|
|
|
|
2015-07-10 04:05:37 +02:00
|
|
|
def killServer(irc, numeric, target, reason):
|
|
|
|
"""<irc object> <server SID> <target> <reason>
|
|
|
|
|
|
|
|
Sends a kill to <target> from a PyLink PseudoServer.
|
|
|
|
"""
|
|
|
|
if not utils.isInternalServer(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoServer exists.')
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'KILL %s :%s' % (target, reason))
|
2015-07-10 04:05:37 +02:00
|
|
|
# We don't need to call removeClient here, since the remote server
|
|
|
|
# will send a QUIT from the target if the command succeeds.
|
|
|
|
|
|
|
|
def killClient(irc, numeric, target, reason):
|
|
|
|
"""<irc object> <client numeric> <target> <reason>
|
|
|
|
|
|
|
|
Sends a kill to <target> from a PyLink PseudoClient.
|
|
|
|
"""
|
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'KILL %s :%s' % (target, reason))
|
2015-07-10 04:05:37 +02:00
|
|
|
# We don't need to call removeClient here, since the remote server
|
|
|
|
# will send a QUIT from the target if the command succeeds.
|
|
|
|
|
2015-07-15 08:23:35 +02:00
|
|
|
def topicServer(irc, numeric, target, text):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Sends a burst topic from a PyLink server. This is usally used on burst."""
|
2015-07-15 08:23:35 +02:00
|
|
|
if not utils.isInternalServer(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoServer exists.')
|
2015-07-20 02:27:53 +02:00
|
|
|
ts = int(time.time())
|
|
|
|
servername = irc.servers[numeric].name
|
|
|
|
_send(irc, numeric, 'FTOPIC %s %s %s :%s' % (target, ts, servername, text))
|
2015-08-16 07:34:40 +02:00
|
|
|
irc.channels[target].topic = text
|
|
|
|
irc.channels[target].topicset = True
|
2015-07-10 01:32:29 +02:00
|
|
|
|
|
|
|
def inviteClient(irc, numeric, target, channel):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Sends an INVITE from a PyLink client.."""
|
2015-07-10 01:32:29 +02:00
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'INVITE %s %s' % (target, channel))
|
2015-07-10 01:32:29 +02:00
|
|
|
|
|
|
|
def knockClient(irc, numeric, target, text):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Sends a KNOCK from a PyLink client."""
|
2015-07-10 01:32:29 +02:00
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'ENCAP * KNOCK %s :%s' % (target, text))
|
2015-07-10 01:32:29 +02:00
|
|
|
|
2015-07-10 02:04:24 +02:00
|
|
|
def updateClient(irc, numeric, field, text):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Updates the ident, host, or realname of a PyLink client."""
|
2015-07-10 02:04:24 +02:00
|
|
|
field = field.upper()
|
|
|
|
if field == 'IDENT':
|
2015-07-23 20:01:12 +02:00
|
|
|
irc.users[numeric].ident = text
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'FIDENT %s' % text)
|
2015-07-10 02:04:24 +02:00
|
|
|
elif field == 'HOST':
|
2015-07-23 20:01:12 +02:00
|
|
|
irc.users[numeric].host = text
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'FHOST %s' % text)
|
2015-07-10 02:04:24 +02:00
|
|
|
elif field in ('REALNAME', 'GECOS'):
|
2015-07-23 20:01:12 +02:00
|
|
|
irc.users[numeric].realname = text
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'FNAME :%s' % text)
|
2015-07-10 02:04:24 +02:00
|
|
|
else:
|
2015-07-23 20:01:12 +02:00
|
|
|
raise NotImplementedError("Changing field %r of a client is unsupported by this protocol." % field)
|
2015-07-10 01:32:29 +02:00
|
|
|
|
2015-07-17 22:39:57 +02:00
|
|
|
def pingServer(irc, source=None, target=None):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Sends a PING to a target server. Periodic PINGs are sent to our uplink
|
|
|
|
automatically by the Irc() internals; plugins shouldn't have to use this."""
|
2015-07-17 22:39:57 +02:00
|
|
|
source = source or irc.sid
|
|
|
|
target = target or irc.uplink
|
|
|
|
if not (target is None or source is None):
|
|
|
|
_send(irc, source, 'PING %s %s' % (source, target))
|
|
|
|
|
2015-07-23 06:15:34 +02:00
|
|
|
def numericServer(irc, source, numeric, text):
|
|
|
|
raise NotImplementedError("Numeric sending is not yet implemented by this "
|
|
|
|
"protocol module. WHOIS requests are handled "
|
|
|
|
"locally by InspIRCd servers, so there is no "
|
|
|
|
"need for PyLink to send numerics directly yet.")
|
|
|
|
|
2015-08-12 13:17:01 +02:00
|
|
|
def awayClient(irc, source, text):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Sends an AWAY message from a PyLink client. <text> can be an empty string
|
|
|
|
to unset AWAY status."""
|
2015-08-12 13:17:01 +02:00
|
|
|
if text:
|
|
|
|
_send(irc, source, 'AWAY %s :%s' % (int(time.time()), text))
|
|
|
|
else:
|
|
|
|
_send(irc, source, 'AWAY')
|
|
|
|
|
2015-09-05 23:25:11 +02:00
|
|
|
def spawnServer(irc, name, sid=None, uplink=None, desc=None):
|
|
|
|
"""Spawns a server off a PyLink server."""
|
|
|
|
# -> :0AL SERVER test.server * 1 0AM :some silly pseudoserver
|
|
|
|
uplink = uplink or irc.sid
|
|
|
|
name = name.lower()
|
|
|
|
# "desc" defaults to the configured server description.
|
|
|
|
desc = desc or irc.serverdata.get('serverdesc') or irc.botdata['serverdesc']
|
|
|
|
if sid is None: # No sid given; generate one!
|
|
|
|
irc.sidgen = utils.TS6SIDGenerator(irc.serverdata["sidrange"])
|
|
|
|
sid = irc.sidgen.next_sid()
|
|
|
|
assert len(sid) == 3, "Incorrect SID length"
|
|
|
|
if sid in irc.servers:
|
|
|
|
raise ValueError('A server with SID %r already exists!' % sid)
|
|
|
|
for server in irc.servers.values():
|
|
|
|
if name == server.name:
|
|
|
|
raise ValueError('A server named %r already exists!' % name)
|
|
|
|
if not utils.isInternalServer(irc, uplink):
|
|
|
|
raise ValueError('Server %r is not a PyLink internal PseudoServer!' % uplink)
|
|
|
|
if not utils.isServerName(name):
|
|
|
|
raise ValueError('Invalid server name %r' % name)
|
|
|
|
_send(irc, uplink, 'SERVER %s * 1 %s :%s' % (name, sid, desc))
|
|
|
|
irc.servers[sid] = IrcServer(uplink, name, internal=True)
|
|
|
|
_send(irc, sid, 'ENDBURST')
|
|
|
|
return sid
|
|
|
|
|
|
|
|
def squitServer(irc, source, target, text='No reason given'):
|
|
|
|
"""SQUITs a PyLink server."""
|
|
|
|
# -> :9PY SQUIT 9PZ :blah, blah
|
|
|
|
_send(irc, source, 'SQUIT %s :%s' % (target, text))
|
|
|
|
handle_squit(irc, source, 'SQUIT', [target, text])
|
|
|
|
|
2015-04-04 03:45:18 +02:00
|
|
|
def connect(irc):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Initializes a connection to a server."""
|
2015-07-18 07:52:55 +02:00
|
|
|
ts = irc.start_ts
|
2015-04-25 07:37:07 +02:00
|
|
|
|
2015-04-03 09:17:03 +02:00
|
|
|
f = irc.send
|
2015-07-05 08:12:00 +02:00
|
|
|
f('CAPAB START 1202')
|
|
|
|
f('CAPAB CAPABILITIES :PROTOCOL=1202')
|
2015-04-03 09:17:03 +02:00
|
|
|
f('CAPAB END')
|
2015-08-25 03:14:35 +02:00
|
|
|
f('SERVER {host} {Pass} 0 {sid} :{sdesc}'.format(host=irc.serverdata["hostname"],
|
|
|
|
Pass=irc.serverdata["sendpass"], sid=irc.sid,
|
|
|
|
sdesc=irc.serverdata.get('serverdesc') or irc.botdata['serverdesc']))
|
2015-04-04 03:45:18 +02:00
|
|
|
f(':%s BURST %s' % (irc.sid, ts))
|
2015-04-03 09:17:03 +02:00
|
|
|
f(':%s ENDBURST' % (irc.sid))
|
2015-03-20 00:21:49 +01:00
|
|
|
|
2015-04-03 09:17:03 +02:00
|
|
|
def handle_events(irc, data):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Event handler for the InspIRCd protocol. This passes most commands to
|
|
|
|
the various handle_ABCD() functions elsewhere in this module, but also
|
|
|
|
handles commands sent in the initial server linking phase."""
|
2015-04-18 07:11:49 +02:00
|
|
|
# Each server message looks something like this:
|
|
|
|
# :70M FJOIN #chat 1423790411 +AFPfjnt 6:5 7:5 9:5 :v,1SRAAESWE
|
|
|
|
# :<sid> <command> <argument1> <argument2> ... :final multi word argument
|
2015-07-24 04:09:19 +02:00
|
|
|
args = data.split(" ")
|
2015-07-05 08:12:00 +02:00
|
|
|
if not args:
|
|
|
|
# No data??
|
|
|
|
return
|
|
|
|
if args[0] == 'SERVER':
|
2015-07-06 07:59:55 +02:00
|
|
|
# <- SERVER whatever.net abcdefgh 0 10X :something
|
2015-07-04 20:57:21 +02:00
|
|
|
servername = args[1].lower()
|
2015-06-22 00:00:33 +02:00
|
|
|
numeric = args[4]
|
2015-06-03 01:55:04 +02:00
|
|
|
if args[2] != irc.serverdata['recvpass']:
|
|
|
|
# Check if recvpass is correct
|
2015-07-04 03:07:01 +02:00
|
|
|
raise ProtocolError('Error: recvpass from uplink server %s does not match configuration!' % servername)
|
2015-06-22 00:00:33 +02:00
|
|
|
irc.servers[numeric] = IrcServer(None, servername)
|
2015-07-17 22:39:57 +02:00
|
|
|
irc.uplink = numeric
|
2015-06-03 01:55:04 +02:00
|
|
|
return
|
2015-07-05 08:12:00 +02:00
|
|
|
elif args[0] == 'CAPAB':
|
|
|
|
# Capability negotiation with our uplink
|
|
|
|
if args[1] == 'CHANMODES':
|
2015-07-06 07:59:55 +02:00
|
|
|
# <- CAPAB CHANMODES :admin=&a allowinvite=A autoop=w ban=b banexception=e blockcolor=c c_registered=r exemptchanops=X filter=g flood=f halfop=%h history=H invex=I inviteonly=i joinflood=j key=k kicknorejoin=J limit=l moderated=m nickflood=F noctcp=C noextmsg=n nokick=Q noknock=K nonick=N nonotice=T official-join=!Y op=@o operonly=O opmoderated=U owner=~q permanent=P private=p redirect=L reginvite=R regmoderated=M secret=s sslonly=z stripcolor=S topiclock=t voice=+v
|
2015-07-05 08:12:00 +02:00
|
|
|
|
|
|
|
# Named modes are essential for a cross-protocol IRC service. We
|
2015-08-31 08:16:39 +02:00
|
|
|
# can use InspIRCd as a model here and assign a similar mode map to our cmodes list.
|
2015-07-05 08:12:00 +02:00
|
|
|
for modepair in args[2:]:
|
|
|
|
name, char = modepair.split('=')
|
2015-07-20 00:56:04 +02:00
|
|
|
if name == 'reginvite': # Reginvite? That's a dumb name.
|
|
|
|
name = 'regonly'
|
2015-08-31 08:16:39 +02:00
|
|
|
if name == 'founder': # Channel mode +q
|
|
|
|
# Founder, owner; same thing. m_customprefix allows you to name it anything you like
|
|
|
|
# (the former is config default, but I personally prefer the latter.)
|
|
|
|
name = 'owner'
|
2015-07-05 08:12:00 +02:00
|
|
|
# We don't really care about mode prefixes; just the mode char
|
|
|
|
irc.cmodes[name.lstrip(':')] = char[-1]
|
|
|
|
elif args[1] == 'USERMODES':
|
2015-07-20 00:20:23 +02:00
|
|
|
# <- CAPAB USERMODES :bot=B callerid=g cloak=x deaf_commonchan=c helpop=h hidechans=I hideoper=H invisible=i oper=o regdeaf=R servprotect=k showwhois=W snomask=s u_registered=r u_stripcolor=S wallops=w
|
2015-07-05 08:12:00 +02:00
|
|
|
# Ditto above.
|
|
|
|
for modepair in args[2:]:
|
|
|
|
name, char = modepair.split('=')
|
|
|
|
irc.umodes[name.lstrip(':')] = char
|
|
|
|
elif args[1] == 'CAPABILITIES':
|
2015-07-06 07:59:55 +02:00
|
|
|
# <- CAPAB CAPABILITIES :NICKMAX=21 CHANMAX=64 MAXMODES=20 IDENTMAX=11 MAXQUIT=255 MAXTOPIC=307 MAXKICK=255 MAXGECOS=128 MAXAWAY=200 IP6SUPPORT=1 PROTOCOL=1202 PREFIX=(Yqaohv)!~&@%+ CHANMODES=IXbegw,k,FHJLfjl,ACKMNOPQRSTUcimnprstz USERMODES=,,s,BHIRSWcghikorwx GLOBOPS=1 SVSPART=1
|
2015-07-05 08:12:00 +02:00
|
|
|
caps = dict([x.lstrip(':').split('=') for x in args[2:]])
|
2015-07-07 19:55:14 +02:00
|
|
|
protocol_version = int(caps['PROTOCOL'])
|
|
|
|
if protocol_version < 1202:
|
|
|
|
raise ProtocolError("Remote protocol version is too old! At least 1202 (InspIRCd 2.0.x) is needed. (got %s)" % protocol_version)
|
2015-07-12 23:00:52 +02:00
|
|
|
irc.maxnicklen = int(caps['NICKMAX'])
|
|
|
|
irc.maxchanlen = int(caps['CHANMAX'])
|
2015-07-05 08:12:00 +02:00
|
|
|
# Modes are divided into A, B, C, and D classes
|
|
|
|
# See http://www.irc.org/tech_docs/005.html
|
2015-07-20 00:20:23 +02:00
|
|
|
|
2015-07-05 08:12:00 +02:00
|
|
|
# FIXME: Find a better way to assign/store this.
|
|
|
|
irc.cmodes['*A'], irc.cmodes['*B'], irc.cmodes['*C'], irc.cmodes['*D'] \
|
|
|
|
= caps['CHANMODES'].split(',')
|
|
|
|
irc.umodes['*A'], irc.umodes['*B'], irc.umodes['*C'], irc.umodes['*D'] \
|
|
|
|
= caps['USERMODES'].split(',')
|
2015-07-21 01:48:59 +02:00
|
|
|
prefixsearch = re.search(r'\(([A-Za-z]+)\)(.*)', caps['PREFIX'])
|
|
|
|
irc.prefixmodes = dict(zip(prefixsearch.group(1), prefixsearch.group(2)))
|
|
|
|
log.debug('(%s) irc.prefixmodes set to %r', irc.name, irc.prefixmodes)
|
2015-07-15 22:49:12 +02:00
|
|
|
# Sanity check: set this AFTER we fetch the capabilities for the network!
|
|
|
|
irc.connected.set()
|
2015-04-03 21:35:55 +02:00
|
|
|
try:
|
2015-09-04 20:24:40 +02:00
|
|
|
args = parseTS6Args(args)
|
2015-04-03 21:35:55 +02:00
|
|
|
numeric = args[0]
|
|
|
|
command = args[1]
|
2015-04-25 07:37:07 +02:00
|
|
|
args = args[2:]
|
2015-04-03 21:35:55 +02:00
|
|
|
except IndexError:
|
|
|
|
return
|
|
|
|
|
2015-06-24 04:08:43 +02:00
|
|
|
# We will do wildcard event handling here. Unhandled events are just ignored.
|
2015-04-03 21:35:55 +02:00
|
|
|
try:
|
2015-07-05 04:00:29 +02:00
|
|
|
func = globals()['handle_'+command.lower()]
|
2015-04-03 21:35:55 +02:00
|
|
|
except KeyError: # unhandled event
|
|
|
|
pass
|
2015-07-05 21:38:55 +02:00
|
|
|
else:
|
|
|
|
parsed_args = func(irc, numeric, command, args)
|
2015-07-13 08:28:34 +02:00
|
|
|
if parsed_args is not None:
|
2015-07-18 01:09:50 +02:00
|
|
|
return [numeric, command, parsed_args]
|
2015-06-22 00:00:33 +02:00
|
|
|
|
2015-09-05 23:25:11 +02:00
|
|
|
def handle_ping(irc, source, command, args):
|
|
|
|
"""Handles incoming PING commands, so we don't time out."""
|
|
|
|
# <- :70M PING 70M 0AL
|
|
|
|
# -> :0AL PONG 0AL 70M
|
|
|
|
if utils.isInternalServer(irc, args[1]):
|
|
|
|
_send(irc, args[1], 'PONG %s %s' % (args[1], source))
|
2015-07-07 00:33:23 +02:00
|
|
|
|
2015-09-05 23:25:11 +02:00
|
|
|
def handle_pong(irc, source, command, args):
|
|
|
|
"""Handles incoming PONG commands. This is used to keep track of whether
|
|
|
|
the uplink is alive by the Irc() internals - a server that fails to reply
|
|
|
|
to our PINGs eventually times out and is disconnected."""
|
|
|
|
if source == irc.uplink and args[1] == irc.sid:
|
|
|
|
irc.lastping = time.time()
|
|
|
|
|
|
|
|
def handle_fjoin(irc, servernumeric, command, args):
|
|
|
|
"""Handles incoming FJOIN commands (InspIRCd equivalent of JOIN/SJOIN)."""
|
|
|
|
# :70M FJOIN #chat 1423790411 +AFPfjnt 6:5 7:5 9:5 :o,1SRAABIT4 v,1IOAAF53R <...>
|
|
|
|
channel = utils.toLower(irc, args[0])
|
2015-09-05 23:28:18 +02:00
|
|
|
# InspIRCd sends each channel's users in the form of 'modeprefix(es),UID'
|
2015-09-05 23:25:11 +02:00
|
|
|
userlist = args[-1].split()
|
|
|
|
our_ts = irc.channels[channel].ts
|
|
|
|
their_ts = int(args[1])
|
|
|
|
if their_ts < our_ts:
|
|
|
|
# Channel timestamp was reset on burst
|
|
|
|
log.debug('(%s) Setting channel TS of %s to %s from %s',
|
|
|
|
irc.name, channel, their_ts, our_ts)
|
|
|
|
irc.channels[channel].ts = their_ts
|
|
|
|
irc.channels[channel].modes.clear()
|
|
|
|
for p in irc.channels[channel].prefixmodes.values():
|
|
|
|
p.clear()
|
|
|
|
modestring = args[2:-1] or args[2]
|
|
|
|
parsedmodes = utils.parseModes(irc, channel, modestring)
|
|
|
|
utils.applyModes(irc, channel, parsedmodes)
|
|
|
|
namelist = []
|
|
|
|
for user in userlist:
|
|
|
|
modeprefix, user = user.split(',', 1)
|
|
|
|
namelist.append(user)
|
|
|
|
irc.users[user].channels.add(channel)
|
|
|
|
if their_ts <= our_ts:
|
|
|
|
utils.applyModes(irc, channel, [('+%s' % mode, user) for mode in modeprefix])
|
|
|
|
irc.channels[channel].users.add(user)
|
|
|
|
return {'channel': channel, 'users': namelist, 'modes': parsedmodes, 'ts': their_ts}
|
|
|
|
|
|
|
|
def handle_uid(irc, numeric, command, args):
|
|
|
|
"""Handles incoming UID commands (user introduction)."""
|
|
|
|
# :70M UID 70MAAAAAB 1429934638 GL 0::1 hidden-7j810p.9mdf.lrek.0000.0000.IP gl 0::1 1429934638 +Wioswx +ACGKNOQXacfgklnoqvx :realname
|
|
|
|
uid, ts, nick, realhost, host, ident, ip = args[0:7]
|
|
|
|
realname = args[-1]
|
|
|
|
irc.users[uid] = IrcUser(nick, ts, uid, ident, host, realname, realhost, ip)
|
|
|
|
parsedmodes = utils.parseModes(irc, uid, [args[8], args[9]])
|
|
|
|
log.debug('Applying modes %s for %s', parsedmodes, uid)
|
|
|
|
utils.applyModes(irc, uid, parsedmodes)
|
|
|
|
irc.servers[numeric].users.add(uid)
|
|
|
|
return {'uid': uid, 'ts': ts, 'nick': nick, 'realhost': realhost, 'host': host, 'ident': ident, 'ip': ip}
|
|
|
|
|
|
|
|
def handle_server(irc, numeric, command, args):
|
|
|
|
"""Handles incoming SERVER commands (introduction of servers)."""
|
|
|
|
# SERVER is sent by our uplink or any other server to introduce others.
|
|
|
|
# <- :00A SERVER test.server * 1 00C :testing raw message syntax
|
|
|
|
# <- :70M SERVER millennium.overdrive.pw * 1 1ML :a relatively long period of time... (Fremont, California)
|
|
|
|
servername = args[0].lower()
|
|
|
|
sid = args[3]
|
|
|
|
sdesc = args[-1]
|
|
|
|
irc.servers[sid] = IrcServer(numeric, servername)
|
|
|
|
return {'name': servername, 'sid': args[3], 'text': sdesc}
|
|
|
|
|
|
|
|
def handle_fmode(irc, numeric, command, args):
|
|
|
|
"""Handles the FMODE command, used for channel mode changes."""
|
|
|
|
# <- :70MAAAAAA FMODE #chat 1433653462 +hhT 70MAAAAAA 70MAAAAAD
|
|
|
|
channel = utils.toLower(irc, args[0])
|
|
|
|
modes = args[2:]
|
|
|
|
changedmodes = utils.parseModes(irc, channel, modes)
|
|
|
|
utils.applyModes(irc, channel, changedmodes)
|
|
|
|
ts = int(args[1])
|
|
|
|
return {'target': channel, 'modes': changedmodes, 'ts': ts}
|
|
|
|
|
|
|
|
def handle_idle(irc, numeric, command, args):
|
|
|
|
"""Handles the IDLE command, sent between servers in remote WHOIS queries."""
|
|
|
|
# <- :70MAAAAAA IDLE 1MLAAAAIG
|
|
|
|
# -> :1MLAAAAIG IDLE 70MAAAAAA 1433036797 319
|
|
|
|
sourceuser = numeric
|
|
|
|
targetuser = args[0]
|
|
|
|
_send(irc, targetuser, 'IDLE %s %s 0' % (sourceuser, irc.users[targetuser].ts))
|
2015-09-05 03:55:39 +02:00
|
|
|
|
2015-07-07 00:33:23 +02:00
|
|
|
def handle_ftopic(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles incoming FTOPIC (sets topic on burst)."""
|
2015-07-07 00:33:23 +02:00
|
|
|
# <- :70M FTOPIC #channel 1434510754 GLo|o|!GLolol@escape.the.dreamland.ca :Some channel topic
|
2015-09-02 07:01:22 +02:00
|
|
|
channel = utils.toLower(irc, args[0])
|
2015-07-07 00:33:23 +02:00
|
|
|
ts = args[1]
|
|
|
|
setter = args[2]
|
|
|
|
topic = args[-1]
|
|
|
|
irc.channels[channel].topic = topic
|
2015-07-20 22:18:04 +02:00
|
|
|
irc.channels[channel].topicset = True
|
2015-07-07 00:33:23 +02:00
|
|
|
return {'channel': channel, 'setter': setter, 'ts': ts, 'topic': topic}
|
|
|
|
|
2015-07-08 00:31:12 +02:00
|
|
|
def handle_invite(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles incoming INVITEs."""
|
2015-07-08 00:31:12 +02:00
|
|
|
# <- :70MAAAAAC INVITE 0ALAAAAAA #blah 0
|
|
|
|
target = args[0]
|
2015-09-02 07:01:22 +02:00
|
|
|
channel = utils.toLower(irc, args[1])
|
2015-09-05 23:25:11 +02:00
|
|
|
# We don't actually need to process this; just send the hook so plugins can use it
|
2015-07-08 00:31:12 +02:00
|
|
|
return {'target': target, 'channel': channel}
|
|
|
|
|
|
|
|
def handle_encap(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles incoming encapsulated commands (ENCAP). Hook arguments
|
|
|
|
returned by this should have a parse_as field, that sets the correct
|
|
|
|
hook name for the message.
|
|
|
|
|
|
|
|
For InspIRCd, the only ENCAP command we handle right now is KNOCK."""
|
2015-07-08 00:31:12 +02:00
|
|
|
# <- :70MAAAAAA ENCAP * KNOCK #blah :agsdfas
|
|
|
|
# From charybdis TS6 docs: https://github.com/grawity/irc-docs/blob/03ba884a54f1cef2193cd62b6a86803d89c1ac41/server/ts6.txt
|
|
|
|
|
|
|
|
# ENCAP
|
|
|
|
# source: any
|
|
|
|
# parameters: target server mask, subcommand, opt. parameters...
|
|
|
|
|
|
|
|
# Sends a command to matching servers. Propagation is independent of
|
|
|
|
# understanding the subcommand.
|
|
|
|
|
|
|
|
targetmask = args[0]
|
|
|
|
real_command = args[1]
|
|
|
|
if targetmask == '*' and real_command == 'KNOCK':
|
2015-09-02 07:01:22 +02:00
|
|
|
channel = utils.toLower(irc, args[2])
|
2015-07-08 00:31:12 +02:00
|
|
|
text = args[3]
|
2015-08-03 14:00:32 +02:00
|
|
|
return {'parse_as': real_command, 'channel': channel,
|
2015-07-08 00:31:12 +02:00
|
|
|
'text': text}
|
|
|
|
|
2015-07-09 03:03:08 +02:00
|
|
|
def handle_opertype(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles incoming OPERTYPE, which is used to denote an oper up.
|
|
|
|
|
|
|
|
This calls the internal hook PYLINK_CLIENT_OPERED, sets the internal
|
|
|
|
opertype of the client, and assumes setting user mode +o on the caller."""
|
2015-07-09 03:03:08 +02:00
|
|
|
# This is used by InspIRCd to denote an oper up; there is no MODE
|
|
|
|
# command sent for it.
|
|
|
|
# <- :70MAAAAAB OPERTYPE Network_Owner
|
|
|
|
omode = [('+o', None)]
|
2015-08-31 19:31:50 +02:00
|
|
|
irc.users[numeric].opertype = opertype = args[0]
|
2015-07-09 03:03:08 +02:00
|
|
|
utils.applyModes(irc, numeric, omode)
|
2015-08-31 22:31:20 +02:00
|
|
|
# OPERTYPE is essentially umode +o and metadata in one command;
|
|
|
|
# we'll call that too.
|
|
|
|
irc.callHooks([numeric, 'PYLINK_CLIENT_OPERED', {'text': opertype}])
|
|
|
|
return {'target': numeric, 'modes': omode}
|
2015-07-10 02:04:24 +02:00
|
|
|
|
|
|
|
def handle_fident(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles FIDENT, used for denoting ident changes."""
|
|
|
|
# <- :70MAAAAAB FIDENT test
|
2015-07-10 02:04:24 +02:00
|
|
|
irc.users[numeric].ident = newident = args[0]
|
|
|
|
return {'target': numeric, 'newident': newident}
|
|
|
|
|
|
|
|
def handle_fhost(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles FHOST, used for denoting hostname changes."""
|
|
|
|
# <- :70MAAAAAB FIDENT some.host
|
2015-07-10 02:04:24 +02:00
|
|
|
irc.users[numeric].host = newhost = args[0]
|
|
|
|
return {'target': numeric, 'newhost': newhost}
|
|
|
|
|
|
|
|
def handle_fname(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles FNAME, used for denoting real name/gecos changes."""
|
|
|
|
# <- :70MAAAAAB FNAME :afdsafasf
|
2015-07-10 02:04:24 +02:00
|
|
|
irc.users[numeric].realname = newgecos = args[0]
|
|
|
|
return {'target': numeric, 'newgecos': newgecos}
|
2015-07-13 08:28:34 +02:00
|
|
|
|
|
|
|
def handle_endburst(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""ENDBURST handler; sends a hook with empty contents."""
|
2015-07-13 08:28:34 +02:00
|
|
|
return {}
|
2015-08-12 13:17:01 +02:00
|
|
|
|
|
|
|
def handle_away(irc, numeric, command, args):
|
2015-09-05 23:25:11 +02:00
|
|
|
"""Handles incoming AWAY messages."""
|
2015-08-12 13:17:01 +02:00
|
|
|
# <- :1MLAAAAIG AWAY 1439371390 :Auto-away
|
|
|
|
try:
|
|
|
|
ts = args[0]
|
|
|
|
irc.users[numeric].away = text = args[1]
|
|
|
|
return {'text': text, 'ts': ts}
|
|
|
|
except IndexError: # User is unsetting away status
|
|
|
|
irc.users[numeric].away = ''
|
|
|
|
return {'text': ''}
|