diff options
-rw-r--r-- | doc/en/plugins/replace.txt | 71 | ||||
-rw-r--r-- | plugins/replace.py | 66 |
2 files changed, 137 insertions, 0 deletions
diff --git a/doc/en/plugins/replace.txt b/doc/en/plugins/replace.txt new file mode 100644 index 00000000..3a17ff20 --- /dev/null +++ b/doc/en/plugins/replace.txt @@ -0,0 +1,71 @@ +Replace +======= + +Replace some patterns in a message before sending it. + + +Installation +------------ +You only have to load the plugin. + +============== + + /load replace + +============== + +Usage +----- +Insert a pattern in the form + +========= + +%pattern% + +========= + +in your message, and it will be replaced by the corresponding text. + +The list of provided patterns is: + +- _time_: Insert the current time +- _date_: Insert the current date +- _datetime_: Insert the current date and time +- _random_nick_: Insert a random nick from the current MUC +- _dice_: Insert a random number between 1 and 6 + + +Add your own pattern +-------------------- + +You can easily edit this plugin to add your own patterns. For example if +don’t want to search for an insult everytime you’re angry, you can create a +curse pattern this way: + +- In the init(self) method of the Plugin class, add something like + +================ +[source,python] + self.patterns['curse'] = replace_curse +================ + +- then define a function (not a method of the Plugin class) at the bottom +of the file. For example: + + +========================================================== +[source,python] +def replace_curse(message, tab): + return random.choice(['dumb shit', 'idiot', 'moron']) +========================================================== + +and you can now use something like + +==================== +Shut up, %curse%! +==================== + +in your everyday-conversations. + +For more conveniance, you can read your nice words from a file, do whatever +you want in that function, as long as it returns a string. diff --git a/plugins/replace.py b/plugins/replace.py new file mode 100644 index 00000000..d1fbac57 --- /dev/null +++ b/plugins/replace.py @@ -0,0 +1,66 @@ +""" +Replace a pattern from a message you're about to send, by the result of a +function. For example you can insert the current time in your sentence by +writing %time% in it. +""" + +from plugin import BasePlugin +import tabs +import datetime +import random +from sleekxmpp.xmlstream.stanzabase import JID + +class Plugin(BasePlugin): + def init(self): + self.patterns = {} + self.add_event_handler('conversation_say', self.replace_pattern) + self.add_event_handler('muc_say', self.replace_pattern) + self.add_event_handler('private_say', self.replace_pattern) + self.patterns['time'] = replace_time + self.patterns['date'] = replace_date + self.patterns['datetime'] = replace_datetime + self.patterns['random_nick'] = replace_random_user + self.patterns['dice'] = replace_dice + + def replace_pattern(self, message, tab): + """ + Look for a %*% pattern in the message and replace it by the result + of the corresponding function. + """ + body = message['body'] + for pattern in self.patterns: + new = body + while True: + # we don't use a replace on all occurence, otherwise the + # result would be the same for all occurence of the pattern + # and that's not desirable in some of them (for example the + # ones that provide random results) + new = body.replace('%%%s%%' % pattern, + self.patterns[pattern](message, tab), 1) + if new == body: + break + body = new + message['body'] = body + + +def replace_time(message, tab): + return datetime.datetime.now().strftime("%X") + +def replace_date(message, tab): + return datetime.datetime.now().strftime("%x") + +def replace_datetime(message, tab): + return datetime.datetime.now().strftime("%c") + +def replace_random_user(message, tab): + if isinstance(tab, tabs.MucTab): + return random.choice(tab.users).nick + elif isinstance(tab, tabs.PrivateTab): + return random.choice([JID(tab.name).resource, tab.own_nick]) + else isinstance(tab, tabs.ConversationTab): + # that doesn’t make any sense. By why use this pattern in a + # ConversationTab anyway? + return str(tab.name) + +def replace_dice(message, tab): + return str(random.randrange(1, 7)) |