cq: Format with black and isort

This commit is contained in:
Philipp Hörist
2025-01-25 19:15:37 +01:00
parent e6e71d82bf
commit 841b1fb25e
44 changed files with 1641 additions and 1660 deletions

View File

@@ -12,37 +12,39 @@
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
'''
"""
:author: Yann Leboulanger <asterix@lagaule.org>
:since: 16 August 2012
:copyright: Copyright (2012) Yann Leboulanger <asterix@lagaule.org>
:license: GPLv3
'''
"""
from functools import partial
from gajim.plugins import GajimPlugin
from gajim.plugins.plugins_i18n import _
from anti_spam.modules import anti_spam
from anti_spam.config_dialog import AntiSpamConfigDialog
from anti_spam.modules import anti_spam
class AntiSpamPlugin(GajimPlugin):
def init(self) -> None:
self.description = _('Allows you to block various kinds of incoming '
'messages (Spam, XHTML formatting, etc.)')
self.description = _(
"Allows you to block various kinds of incoming "
"messages (Spam, XHTML formatting, etc.)"
)
self.config_dialog = partial(AntiSpamConfigDialog, self)
self.config_default_values = {
'disable_xhtml_muc': (False, ''),
'disable_xhtml_pm': (False, ''),
'block_subscription_requests': (False, ''),
'msgtxt_limit': (0, ''),
'msgtxt_question': ('12 x 12 = ?', ''),
'msgtxt_answer': ('', ''),
'antispam_for_conference': (False, ''),
'block_domains': ('', ''),
'whitelist': ([], ''),
"disable_xhtml_muc": (False, ""),
"disable_xhtml_pm": (False, ""),
"block_subscription_requests": (False, ""),
"msgtxt_limit": (0, ""),
"msgtxt_question": ("12 x 12 = ?", ""),
"msgtxt_answer": ("", ""),
"antispam_for_conference": (False, ""),
"block_domains": ("", ""),
"whitelist": ([], ""),
}
self.gui_extension_points = {}
self.modules = [anti_spam]

View File

@@ -21,11 +21,10 @@ from typing import TYPE_CHECKING
from gi.repository import Gtk
from gajim.gtk.settings import SettingsDialog
from gajim.gtk.const import Setting
from gajim.gtk.const import SettingKind
from gajim.gtk.const import SettingType
from gajim.gtk.settings import SettingsDialog
from gajim.plugins.plugins_i18n import _
if TYPE_CHECKING:
@@ -35,72 +34,89 @@ if TYPE_CHECKING:
class AntiSpamConfigDialog(SettingsDialog):
def __init__(self, plugin: AntiSpamPlugin, parent: Gtk.Window) -> None:
self.plugin = plugin
msgtxt_limit = cast(int, self.plugin.config['msgtxt_limit'])
max_length = '' if msgtxt_limit == 0 else msgtxt_limit
msgtxt_limit = cast(int, self.plugin.config["msgtxt_limit"])
max_length = "" if msgtxt_limit == 0 else msgtxt_limit
settings = [
Setting(SettingKind.ENTRY,
_('Limit Message Length'),
SettingType.VALUE,
str(max_length),
callback=self._on_length_setting,
data='msgtxt_limit',
desc=_('Limits maximum message length (leave empty to '
'disable)')),
Setting(SettingKind.SWITCH,
_('Deny Subscription Requests'),
SettingType.VALUE,
self.plugin.config['block_subscription_requests'],
callback=self._on_setting,
data='block_subscription_requests'),
Setting(SettingKind.SWITCH,
_('Disable XHTML for Group Chats'),
SettingType.VALUE,
self.plugin.config['disable_xhtml_muc'],
callback=self._on_setting,
data='disable_xhtml_muc',
desc=_('Removes XHTML formatting from group chat '
'messages')),
Setting(SettingKind.SWITCH,
_('Disable XHTML for PMs'),
SettingType.VALUE,
self.plugin.config['disable_xhtml_pm'],
callback=self._on_setting,
data='disable_xhtml_pm',
desc=_('Removes XHTML formatting from private messages '
'in group chats')),
Setting(SettingKind.ENTRY,
_('Anti Spam Question'),
SettingType.VALUE,
self.plugin.config['msgtxt_question'],
callback=self._on_setting,
data='msgtxt_question',
desc=_('Question has to be answered in order to '
'contact you')),
Setting(SettingKind.ENTRY,
_('Anti Spam Answer'),
SettingType.VALUE,
self.plugin.config['msgtxt_answer'],
callback=self._on_setting,
data='msgtxt_answer',
desc=_('Correct answer to your Anti Spam Question '
'(leave empty to disable question)')),
Setting(SettingKind.SWITCH,
_('Anti Spam Question in Group Chats'),
SettingType.VALUE,
self.plugin.config['antispam_for_conference'],
callback=self._on_setting,
data='antispam_for_conference',
desc=_('Enables anti spam question for private messages '
'in group chats')),
]
Setting(
SettingKind.ENTRY,
_("Limit Message Length"),
SettingType.VALUE,
str(max_length),
callback=self._on_length_setting,
data="msgtxt_limit",
desc=_("Limits maximum message length (leave empty to " "disable)"),
),
Setting(
SettingKind.SWITCH,
_("Deny Subscription Requests"),
SettingType.VALUE,
self.plugin.config["block_subscription_requests"],
callback=self._on_setting,
data="block_subscription_requests",
),
Setting(
SettingKind.SWITCH,
_("Disable XHTML for Group Chats"),
SettingType.VALUE,
self.plugin.config["disable_xhtml_muc"],
callback=self._on_setting,
data="disable_xhtml_muc",
desc=_("Removes XHTML formatting from group chat " "messages"),
),
Setting(
SettingKind.SWITCH,
_("Disable XHTML for PMs"),
SettingType.VALUE,
self.plugin.config["disable_xhtml_pm"],
callback=self._on_setting,
data="disable_xhtml_pm",
desc=_(
"Removes XHTML formatting from private messages " "in group chats"
),
),
Setting(
SettingKind.ENTRY,
_("Anti Spam Question"),
SettingType.VALUE,
self.plugin.config["msgtxt_question"],
callback=self._on_setting,
data="msgtxt_question",
desc=_("Question has to be answered in order to " "contact you"),
),
Setting(
SettingKind.ENTRY,
_("Anti Spam Answer"),
SettingType.VALUE,
self.plugin.config["msgtxt_answer"],
callback=self._on_setting,
data="msgtxt_answer",
desc=_(
"Correct answer to your Anti Spam Question "
"(leave empty to disable question)"
),
),
Setting(
SettingKind.SWITCH,
_("Anti Spam Question in Group Chats"),
SettingType.VALUE,
self.plugin.config["antispam_for_conference"],
callback=self._on_setting,
data="antispam_for_conference",
desc=_(
"Enables anti spam question for private messages " "in group chats"
),
),
]
SettingsDialog.__init__(self,
parent,
_('Anti Spam Configuration'),
Gtk.DialogFlags.MODAL,
settings,
'')
SettingsDialog.__init__(
self,
parent,
_("Anti Spam Configuration"),
Gtk.DialogFlags.MODAL,
settings,
"",
)
def _on_setting(self, value: Any, data: Any) -> None:
self.plugin.config[data] = value

View File

@@ -32,7 +32,7 @@ from gajim.common.events import MessageSent
from gajim.common.modules.base import BaseModule
# Module name
name = 'AntiSpam'
name = "AntiSpam"
zeroconf = False
@@ -41,21 +41,23 @@ class AntiSpam(BaseModule):
BaseModule.__init__(self, client, plugin=True)
self.handlers = [
StanzaHandler(name='message',
callback=self._message_received,
priority=48),
StanzaHandler(name='presence',
callback=self._subscribe_received,
typ='subscribe',
priority=48),
StanzaHandler(name="message", callback=self._message_received, priority=48),
StanzaHandler(
name="presence",
callback=self._subscribe_received,
typ="subscribe",
priority=48,
),
]
self.register_events([
('message-sent', ged.GUI2, self._on_message_sent),
])
self.register_events(
[
("message-sent", ged.GUI2, self._on_message_sent),
]
)
for plugin in app.plugin_manager.plugins:
if plugin.manifest.short_name == 'anti_spam':
if plugin.manifest.short_name == "anti_spam":
self._config = plugin.config
self._contacted_jids: set[JID] = set()
@@ -66,11 +68,9 @@ class AntiSpam(BaseModule):
# This set contains JIDs of all outgoing chats.
self._contacted_jids.add(event.jid)
def _message_received(self,
_con: Client,
_stanza: Message,
properties: MessageProperties
) -> None:
def _message_received(
self, _con: Client, _stanza: Message, properties: MessageProperties
) -> None:
if properties.is_sent_carbon:
# Another device already sent a message
@@ -86,33 +86,35 @@ class AntiSpam(BaseModule):
raise NodeProcessed
msg_from = properties.jid
limit = cast(int, self._config['msgtxt_limit'])
limit = cast(int, self._config["msgtxt_limit"])
if limit > 0 and len(msg_body) > limit:
self._log.info('Discarded message from %s: message '
'length exceeded' % msg_from)
self._log.info(
"Discarded message from %s: message " "length exceeded" % msg_from
)
raise NodeProcessed
if self._config['disable_xhtml_muc'] and properties.type.is_groupchat:
if self._config["disable_xhtml_muc"] and properties.type.is_groupchat:
properties.xhtml = None
self._log.info('Stripped message from %s: message '
'contained XHTML' % msg_from)
self._log.info(
"Stripped message from %s: message " "contained XHTML" % msg_from
)
if self._config['disable_xhtml_pm'] and properties.is_muc_pm:
if self._config["disable_xhtml_pm"] and properties.is_muc_pm:
properties.xhtml = None
self._log.info('Stripped message from %s: message '
'contained XHTML' % msg_from)
self._log.info(
"Stripped message from %s: message " "contained XHTML" % msg_from
)
def _ask_question(self, properties: MessageProperties) -> bool:
answer = cast(str, self._config['msgtxt_answer'])
answer = cast(str, self._config["msgtxt_answer"])
if len(answer) == 0:
return False
is_muc_pm = properties.is_muc_pm
if is_muc_pm and not self._config['antispam_for_conference']:
if is_muc_pm and not self._config["antispam_for_conference"]:
return False
if (properties.type.value not in ('chat', 'normal') or
properties.is_mam_message):
if properties.type.value not in ("chat", "normal") or properties.is_mam_message:
return False
assert properties.jid
@@ -126,15 +128,15 @@ class AntiSpam(BaseModule):
# If we receive a PM or a message from an unknown user, our anti spam
# question will silently be sent in the background
whitelist = cast(list[str], self._config['whitelist'])
whitelist = cast(list[str], self._config["whitelist"])
if str(msg_from) in whitelist:
return False
roster_item = self._client.get_module('Roster').get_item(msg_from)
roster_item = self._client.get_module("Roster").get_item(msg_from)
if is_muc_pm or roster_item is None:
assert properties.body
if answer in properties.body.split('\n'):
if answer in properties.body.split("\n"):
if str(msg_from) not in whitelist:
whitelist.append(str(msg_from))
# We need to explicitly save, because 'append' does not
@@ -146,26 +148,24 @@ class AntiSpam(BaseModule):
return False
def _send_question(self, properties: MessageProperties, jid: JID) -> None:
message = 'Anti Spam Question: %s' % self._config['msgtxt_question']
message = "Anti Spam Question: %s" % self._config["msgtxt_question"]
stanza = Message(to=jid, body=message, typ=properties.type.value)
self._client.connection.send_stanza(stanza)
self._log.info('Anti spam question sent to %s', jid)
self._log.info("Anti spam question sent to %s", jid)
def _subscribe_received(self,
_con: Client,
_stanza: Presence,
properties: PresenceProperties
) -> None:
def _subscribe_received(
self, _con: Client, _stanza: Presence, properties: PresenceProperties
) -> None:
msg_from = properties.jid
block_sub = self._config['block_subscription_requests']
roster_item = self._client.get_module('Roster').get_item(msg_from)
block_sub = self._config["block_subscription_requests"]
roster_item = self._client.get_module("Roster").get_item(msg_from)
if block_sub and roster_item is None:
self._client.get_module('Presence').unsubscribed(msg_from)
self._log.info('Denied subscription request from %s' % msg_from)
self._client.get_module("Presence").unsubscribed(msg_from)
self._log.info("Denied subscription request from %s" % msg_from)
raise NodeProcessed
def get_instance(*args: Any, **kwargs: Any) -> tuple[AntiSpam, str]:
return AntiSpam(*args, **kwargs), 'AntiSpam'
return AntiSpam(*args, **kwargs), "AntiSpam"