Make line parser reuseable (and make it work for client & server lines)

This commit is contained in:
dave 2017-12-04 23:53:44 -08:00
parent 3cd61f6ce1
commit 6be2d92bac
2 changed files with 48 additions and 38 deletions

View File

@ -132,3 +132,43 @@ def load(filepath):
return json_load(f)
else:
raise Exception("Unknown config format")
def parse_irc_line(data, client=True):
"""
Process one line of text irc sent us.
Return tuple of (command, args, prefix, trailing)
:param data: the data to process
:type data: str
:return tuple:"""
if data.strip() == "":
return
prefix = None
command = None
args = []
trailing = None
if data[0] == ":":
prefix = data.split(" ")[0][1:]
data = data[data.find(" ") + 1:]
command = data.split(" ")[0]
data = data[data.find(" ") + 1:]
if(data[0] == ":"):
# no args
trailing = data[1:].strip()
else:
# find trailing
pos = data.find(" :")
if pos == -1:
trailing = None
else:
trailing = data[pos + 2:].strip()
data = data[:data.find(" :")]
args = data.split(" ")
for index, arg in enumerate(args):
args[index] = arg.strip()
return (command, args, prefix, trailing)

View File

@ -12,7 +12,7 @@ import logging
import traceback
import sys
from inspect import getargspec
from pyircbot.common import burstbucket
from pyircbot.common import burstbucket, parse_irc_line
from collections import namedtuple
from io import StringIO
@ -81,12 +81,17 @@ class IRCCore(object):
self.server = (self.server + 1) % len(self.servers)
await asyncio.sleep(1, loop=loop)
continue
while self.alive:
try:
data = await self.reader.readuntil()
self.log.debug("<<< {}".format(repr(data)))
self.process_line(data.decode("UTF-8"))
command, args, prefix, trailing = parse_irc_line(data.decode("UTF-8"))
self.fire_hook("_RECV", args=args, prefix=prefix, trailing=trailing)
if command not in self.hookcalls:
self.log.warning("Unknown command: cmd='{}' prefix='{}' args='{}' trailing='{}'"
.format(command, prefix, args, trailing))
else:
self.fire_hook(command, args=args, prefix=prefix, trailing=trailing)
except (ConnectionResetError, asyncio.streams.IncompleteReadError):
traceback.print_exc()
break
@ -132,41 +137,6 @@ class IRCCore(object):
self.writer.close()
self.log.info("Kill complete")
def process_line(self, data):
"""Process one line of text irc sent us
:param data: the data to process
:type data: str"""
if data.strip() == "":
return
prefix = None
command = None
args = []
trailing = None
if data[0] == ":":
prefix = data.split(" ")[0][1:]
data = data[data.find(" ") + 1:]
command = data.split(" ")[0]
data = data[data.find(" ") + 1:]
if(data[0] == ":"):
# no args
trailing = data[1:].strip()
else:
trailing = data[data.find(" :") + 2:].strip()
data = data[:data.find(" :")]
args = data.split(" ")
for index, arg in enumerate(args):
args[index] = arg.strip()
self.fire_hook("_RECV", args=args, prefix=prefix, trailing=trailing)
if command not in self.hookcalls:
self.log.warning("Unknown command: cmd='%s' prefix='%s' args='%s' trailing='%s'" % (command, prefix, args,
trailing))
else:
self.fire_hook(command, args=args, prefix=prefix, trailing=trailing)
def sendRaw(self, data):
asyncio.run_coroutine_threadsafe(self.outputq.put((5, data, )), self._loop)