2012-08-14 03:51:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Nemubot is a modulable IRC bot, built around XML configuration files.
|
|
|
|
# Copyright (C) 2012 Mercier Pierre-Olivier
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-04-09 02:19:39 +00:00
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
import re
|
2012-06-28 23:16:06 +00:00
|
|
|
import shlex
|
2012-04-30 16:22:10 +00:00
|
|
|
|
2012-08-30 13:29:11 +00:00
|
|
|
from response import Response
|
2012-06-16 20:48:15 +00:00
|
|
|
|
2014-08-29 14:33:45 +00:00
|
|
|
mgx = re.compile(b'''^(?:@(?P<tags>[^ ]+)\ )?
|
|
|
|
(?::(?P<prefix>
|
2014-09-07 22:21:10 +00:00
|
|
|
(?P<nick>[^!@ ]+)
|
2014-08-29 14:33:45 +00:00
|
|
|
(?: !(?P<user>[^@ ]+))?
|
|
|
|
(?:@(?P<host>[^ ]+))?
|
|
|
|
)\ )?
|
|
|
|
(?P<command>(?:[a-zA-Z]+|[0-9]{3}))
|
|
|
|
(?P<params>(?:\ [^:][^ ]*)*)(?:\ :(?P<trailing>.*))?
|
|
|
|
$''', re.X)
|
2012-04-30 16:22:10 +00:00
|
|
|
|
2012-04-09 02:19:39 +00:00
|
|
|
class Message:
|
2014-08-29 14:33:45 +00:00
|
|
|
def __init__ (self, raw_line, timestamp, private=False):
|
|
|
|
self.raw = raw_line
|
|
|
|
self.private = private
|
|
|
|
self.tags = { 'time': timestamp }
|
|
|
|
self.params = list()
|
|
|
|
|
|
|
|
p = mgx.match(raw_line.rstrip())
|
|
|
|
|
|
|
|
# Parse tags if exists: @aaa=bbb;ccc;example.com/ddd=eee
|
|
|
|
if p.group("tags"):
|
|
|
|
for tgs in self.decode(p.group("tags")).split(';'):
|
|
|
|
tag = tgs.split('=')
|
|
|
|
if len(tag) > 1:
|
|
|
|
self.add_tag(tag[0], tag[1])
|
|
|
|
else:
|
|
|
|
self.add_tag(tag[0])
|
|
|
|
|
|
|
|
# Parse prefix if exists: :nick!user@host.com
|
|
|
|
self.prefix = self.decode(p.group("prefix"))
|
|
|
|
self.nick = self.decode(p.group("nick"))
|
|
|
|
self.user = self.decode(p.group("user"))
|
|
|
|
self.host = self.decode(p.group("host"))
|
|
|
|
|
|
|
|
# Parse command
|
|
|
|
self.cmd = self.decode(p.group("command"))
|
|
|
|
|
|
|
|
# Parse params
|
|
|
|
if p.group("params"):
|
|
|
|
for param in p.group("params").strip().split(b' '):
|
|
|
|
self.params.append(param)
|
|
|
|
|
|
|
|
if p.group("trailing"):
|
|
|
|
self.params.append(p.group("trailing"))
|
|
|
|
|
|
|
|
# Special commands
|
2014-09-01 17:21:54 +00:00
|
|
|
if self.cmd == 'PRIVMSG' or self.cmd == 'NOTICE':
|
2014-08-29 14:33:45 +00:00
|
|
|
self.receivers = self.decode(self.params[0]).split(',')
|
|
|
|
|
|
|
|
# If CTCP, remove 0x01
|
|
|
|
if len(self.params[1]) > 1 and (self.params[1][0] == 0x01 or self.params[1][1] == 0x01):
|
|
|
|
self.is_ctcp = True
|
|
|
|
self.text = self.decode(self.params[1][1:len(self.params[1])-1])
|
|
|
|
else:
|
|
|
|
self.is_ctcp = False
|
|
|
|
self.text = self.decode(self.params[1])
|
|
|
|
|
|
|
|
# Split content by words
|
2012-11-02 19:28:48 +00:00
|
|
|
self.parse_content()
|
2014-08-29 14:33:45 +00:00
|
|
|
|
|
|
|
elif self.cmd == '353': # RPL_NAMREPLY
|
|
|
|
self.receivers = [ self.decode(self.params[0]) ]
|
|
|
|
self.nicks = self.decode(self.params[1]).split(" ")
|
|
|
|
|
|
|
|
elif self.cmd == '332':
|
|
|
|
self.receivers = [ self.decode(self.params[0]) ]
|
|
|
|
self.topic = self.decode(self.params[1]).split(" ")
|
|
|
|
|
|
|
|
else:
|
2014-09-07 23:55:36 +00:00
|
|
|
for i in range(0, len(self.params)):
|
2014-08-29 14:33:45 +00:00
|
|
|
self.params[i] = self.decode(self.params[i])
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: here for legacy content
|
|
|
|
@property
|
|
|
|
def sender(self):
|
|
|
|
return self.prefix
|
|
|
|
@property
|
|
|
|
def channel(self):
|
|
|
|
return self.receivers[0]
|
|
|
|
|
2012-11-02 19:28:48 +00:00
|
|
|
|
|
|
|
def parse_content(self):
|
|
|
|
"""Parse or reparse the message content"""
|
2014-09-01 17:21:54 +00:00
|
|
|
# Remove !
|
|
|
|
if self.text[0] == '!':
|
|
|
|
self.qual = "cmd"
|
|
|
|
self.text = self.text[1:].strip()
|
|
|
|
|
2012-11-02 19:28:48 +00:00
|
|
|
# Split content by words
|
|
|
|
try:
|
2014-08-29 14:33:45 +00:00
|
|
|
self.cmds = shlex.split(self.text)
|
2012-11-02 19:28:48 +00:00
|
|
|
except ValueError:
|
2014-08-29 14:33:45 +00:00
|
|
|
self.cmds = self.text.split(' ')
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-07-23 16:08:11 +00:00
|
|
|
|
2014-09-08 00:26:07 +00:00
|
|
|
def add_tag(self, key, value=None):
|
|
|
|
"""Add an IRCv3.2 Message Tags"""
|
|
|
|
# Treat special tags
|
|
|
|
if key == "time":
|
|
|
|
# TODO: this is UTC timezone, nemubot works with local timezone
|
|
|
|
value = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
|
|
|
|
|
# Store tag
|
|
|
|
self.tags[key] = value
|
|
|
|
|
|
|
|
|
2014-08-29 14:33:45 +00:00
|
|
|
def decode(self, s):
|
2012-07-23 16:08:11 +00:00
|
|
|
"""Decode the content string usign a specific encoding"""
|
2014-08-29 14:33:45 +00:00
|
|
|
if isinstance(s, bytes):
|
2012-07-23 16:08:11 +00:00
|
|
|
try:
|
2014-08-29 14:33:45 +00:00
|
|
|
s = s.decode()
|
2012-07-23 16:08:11 +00:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
#TODO: use encoding from config file
|
2014-08-29 14:33:45 +00:00
|
|
|
s = s.decode('utf-8', 'replace')
|
|
|
|
return s
|