2011-01-21 05:18:22 -05:00
|
|
|
# Copyright (c) 2008 Duncan Fordyce
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
2022-03-19 19:48:19 -04:00
|
|
|
import logging
|
2022-04-10 23:57:13 -04:00
|
|
|
|
|
|
|
from oyoyo.ircevents import numeric_events
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
PchumLog = logging.getLogger("pchumLogger")
|
|
|
|
|
2022-03-16 23:14:46 -04:00
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
def parse_raw_irc_command(element):
|
|
|
|
"""
|
|
|
|
This function parses a raw irc command and returns a tuple
|
|
|
|
of (prefix, command, args).
|
|
|
|
The following is a psuedo BNF of the input text:
|
|
|
|
|
|
|
|
<message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
|
|
|
|
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
|
|
|
|
<command> ::= <letter> { <letter> } | <number> <number> <number>
|
|
|
|
<SPACE> ::= ' ' { ' ' }
|
|
|
|
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
|
|
|
|
|
|
|
|
<middle> ::= <Any *non-empty* sequence of octets not including SPACE
|
|
|
|
or NUL or CR or LF, the first of which may not be ':'>
|
|
|
|
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
|
|
|
|
NUL or CR or LF>
|
|
|
|
|
|
|
|
<crlf> ::= CR LF
|
2022-06-05 20:05:00 -04:00
|
|
|
"""
|
|
|
|
"""
|
|
|
|
When message-tags are enabled, the message pseudo-BNF,
|
|
|
|
as defined in RFC 1459, section 2.3.1 is extended as follows:
|
|
|
|
|
|
|
|
<message> ::= ['@' <tags> <SPACE>] [':' <prefix> <SPACE> ] <command> [params] <crlf>
|
|
|
|
<tags> ::= <tag> [';' <tag>]*
|
|
|
|
<tag> ::= <key> ['=' <escaped_value>]
|
|
|
|
<key> ::= [ <client_prefix> ] [ <vendor> '/' ] <key_name>
|
|
|
|
<client_prefix> ::= '+'
|
|
|
|
<key_name> ::= <non-empty sequence of ascii letters, digits, hyphens ('-')>
|
|
|
|
<escaped_value> ::= <sequence of zero or more utf8 characters except NUL, CR, LF, semicolon (`;`) and SPACE>
|
|
|
|
<vendor> ::= <host>
|
|
|
|
|
|
|
|
|
2011-01-21 05:18:22 -05:00
|
|
|
"""
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2021-04-01 16:15:38 -04:00
|
|
|
try:
|
|
|
|
element = element.decode("utf-8")
|
2022-03-16 23:14:46 -04:00
|
|
|
except UnicodeDecodeError as e:
|
2022-03-18 09:50:35 -04:00
|
|
|
PchumLog.debug("utf-8 error %s" % str(e))
|
2022-10-07 16:51:40 -04:00
|
|
|
element = element.decode("latin-1", "replace")
|
|
|
|
|
2021-03-23 17:40:24 -04:00
|
|
|
parts = element.strip().split(" ")
|
2022-10-07 16:51:40 -04:00
|
|
|
if parts[0].startswith(":"):
|
2022-06-05 20:05:00 -04:00
|
|
|
tags = None
|
2011-01-21 05:18:22 -05:00
|
|
|
prefix = parts[0][1:]
|
|
|
|
command = parts[1]
|
|
|
|
args = parts[2:]
|
2022-10-07 16:51:40 -04:00
|
|
|
elif parts[0].startswith("@"):
|
2022-06-05 20:05:00 -04:00
|
|
|
# Message tag
|
|
|
|
tags = parts[0]
|
|
|
|
prefix = parts[1][1:]
|
|
|
|
command = parts[2]
|
|
|
|
args = parts[3:]
|
2011-01-21 05:18:22 -05:00
|
|
|
else:
|
2022-06-05 20:05:00 -04:00
|
|
|
tags = None
|
2011-01-21 05:18:22 -05:00
|
|
|
prefix = None
|
|
|
|
command = parts[0]
|
|
|
|
args = parts[1:]
|
|
|
|
|
|
|
|
if command.isdigit():
|
|
|
|
try:
|
|
|
|
command = numeric_events[command]
|
|
|
|
except KeyError:
|
2022-10-07 16:51:40 -04:00
|
|
|
PchumLog.info("unknown numeric event %s" % command)
|
2011-01-21 05:18:22 -05:00
|
|
|
command = command.lower()
|
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
if args[0].startswith(":"):
|
2021-03-23 17:40:24 -04:00
|
|
|
args = [" ".join(args)[1:]]
|
2011-01-21 05:18:22 -05:00
|
|
|
else:
|
2021-03-23 17:40:24 -04:00
|
|
|
for idx, arg in enumerate(args):
|
2022-10-07 16:51:40 -04:00
|
|
|
if arg.startswith(":"):
|
2021-03-23 17:40:24 -04:00
|
|
|
args = args[:idx] + [" ".join(args[idx:])[1:]]
|
2011-01-21 05:18:22 -05:00
|
|
|
break
|
|
|
|
|
2022-06-05 20:05:00 -04:00
|
|
|
return (tags, prefix, command, args)
|
2011-01-21 05:18:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
def parse_nick(name):
|
2022-10-07 16:51:40 -04:00
|
|
|
"""parse a nickname and return a tuple of (nick, mode, user, host)
|
2011-01-21 05:18:22 -05:00
|
|
|
|
|
|
|
<nick> [ '!' [<mode> = ] <user> ] [ '@' <host> ]
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2022-10-07 16:51:40 -04:00
|
|
|
nick, rest = name.split("!")
|
2011-01-21 05:18:22 -05:00
|
|
|
except ValueError:
|
|
|
|
return (name, None, None, None)
|
|
|
|
try:
|
2022-10-07 16:51:40 -04:00
|
|
|
mode, rest = rest.split("=")
|
2011-01-21 05:18:22 -05:00
|
|
|
except ValueError:
|
|
|
|
mode, rest = None, rest
|
|
|
|
try:
|
2022-10-07 16:51:40 -04:00
|
|
|
user, host = rest.split("@")
|
2011-01-21 05:18:22 -05:00
|
|
|
except ValueError:
|
|
|
|
return (name, mode, rest, None)
|
|
|
|
|
|
|
|
return (name, mode, user, host)
|