summary refs log tree commit diff
path: root/synapse/push/__init__.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-02-10 11:04:37 +0000
committerErik Johnston <erik@matrix.org>2015-02-10 11:04:37 +0000
commitc86ebe76736a4cde48cfb3229d894a174c6bd379 (patch)
treeafc236e8ef1bdbd6d0686747b99743661522638e /synapse/push/__init__.py
parentOops, we do want to defer.return regardless of whether we are caching or not (diff)
parentMerge branch 'release-v0.6.2' of github.com:matrix-org/synapse into develop (diff)
downloadsynapse-c86ebe76736a4cde48cfb3229d894a174c6bd379.tar.xz
Merge branch 'develop' of github.com:matrix-org/synapse into state-chache
Conflicts:
	synapse/app/homeserver.py
	synapse/state.py
Diffstat (limited to 'synapse/push/__init__.py')
-rw-r--r--synapse/push/__init__.py35
1 files changed, 23 insertions, 12 deletions
diff --git a/synapse/push/__init__.py b/synapse/push/__init__.py
index 7293715293..6f143a5df9 100644
--- a/synapse/push/__init__.py
+++ b/synapse/push/__init__.py
@@ -22,7 +22,6 @@ import synapse.util.async
 import baserules
 
 import logging
-import fnmatch
 import json
 import re
 
@@ -130,26 +129,38 @@ class Pusher(object):
 
         defer.returnValue(Pusher.DEFAULT_ACTIONS)
 
+    @staticmethod
+    def _glob_to_regexp(glob):
+        r = re.escape(glob)
+        r = re.sub(r'\\\*', r'.*?', r)
+        r = re.sub(r'\\\?', r'.', r)
+
+        # handle [abc], [a-z] and [!a-z] style ranges.
+        r = re.sub(r'\\\[(\\\!|)(.*)\\\]',
+                   lambda x: ('[%s%s]' % (x.group(1) and '^' or '',
+                                          re.sub(r'\\\-', '-', x.group(2)))), r)
+        return r
+    
     def _event_fulfills_condition(self, ev, condition, display_name, room_member_count):
         if condition['kind'] == 'event_match':
             if 'pattern' not in condition:
                 logger.warn("event_match condition with no pattern")
                 return False
-            pat = condition['pattern']
-
-            if pat.strip("*?[]") == pat:
-                # no special glob characters so we assume the user means
-                # 'contains this string' rather than 'is this string'
-                pat = "*%s*" % (pat,)
-
+            # XXX: optimisation: cache our pattern regexps
+            if condition['key'] == 'content.body':
+                r = r'\b%s\b' % self._glob_to_regexp(condition['pattern'])
+            else:
+                r = r'^%s$' % self._glob_to_regexp(condition['pattern'])
             val = _value_for_dotted_key(condition['key'], ev)
             if val is None:
                 return False
-            return fnmatch.fnmatch(val.upper(), pat.upper())
+            return re.search(r, val, flags=re.IGNORECASE) is not None
+
         elif condition['kind'] == 'device':
             if 'profile_tag' not in condition:
                 return True
             return condition['profile_tag'] == self.profile_tag
+
         elif condition['kind'] == 'contains_display_name':
             # This is special because display names can be different
             # between rooms and so you can't really hard code it in a rule.
@@ -159,9 +170,9 @@ class Pusher(object):
                 return False
             if not display_name:
                 return False
-            return fnmatch.fnmatch(
-                ev['content']['body'].upper(), "*%s*" % (display_name.upper(),)
-            )
+            return re.search("\b%s\b" % re.escape(display_name),
+                            ev['content']['body'], flags=re.IGNORECASE) is not None
+
         elif condition['kind'] == 'room_member_count':
             if 'is' not in condition:
                 return False