summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorGeorg Lukas <georg@op-co.de>2019-07-29 18:48:54 +0200
committerGeorg Lukas <georg@op-co.de>2019-07-29 18:50:07 +0200
commitb48c8c3c6cb83300ca1174b4afa8c3086a44d526 (patch)
tree9a12b31e540bd7a6f0262e50444868ba49ca9f03 /plugins
parentb5849a868eee40b430e885f65ed4302df8e722aa (diff)
downloadpoezio-b48c8c3c6cb83300ca1174b4afa8c3086a44d526.tar.gz
poezio-b48c8c3c6cb83300ca1174b4afa8c3086a44d526.tar.bz2
poezio-b48c8c3c6cb83300ca1174b4afa8c3086a44d526.tar.xz
poezio-b48c8c3c6cb83300ca1174b4afa8c3086a44d526.zip
plugins: "new" emoji_ascii conversion plugin for terminally broken users
Diffstat (limited to 'plugins')
-rw-r--r--plugins/emoji_ascii.py58
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'])