diff options
author | Maxime “pep” Buquet <pep@bouah.net> | 2019-12-20 16:27:32 +0100 |
---|---|---|
committer | Maxime “pep” Buquet <pep@bouah.net> | 2019-12-22 19:27:08 +0100 |
commit | 12a9528d6282303e5e130301a1beca621cfa2e2d (patch) | |
tree | 72fabda1e115d46cbf61e97b21edcc9161bd2799 /poezio/core/commands.py | |
parent | 4d7a19da81d0bb7e8f4e342a5ea39b9a00b26369 (diff) | |
download | poezio-12a9528d6282303e5e130301a1beca621cfa2e2d.tar.gz poezio-12a9528d6282303e5e130301a1beca621cfa2e2d.tar.bz2 poezio-12a9528d6282303e5e130301a1beca621cfa2e2d.tar.xz poezio-12a9528d6282303e5e130301a1beca621cfa2e2d.zip |
WIP: Allow blocking commands in ConversationTab
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
Diffstat (limited to 'poezio/core/commands.py')
-rw-r--r-- | poezio/core/commands.py | 106 |
1 files changed, 104 insertions, 2 deletions
diff --git a/poezio/core/commands.py b/poezio/core/commands.py index 33bd3ecf..50bbca68 100644 --- a/poezio/core/commands.py +++ b/poezio/core/commands.py @@ -7,7 +7,7 @@ from xml.etree import cElementTree as ET from typing import List, Optional, Tuple import logging -from slixmpp import JID, InvalidJID +from slixmpp import Iq, JID, InvalidJID from slixmpp.exceptions import XMPPError from slixmpp.xmlstream.xmlstream import NotConnectedError from slixmpp.xmlstream.stanzabase import StanzaBase @@ -21,7 +21,7 @@ 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 +from poezio.contact import Contact, Resource from poezio.plugin import PluginConfig from poezio.roster import roster from poezio.theming import dump_tuple, get_theme @@ -899,6 +899,108 @@ class CommandCore: self.core.xmpp.plugin['xep_0045'].decline_invite( jid.bare, self.core.pending_invites[jid.bare], reason) + @command_args_parser.quoted(0, 1) + def block(self, args: List[str]) -> None: + """ + /block [jid] + + If a JID is specified, use it. Otherwise if in RosterInfoTab, use the + selected JID, if in ConversationsTab use the Tab's JID. + """ + + jid = None + if args: + try: + jid = JID(args[0]).full + except InvalidJID: + self.core.information('Invalid JID %s' % args, 'Error') + + current_tab = self.core.tabs.current_tab + if jid is None: + if isinstance(current_tab, tabs.RosterInfoTab): + roster_win = self.core.tabs.by_name_and_class( + 'Roster', + tabs.RosterInfoTab, + ) + item = roster_win.selected_row + if isinstance(item, Contact): + jid = item.bare_jid + elif isinstance(item, Resource): + jid = item.jid + + chattabs = ( + tabs.ConversationTab, + tabs.StaticConversationTab, + tabs.DynamicConversationTab, + ) + if isinstance(current_tab, chattabs): + jid = current_tab.jid.bare + + def callback(iq: Iq) -> None: + if iq['type'] == 'error': + return self.core.information( + 'Could not block %s.' % jid, 'Error', + ) + if iq['type'] == 'result': + return self.core.information('Blocked %s.' % jid, 'Info') + return None + + + if jid is not None: + self.core.xmpp.plugin['xep_0191'].block(jid, callback=callback) + else: + self.core.information('No specified JID to block', 'Error') + + @command_args_parser.quoted(0, 1) + def unblock(self, args: List[str]) -> None: + """ + /unblock [jid] + """ + + item = self.core.tabs.by_name_and_class( + 'Roster', + tabs.RosterInfoTab, + ).selected_row + + jid = None + if args: + try: + jid = JID(args[0]).full + except InvalidJID: + self.core.information('Invalid JID %s' % args, 'Error') + + current_tab = self.core.tabs.current_tab + if jid is None: + if isinstance(current_tab, tabs.RosterInfoTab): + roster_win = self.core.tabs.by_name_and_class( + 'Roster', + tabs.RosterInfoTab, + ) + item = roster_win.selected_row + if isinstance(item, Contact): + jid = item.bare_jid + elif isinstance(item, Resource): + jid = item.jid + + chattabs = ( + tabs.ConversationTab, + tabs.StaticConversationTab, + tabs.DynamicConversationTab, + ) + if isinstance(current_tab, chattabs): + jid = current_tab.jid.bare + + if jid is not None: + def callback(iq: Iq): + if iq['type'] == 'error': + return self.core.information('Could not unblock the contact.', + 'Error') + elif iq['type'] == 'result': + return self.core.information('Unblocked %s.' % jid, 'Info') + + self.core.xmpp.plugin['xep_0191'].unblock(jid, callback=callback) + else: + self.core.information('No specified JID to unblock', 'Error') ### Commands without a completion in this class ### |