3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-24 11:39:25 +01:00

Make TS6 command parsing a shared library (#78)

This commit is contained in:
James Lu 2015-09-03 12:57:57 -07:00
parent ce83bea09a
commit 1be4034681
3 changed files with 40 additions and 40 deletions

View File

@ -4,9 +4,11 @@ import os
import re
from copy import copy
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
curdir = os.path.dirname(__file__)
sys.path += [curdir, os.path.dirname(curdir)]
import utils
from log import log
import proto_common
from classes import *
@ -597,26 +599,7 @@ def handle_events(irc, data):
# Sanity check: set this AFTER we fetch the capabilities for the network!
irc.connected.set()
try:
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]
real_args.append(arg)
break
real_args[0] = real_args[0].split(':', 1)[1]
args = real_args
args = proto_common.parseTS6Args(args)
numeric = args[0]
command = args[1]
args = args[2:]

34
protocols/proto_common.py Normal file
View File

@ -0,0 +1,34 @@
def parseArgs(args):
"""<arg list>
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]
real_args.append(arg)
break
return real_args
def parseTS6Args(args):
"""<arg list>
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

View File

@ -15,6 +15,7 @@ from inspircd import nickClient, kickServer, kickClient, _sendKick, quitClient,
from inspircd import handle_privmsg, handle_kill, handle_kick, handle_error, \
handle_quit, handle_nick, handle_save, handle_squit, handle_mode, handle_topic, \
handle_notice
import proto_common
casemapping = 'rfc1459'
hook_map = {'SJOIN': 'JOIN', 'TB': 'TOPIC', 'TMODE': 'MODE', 'BMASK': 'MODE'}
@ -577,25 +578,7 @@ def handle_events(irc, data):
log.debug('(%s) Starting delay to send ENDBURST', irc.name)
endburst_timer.start()
try:
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]
real_args.append(arg)
break
real_args[0] = real_args[0].split(':', 1)[1]
args = real_args
args = proto_common.parseTS6Args(args)
numeric = args[0]
command = args[1]