# -*- coding: utf-8 -*-

import re

from stdPlugin import stdPlugin

class autopan(stdPlugin):
    u'''Réagit pragmatiquement aux invasions palmipèdes sur les canaux.'''

    events = {'pubmsg': {'exclusive': True},
              'action': {'exclusive': True}}

    targets = (
        # Must not overlap
            ('coin', 'pan'),
            ('nioc', 'nap'),
            ('\_o<', '\_x<'),
            ('>o_/', '>x_/'),
            (u'ᴎIOↃ', u'ᴎAP'),
            ('koin', 'pang'),
            ('c01n', 'p4n'),
            ('n10c', 'n4p'),
        )

    def on_pubmsg(self, serv, ev, helper):
        words = re.findall(r"[\w'<>/\\]+", helper['message'], re.U)
        output = []
        for w in words:
            tmp = w
            for coin, pan in self.targets:
                tmp = tmp.replace(coin, pan)
            # If the word has been modified
            if tmp != w:
                output.append(tmp)
        # Print all those words
        if len(output) > 0:
            serv.privmsg(helper['target'], ' '.join(output))
            return True
        return False

    def on_action(self, serv, ev, helper):
        return self.on_pubmsg(serv, ev, helper)

@@ -62,16 +62,519 @@
 Plugin%0A%0A
+# From http://stackoverflow.com/a/3009124%0Adef case_sensitive_replace(s, before, after):%0A    %22%22%22Replaces 'before' by 'after' in 's'. Matching is case-insensitive,%0A    but the replaced string keeps the original case of the matched%0A    part. Note that 'after' MUST be shorter than 'before'.%22%22%22%0A    regex = re.compile(re.escape(before), re.I %7C re.U)%0A    return regex.sub(lambda x: ''.join(d.upper() if c.isupper() else d.lower()%0A                                       for c,d in zip(x.group(), after)), s)%0A%0A
 class au
@@ -1130,16 +1130,17 @@
 %5Cw'%3C%3E/%5C%5C
+-
 %5D+%22, hel
@@ -1294,20 +1294,33 @@
 p = 
-tmp.
+case_insensitive_
 replace(
 coin
@@ -1315,16 +1315,21 @@
 replace(
+tmp, 
 coin, pa

replicate case of the matched substrings