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
|
2012-06-28 23:16:06 +00:00
|
|
|
import shlex
|
2012-04-30 16:22:10 +00:00
|
|
|
|
2012-04-09 02:19:39 +00:00
|
|
|
class Message:
|
2014-09-13 21:49:00 +00:00
|
|
|
def __init__ (self, orig, private=False):
|
|
|
|
self.cmd = orig.cmd
|
|
|
|
self.tags = orig.tags
|
|
|
|
self.params = orig.params
|
2014-08-29 14:33:45 +00:00
|
|
|
self.private = private
|
2014-09-13 21:49:00 +00:00
|
|
|
self.prefix = orig.prefix
|
|
|
|
self.nick = orig.nick
|
2014-08-29 14:33:45 +00:00
|
|
|
|
|
|
|
# Special commands
|
2014-09-01 17:21:54 +00:00
|
|
|
if self.cmd == 'PRIVMSG' or self.cmd == 'NOTICE':
|
2014-09-13 21:49:00 +00:00
|
|
|
self.receivers = orig.decode(self.params[0]).split(',')
|
2014-08-29 14:33:45 +00:00
|
|
|
|
|
|
|
# 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
|
2014-09-13 21:49:00 +00:00
|
|
|
self.text = orig.decode(self.params[1][1:len(self.params[1])-1])
|
2014-08-29 14:33:45 +00:00
|
|
|
else:
|
|
|
|
self.is_ctcp = False
|
2014-09-13 21:49:00 +00:00
|
|
|
self.text = orig.decode(self.params[1])
|
2014-08-29 14:33:45 +00:00
|
|
|
|
|
|
|
# Split content by words
|
2012-11-02 19:28:48 +00:00
|
|
|
self.parse_content()
|
2014-08-29 14:33:45 +00:00
|
|
|
|
|
|
|
else:
|
2014-09-07 23:55:36 +00:00
|
|
|
for i in range(0, len(self.params)):
|
2014-09-13 21:49:00 +00:00
|
|
|
self.params[i] = orig.decode(self.params[i])
|
2014-08-29 14:33:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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 !
|
2014-09-17 04:59:40 +00:00
|
|
|
if len(self.text) > 1 and self.text[0] == '!':
|
2014-09-01 17:21:54 +00:00
|
|
|
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(' ')
|