Skip to content
Snippets Groups Projects

New plugin: Ascii Emoji

Open Arthur Bols requested to merge principis/gajim-plugins:ascii-emoji into master
Files
3
+ 71
0
# Copyright (C) 2022 Arthur Bols <arthur@bols.dev>
#
# This file is part of Ascii Emoji Gajim Plugin.
#
# Ascii Emoji Gajim Plugin 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; version 3 only.
#
# Ascii Emoji Gajim Plugin 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 Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
import re
import logging
from nbxmpp.structs import StanzaHandler
from gajim.common import ged
from gajim.common.modules.base import BaseModule
from ascii_emoji.modules.data import emoji_map
log = logging.getLogger('gajim.p.ascii_emoji')
# Module name
name = 'AsciiEmoji'
class AsciiEmoji(BaseModule):
def __init__(self, con):
BaseModule.__init__(self, con, plugin=True)
self.handlers = [
StanzaHandler(name='message',
callback=self._on_message_received,
priority=ged.PREGUI),
]
self.emoticon_regex = re.compile(emoji_map.get_regex())
def replace(self, msg):
iterator = self.emoticon_regex.finditer(msg)
new_msgtxt = ''
prev_repl = 0
for match in iterator:
start, end = match.span()
ascii_text = msg[start:end]
emoji = emoji_map.get(ascii_text, None)
if emoji is not None:
new_msgtxt += msg[prev_repl:start] + emoji
prev_repl = end
return new_msgtxt + msg[prev_repl:]
def _on_message_received(self, _con, _stanza, properties):
if not properties.body:
log.debug("Received message does not have a body")
return
properties.body = self.replace(properties.body)
def get_instance(*args, **kwargs):
return AsciiEmoji(*args, **kwargs), 'AsciiEmoji'
Loading