diff options
author | Link Mauve <linkmauve@linkmauve.fr> | 2019-07-29 18:54:38 +0200 |
---|---|---|
committer | Link Mauve <linkmauve@linkmauve.fr> | 2019-07-29 18:54:38 +0200 |
commit | a9d17a420d98af022332d4cdf3dc8f5f8d844b71 (patch) | |
tree | 9a12b31e540bd7a6f0262e50444868ba49ca9f03 | |
parent | b5849a868eee40b430e885f65ed4302df8e722aa (diff) | |
parent | b48c8c3c6cb83300ca1174b4afa8c3086a44d526 (diff) | |
download | poezio-a9d17a420d98af022332d4cdf3dc8f5f8d844b71.tar.gz poezio-a9d17a420d98af022332d4cdf3dc8f5f8d844b71.tar.bz2 poezio-a9d17a420d98af022332d4cdf3dc8f5f8d844b71.tar.xz poezio-a9d17a420d98af022332d4cdf3dc8f5f8d844b71.zip |
Merge branch 'smile' into 'master'
plugins: "new" emoji_ascii conversion plugin for terminally broken users
See merge request poezio/poezio!39
-rw-r--r-- | plugins/emoji_ascii.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/plugins/emoji_ascii.py b/plugins/emoji_ascii.py new file mode 100644 index 00000000..6629c50e --- /dev/null +++ b/plugins/emoji_ascii.py @@ -0,0 +1,58 @@ +# poezio emoji_ascii plugin +# +# Will translate received Emoji to :emoji: for better display on text terminals, +# and outgoing :emoji: into Emoji on the wire. +# +# Requires emojis.json.gz (MIT licensed) from: +# +# git clone https://github.com/vdurmont/emoji-java +# gzip -9 < ./src/main/resources/emojis.json > poezio/plugins/emojis.json.gz + +# TODOs: +# 1. it messes up your log files (doesn't log original message, logs mutilated :emoji: instead) +# 2. Doesn't work on outgoing direct messages +# 3. Doesn't detect pastes, corrupts jabber:x:foobar +# 4. no auto-completion of emoji aliases +# 5. coloring of converted Emojis to be able to differentiate them from incoming ASCII + +import gzip +import json +import os +import re + +from poezio.plugin import BasePlugin + +class Plugin(BasePlugin): + emoji_to_ascii = {} + ascii_to_emoji = {} + emoji_pattern = None + alias_pattern = None + + def init(self): + emoji_map_file_name = os.path.abspath(os.path.dirname(__file__) + '/emojis.json.gz') + emoji_map_data = gzip.open(emoji_map_file_name, 'r').read().decode('utf-8') + emoji_map = json.loads(emoji_map_data) + for e in emoji_map: + self.emoji_to_ascii[e['emoji']] = ':%s:' % e['aliases'][0] + for alias in e['aliases']: + # work around :iq: and similar country code misdetection + flag = re.match('^[a-z][a-z]$', alias) and "flag" in e["tags"] + if not flag: + self.ascii_to_emoji[':%s:' % alias] = e['emoji'] + self.emoji_pattern = re.compile('|'.join(self.emoji_to_ascii.keys()).replace('*', '\*')) + self.alias_pattern = re.compile('|'.join(self.ascii_to_emoji.keys()).replace('+', '\+')) + + self.api.add_event_handler('muc_msg', self.emoji2alias) + self.api.add_event_handler('conversation_msg', self.emoji2alias) + self.api.add_event_handler('private_msg', self.emoji2alias) + + self.api.add_event_handler('muc_say', self.alias2emoji) + self.api.add_event_handler('private_say', self.alias2emoji) + self.api.add_event_handler('conversation_say', self.alias2emoji) + + + def emoji2alias(self, msg, tab): + msg['body'] = self.emoji_pattern.sub(lambda m: self.emoji_to_ascii[m.group()], msg['body']) + + def alias2emoji(self, msg, tab): + msg['body'] = self.alias_pattern.sub(lambda m: self.ascii_to_emoji[m.group()], msg['body']) |