2013-11-28 20:22:17 +00:00
|
|
|
|
"""
|
|
|
|
|
This plugin lets you perform simple replacements on the last message.
|
|
|
|
|
|
|
|
|
|
Usage
|
|
|
|
|
-----
|
|
|
|
|
|
2014-12-09 18:33:55 +00:00
|
|
|
|
.. note:: the ``/``, ``#``, ``!``, ``:`` and ``;`` chars can be used as separators,
|
|
|
|
|
even if the examples only use ``/``
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Regex replacement
|
|
|
|
|
~~~~~~~~~~~~~~~~~
|
2013-11-28 20:22:17 +00:00
|
|
|
|
|
|
|
|
|
Once the plugin is loaded, any message matching the following regex::
|
|
|
|
|
|
|
|
|
|
^s/(.+?)/(.*?)(/|/g)?$
|
|
|
|
|
|
2014-12-09 18:33:55 +00:00
|
|
|
|
will be interpreted as a regex replacement, and the substitution will be
|
2013-11-28 20:22:17 +00:00
|
|
|
|
applied to the last sent message.
|
|
|
|
|
|
|
|
|
|
For example, if you sent the message::
|
|
|
|
|
|
|
|
|
|
This tab lists all public rooms on a MUC service. It is currently very limited but will be improved in the future. There currently is no way to search a room.
|
|
|
|
|
|
|
|
|
|
And you now want to replace “MUC” with “multi-user chat”, you input::
|
|
|
|
|
|
|
|
|
|
s/MUC/multi-user chat
|
|
|
|
|
|
|
|
|
|
And poezio will correct the message for you.
|
2014-12-09 18:33:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Raw string replacement
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Once the plugin is loaded, any message matching the following regex::
|
|
|
|
|
|
|
|
|
|
^r/(.+?)/(.*?)(/|/g)?$
|
|
|
|
|
|
|
|
|
|
will be interpreted as a replacement, and the substitution will be applied
|
|
|
|
|
to the last send message.
|
|
|
|
|
|
|
|
|
|
This variant is useful if you don’t want to care about regular expressions
|
|
|
|
|
(and you do not want to have to escape stuff like space or backslashes).
|
|
|
|
|
|
|
|
|
|
|
2013-11-28 20:22:17 +00:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from plugin import BasePlugin
|
|
|
|
|
import re
|
|
|
|
|
|
2014-12-09 18:33:55 +00:00
|
|
|
|
allowed_separators = '/#!:;'
|
|
|
|
|
sed_re = re.compile('^([sr])(?P<sep>[%s])(.+?)(?P=sep)(.*?)((?P=sep)|(?P=sep)g)?$' % allowed_separators)
|
2013-11-28 20:22:17 +00:00
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
def init(self):
|
|
|
|
|
self.api.add_event_handler('muc_say', self.sed_fix)
|
|
|
|
|
self.api.add_event_handler('conversation_say', self.sed_fix)
|
|
|
|
|
self.api.add_event_handler('private_say', self.sed_fix)
|
|
|
|
|
|
|
|
|
|
def sed_fix(self, msg, tab):
|
|
|
|
|
if not tab.last_sent_message:
|
|
|
|
|
return
|
2014-05-17 13:54:06 +00:00
|
|
|
|
if 'correct' not in tab.commands:
|
|
|
|
|
return
|
2013-11-28 20:22:17 +00:00
|
|
|
|
body = tab.last_sent_message['body']
|
|
|
|
|
match = sed_re.match(msg['body'])
|
|
|
|
|
if not match:
|
|
|
|
|
return
|
2014-12-09 18:33:55 +00:00
|
|
|
|
typ, sep, remove, put, matchall = match.groups()
|
2013-11-28 20:22:17 +00:00
|
|
|
|
|
|
|
|
|
replace_all = False
|
2014-12-09 18:33:55 +00:00
|
|
|
|
if matchall == sep + 'g':
|
2013-11-28 20:22:17 +00:00
|
|
|
|
replace_all = True
|
|
|
|
|
|
2014-12-09 18:33:55 +00:00
|
|
|
|
if typ == 's':
|
|
|
|
|
try:
|
|
|
|
|
regex = re.compile(remove)
|
|
|
|
|
|
|
|
|
|
if replace_all:
|
|
|
|
|
new_body = re.sub(remove, put, body)
|
|
|
|
|
else:
|
|
|
|
|
new_body = re.sub(remove, put, body, count=1)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.api.information('Invalid regex for the autocorrect '
|
|
|
|
|
'plugin: %s' % e, 'Error')
|
|
|
|
|
return
|
|
|
|
|
elif typ == 'r':
|
|
|
|
|
if replace_all:
|
|
|
|
|
new_body = body.replace(remove, put)
|
|
|
|
|
else:
|
|
|
|
|
new_body = body.replace(remove, put, 1)
|
2013-11-28 20:22:17 +00:00
|
|
|
|
|
|
|
|
|
if body != new_body:
|
|
|
|
|
msg['body'] = new_body
|
|
|
|
|
msg['replace']['id'] = tab.last_sent_message['id']
|