66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
# This file is part of Ascii Emoji.
|
|
#
|
|
# Copyright (C) 2021 Arthur Bols <arthur@bols.dev>
|
|
#
|
|
# Ascii Emoji is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Ascii Emoji 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 General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Ascii Emoji. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import re
|
|
|
|
from ascii_emoji.ascii_emoticons_data import ascii_emoticons_data
|
|
from gajim.common import ged
|
|
from gajim.plugins import GajimPlugin
|
|
from gajim.plugins.helpers import log_calls
|
|
from gajim.plugins.plugins_i18n import _
|
|
|
|
|
|
class AsciiEmojiPlugin(GajimPlugin):
|
|
|
|
@log_calls('AsciiEmojiPlugin')
|
|
def init(self):
|
|
self.description = _('Replaces ascii emoticons with emoji.')
|
|
self.config_dialog = None
|
|
|
|
self.events_handlers = {
|
|
'decrypted-message-received': (ged.PREGUI1,
|
|
self._decrypted_message_received),
|
|
'gc-message-received': (ged.PREGUI1, self._gc_message_received),
|
|
}
|
|
|
|
self.emoticon_regex = re.compile(ascii_emoticons_data.get_regex())
|
|
|
|
@log_calls('AsciiEmojiPlugin')
|
|
def _replace(self, obj):
|
|
if not obj.msgtxt:
|
|
return
|
|
iterator = self.emoticon_regex.finditer(obj.msgtxt)
|
|
new_msgtxt = ''
|
|
prev_repl = 0
|
|
for match in iterator:
|
|
start, end = match.span()
|
|
ascii_text = obj.msgtxt[start:end]
|
|
emoji = ascii_emoticons_data.get(ascii_text, None)
|
|
|
|
if emoji is not None:
|
|
new_msgtxt += obj.msgtxt[prev_repl:start] + emoji
|
|
prev_repl = end
|
|
obj.msgtxt = new_msgtxt + obj.msgtxt[prev_repl:]
|
|
|
|
@log_calls('AsciiEmojiPlugin')
|
|
def _decrypted_message_received(self, obj):
|
|
self._replace(obj)
|
|
|
|
@log_calls('AsciiEmojiPlugin')
|
|
def _gc_message_received(self, obj):
|
|
self._replace(obj)
|