1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
from datetime import datetime
from math import ceil, log10
from typing import Union, Optional, List, Tuple
from poezio.ui.funcs import truncate_nick
from poezio import poopt
from poezio.user import User
from poezio.theming import dump_tuple, get_theme
from poezio.ui.consts import (
SHORT_FORMAT_LENGTH,
LONG_FORMAT_LENGTH,
)
class BaseMessage:
__slots__ = ('txt', 'time', 'identifier')
def __init__(self, txt: str, identifier: str = '', time: Optional[datetime] = None):
self.txt = txt
self.identifier = identifier
if time is not None:
self.time = time
else:
self.time = datetime.now()
def compute_offset(self, with_timestamps: bool, nick_size: int) -> int:
return SHORT_FORMAT_LENGTH + 1
class EndOfArchive(BaseMessage):
"""Marker added to a buffer when we reach the end of a MAM archive"""
class InfoMessage(BaseMessage):
def __init__(self, txt: str, identifier: str = '', time: Optional[datetime] = None):
txt = ('\x19%s}' % dump_tuple(get_theme().COLOR_INFORMATION_TEXT)) + txt
super().__init__(txt=txt, identifier=identifier, time=time)
class MucOwnLeaveMessage(InfoMessage):
"""Status message displayed on our room leave/kick/ban"""
class MucOwnJoinMessage(InfoMessage):
"""Status message displayed on our room join"""
class XMLLog(BaseMessage):
"""XML Log message"""
__slots__ = ('txt', 'time', 'identifier', 'incoming')
def __init__(
self,
txt: str,
incoming: bool,
):
BaseMessage.__init__(
self,
txt=txt,
identifier='',
)
self.txt = txt
self.identifier = ''
self.incoming = incoming
def compute_offset(self, with_timestamps: bool, nick_size: int) -> int:
offset = 0
theme = get_theme()
if with_timestamps:
offset += 1 + SHORT_FORMAT_LENGTH
if self.incoming:
nick = theme.CHAR_XML_IN
else:
nick = theme.CHAR_XML_OUT
nick = truncate_nick(nick, nick_size) or ''
offset += 1 + len(nick)
return offset
class StatusMessage(BaseMessage):
__slots__ = ('txt', 'time', 'identifier', 'format_string', 'format_args')
def __init__(self, format_string: str, format_args: dict):
BaseMessage.__init__(
self,
txt='',
)
self.format_string = format_string
self.format_args = format_args
self.rebuild()
def rebuild(self):
real_args = {}
for key, func in self.format_args.items():
real_args[key] = func()
self.txt = self.format_string.format(**real_args)
class Message(BaseMessage):
__slots__ = ('txt', 'nick_color', 'time', 'nickname', 'user', 'delayed', 'history',
'identifier', 'top', 'highlight', 'me', 'old_message', 'revisions',
'jid', 'ack')
def __init__(self,
txt: str,
nickname: Optional[str],
time: Optional[datetime] = None,
nick_color: Optional[Tuple] = None,
delayed: bool = False,
history: bool = False,
user: Optional[User] = None,
identifier: Optional[str] = '',
top: Optional[bool] = False,
highlight: bool = False,
old_message: Optional['Message'] = None,
revisions: int = 0,
jid: Optional[str] = None,
ack: int = 0) -> None:
"""
Create a new Message object with parameters, check for /me messages,
and delayed messages
"""
BaseMessage.__init__(
self,
txt=txt.replace('\t', ' ') + '\x19o',
identifier=identifier or '',
time=time,
)
if txt.startswith('/me '):
me = True
txt = '\x19%s}%s\x19o' % (dump_tuple(get_theme().COLOR_ME_MESSAGE),
txt[4:])
else:
me = False
self.txt = txt
self.delayed = delayed or history
self.history = history
self.nickname = nickname
self.nick_color = nick_color
self.user = user
self.top = top
self.highlight = highlight
self.me = me
self.old_message = old_message
self.revisions = revisions
self.jid = jid
self.ack = ack
def _other_elems(self) -> str:
"Helper for the repr_message function"
acc = []
fields = list(self.__slots__)
fields.remove('old_message')
for field in fields:
acc.append('%s=%s' % (field, repr(getattr(self, field))))
return 'Message(%s, %s' % (', '.join(acc), 'old_message=')
def __repr__(self) -> str:
"""
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
"""
init = self._other_elems()
acc = [init]
next_message = self.old_message
rev = 1
while next_message is not None:
acc.append(next_message._other_elems())
next_message = next_message.old_message
rev += 1
acc.append('None')
while rev:
acc.append(')')
rev -= 1
return ''.join(acc)
def compute_offset(self, with_timestamps: bool, nick_size: int) -> int:
offset = 0
if with_timestamps:
if self.history:
offset += 1 + LONG_FORMAT_LENGTH
else:
offset += 1 + SHORT_FORMAT_LENGTH
if not self.nickname: # not a message, nothing to do afterwards
return offset
nick = truncate_nick(self.nickname, nick_size) or ''
offset += poopt.wcswidth(nick)
if self.ack:
theme = get_theme()
if self.ack > 0:
offset += poopt.wcswidth(theme.CHAR_ACK_RECEIVED) + 1
else:
offset += poopt.wcswidth(theme.CHAR_NACK) + 1
if self.me:
offset += 3
else:
offset += 2
if self.revisions:
offset += ceil(log10(self.revisions + 1))
return offset
|