core commands: remove all remaining usage of safeJID()

This commit is contained in:
Emmanuel Gil Peyrot 2021-02-04 00:39:40 +01:00 committed by mathieui
parent 3b7e5f93bd
commit ac96beb240

View file

@ -19,7 +19,6 @@ from poezio import pep
from poezio import tabs from poezio import tabs
from poezio import multiuserchat as muc from poezio import multiuserchat as muc
from poezio.bookmarks import Bookmark from poezio.bookmarks import Bookmark
from poezio.common import safeJID
from poezio.config import config, DEFAULT_CONFIG, options as config_opts from poezio.config import config, DEFAULT_CONFIG, options as config_opts
from poezio.contact import Contact, Resource from poezio.contact import Contact, Resource
from poezio.decorators import deny_anonymous from poezio.decorators import deny_anonymous
@ -631,9 +630,12 @@ class CommandCore:
else: else:
return self.core.information('No subscription to accept', 'Warning') return self.core.information('No subscription to accept', 'Warning')
else: else:
jid = safeJID(args[0]).bare try:
nodepart = safeJID(jid).user jid = JID(args[0]).bare
jid = safeJID(jid) except InvalidJID:
return self.core.information('Invalid JID for /accept: %s' % args[0], 'Error')
jid = JID(jid)
nodepart = jid.user
# crappy transports putting resources inside the node part # crappy transports putting resources inside the node part
if '\\2f' in nodepart: if '\\2f' in nodepart:
jid.user = nodepart.split('\\2f')[0] jid.user = nodepart.split('\\2f')[0]
@ -669,11 +671,10 @@ class CommandCore:
return self.core.information('%s was added to the roster' % jid, 'Roster') return self.core.information('%s was added to the roster' % jid, 'Roster')
else: else:
return self.core.information('No JID specified', 'Error') return self.core.information('No JID specified', 'Error')
jid = safeJID(safeJID(args[0]).bare) try:
if not str(jid): jid = JID(args[0]).bare
self.core.information( except InvalidJID:
'The provided JID (%s) is not valid' % (args[0], ), 'Error') return self.core.information('Invalid JID for /add: %s' % args[0], 'Error')
return
if jid in roster and roster[jid].subscription in ('to', 'both'): if jid in roster and roster[jid].subscription in ('to', 'both'):
return self.core.information('Already subscribed.', 'Roster') return self.core.information('Already subscribed.', 'Roster')
roster.add(jid) roster.add(jid)
@ -695,7 +696,10 @@ class CommandCore:
if isinstance(item, Contact): if isinstance(item, Contact):
jid = item.bare_jid jid = item.bare_jid
else: else:
jid = safeJID(args[0]).bare try:
jid = JID(args[0]).bare
except InvalidJID:
return self.core.information('Invalid JID for /deny: %s' % args[0], 'Error')
if jid not in [jid for jid in roster.jids()]: if jid not in [jid for jid in roster.jids()]:
jid = None jid = None
if jid is None: if jid is None:
@ -717,7 +721,10 @@ class CommandCore:
""" """
jid = None jid = None
if args: if args:
jid = safeJID(args[0]).bare try:
jid = JID(args[0]).bare
except InvalidJID:
return self.core.information('Invalid JID for /remove: %s' % args[0], 'Error')
else: else:
tab = self.core.tabs.current_tab tab = self.core.tabs.current_tab
if isinstance(tab, tabs.RosterInfoTab): if isinstance(tab, tabs.RosterInfoTab):
@ -914,7 +921,7 @@ class CommandCore:
seconds = iq['last_activity']['seconds'] seconds = iq['last_activity']['seconds']
status = iq['last_activity']['status'] status = iq['last_activity']['status']
from_ = iq['from'] from_ = iq['from']
if not safeJID(from_).user: if not from_.user:
msg = 'The uptime of %s is %s.' % ( msg = 'The uptime of %s is %s.' % (
from_, common.parse_secs_to_str(seconds)) from_, common.parse_secs_to_str(seconds))
else: else:
@ -926,7 +933,10 @@ class CommandCore:
if args is None: if args is None:
return self.help('last_activity') return self.help('last_activity')
jid = safeJID(args[0]) try:
jid = JID(args[0])
except InvalidJID:
return self.core.information('Invalid JID for /last_activity: %s' % args[0], 'Error')
self.core.xmpp.plugin['xep_0012'].get_last_activity( self.core.xmpp.plugin['xep_0012'].get_last_activity(
jid, callback=callback) jid, callback=callback)
@ -1030,7 +1040,11 @@ class CommandCore:
jids.add(current_tab.general_jid) jids.add(current_tab.general_jid)
for jid in common.shell_split(' '.join(args)): for jid in common.shell_split(' '.join(args)):
jids.add(safeJID(jid).bare) try:
bare = JID(jid).bare
except InvalidJID:
return self.core.information('Invalid JID for /impromptu: %s' % args[0], 'Error')
jids.add(bare)
asyncio.ensure_future(self.core.impromptu(jids)) asyncio.ensure_future(self.core.impromptu(jids))
self.core.information('Invited %s to a random room' % (', '.join(jids)), 'Info') self.core.information('Invited %s to a random room' % (', '.join(jids)), 'Info')
@ -1040,7 +1054,10 @@ class CommandCore:
"""/decline <room@server.tld> [reason]""" """/decline <room@server.tld> [reason]"""
if args is None: if args is None:
return self.help('decline') return self.help('decline')
jid = safeJID(args[0]) try:
jid = JID(args[0])
except InvalidJID:
return self.core.information('Invalid JID for /decline: %s' % args[0], 'Error')
if jid.bare not in self.core.pending_invites: if jid.bare not in self.core.pending_invites:
return return
reason = args[1] reason = args[1]
@ -1162,15 +1179,18 @@ class CommandCore:
@command_args_parser.ignored @command_args_parser.ignored
def invitations(self): def invitations(self):
"""/invitations""" """/invitations"""
build = "" build = []
for invite in self.core.pending_invites: for invite in self.core.pending_invites:
build += "%s by %s" % ( try:
invite, safeJID(self.core.pending_invites[invite]).bare) bare = JID(self.core.pending_invites[invite]).bare
if self.core.pending_invites: except InvalidJID:
build = "You are invited to the following rooms:\n" + build self.core.information('Invalid JID found in /invitations: %s' % args[0], 'Error')
build.append('%s by %s' % (invite, bare))
if build:
message = 'You are invited to the following rooms:\n' + ','.join(build)
else: else:
build = "You do not have any pending invitations." message = 'You do not have any pending invitations.'
self.core.information(build, 'Info') self.core.information(message, 'Info')
@command_args_parser.quoted(0, 1, [None]) @command_args_parser.quoted(0, 1, [None])
def quit(self, args): def quit(self, args):
@ -1331,7 +1351,10 @@ class CommandCore:
""" """
if args is None: if args is None:
return self.help('message') return self.help('message')
jid = safeJID(args[0]) try:
jid = JID(args[0])
except InvalidJID:
return self.core.information('Invalid JID for /message: %s' % args[0], 'Error')
if not jid.user and not jid.domain and not jid.resource: if not jid.user and not jid.domain and not jid.resource:
return self.core.information('Invalid JID.', 'Error') return self.core.information('Invalid JID.', 'Error')
tab = self.core.get_conversation_by_jid( tab = self.core.get_conversation_by_jid(