Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gajim/common/helpers.py55
-rw-r--r--test/common/test_message_needs_highlight.py45
2 files changed, 77 insertions, 23 deletions
diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py
index bd52dfc16..4869bc753 100644
--- a/gajim/common/helpers.py
+++ b/gajim/common/helpers.py
@@ -562,35 +562,44 @@ def get_os_info() -> str:
def message_needs_highlight(text: str, nickname: str, own_jid: str) -> bool:
'''
- Check text to see whether any of the words in (muc_highlight_words and
- nick) appear
+ Check whether 'text' contains 'nickname', 'own_jid', or any string of the
+ 'muc_highlight_words' setting.
'''
- uri = parse_uri(text)
- if uri.type != URIType.INVALID:
- # Don't highlight message if it's an URI
- return False
+ search_strings = app.settings.get('muc_highlight_words').split(';')
+ search_strings.append(nickname)
+ search_strings.append(own_jid)
- special_words = app.settings.get('muc_highlight_words').split(';')
- special_words.append(nickname)
- special_words.append(own_jid)
- # Strip empties: ''.split(';') == [''] and would highlight everything.
- # Also lowercase everything for case insensitive compare.
- special_words = [word.lower() for word in special_words if word]
+ search_strings = [word.lower() for word in search_strings if word]
text = text.lower()
- for special_word in special_words:
- found_here = text.find(special_word)
- while found_here > -1:
- end_here = found_here + len(special_word)
- if ((found_here == 0 or not text[found_here - 1].isalpha()) and
- (end_here == len(text) or not text[end_here].isalpha())):
- # It is beginning of text or char before is not alpha AND
- # it is end of text or char after is not alpha
+ for search_string in search_strings:
+ match = text.find(search_string)
+
+ while match > -1:
+ search_end = match + len(search_string)
+
+ if match == 0 and search_end == len(text):
+ # Text contains search_string only (exact match)
+ return True
+
+ char_before_allowed = bool(
+ not text[match - 1].isalpha() and
+ text[match - 1] not in ('/', '-'))
+
+ if char_before_allowed and search_end == len(text):
+ # search_string found at the end of text and
+ # char before search_string is allowed.
+ return True
+
+ if char_before_allowed and not text[search_end].isalpha():
+ # char_before search_string is allowed and
+ # char_after search_string is not alpha.
return True
- # continue searching
- start = found_here + 1
- found_here = text.find(special_word, start)
+
+ start = match + 1
+ match = text.find(search_string, start)
+
return False
diff --git a/test/common/test_message_needs_highlight.py b/test/common/test_message_needs_highlight.py
new file mode 100644
index 000000000..e4b689390
--- /dev/null
+++ b/test/common/test_message_needs_highlight.py
@@ -0,0 +1,45 @@
+import unittest
+
+from gajim.common import app
+from gajim.common.helpers import message_needs_highlight
+
+app.settings.set('muc_highlight_words', 'test;gajim')
+JID = 'romemo@xmppserver'
+NICK = 'Romeo'
+
+
+class HighlightTest(unittest.TestCase):
+ '''Tests for message_needs_highlight'''
+
+ def test_highlight(self):
+ t_text1 = 'Romeo: Does this work?'
+ t_text2 = 'Romeo:Does this work?'
+ t_text3 = 'Romeo Does this work?'
+ t_text4 = 'Does this work, romeo?'
+ t_text5 = 'Does this work,Romeo?'
+ t_text6 = 'Are you using Gajim?'
+ t_text7 = 'Did you test this?'
+ t_text8 = 'Hi romeo'
+ t_text9 = 'My address is romeo@xmppserver'
+
+ f_text1 = 'RomeoDoes this work?'
+ f_text2 = ''
+ f_text3 = 'https://romeo.tld'
+ f_text4 = 'https://romeo.tld message'
+ f_text5 = 'https://test.tld/where-is-romeo'
+
+ self.assertTrue(message_needs_highlight(t_text1, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text2, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text3, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text4, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text5, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text6, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text7, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text8, NICK, JID))
+ self.assertTrue(message_needs_highlight(t_text9, NICK, JID))
+
+ self.assertFalse(message_needs_highlight(f_text1, NICK, JID))
+ self.assertFalse(message_needs_highlight(f_text2, NICK, JID))
+ self.assertFalse(message_needs_highlight(f_text3, NICK, JID))
+ self.assertFalse(message_needs_highlight(f_text4, NICK, JID))
+ self.assertFalse(message_needs_highlight(f_text5, NICK, JID))