Last active
August 29, 2015 14:21
-
-
Save zephyrtronium/2486480a14399b192466 to your computer and use it in GitHub Desktop.
autosed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| autosed uses sed-style syntax to perform replacements on previous messages, much like {bot.prefix}s does. It triggers automatically when someone says a line that starts with s or S with the second character being any of /\|!#$%&.:;?@ (usually / is used) with at least one more occurrence of the same character. | |
| Text between the first and second occurrences is used as the search parameter. It is replaced with the text between the second and third occurrences. If there is no third occurrence, the first word after the second character is used. This can be empty, as well. If S was used instead of s, the search is case-sensitive. | |
| Example: {b}s/something//{b} removes the last occurrence of the word "something". {b}s/something/some stuff/{b} replaces the last occurrence of the word "something" with "some stuff". {b}s/something/some stuff{b} replaces the last occurrence of the word "something" with "some". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '''Automatic sed-style replacements.''' | |
| import re | |
| import string | |
| realtimes = ['autosedd'] | |
| ignore = re.compile('^[Ss]([/\\\\|!#$%&.:;?@]).*\\1') | |
| msgre = re.compile(r'[^]]+\] (<.*?>|\* \S+) (.*)') | |
| def init(self): | |
| if 'rex' not in self.modules: | |
| raise ImportError('rex must be loaded before autosed') | |
| def autosedd(self, args): | |
| if args['command'] == 'PRIVMSG': | |
| s = args['trailing'] | |
| if len(s) < 4: | |
| return | |
| if s[0] in 'Ss' and s[1] in '/\\|!#$%&.:;?@': | |
| p = s[2:].split(s[1]) | |
| if len(p) < 2: | |
| return | |
| try: | |
| pattern = re.compile('(?<!^[Ss][/\\\\|!#$%&.:;?@])' + p[0], re.I * (s[0] == 's')) | |
| except Exception as e: | |
| print('autosed: failed to compile pattern', p[0]) | |
| return | |
| ch = self.get_channel(args['middle']) | |
| if len(p) == 2: | |
| if not p[1] or p[1][0].isspace(): | |
| x = '' | |
| else: | |
| x = p[1].split(maxsplit=1)[0] | |
| else: | |
| x = p[1] | |
| m = rep(self, ch, pattern, x) | |
| self.send(ch, m) | |
| def rep(self, channel, pattern, params): | |
| for i in reversed(self.logs[channel.target]): | |
| m = msgre.match(i) | |
| if m is None or m.group(2).startswith(self.prefix) or ignore.match(m.group(2)): | |
| continue | |
| s, n = pattern.subn(params, m.group(2)) | |
| if n: | |
| return m.group(1) + ' ' + s if len(s) < 400 else 'Too long.' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| s exists for your entertainment. It allows you to replace text in a previous message that matches a given regular expression with other text of your choosing. It processes its arguments as a shell would, so if you'd like to match a string containing spaces, you can quote the pattern. Apostrophes also count as quotes. Note that this also involves processing backslash escapes, so backslashes tend to disappear. | |
| For more information on the regex patterns used, see http://docs.python.org/py3k/library/re.html . For the substitution process, see the sub() function on the same page. For documentation on the argument processing, see http://docs.python.org/py3k/library/shlex.html#shlex.split . For related replacements using sed-style syntax, see {bot.prefix}help autosed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment