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-06-17 05:05:41 +02:00
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
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-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
|
|
|
|
# non-standard names to our hook handlers, so plugins get the information they need.
|
2015-07-08 00:31:12 +02:00
|
|
|
|
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-15 07:33:56 +02:00
|
|
|
def _send(irc, sid, msg):
|
2015-06-22 00:00:33 +02:00
|
|
|
irc.send(':%s %s' % (sid, msg))
|
2015-04-18 07:11:49 +02:00
|
|
|
|
2015-07-08 02:46:56 +02:00
|
|
|
def spawnClient(irc, nick, ident='null', host='null', realhost=None, modes=set(),
|
2015-07-23 09:11:24 +02:00
|
|
|
server=None, ip='0.0.0.0', realname=None, ts=None):
|
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-07-18 00:08:24 +02:00
|
|
|
irc.servers[server].users.append(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-06-07 07:17:45 +02:00
|
|
|
return u
|
|
|
|
|
|
|
|
def joinClient(irc, client, 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,
|
|
|
|
# on behalf of the clients that call it.
|
2015-07-04 08:30:38 +02:00
|
|
|
channel = channel.lower()
|
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-07-14 01:06:58 +02:00
|
|
|
channel = channel.lower()
|
|
|
|
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-08-12 16:05:05 +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-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-06-08 04:31:56 +02:00
|
|
|
def partClient(irc, client, channel, reason=None):
|
2015-07-04 08:30:38 +02:00
|
|
|
channel = channel.lower()
|
2015-06-24 04:29:53 +02:00
|
|
|
if not utils.isInternalClient(irc, client):
|
2015-07-14 08:29:20 +02:00
|
|
|
log.error('(%s) Error trying to part 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.')
|
|
|
|
msg = "PART %s" % channel
|
|
|
|
if reason:
|
|
|
|
msg += " :%s" % reason
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, client, msg)
|
2015-07-04 08:30:38 +02:00
|
|
|
handle_part(irc, client, 'PART', [channel])
|
2015-05-31 08:31:22 +02:00
|
|
|
|
2015-06-07 08:04:11 +02:00
|
|
|
def removeClient(irc, numeric):
|
|
|
|
"""<irc object> <client numeric>
|
2015-06-08 03:08:25 +02:00
|
|
|
|
2015-06-07 08:04:11 +02:00
|
|
|
Removes a client from our internal databases, regardless
|
|
|
|
of whether it's one of our pseudoclients or not."""
|
2015-08-16 08:29:40 +02:00
|
|
|
for c, v in irc.channels.copy().items():
|
2015-07-05 22:07:18 +02:00
|
|
|
v.removeuser(numeric)
|
2015-08-16 08:29:40 +02:00
|
|
|
# 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]
|
|
|
|
|
2015-06-07 08:04:11 +02:00
|
|
|
sid = numeric[:3]
|
2015-07-05 22:29:01 +02:00
|
|
|
log.debug('Removing client %s from irc.users', numeric)
|
2015-06-07 08:04:11 +02:00
|
|
|
del irc.users[numeric]
|
2015-07-05 22:29:01 +02:00
|
|
|
log.debug('Removing client %s from irc.servers[%s]', numeric, sid)
|
2015-06-07 08:04:11 +02:00
|
|
|
irc.servers[sid].users.remove(numeric)
|
|
|
|
|
2015-06-08 04:31:56 +02:00
|
|
|
def quitClient(irc, numeric, reason):
|
2015-06-08 03:15:36 +02:00
|
|
|
"""<irc object> <client numeric>
|
|
|
|
|
|
|
|
Quits a PyLink PseudoClient."""
|
2015-06-24 04:29:53 +02:00
|
|
|
if utils.isInternalClient(irc, numeric):
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, "QUIT :%s" % reason)
|
2015-06-08 04:31:56 +02:00
|
|
|
removeClient(irc, numeric)
|
2015-06-08 03:15:36 +02:00
|
|
|
else:
|
2015-06-08 04:31:56 +02:00
|
|
|
raise LookupError("No such PyLink PseudoClient exists. If you're trying to remove "
|
2015-06-08 03:15:36 +02:00
|
|
|
"a user that's not a PyLink PseudoClient from "
|
|
|
|
"the internal state, use removeClient() instead.")
|
|
|
|
|
2015-07-15 07:32:05 +02:00
|
|
|
def _sendKick(irc, numeric, channel, target, reason=None):
|
2015-06-08 03:15:36 +02:00
|
|
|
"""<irc object> <kicker client numeric>
|
|
|
|
|
|
|
|
Sends a kick from a PyLink PseudoClient."""
|
2015-07-04 08:30:38 +02:00
|
|
|
channel = channel.lower()
|
2015-06-08 04:31:56 +02:00
|
|
|
if not reason:
|
|
|
|
reason = 'No reason given'
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'KICK %s %s :%s' % (channel, target, reason))
|
2015-07-04 08:48:28 +02:00
|
|
|
# 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])
|
2015-06-08 03:15:36 +02:00
|
|
|
|
2015-07-15 07:32:05 +02:00
|
|
|
def kickClient(irc, numeric, channel, target, reason=None):
|
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
|
|
|
_sendKick(irc, numeric, channel, target, reason=reason)
|
|
|
|
|
|
|
|
def kickServer(irc, numeric, channel, target, reason=None):
|
|
|
|
if not utils.isInternalServer(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoServer exists.')
|
|
|
|
_sendKick(irc, numeric, channel, target, reason=reason)
|
|
|
|
|
2015-06-08 04:31:56 +02:00
|
|
|
def nickClient(irc, numeric, newnick):
|
2015-06-08 03:15:36 +02:00
|
|
|
"""<irc object> <client numeric> <new nickname>
|
|
|
|
|
|
|
|
Changes the nick of a PyLink PseudoClient."""
|
2015-06-24 04:29:53 +02:00
|
|
|
if not utils.isInternalClient(irc, numeric):
|
2015-06-08 04:31:56 +02:00
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'NICK %s %s' % (newnick, int(time.time())))
|
2015-06-08 04:31:56 +02:00
|
|
|
irc.users[numeric].nick = newnick
|
2015-06-08 03:15:36 +02:00
|
|
|
|
2015-07-14 07:42:33 +02:00
|
|
|
def _sendModes(irc, numeric, target, modes, ts=None):
|
2015-07-09 03:23:32 +02:00
|
|
|
# -> :9PYAAAAAA FMODE #pylink 1433653951 +os 9PYAAAAAA
|
|
|
|
# -> :9PYAAAAAA MODE 9PYAAAAAA -i+w
|
|
|
|
joinedmodes = utils.joinModes(modes)
|
|
|
|
utils.applyModes(irc, target, modes)
|
|
|
|
if utils.isChannel(target):
|
2015-07-14 07:42:33 +02:00
|
|
|
ts = ts or irc.channels[target.lower()].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-07-09 03:23:32 +02:00
|
|
|
"""<irc object> <client numeric> <list of modes>
|
|
|
|
|
|
|
|
Sends modes from a PyLink PseudoClient. <list of modes> should be
|
|
|
|
a list of (mode, arg) tuples, in the format of utils.parseModes() output.
|
|
|
|
"""
|
|
|
|
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-07-09 03:23:32 +02:00
|
|
|
"""<irc object> <server SID> <list of modes>
|
|
|
|
|
|
|
|
Sends modes from a PyLink PseudoServer. <list of modes> should be
|
|
|
|
a list of (mode, arg) tuples, in the format of utils.parseModes() output.
|
|
|
|
"""
|
|
|
|
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-10 01:23:20 +02:00
|
|
|
def messageClient(irc, numeric, target, text):
|
|
|
|
"""<irc object> <client numeric> <text>
|
|
|
|
|
|
|
|
Sends PRIVMSG <text> from PyLink client <client numeric>."""
|
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'PRIVMSG %s :%s' % (target, text))
|
2015-07-10 01:23:20 +02:00
|
|
|
|
|
|
|
def noticeClient(irc, numeric, target, text):
|
|
|
|
"""<irc object> <client numeric> <text>
|
|
|
|
|
|
|
|
Sends NOTICE <text> from PyLink client <client numeric>."""
|
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, numeric, 'NOTICE %s :%s' % (target, text))
|
2015-07-10 01:23:20 +02:00
|
|
|
|
2015-07-15 08:23:35 +02:00
|
|
|
def topicClient(irc, numeric, target, text):
|
2015-07-10 01:32:29 +02:00
|
|
|
if not utils.isInternalClient(irc, numeric):
|
|
|
|
raise LookupError('No such PyLink PseudoClient exists.')
|
2015-07-20 02:27:53 +02:00
|
|
|
_send(irc, numeric, 'TOPIC %s :%s' % (target, text))
|
2015-08-16 07:34:40 +02:00
|
|
|
irc.channels[target].topic = text
|
|
|
|
irc.channels[target].topicset = True
|
2015-07-15 08:23:35 +02:00
|
|
|
|
|
|
|
def topicServer(irc, numeric, target, text):
|
|
|
|
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):
|
|
|
|
"""<irc object> <client numeric> <text>
|
|
|
|
|
|
|
|
Invites <target> to <channel> to <text> from PyLink client <client numeric>."""
|
|
|
|
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):
|
|
|
|
"""<irc object> <client numeric> <text>
|
|
|
|
|
|
|
|
Knocks on <channel> with <text> from PyLink client <client numeric>."""
|
|
|
|
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):
|
|
|
|
"""<irc object> <client numeric> <field> <text>
|
|
|
|
|
|
|
|
Changes the <field> field of <target> PyLink PseudoClient <client numeric>."""
|
|
|
|
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):
|
|
|
|
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):
|
|
|
|
"""<irc object> <numeric> <text>
|
|
|
|
|
|
|
|
Sends an AWAY message with text <text> from PyLink client <numeric>.
|
|
|
|
<text> can be an empty string to unset AWAY status."""
|
|
|
|
if text:
|
|
|
|
_send(irc, source, 'AWAY %s :%s' % (int(time.time()), text))
|
|
|
|
else:
|
|
|
|
_send(irc, source, 'AWAY')
|
|
|
|
|
2015-04-04 03:45:18 +02:00
|
|
|
def connect(irc):
|
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-06-22 00:01:45 +02:00
|
|
|
def handle_ping(irc, source, command, args):
|
|
|
|
# <- :70M PING 70M 0AL
|
|
|
|
# -> :0AL PONG 0AL 70M
|
2015-06-24 04:29:53 +02:00
|
|
|
if utils.isInternalServer(irc, args[1]):
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, args[1], 'PONG %s %s' % (args[1], source))
|
2015-04-03 21:35:55 +02:00
|
|
|
|
2015-07-17 22:39:57 +02:00
|
|
|
def handle_pong(irc, source, command, args):
|
|
|
|
if source == irc.uplink and args[1] == irc.sid:
|
|
|
|
irc.lastping = time.time()
|
|
|
|
|
2015-04-25 07:37:07 +02:00
|
|
|
def handle_privmsg(irc, source, command, args):
|
2015-07-05 04:00:29 +02:00
|
|
|
return {'target': args[0], 'text': args[1]}
|
2015-04-03 21:35:55 +02:00
|
|
|
|
2015-06-08 03:08:25 +02:00
|
|
|
def handle_kill(irc, source, command, args):
|
|
|
|
killed = args[0]
|
2015-07-25 03:26:31 +02:00
|
|
|
data = irc.users.get(killed)
|
|
|
|
if data:
|
|
|
|
removeClient(irc, killed)
|
2015-07-15 08:23:35 +02:00
|
|
|
return {'target': killed, 'text': args[1], 'userdata': data}
|
2015-06-08 03:08:25 +02:00
|
|
|
|
2015-06-07 19:06:54 +02:00
|
|
|
def handle_kick(irc, source, command, args):
|
|
|
|
# :70MAAAAAA KICK #endlessvoid 70MAAAAAA :some reason
|
2015-07-05 04:00:29 +02:00
|
|
|
channel = args[0].lower()
|
2015-06-07 19:06:54 +02:00
|
|
|
kicked = args[1]
|
2015-07-04 21:34:33 +02:00
|
|
|
handle_part(irc, kicked, 'KICK', [channel, args[2]])
|
2015-07-08 05:59:35 +02:00
|
|
|
return {'channel': channel, 'target': kicked, 'text': args[2]}
|
2015-06-07 19:06:54 +02:00
|
|
|
|
2015-06-07 18:43:13 +02:00
|
|
|
def handle_part(irc, source, command, args):
|
2015-07-21 04:52:52 +02:00
|
|
|
channels = args[0].lower().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 = ''
|
2015-08-16 08:29:40 +02:00
|
|
|
# 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]
|
2015-07-21 04:52:52 +02:00
|
|
|
return {'channels': channels, 'text': reason}
|
2015-06-07 18:43:13 +02:00
|
|
|
|
2015-04-03 21:35:55 +02:00
|
|
|
def handle_error(irc, numeric, command, args):
|
2015-07-22 02:33:36 +02:00
|
|
|
irc.connected.clear()
|
2015-07-04 03:07:01 +02:00
|
|
|
raise ProtocolError('Received an ERROR, killing!')
|
2015-04-18 07:11:49 +02:00
|
|
|
|
|
|
|
def handle_fjoin(irc, servernumeric, command, args):
|
|
|
|
# :70M FJOIN #chat 1423790411 +AFPfjnt 6:5 7:5 9:5 :o,1SRAABIT4 v,1IOAAF53R <...>
|
2015-07-04 08:30:38 +02:00
|
|
|
channel = args[0].lower()
|
2015-06-07 08:04:11 +02:00
|
|
|
# InspIRCd sends each user's channel data in the form of 'modeprefix(es),UID'
|
|
|
|
userlist = args[-1].split()
|
2015-07-07 04:00:20 +02:00
|
|
|
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
|
2015-08-05 15:14:34 +02:00
|
|
|
irc.channels[channel].modes.clear()
|
|
|
|
for p in irc.channels[channel].prefixmodes.values():
|
|
|
|
p.clear()
|
2015-07-05 09:20:45 +02:00
|
|
|
modestring = args[2:-1] or args[2]
|
2015-07-08 05:59:35 +02:00
|
|
|
parsedmodes = utils.parseModes(irc, channel, modestring)
|
|
|
|
utils.applyModes(irc, channel, parsedmodes)
|
2015-07-05 04:00:29 +02:00
|
|
|
namelist = []
|
2015-06-07 08:04:11 +02:00
|
|
|
for user in userlist:
|
|
|
|
modeprefix, user = user.split(',', 1)
|
2015-07-05 04:00:29 +02:00
|
|
|
namelist.append(user)
|
2015-07-16 21:20:40 +02:00
|
|
|
irc.users[user].channels.add(channel)
|
2015-07-05 21:48:39 +02:00
|
|
|
utils.applyModes(irc, channel, [('+%s' % mode, user) for mode in modeprefix])
|
2015-06-07 18:43:13 +02:00
|
|
|
irc.channels[channel].users.add(user)
|
2015-07-08 22:35:20 +02:00
|
|
|
return {'channel': channel, 'users': namelist, 'modes': parsedmodes, 'ts': their_ts}
|
2015-04-18 07:11:49 +02:00
|
|
|
|
|
|
|
def handle_uid(irc, numeric, command, args):
|
2015-04-25 07:37:07 +02:00
|
|
|
# :70M UID 70MAAAAAB 1429934638 GL 0::1 hidden-7j810p.9mdf.lrek.0000.0000.IP gl 0::1 1429934638 +Wioswx +ACGKNOQXacfgklnoqvx :realname
|
2015-04-18 07:11:49 +02:00
|
|
|
uid, ts, nick, realhost, host, ident, ip = args[0:7]
|
|
|
|
realname = args[-1]
|
2015-06-21 05:54:01 +02:00
|
|
|
irc.users[uid] = IrcUser(nick, ts, uid, ident, host, realname, realhost, ip)
|
2015-07-05 21:48:39 +02:00
|
|
|
parsedmodes = utils.parseModes(irc, uid, [args[8], args[9]])
|
2015-07-05 22:29:01 +02:00
|
|
|
log.debug('Applying modes %s for %s', parsedmodes, uid)
|
2015-07-05 21:48:39 +02:00
|
|
|
utils.applyModes(irc, uid, parsedmodes)
|
2015-05-31 07:15:19 +02:00
|
|
|
irc.servers[numeric].users.append(uid)
|
2015-07-05 04:00:29 +02:00
|
|
|
return {'uid': uid, 'ts': ts, 'nick': nick, 'realhost': realhost, 'host': host, 'ident': ident, 'ip': ip}
|
2015-04-18 07:11:49 +02:00
|
|
|
|
|
|
|
def handle_quit(irc, numeric, command, args):
|
2015-06-22 00:00:33 +02:00
|
|
|
# <- :1SRAAGB4T QUIT :Quit: quit message goes here
|
2015-06-07 08:04:11 +02:00
|
|
|
removeClient(irc, numeric)
|
2015-07-08 05:59:35 +02:00
|
|
|
return {'text': args[0]}
|
2015-03-20 00:21:49 +01:00
|
|
|
|
2015-05-31 07:15:19 +02:00
|
|
|
def handle_server(irc, numeric, command, args):
|
2015-06-22 00:00:33 +02:00
|
|
|
# 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)
|
2015-07-04 20:57:21 +02:00
|
|
|
servername = args[0].lower()
|
2015-05-31 07:15:19 +02:00
|
|
|
sid = args[3]
|
2015-07-08 22:35:20 +02:00
|
|
|
sdesc = args[-1]
|
2015-06-22 00:00:33 +02:00
|
|
|
irc.servers[sid] = IrcServer(numeric, servername)
|
2015-07-08 22:35:20 +02:00
|
|
|
return {'name': servername, 'sid': args[3], 'text': sdesc}
|
2015-05-31 07:15:19 +02:00
|
|
|
|
|
|
|
def handle_nick(irc, numeric, command, args):
|
2015-06-19 22:15:49 +02:00
|
|
|
# <- :70MAAAAAA NICK GL-devel 1434744242
|
2015-07-07 04:44:55 +02:00
|
|
|
oldnick = irc.users[numeric].nick
|
|
|
|
newnick = irc.users[numeric].nick = args[0]
|
2015-07-08 22:35:20 +02:00
|
|
|
return {'newnick': newnick, 'oldnick': oldnick, 'ts': int(args[1])}
|
2015-05-31 07:15:19 +02:00
|
|
|
|
2015-06-08 04:31:56 +02:00
|
|
|
def handle_save(irc, numeric, command, args):
|
|
|
|
# This is used to handle nick collisions. Here, the client Derp_ already exists,
|
|
|
|
# so trying to change nick to it will cause a nick collision. On InspIRCd,
|
|
|
|
# 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]
|
2015-07-26 03:20:49 +02:00
|
|
|
oldnick = irc.users[user].nick
|
2015-06-08 04:31:56 +02:00
|
|
|
irc.users[user].nick = user
|
2015-07-26 03:20:49 +02:00
|
|
|
return {'target': user, 'ts': int(args[1]), 'oldnick': oldnick}
|
2015-06-08 04:31:56 +02:00
|
|
|
|
2015-06-07 08:04:11 +02:00
|
|
|
def handle_fmode(irc, numeric, command, args):
|
|
|
|
# <- :70MAAAAAA FMODE #chat 1433653462 +hhT 70MAAAAAA 70MAAAAAD
|
2015-07-05 09:20:45 +02:00
|
|
|
channel = args[0].lower()
|
|
|
|
modes = args[2:]
|
2015-07-05 21:48:39 +02:00
|
|
|
changedmodes = utils.parseModes(irc, channel, modes)
|
|
|
|
utils.applyModes(irc, channel, changedmodes)
|
2015-07-08 22:35:20 +02:00
|
|
|
ts = int(args[1])
|
|
|
|
return {'target': channel, 'modes': changedmodes, 'ts': ts}
|
2015-06-07 08:04:11 +02:00
|
|
|
|
2015-06-21 05:36:35 +02:00
|
|
|
def handle_mode(irc, numeric, command, args):
|
|
|
|
# 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:]
|
2015-07-05 21:48:39 +02:00
|
|
|
changedmodes = utils.parseModes(irc, numeric, modestrings)
|
2015-07-09 00:10:09 +02:00
|
|
|
utils.applyModes(irc, target, changedmodes)
|
2015-07-05 08:47:27 +02:00
|
|
|
return {'target': target, 'modes': changedmodes}
|
2015-06-21 05:36:35 +02:00
|
|
|
|
2015-05-31 07:15:19 +02:00
|
|
|
def handle_squit(irc, numeric, command, args):
|
|
|
|
# :70M SQUIT 1ML :Server quit by GL!gl@0::1
|
|
|
|
split_server = args[0]
|
2015-07-16 04:08:49 +02:00
|
|
|
affected_users = []
|
2015-07-05 22:29:01 +02:00
|
|
|
log.info('(%s) Netsplit on server %s', irc.name, split_server)
|
2015-05-31 07:15:19 +02:00
|
|
|
# Prevent RuntimeError: dictionary changed size during iteration
|
|
|
|
old_servers = copy(irc.servers)
|
|
|
|
for sid, data in old_servers.items():
|
|
|
|
if data.uplink == split_server:
|
2015-07-05 22:29:01 +02:00
|
|
|
log.debug('Server %s also hosts server %s, removing those users too...', split_server, sid)
|
2015-07-16 04:08:49 +02:00
|
|
|
args = handle_squit(irc, sid, 'SQUIT', [sid, "PyLink: Automatically splitting leaf servers of %s" % sid])
|
|
|
|
affected_users += args['users']
|
2015-06-07 18:33:35 +02:00
|
|
|
for user in copy(irc.servers[split_server].users):
|
2015-07-16 04:08:49 +02:00
|
|
|
affected_users.append(user)
|
2015-07-05 22:29:01 +02:00
|
|
|
log.debug('Removing client %s (%s)', user, irc.users[user].nick)
|
2015-06-07 08:04:11 +02:00
|
|
|
removeClient(irc, user)
|
2015-05-31 07:15:19 +02:00
|
|
|
del irc.servers[split_server]
|
2015-07-16 04:08:49 +02:00
|
|
|
log.debug('(%s) Netsplit affected users: %s', irc.name, affected_users)
|
|
|
|
return {'target': split_server, 'users': affected_users}
|
2015-05-31 07:15:19 +02:00
|
|
|
|
2015-06-03 01:39:13 +02:00
|
|
|
def handle_idle(irc, numeric, command, args):
|
|
|
|
"""Handle the IDLE command, sent between servers in remote WHOIS queries."""
|
|
|
|
# <- :70MAAAAAA IDLE 1MLAAAAIG
|
|
|
|
# -> :1MLAAAAIG IDLE 70MAAAAAA 1433036797 319
|
|
|
|
sourceuser = numeric
|
|
|
|
targetuser = args[0]
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, targetuser, 'IDLE %s %s 0' % (sourceuser, irc.users[targetuser].ts))
|
2015-06-03 01:39:13 +02:00
|
|
|
|
2015-04-03 09:17:03 +02:00
|
|
|
def handle_events(irc, data):
|
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
|
|
|
|
# can use InspIRCd as a model here and assign their mode map to our cmodes list.
|
|
|
|
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-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:
|
|
|
|
real_args = []
|
|
|
|
for arg in args:
|
|
|
|
real_args.append(arg)
|
2015-04-18 07:11:49 +02:00
|
|
|
# If the argument starts with ':' and ISN'T the first argument.
|
|
|
|
# The first argument is used for denoting the source UID/SID.
|
2015-04-03 21:35:55 +02:00
|
|
|
if arg.startswith(':') and args.index(arg) != 0:
|
2015-04-18 07:11:49 +02:00
|
|
|
# : 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.
|
2015-05-31 07:15:19 +02:00
|
|
|
index = args.index(arg) # Get the array index of the multi-word arg
|
|
|
|
# Set the last arg to a joined version of the remaining args
|
|
|
|
arg = args[index:]
|
|
|
|
arg = ' '.join(arg)[1:]
|
|
|
|
# Cut the original argument list right before the multi-word arg,
|
|
|
|
# and then append the multi-word arg.
|
2015-04-03 21:35:55 +02:00
|
|
|
real_args = args[:index]
|
|
|
|
real_args.append(arg)
|
|
|
|
break
|
|
|
|
real_args[0] = real_args[0].split(':', 1)[1]
|
|
|
|
args = real_args
|
|
|
|
|
|
|
|
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-07-23 05:39:38 +02:00
|
|
|
def spawnServer(irc, name, sid=None, uplink=None, desc='PyLink Server'):
|
2015-06-22 00:00:33 +02:00
|
|
|
# -> :0AL SERVER test.server * 1 0AM :some silly pseudoserver
|
|
|
|
uplink = uplink or irc.sid
|
|
|
|
name = name.lower()
|
2015-07-14 01:06:58 +02:00
|
|
|
if sid is None: # No sid given; generate one!
|
2015-07-12 05:09:36 +02:00
|
|
|
irc.sidgen = utils.TS6SIDGenerator(irc.serverdata["sidrange"])
|
|
|
|
sid = irc.sidgen.next_sid()
|
2015-07-04 02:10:32 +02:00
|
|
|
assert len(sid) == 3, "Incorrect SID length"
|
2015-06-22 00:00:33 +02:00
|
|
|
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)
|
2015-06-24 04:29:53 +02:00
|
|
|
if not utils.isInternalServer(irc, uplink):
|
2015-06-22 00:00:33 +02:00
|
|
|
raise ValueError('Server %r is not a PyLink internal PseudoServer!' % uplink)
|
|
|
|
if not utils.isServerName(name):
|
|
|
|
raise ValueError('Invalid server name %r' % name)
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, uplink, 'SERVER %s * 1 %s :%s' % (name, sid, desc))
|
2015-07-14 21:03:22 +02:00
|
|
|
irc.servers[sid] = IrcServer(uplink, name, internal=True)
|
2015-07-15 07:33:56 +02:00
|
|
|
_send(irc, sid, 'ENDBURST')
|
2015-07-23 05:39:38 +02:00
|
|
|
return sid
|
2015-07-07 00:33:23 +02:00
|
|
|
|
|
|
|
def handle_ftopic(irc, numeric, command, args):
|
|
|
|
# <- :70M FTOPIC #channel 1434510754 GLo|o|!GLolol@escape.the.dreamland.ca :Some channel topic
|
|
|
|
channel = args[0].lower()
|
|
|
|
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}
|
|
|
|
|
|
|
|
def handle_topic(irc, numeric, command, args):
|
|
|
|
# <- :70MAAAAAA TOPIC #test :test
|
|
|
|
channel = args[0].lower()
|
|
|
|
topic = args[1]
|
|
|
|
ts = int(time.time())
|
|
|
|
irc.channels[channel].topic = topic
|
2015-07-20 22:18:04 +02:00
|
|
|
irc.channels[channel].topicset = True
|
2015-07-16 04:08:36 +02:00
|
|
|
return {'channel': channel, 'setter': numeric, 'ts': ts, 'topic': topic}
|
2015-07-08 00:31:12 +02:00
|
|
|
|
|
|
|
def handle_invite(irc, numeric, command, args):
|
|
|
|
# <- :70MAAAAAC INVITE 0ALAAAAAA #blah 0
|
|
|
|
target = args[0]
|
|
|
|
channel = args[1].lower()
|
|
|
|
# We don't actually need to process this; it's just something plugins/hooks can use
|
|
|
|
return {'target': target, 'channel': channel}
|
|
|
|
|
|
|
|
def handle_encap(irc, numeric, command, args):
|
|
|
|
# <- :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':
|
|
|
|
channel = args[2].lower()
|
|
|
|
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}
|
|
|
|
|
|
|
|
def handle_notice(irc, numeric, command, args):
|
|
|
|
# <- :70MAAAAAA NOTICE #dev :afasfsa
|
|
|
|
# <- :70MAAAAAA NOTICE 0ALAAAAAA :afasfsa
|
|
|
|
return {'target': args[0], 'text': args[1]}
|
2015-07-09 03:03:08 +02:00
|
|
|
|
|
|
|
def handle_opertype(irc, numeric, command, args):
|
|
|
|
# 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)]
|
|
|
|
utils.applyModes(irc, numeric, omode)
|
|
|
|
return {'target': numeric, 'modes': omode}
|
2015-07-10 02:04:24 +02:00
|
|
|
|
|
|
|
def handle_fident(irc, numeric, command, args):
|
|
|
|
# :70MAAAAAB FHOST test
|
|
|
|
# :70MAAAAAB FNAME :afdsafasf
|
|
|
|
# :70MAAAAAB FIDENT test
|
|
|
|
irc.users[numeric].ident = newident = args[0]
|
|
|
|
return {'target': numeric, 'newident': newident}
|
|
|
|
|
|
|
|
def handle_fhost(irc, numeric, command, args):
|
|
|
|
irc.users[numeric].host = newhost = args[0]
|
|
|
|
return {'target': numeric, 'newhost': newhost}
|
|
|
|
|
|
|
|
def handle_fname(irc, numeric, command, args):
|
|
|
|
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):
|
|
|
|
return {}
|
2015-08-12 13:17:01 +02:00
|
|
|
|
|
|
|
def handle_away(irc, numeric, command, args):
|
|
|
|
# <- :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': ''}
|