From 2eb362ffe86dda15e75e3be227acdfce695bfeab Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 9 May 2020 23:12:57 +0200 Subject: Move the deny_anonymous decorator to the poezio.decorators module --- poezio/decorators.py | 12 ++++++++++++ poezio/tabs/rostertab.py | 13 +------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/poezio/decorators.py b/poezio/decorators.py index 4b5d0320..51abf32c 100644 --- a/poezio/decorators.py +++ b/poezio/decorators.py @@ -162,3 +162,15 @@ class CommandArgParser: command_args_parser = CommandArgParser() + + +def deny_anonymous(func: Callable) -> Callable: + """Decorator to disable commands when using an anonymous account.""" + def wrap(self: 'RosterInfoTab', *args, **kwargs): + if self.core.xmpp.anon: + return self.core.information( + 'This command is not available for anonymous accounts.', + 'Info' + ) + return func(self, *args, **kwargs) + return wrap diff --git a/poezio/tabs/rostertab.py b/poezio/tabs/rostertab.py index 50b8c0d5..2cd6e010 100644 --- a/poezio/tabs/rostertab.py +++ b/poezio/tabs/rostertab.py @@ -24,7 +24,7 @@ from poezio.contact import Contact, Resource from poezio.decorators import refresh_wrapper from poezio.roster import RosterGroup, roster from poezio.theming import get_theme, dump_tuple -from poezio.decorators import command_args_parser +from poezio.decorators import command_args_parser, deny_anonymous from poezio.core.structs import Command, Completion from poezio.tabs import Tab from poezio.ui.types import InfoMessage @@ -32,17 +32,6 @@ from poezio.ui.types import InfoMessage log = logging.getLogger(__name__) -def deny_anonymous(func: Callable) -> Callable: - def wrap(self: 'RosterInfoTab', *args, **kwargs): - if self.core.xmpp.anon: - return self.core.information( - 'This command is not available for anonymous accounts.', - 'Info' - ) - return func(self, *args, **kwargs) - return wrap - - class RosterInfoTab(Tab): """ A tab, split in two, containing the roster and infos -- cgit v1.2.3 From eda88a59a6b56c70fcbc9e2f5ef705a17d74c381 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 9 May 2020 23:14:26 +0200 Subject: Add back the deny_anonymous decorator to /add and /accept --- poezio/core/commands.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/poezio/core/commands.py b/poezio/core/commands.py index b00cf24a..f5594d5d 100644 --- a/poezio/core/commands.py +++ b/poezio/core/commands.py @@ -17,11 +17,12 @@ from slixmpp.xmlstream.matcher import StanzaPath from poezio import common from poezio import pep from poezio import tabs +from poezio import multiuserchat as muc from poezio.bookmarks import Bookmark from poezio.common import safeJID from poezio.config import config, DEFAULT_CONFIG, options as config_opts -from poezio import multiuserchat as muc from poezio.contact import Contact, Resource +from poezio.decorators import deny_anonymous from poezio.plugin import PluginConfig from poezio.roster import roster from poezio.theming import dump_tuple, get_theme @@ -517,6 +518,7 @@ class CommandCore: else: self.core.information('No bookmark to remove', 'Info') + @deny_anonymous @command_args_parser.quoted(0, 1) def command_accept(self, args): """ @@ -553,6 +555,7 @@ class CommandCore: pto=jid, ptype='subscribe', pnick=self.core.own_nick) self.core.information('%s is now authorized' % jid, 'Roster') + @deny_anonymous @command_args_parser.quoted(1) def command_add(self, args): """ -- cgit v1.2.3 From e03fb0b3ce41de208e4b52ee1224a080cfc5c6f5 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 9 May 2020 23:32:26 +0200 Subject: Fix completion and function names for /accept and /add --- poezio/core/commands.py | 4 ++-- poezio/core/completions.py | 8 ++++++++ poezio/core/core.py | 7 ++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/poezio/core/commands.py b/poezio/core/commands.py index f5594d5d..60bcc7b0 100644 --- a/poezio/core/commands.py +++ b/poezio/core/commands.py @@ -520,7 +520,7 @@ class CommandCore: @deny_anonymous @command_args_parser.quoted(0, 1) - def command_accept(self, args): + def accept(self, args): """ Accept a JID. Authorize it AND subscribe to it """ @@ -557,7 +557,7 @@ class CommandCore: @deny_anonymous @command_args_parser.quoted(1) - def command_add(self, args): + def add(self, args): """ Add the specified JID to the roster, and automatically accept the reverse subscription diff --git a/poezio/core/completions.py b/poezio/core/completions.py index ee3e95bf..544a7889 100644 --- a/poezio/core/completions.py +++ b/poezio/core/completions.py @@ -44,6 +44,14 @@ class CompletionCore: ' ', quotify=False) + def roster_barejids(self, the_input): + """Complete roster bare jids""" + jids = sorted( + str(contact.bare_jid) for contact in roster.contacts.values() + if contact.pending_in + ) + return Completion(the_input.new_completion, jids, 1, '', quotify=False) + def presence(self, the_input): """ Completion of /presence diff --git a/poezio/core/core.py b/poezio/core/core.py index 8f25d551..a0976c3c 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -1770,14 +1770,15 @@ class Core: completion=self.completion.bookmark) self.register_command( 'accept', - self.command.command_accept, + self.command.accept, usage='[jid]', desc='Allow the provided JID (or the selected contact ' 'in your roster), to see your presence.', - shortdesc='Allow a user your presence.',) + shortdesc='Allow a user your presence.', + completion=self.completion.roster_barejids) self.register_command( 'add', - self.command.command_add, + self.command.add, usage='', desc='Add the specified JID to your roster, ask them to' ' allow you to see his presence, and allow them to' -- cgit v1.2.3 From a1c8efdfb2f28976aaa653e5fc1e695b943bb739 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 10 May 2020 00:10:46 +0200 Subject: Move /deny and /remove to global scope as well --- poezio/core/commands.py | 50 +++++++++++++++++++++++++++++++ poezio/core/completions.py | 5 ++++ poezio/core/core.py | 19 ++++++++++++ poezio/tabs/rostertab.py | 73 ---------------------------------------------- 4 files changed, 74 insertions(+), 73 deletions(-) diff --git a/poezio/core/commands.py b/poezio/core/commands.py index 60bcc7b0..6bf1d338 100644 --- a/poezio/core/commands.py +++ b/poezio/core/commands.py @@ -585,6 +585,56 @@ class CommandCore: roster.modified() self.core.information('%s was added to the roster' % jid, 'Roster') + @deny_anonymous + @command_args_parser.quoted(0, 1) + def deny(self, args): + """ + /deny [jid] + Denies a JID from our roster + """ + jid = None + if not args: + tab = self.core.tabs.current_tab + if isinstance(tab, tabs.RosterInfoTab): + item = tab.roster_win.selected_row + if isinstance(item, Contact): + jid = item.bare_jid + else: + jid = safeJID(args[0]).bare + if jid not in [jid for jid in roster.jids()]: + jid = None + if jid is None: + self.core.information('No subscription to deny', 'Warning') + return + + contact = roster[jid] + if contact: + contact.unauthorize() + self.core.information('Subscription to %s was revoked' % jid, + 'Roster') + + @deny_anonymous + @command_args_parser.quoted(0, 1) + def remove(self, args): + """ + Remove the specified JID from the roster. i.e.: unsubscribe + from its presence, and cancel its subscription to our. + """ + jid = None + if args: + jid = safeJID(args[0]).bare + else: + tab = self.core.tabs.current_tab + if isinstance(tab, tabs.RosterInfoTab): + item = tab.roster_win.selected_row + if isinstance(item, Contact): + jid = item.bare_jid + if jid is None: + self.core.information('No roster item to remove', 'Error') + return + roster.remove(jid) + del roster[jid] + @command_args_parser.ignored def command_reconnect(self): """ diff --git a/poezio/core/completions.py b/poezio/core/completions.py index 544a7889..ada8d2b9 100644 --- a/poezio/core/completions.py +++ b/poezio/core/completions.py @@ -52,6 +52,11 @@ class CompletionCore: ) return Completion(the_input.new_completion, jids, 1, '', quotify=False) + def remove(self, the_input): + """Completion for /remove""" + jids = [jid for jid in roster.jids()] + return Completion(the_input.auto_completion, jids, '', quotify=False) + def presence(self, the_input): """ Completion of /presence diff --git a/poezio/core/core.py b/poezio/core/core.py index a0976c3c..06d56062 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -1784,6 +1784,25 @@ class Core: ' allow you to see his presence, and allow them to' ' see your presence.', shortdesc='Add a user to your roster.') + self.register_command( + 'deny', + self.command.deny, + usage='[jid]', + desc='Deny your presence to the provided JID (or the ' + 'selected contact in your roster), who is asking' + 'you to be in their roster.', + shortdesc='Deny a user your presence.', + completion=self.completion.roster_barejids) + self.register_command( + 'remove', + self.command.remove, + usage='[jid]', + desc='Remove the specified JID from your roster. This ' + 'will unsubscribe you from its presence, cancel ' + 'its subscription to yours, and remove the item ' + 'from your roster.', + shortdesc='Remove a user from your roster.', + completion=self.completion.remove) self.register_command( 'reconnect', self.command.command_reconnect, diff --git a/poezio/tabs/rostertab.py b/poezio/tabs/rostertab.py index 2cd6e010..072e0776 100644 --- a/poezio/tabs/rostertab.py +++ b/poezio/tabs/rostertab.py @@ -72,15 +72,6 @@ class RosterInfoTab(Tab): self.key_func["s"] = self.start_search self.key_func["S"] = self.start_search_slow self.key_func["n"] = self.change_contact_name - self.register_command( - 'deny', - self.command_deny, - usage='[jid]', - desc='Deny your presence to the provided JID (or the ' - 'selected contact in your roster), who is asking' - 'you to be in their roster.', - shortdesc='Deny a user your presence.', - completion=self.completion_deny) self.register_command( 'name', self.command_name, @@ -108,16 +99,6 @@ class RosterInfoTab(Tab): desc='Remove the given JID from the given group.', shortdesc='Remove a user from a group.', completion=self.completion_groupremove) - self.register_command( - 'remove', - self.command_remove, - usage='[jid]', - desc='Remove the specified JID from your roster. This ' - 'will unsubscribe you from its presence, cancel ' - 'its subscription to yours, and remove the item ' - 'from your roster.', - shortdesc='Remove a user from your roster.', - completion=self.completion_remove) self.register_command( 'export', self.command_export, @@ -559,31 +540,6 @@ class RosterInfoTab(Tab): self.core.xmpp.plugin['xep_0077'].change_password( args[0], callback=callback) - @deny_anonymous - @command_args_parser.quoted(0, 1) - def command_deny(self, args): - """ - /deny [jid] - Denies a JID from our roster - """ - if not args: - item = self.roster_win.selected_row - if isinstance(item, Contact): - jid = item.bare_jid - else: - self.core.information('No subscription to deny', 'Warning') - return - else: - jid = safeJID(args[0]).bare - if jid not in [jid for jid in roster.jids()]: - self.core.information('No subscription to deny', 'Warning') - return - - contact = roster[jid] - if contact: - contact.unauthorize() - self.core.information('Subscription to %s was revoked' % jid, - 'Roster') @deny_anonymous @command_args_parser.quoted(1, 1) @@ -779,25 +735,6 @@ class RosterInfoTab(Tab): subscription=subscription, callback=callback) - @deny_anonymous - @command_args_parser.quoted(0, 1) - def command_remove(self, args): - """ - Remove the specified JID from the roster. i.e.: unsubscribe - from its presence, and cancel its subscription to our. - """ - if args: - jid = safeJID(args[0]).bare - else: - item = self.roster_win.selected_row - if isinstance(item, Contact): - jid = item.bare_jid - else: - self.core.information('No roster item to remove', 'Error') - return - roster.remove(jid) - del roster[jid] - @deny_anonymous @command_args_parser.quoted(0, 1) def command_import(self, args): @@ -922,16 +859,6 @@ class RosterInfoTab(Tab): the_input.new_completion, groups, n, '', quotify=True) return False - def completion_deny(self, the_input): - """ - Complete the first argument from the list of the - contact with ask=='subscribe' - """ - jids = sorted( - str(contact.bare_jid) for contact in roster.contacts.values() - if contact.pending_in) - return Completion(the_input.new_completion, jids, 1, '', quotify=False) - def refresh(self): if self.need_resize: self.resize() -- cgit v1.2.3