From 2e31de3f456ac2e9e4d2950c04ff3594fe41db65 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 24 May 2020 13:33:51 +0200 Subject: Add a plugin for XEP-0377: spam reporting --- slixmpp/plugins/__init__.py | 1 + slixmpp/plugins/xep_0377/__init__.py | 15 +++++++ slixmpp/plugins/xep_0377/spam_reporting.py | 41 +++++++++++++++++++ slixmpp/plugins/xep_0377/stanza.py | 63 ++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 slixmpp/plugins/xep_0377/__init__.py create mode 100644 slixmpp/plugins/xep_0377/spam_reporting.py create mode 100644 slixmpp/plugins/xep_0377/stanza.py (limited to 'slixmpp') diff --git a/slixmpp/plugins/__init__.py b/slixmpp/plugins/__init__.py index 716b15c3..c7736adc 100644 --- a/slixmpp/plugins/__init__.py +++ b/slixmpp/plugins/__init__.py @@ -85,6 +85,7 @@ __all__ = [ 'xep_0323', # IoT Systems Sensor Data 'xep_0325', # IoT Systems Control 'xep_0332', # HTTP Over XMPP Transport + 'xep_0377', # Spam reporting 'protoxep_reactions', # https://dino.im/xeps/reactions.html 'protoxep_occupantid', # https://dino.im/xeps/occupant-id.html ] diff --git a/slixmpp/plugins/xep_0377/__init__.py b/slixmpp/plugins/xep_0377/__init__.py new file mode 100644 index 00000000..b0802f86 --- /dev/null +++ b/slixmpp/plugins/xep_0377/__init__.py @@ -0,0 +1,15 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 Mathieu Pasquet + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.plugins.base import register_plugin + +from slixmpp.plugins.xep_0377.stanza import Report +from slixmpp.plugins.xep_0377.spam_reporting import XEP_0377 + + +register_plugin(XEP_0377) diff --git a/slixmpp/plugins/xep_0377/spam_reporting.py b/slixmpp/plugins/xep_0377/spam_reporting.py new file mode 100644 index 00000000..e1ca0143 --- /dev/null +++ b/slixmpp/plugins/xep_0377/spam_reporting.py @@ -0,0 +1,41 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 Mathieu Pasquet + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +import logging + +import slixmpp +from slixmpp import Message +from slixmpp.plugins import BasePlugin +from slixmpp.xmlstream import register_stanza_plugin +from slixmpp.xmlstream.handler import Callback +from slixmpp.xmlstream.matcher import StanzaPath +from slixmpp.plugins.xep_0377 import stanza +from slixmpp.plugins.xep_0191 import Block + + +log = logging.getLogger(__name__) + + +class XEP_0377(BasePlugin): + """XEP-0377: Spam reporting""" + + name = 'xep_0377' + description = 'XEP-0377: Spam Reporting' + dependencies = {'xep_0030', 'xep_0191'} + stanza = stanza + + def plugin_init(self): + register_stanza_plugin(Block, stanza.Report) + register_stanza_plugin(stanza.Report, stanza.Text) + + def plugin_end(self): + self.xmpp['xep_0030'].del_feature(feature=stanza.Report.namespace) + + def session_bind(self, jid): + self.xmpp['xep_0030'].add_feature(stanza.Report.namespace) + diff --git a/slixmpp/plugins/xep_0377/stanza.py b/slixmpp/plugins/xep_0377/stanza.py new file mode 100644 index 00000000..89ec66a9 --- /dev/null +++ b/slixmpp/plugins/xep_0377/stanza.py @@ -0,0 +1,63 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 Mathieu Pasquet + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.xmlstream import ET, ElementBase + + +class Report(ElementBase): + """ + A spam/abuse report. + + Example sub stanza: + + + + Never came trouble to my house like this. + + + + + Stanza Interface: + abuse -- Flag the report as abuse + spam -- Flag the report as spam + text -- Add a reason to the report + """ + name = "report" + namespace = "urn:xmpp:reporting:0" + plugin_attrib = "report" + interfaces = ("spam", "abuse", "text") + sub_interfaces = {'text'} + + def get_spam(self): + return self.xml.find('{%s}spam' % self.namespace) is not None + + def set_spam(self, value): + if bool(value) and not self.get_spam(): + self.xml.append(ET.Element('{%s}spam' % self.namespace)) + elif not bool(value): + found = self.xml.findall('{%s}spam' % self.namespace) + if elm: + for item in found: + self.xml.remove(item) + + def get_abuse(self): + return self.xml.find('{%s}abuse' % self.namespace) is not None + + def set_abuse(self, value): + if bool(value) and not self.get_abuse(): + self.xml.append(ET.Element('{%s}abuse' % self.namespace)) + elif not bool(value): + found = self.xml.findall('{%s}abuse' % self.namespace) + if elm: + for item in found: + self.xml.remove(item) + +class Text(ElementBase): + name = "text" + plugin_attrib = "text" + namespace = "urn:xmpp:reporting:0" -- cgit v1.2.3 From ef961392beaf3111d01c32e4d183df681b03d492 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 24 May 2020 13:46:47 +0200 Subject: Enforce and quantity constraints --- slixmpp/plugins/xep_0377/stanza.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'slixmpp') diff --git a/slixmpp/plugins/xep_0377/stanza.py b/slixmpp/plugins/xep_0377/stanza.py index 89ec66a9..1930c382 100644 --- a/slixmpp/plugins/xep_0377/stanza.py +++ b/slixmpp/plugins/xep_0377/stanza.py @@ -26,6 +26,8 @@ class Report(ElementBase): abuse -- Flag the report as abuse spam -- Flag the report as spam text -- Add a reason to the report + + Only one or element can be present at once. """ name = "report" namespace = "urn:xmpp:reporting:0" @@ -33,29 +35,34 @@ class Report(ElementBase): interfaces = ("spam", "abuse", "text") sub_interfaces = {'text'} + def _purge_spam(self): + spam = self.xml.findall('{%s}spam' % self.namespace) + for element in spam: + self.xml.remove(element) + + def _purge_abuse(self): + abuse = self.xml.findall('{%s}abuse' % self.namespace) + for element in abuse: + self.xml.remove(element) + def get_spam(self): return self.xml.find('{%s}spam' % self.namespace) is not None def set_spam(self, value): - if bool(value) and not self.get_spam(): + self._purge_spam() + if bool(value): + self._purge_abuse() self.xml.append(ET.Element('{%s}spam' % self.namespace)) - elif not bool(value): - found = self.xml.findall('{%s}spam' % self.namespace) - if elm: - for item in found: - self.xml.remove(item) def get_abuse(self): return self.xml.find('{%s}abuse' % self.namespace) is not None def set_abuse(self, value): - if bool(value) and not self.get_abuse(): + self._purge_abuse() + if bool(value): + self._purge_spam() self.xml.append(ET.Element('{%s}abuse' % self.namespace)) - elif not bool(value): - found = self.xml.findall('{%s}abuse' % self.namespace) - if elm: - for item in found: - self.xml.remove(item) + class Text(ElementBase): name = "text" -- cgit v1.2.3 From 28123083f7077c990efaf8807fc88246274880b3 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 24 May 2020 15:00:20 +0200 Subject: Add stanza tests for 0377 --- slixmpp/plugins/xep_0377/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'slixmpp') diff --git a/slixmpp/plugins/xep_0377/__init__.py b/slixmpp/plugins/xep_0377/__init__.py index b0802f86..6ae7a097 100644 --- a/slixmpp/plugins/xep_0377/__init__.py +++ b/slixmpp/plugins/xep_0377/__init__.py @@ -8,7 +8,7 @@ from slixmpp.plugins.base import register_plugin -from slixmpp.plugins.xep_0377.stanza import Report +from slixmpp.plugins.xep_0377.stanza import Report, Text from slixmpp.plugins.xep_0377.spam_reporting import XEP_0377 -- cgit v1.2.3