summaryrefslogtreecommitdiff
path: root/poezio/core/completions.py
diff options
context:
space:
mode:
Diffstat (limited to 'poezio/core/completions.py')
-rw-r--r--poezio/core/completions.py130
1 files changed, 79 insertions, 51 deletions
diff --git a/poezio/core/completions.py b/poezio/core/completions.py
index 87bb2d47..084910a2 100644
--- a/poezio/core/completions.py
+++ b/poezio/core/completions.py
@@ -2,23 +2,23 @@
Completions for the global commands
"""
import logging
-
-log = logging.getLogger(__name__)
-
import os
-from pathlib import Path
from functools import reduce
+from pathlib import Path
+from typing import List, Optional
+
+from slixmpp import JID, InvalidJID
from poezio import common
-from poezio import pep
from poezio import tabs
from poezio import xdg
-from poezio.common import safeJID
from poezio.config import config
from poezio.roster import roster
from poezio.core.structs import POSSIBLE_SHOW, Completion
+log = logging.getLogger(__name__)
+
class CompletionCore:
def __init__(self, core):
@@ -41,6 +41,19 @@ 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 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
@@ -67,7 +80,7 @@ class CompletionCore:
def theme(self, the_input):
""" Completion for /theme"""
- themes_dir = config.get('themes_dir')
+ themes_dir = config.getstr('themes_dir')
themes_dir = Path(themes_dir).expanduser(
) if themes_dir else xdg.DATA_HOME / 'themes'
try:
@@ -109,9 +122,12 @@ class CompletionCore:
return False
if len(args) == 1:
args.append('')
- jid = safeJID(args[1])
- if args[1].endswith('@') and not jid.user and not jid.server:
- jid.user = args[1][:-1]
+ try:
+ jid = JID(args[1])
+ except InvalidJID:
+ jid = JID('')
+ if args[1].endswith('@'):
+ jid.user = args[1][:-1]
relevant_rooms = []
relevant_rooms.extend(sorted(self.core.pending_invites.keys()))
@@ -134,7 +150,8 @@ class CompletionCore:
for tab in self.core.get_tabs(tabs.MucTab):
if tab.joined:
serv_list.append(
- '%s@%s' % (jid.user, safeJID(tab.name).host))
+ '%s@%s' % (jid.user, tab.general_jid.server)
+ )
serv_list.extend(relevant_rooms)
return Completion(
the_input.new_completion, serv_list, 1, quotify=True)
@@ -161,8 +178,8 @@ class CompletionCore:
muc_serv_list = []
for tab in self.core.get_tabs(
tabs.MucTab): # TODO, also from an history
- if tab.name not in muc_serv_list:
- muc_serv_list.append(safeJID(tab.name).server)
+ if tab.jid.server not in muc_serv_list:
+ muc_serv_list.append(tab.jid.server)
if muc_serv_list:
return Completion(
the_input.new_completion, muc_serv_list, 1, quotify=False)
@@ -198,14 +215,13 @@ class CompletionCore:
if len(args) == 1:
args.append('')
- jid = safeJID(args[1])
-
- if jid.server and (jid.resource or jid.full.endswith('/')):
+ try:
+ jid = JID(args[1])
tab = self.core.tabs.by_name_and_class(jid.bare, tabs.MucTab)
nicks = [tab.own_nick] if tab else []
default = os.environ.get('USER') if os.environ.get(
'USER') else 'poezio'
- nick = config.get('default_nick')
+ nick = config.getstr('default_nick')
if not nick:
if default not in nicks:
nicks.append(default)
@@ -215,6 +231,8 @@ class CompletionCore:
jids_list = ['%s/%s' % (jid.bare, nick) for nick in nicks]
return Completion(
the_input.new_completion, jids_list, 1, quotify=True)
+ except InvalidJID:
+ pass
muc_list = [tab.name for tab in self.core.get_tabs(tabs.MucTab)]
muc_list.sort()
muc_list.append('*')
@@ -284,7 +302,7 @@ class CompletionCore:
rooms = []
for tab in self.core.get_tabs(tabs.MucTab):
if tab.joined:
- rooms.append(tab.name)
+ rooms.append(tab.jid.bare)
rooms.sort()
return Completion(
the_input.new_completion, rooms, n, '', quotify=True)
@@ -302,33 +320,6 @@ class CompletionCore:
comp = sorted(onlines) + sorted(offlines)
return Completion(the_input.new_completion, comp, n, quotify=True)
- def activity(self, the_input):
- """Completion for /activity"""
- n = the_input.get_argument_position(quoted=True)
- args = common.shell_split(the_input.text)
- if n == 1:
- return Completion(
- the_input.new_completion,
- sorted(pep.ACTIVITIES.keys()),
- n,
- quotify=True)
- elif n == 2:
- if args[1] in pep.ACTIVITIES:
- l = list(pep.ACTIVITIES[args[1]])
- l.remove('category')
- l.sort()
- return Completion(the_input.new_completion, l, n, quotify=True)
-
- def mood(self, the_input):
- """Completion for /mood"""
- n = the_input.get_argument_position(quoted=True)
- if n == 1:
- return Completion(
- the_input.new_completion,
- sorted(pep.MOODS.keys()),
- 1,
- quotify=True)
-
def last_activity(self, the_input):
"""
Completion for /last_activity <jid>
@@ -346,8 +337,7 @@ class CompletionCore:
"""Completion for /server_cycle"""
serv_list = set()
for tab in self.core.get_tabs(tabs.MucTab):
- serv = safeJID(tab.name).server
- serv_list.add(serv)
+ serv_list.add(tab.jid.server)
return Completion(the_input.new_completion, sorted(serv_list), 1, ' ')
def set(self, the_input):
@@ -442,14 +432,13 @@ class CompletionCore:
return False
if len(args) == 1:
args.append('')
- jid = safeJID(args[1])
-
- if jid.server and (jid.resource or jid.full.endswith('/')):
+ try:
+ jid = JID(args[1])
tab = self.core.tabs.by_name_and_class(jid.bare, tabs.MucTab)
nicks = [tab.own_nick] if tab else []
default = os.environ.get('USER') if os.environ.get(
'USER') else 'poezio'
- nick = config.get('default_nick')
+ nick = config.getstr('default_nick')
if not nick:
if default not in nicks:
nicks.append(default)
@@ -459,6 +448,45 @@ class CompletionCore:
jids_list = ['%s/%s' % (jid.bare, nick) for nick in nicks]
return Completion(
the_input.new_completion, jids_list, 1, quotify=True)
+ except InvalidJID:
+ pass
muc_list = [tab.name for tab in self.core.get_tabs(tabs.MucTab)]
muc_list.append('*')
return Completion(the_input.new_completion, muc_list, 1, quotify=True)
+
+ def block(self, the_input) -> Optional[Completion]:
+ """
+ Completion for /block
+ """
+ if the_input.get_argument_position() == 1:
+
+ current_tab = self.core.tabs.current_tab
+ chattabs = (
+ tabs.ConversationTab,
+ tabs.StaticConversationTab,
+ tabs.DynamicConversationTab,
+ )
+ tabjid: List[str] = []
+ if isinstance(current_tab, chattabs):
+ tabjid = [current_tab.jid.bare]
+
+ jids = [str(i) for i in roster.jids()]
+ jids += tabjid
+ return Completion(
+ the_input.new_completion, jids, 1, '', quotify=False)
+ return None
+
+ def unblock(self, the_input) -> Optional[Completion]:
+ """
+ Completion for /unblock
+ """
+
+ def on_result(iq):
+ if iq['type'] == 'error':
+ return None
+ l = sorted(str(item) for item in iq['blocklist']['items'])
+ return Completion(the_input.new_completion, l, 1, quotify=False)
+
+ if the_input.get_argument_position():
+ self.core.xmpp.plugin['xep_0191'].get_blocked(callback=on_result)
+ return None