2010-11-15 11:59:09 +00:00
|
|
|
"""
|
|
|
|
Define the TextBuffer class
|
2014-04-22 18:02:07 +00:00
|
|
|
|
|
|
|
A text buffer contains a list of intermediate representations of messages
|
|
|
|
(not xml stanzas, but neither the Lines used in windows.py.
|
|
|
|
|
|
|
|
Each text buffer can be linked to multiple windows, that will be rendered
|
|
|
|
independantly by their TextWins.
|
2010-11-15 11:59:09 +00:00
|
|
|
"""
|
|
|
|
|
2010-12-15 15:40:43 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2011-03-21 02:18:12 +00:00
|
|
|
import collections
|
|
|
|
|
2010-09-14 02:11:07 +00:00
|
|
|
from datetime import datetime
|
2011-02-14 13:54:26 +00:00
|
|
|
from config import config
|
2014-04-06 23:25:30 +00:00
|
|
|
from theming import get_theme, dump_tuple
|
2010-12-15 15:40:43 +00:00
|
|
|
|
2014-04-22 18:02:07 +00:00
|
|
|
message_fields = ('txt nick_color time str_time nickname user identifier'
|
2014-04-27 14:32:03 +00:00
|
|
|
' highlight me old_message revisions jid ack')
|
2013-01-02 19:36:38 +00:00
|
|
|
Message = collections.namedtuple('Message', message_fields)
|
|
|
|
|
2014-04-06 17:52:59 +00:00
|
|
|
class CorrectionError(Exception):
|
|
|
|
pass
|
2013-02-27 21:09:14 +00:00
|
|
|
|
2013-01-02 19:36:38 +00:00
|
|
|
def other_elems(self):
|
2014-04-22 18:02:07 +00:00
|
|
|
"Helper for the repr_message function"
|
2013-01-02 19:36:38 +00:00
|
|
|
acc = ['Message(']
|
|
|
|
fields = message_fields.split()
|
|
|
|
fields.remove('old_message')
|
|
|
|
for field in fields:
|
2013-07-01 20:10:30 +00:00
|
|
|
acc.append('%s=%s' % (field, repr(getattr(self, field))))
|
2014-04-06 17:52:59 +00:00
|
|
|
return ', '.join(acc) + ', old_message='
|
2013-01-02 19:36:38 +00:00
|
|
|
|
|
|
|
def repr_message(self):
|
2014-04-22 18:02:07 +00:00
|
|
|
"""
|
|
|
|
repr() for the Message class, for debug purposes, since the default
|
|
|
|
repr() is recursive, so it can stack overflow given too many revisions
|
|
|
|
of a message
|
|
|
|
"""
|
2013-01-02 19:36:38 +00:00
|
|
|
init = other_elems(self)
|
2013-03-13 21:49:19 +00:00
|
|
|
acc = [init]
|
2014-04-06 17:52:59 +00:00
|
|
|
next_message = self.old_message
|
2013-03-13 21:49:19 +00:00
|
|
|
rev = 1
|
2014-04-06 17:52:59 +00:00
|
|
|
while next_message:
|
|
|
|
acc.append(other_elems(next_message))
|
|
|
|
next_message = next_message.old_message
|
2013-01-02 19:36:38 +00:00
|
|
|
rev += 1
|
|
|
|
acc.append('None')
|
|
|
|
while rev:
|
|
|
|
acc.append(')')
|
|
|
|
rev -= 1
|
|
|
|
return ''.join(acc)
|
|
|
|
|
|
|
|
Message.__repr__ = repr_message
|
|
|
|
Message.__str__ = repr_message
|
2011-03-21 02:18:12 +00:00
|
|
|
|
2010-09-14 02:11:07 +00:00
|
|
|
class TextBuffer(object):
|
|
|
|
"""
|
|
|
|
This class just keep trace of messages, in a list with various
|
|
|
|
informations and attributes.
|
|
|
|
"""
|
2014-04-22 18:02:07 +00:00
|
|
|
def __init__(self, messages_nb_limit=None):
|
|
|
|
|
|
|
|
if messages_nb_limit is None:
|
2014-10-20 19:04:14 +00:00
|
|
|
messages_nb_limit = config.get('max_messages_in_memory')
|
2011-02-14 13:54:26 +00:00
|
|
|
self.messages_nb_limit = messages_nb_limit
|
2014-04-22 18:02:07 +00:00
|
|
|
# Message objects
|
|
|
|
self.messages = []
|
|
|
|
# we keep track of one or more windows
|
2010-12-15 15:40:43 +00:00
|
|
|
# so we can pass the new messages to them, as they are added, so
|
2011-02-13 21:28:35 +00:00
|
|
|
# they (the windows) can build the lines from the new message
|
2014-04-22 18:02:07 +00:00
|
|
|
self.windows = []
|
2010-12-15 15:40:43 +00:00
|
|
|
|
|
|
|
def add_window(self, win):
|
|
|
|
self.windows.append(win)
|
2010-09-14 02:11:07 +00:00
|
|
|
|
2012-11-17 18:17:02 +00:00
|
|
|
@property
|
|
|
|
def last_message(self):
|
|
|
|
return self.messages[-1] if self.messages else None
|
|
|
|
|
|
|
|
|
2014-04-06 17:52:59 +00:00
|
|
|
@staticmethod
|
2014-04-22 18:02:07 +00:00
|
|
|
def make_message(txt, time, nickname, nick_color, history, user,
|
|
|
|
identifier, str_time=None, highlight=False,
|
2014-04-27 14:32:03 +00:00
|
|
|
old_message=None, revisions=0, jid=None, ack=None):
|
2014-04-22 18:02:07 +00:00
|
|
|
"""
|
|
|
|
Create a new Message object with parameters, check for /me messages,
|
|
|
|
and delayed messages
|
|
|
|
"""
|
2011-09-08 15:05:02 +00:00
|
|
|
time = time or datetime.now()
|
2011-11-25 10:21:20 +00:00
|
|
|
if txt.startswith('/me '):
|
2012-12-26 00:50:11 +00:00
|
|
|
me = True
|
2014-04-22 18:02:07 +00:00
|
|
|
txt = '\x19%s}%s' % (dump_tuple(get_theme().COLOR_ME_MESSAGE),
|
|
|
|
txt[4:])
|
|
|
|
else:
|
|
|
|
me = False
|
2014-04-06 23:25:30 +00:00
|
|
|
if history:
|
2014-04-22 18:02:07 +00:00
|
|
|
txt = txt.replace('\x19o', '\x19o\x19%s}' %
|
|
|
|
dump_tuple(get_theme().COLOR_LOG_MSG))
|
|
|
|
str_time = time.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
else:
|
|
|
|
if str_time is None:
|
|
|
|
str_time = time.strftime("%H:%M:%S")
|
|
|
|
else:
|
|
|
|
str_time = ''
|
|
|
|
|
2012-11-11 15:01:53 +00:00
|
|
|
msg = Message(
|
|
|
|
txt='%s\x19o'%(txt.replace('\t', ' '),),
|
|
|
|
nick_color=nick_color,
|
|
|
|
time=time,
|
2014-04-22 18:02:07 +00:00
|
|
|
str_time=str_time,
|
2012-12-26 00:50:11 +00:00
|
|
|
nickname=nickname,
|
|
|
|
user=user,
|
|
|
|
identifier=identifier,
|
|
|
|
highlight=highlight,
|
|
|
|
me=me,
|
2012-12-26 17:39:31 +00:00
|
|
|
old_message=old_message,
|
2013-05-15 17:03:04 +00:00
|
|
|
revisions=revisions,
|
2014-04-27 14:32:03 +00:00
|
|
|
jid=jid,
|
|
|
|
ack=ack)
|
2013-01-02 19:36:38 +00:00
|
|
|
log.debug('Set message %s with %s.', identifier, msg)
|
2012-10-12 15:29:45 +00:00
|
|
|
return msg
|
|
|
|
|
2014-04-22 18:02:07 +00:00
|
|
|
def add_message(self, txt, time=None, nickname=None,
|
|
|
|
nick_color=None, history=None, user=None, highlight=False,
|
2014-04-27 14:32:03 +00:00
|
|
|
identifier=None, str_time=None, jid=None, ack=None):
|
2014-04-22 18:02:07 +00:00
|
|
|
"""
|
|
|
|
Create a message and add it to the text buffer
|
|
|
|
"""
|
|
|
|
msg = self.make_message(txt, time, nickname, nick_color, history,
|
|
|
|
user, identifier, str_time=str_time,
|
2014-04-27 14:32:03 +00:00
|
|
|
highlight=highlight, jid=jid, ack=ack)
|
2010-12-15 15:40:43 +00:00
|
|
|
self.messages.append(msg)
|
2014-04-22 18:02:07 +00:00
|
|
|
|
2011-02-14 13:54:26 +00:00
|
|
|
while len(self.messages) > self.messages_nb_limit:
|
2010-12-15 15:40:43 +00:00
|
|
|
self.messages.pop(0)
|
2014-04-22 18:02:07 +00:00
|
|
|
|
2011-04-16 16:07:14 +00:00
|
|
|
ret_val = None
|
2014-10-20 19:04:14 +00:00
|
|
|
show_timestamps = config.get('show_timestamps')
|
2010-12-15 15:40:43 +00:00
|
|
|
for window in self.windows: # make the associated windows
|
2014-04-22 18:02:07 +00:00
|
|
|
# build the lines from the new message
|
|
|
|
nb = window.build_new_message(msg, history=history,
|
|
|
|
highlight=highlight,
|
|
|
|
timestamp=show_timestamps)
|
2011-04-16 16:07:14 +00:00
|
|
|
if ret_val is None:
|
|
|
|
ret_val = nb
|
2010-12-15 15:40:43 +00:00
|
|
|
if window.pos != 0:
|
|
|
|
window.scroll_up(nb)
|
2014-04-22 18:02:07 +00:00
|
|
|
|
2011-04-16 16:07:14 +00:00
|
|
|
return ret_val or 1
|
2010-12-31 10:52:15 +00:00
|
|
|
|
2014-04-27 14:32:03 +00:00
|
|
|
def _find_message(self, old_id):
|
|
|
|
"""
|
|
|
|
Find a message in the text buffer from its message id
|
|
|
|
"""
|
|
|
|
for i in range(len(self.messages) -1, -1, -1):
|
|
|
|
msg = self.messages[i]
|
|
|
|
if msg.identifier == old_id:
|
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
|
|
|
def ack_message(self, old_id):
|
|
|
|
"""
|
|
|
|
Ack a message
|
|
|
|
"""
|
|
|
|
i = self._find_message(old_id)
|
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
msg = self.messages[i]
|
|
|
|
new_msg = list(msg)
|
|
|
|
new_msg[12] = True
|
|
|
|
new_msg = Message(*new_msg)
|
|
|
|
self.messages[i] = new_msg
|
|
|
|
return new_msg
|
|
|
|
|
2014-04-22 18:02:07 +00:00
|
|
|
def modify_message(self, txt, old_id, new_id, highlight=False,
|
|
|
|
time=None, user=None, jid=None):
|
|
|
|
"""
|
|
|
|
Correct a message in a text buffer.
|
|
|
|
"""
|
|
|
|
|
2014-04-27 14:32:03 +00:00
|
|
|
i = self._find_message(old_id)
|
|
|
|
|
|
|
|
if i == -1:
|
|
|
|
log.debug('Message %s not found in text_buffer, abort replacement.',
|
|
|
|
old_id)
|
|
|
|
raise CorrectionError("nothing to replace")
|
|
|
|
|
|
|
|
msg = self.messages[i]
|
|
|
|
|
|
|
|
if msg.user and msg.user is not user:
|
|
|
|
raise CorrectionError("Different users")
|
|
|
|
elif len(msg.str_time) > 8: # ugly
|
|
|
|
raise CorrectionError("Delayed message")
|
|
|
|
elif not msg.user and (msg.jid is None or jid is None):
|
|
|
|
raise CorrectionError('Could not check the '
|
|
|
|
'identity of the sender')
|
|
|
|
elif not msg.user and msg.jid != jid:
|
|
|
|
raise CorrectionError('Messages %s and %s have not been '
|
|
|
|
'sent by the same fullJID' %
|
|
|
|
(old_id, new_id))
|
|
|
|
|
|
|
|
if not time:
|
|
|
|
time = msg.time
|
|
|
|
message = self.make_message(txt, time, msg.nickname,
|
|
|
|
msg.nick_color, None, msg.user,
|
|
|
|
new_id, highlight=highlight,
|
|
|
|
old_message=msg,
|
|
|
|
revisions=msg.revisions + 1,
|
|
|
|
jid=jid)
|
|
|
|
self.messages[i] = message
|
|
|
|
log.debug('Replacing message %s with %s.', old_id, new_id)
|
|
|
|
return message
|
2012-10-12 15:29:45 +00:00
|
|
|
|
2011-02-13 21:28:35 +00:00
|
|
|
def del_window(self, win):
|
|
|
|
self.windows.remove(win)
|
|
|
|
|
|
|
|
def __del__(self):
|
2014-04-22 18:02:07 +00:00
|
|
|
size = len(self.messages)
|
|
|
|
log.debug('** Deleting %s messages from textbuffer', size)
|