From 1be40346813c2dd713cfc3c946722de0cf221e74 Mon Sep 17 00:00:00 2001 From: James Lu Date: Thu, 3 Sep 2015 12:57:57 -0700 Subject: [PATCH] Make TS6 command parsing a shared library (#78) --- protocols/inspircd.py | 25 ++++--------------------- protocols/proto_common.py | 34 ++++++++++++++++++++++++++++++++++ protocols/ts6.py | 21 ++------------------- 3 files changed, 40 insertions(+), 40 deletions(-) create mode 100644 protocols/proto_common.py diff --git a/protocols/inspircd.py b/protocols/inspircd.py index f0122c9..c15f5ce 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -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:] diff --git a/protocols/proto_common.py b/protocols/proto_common.py new file mode 100644 index 0000000..11ed38b --- /dev/null +++ b/protocols/proto_common.py @@ -0,0 +1,34 @@ +def parseArgs(args): + """ + Parses a string of RFC1459-style arguments split into a list, where ":" may + be used for multi-word arguments that last until the end of a line. + """ + real_args = [] + for idx, arg in enumerate(args): + real_args.append(arg) + # If the argument starts with ':' and ISN'T the first argument. + # The first argument is used for denoting the source UID/SID. + if arg.startswith(':') and idx != 0: + # : is used for multi-word arguments that last until the end + # of the message. We can use list splicing here to turn them all + # into one argument. + # Set the last arg to a joined version of the remaining args + arg = args[idx:] + arg = ' '.join(arg)[1:] + # Cut the original argument list right before the multi-word arg, + # and then append the multi-word arg. + real_args = args[:idx] + real_args.append(arg) + break + return real_args + + +def parseTS6Args(args): + """ + + Similar to parseArgs(), but stripping leading colons from the first argument + of a line (usually the sender field).""" + args = parseArgs(args) + args[0] = args[0].split(':', 1)[1] + return args + diff --git a/protocols/ts6.py b/protocols/ts6.py index 6f49d00..a28ab8d 100644 --- a/protocols/ts6.py +++ b/protocols/ts6.py @@ -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]