poezio/core/handlers: Use Config from Core

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-08-31 16:13:28 +02:00
parent b56cfbafd9
commit 615120c761
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -26,7 +26,7 @@ from poezio import tabs
from poezio import xhtml from poezio import xhtml
from poezio import multiuserchat as muc from poezio import multiuserchat as muc
from poezio.common import get_error_message from poezio.common import get_error_message
from poezio.config import config, get_image_cache from poezio.config import get_image_cache
from poezio.core.structs import Status from poezio.core.structs import Status
from poezio.contact import Resource from poezio.contact import Resource
from poezio.logger import logger from poezio.logger import logger
@ -93,7 +93,7 @@ class HandlerCore:
rostertab.check_saslexternal(features) rostertab.check_saslexternal(features)
rostertab.check_blocking(features) rostertab.check_blocking(features)
self.core.check_blocking(features) self.core.check_blocking(features)
if (config.getbool('enable_carbons') if (self.core.config.getbool('enable_carbons')
and 'urn:xmpp:carbons:2' in features): and 'urn:xmpp:carbons:2' in features):
self.core.xmpp.plugin['xep_0280'].enable() self.core.xmpp.plugin['xep_0280'].enable()
await self.core.check_bookmark_storage(features) await self.core.check_bookmark_storage(features)
@ -219,7 +219,7 @@ class HandlerCore:
if password: if password:
msg += ". The password is \"%s\"." % password msg += ". The password is \"%s\"." % password
self.core.information(msg, 'Info') self.core.information(msg, 'Info')
if 'invite' in config.getstr('beep_on').split(): if 'invite' in self.core.config.getstr('beep_on').split():
curses.beep() curses.beep()
logger.log_roster_change(inviter.full, 'invited you to %s' % jid.full) logger.log_roster_change(inviter.full, 'invited you to %s' % jid.full)
self.core.pending_invites[jid.bare] = inviter.full self.core.pending_invites[jid.bare] = inviter.full
@ -253,7 +253,7 @@ class HandlerCore:
msg += "\nreason: %s" % reason msg += "\nreason: %s" % reason
self.core.information(msg, 'Info') self.core.information(msg, 'Info')
if 'invite' in config.getstr('beep_on').split(): if 'invite' in self.core.config.getstr('beep_on').split():
curses.beep() curses.beep()
self.core.pending_invites[room.bare] = inviter.full self.core.pending_invites[room.bare] = inviter.full
@ -324,7 +324,7 @@ class HandlerCore:
return self.core.information( return self.core.information(
'%s says: %s' % (message['from'], message['body']), 'Headline') '%s says: %s' % (message['from'], message['body']), 'Headline')
use_xhtml = config.get_by_tabname('enable_xhtml_im', use_xhtml = self.core.config.get_by_tabname('enable_xhtml_im',
message['from'].bare) message['from'].bare)
tmp_dir = get_image_cache() tmp_dir = get_image_cache()
if not xhtml.get_body_from_message_stanza( if not xhtml.get_body_from_message_stanza(
@ -356,8 +356,8 @@ class HandlerCore:
else: else:
await conversation.handle_message(message) await conversation.handle_message(message)
if not own and 'private' in config.getstr('beep_on').split(): if not own and 'private' in self.core.config.getstr('beep_on').split():
if not config.get_by_tabname('disable_beep', conv_jid.bare): if not self.core.config.get_by_tabname('disable_beep', conv_jid.bare):
curses.beep() curses.beep()
if self.core.tabs.current_tab is not conversation: if self.core.tabs.current_tab is not conversation:
if not own: if not own:
@ -487,8 +487,8 @@ class HandlerCore:
self.core.xmpp, room_from, self.core.own_nick, msg='') self.core.xmpp, room_from, self.core.own_nick, msg='')
return return
valid_message = await tab.handle_message(message) valid_message = await tab.handle_message(message)
if valid_message and 'message' in config.getstr('beep_on').split(): if valid_message and 'message' in self.core.config.getstr('beep_on').split():
if (not config.get_by_tabname('disable_beep', room_from) if (not self.core.config.get_by_tabname('disable_beep', room_from)
and self.core.own_nick != message['from'].resource): and self.core.own_nick != message['from'].resource):
curses.beep() curses.beep()
@ -509,7 +509,7 @@ class HandlerCore:
return return
room_from = jid.bare room_from = jid.bare
use_xhtml = config.get_by_tabname( use_xhtml = self.core.config.get_by_tabname(
'enable_xhtml_im', 'enable_xhtml_im',
jid.bare jid.bare
) )
@ -519,10 +519,10 @@ class HandlerCore:
tab = self.core.tabs.by_name_and_class( tab = self.core.tabs.by_name_and_class(
jid.full, jid.full,
tabs.PrivateTab) # get the tab with the private conversation tabs.PrivateTab) # get the tab with the private conversation
ignore = config.get_by_tabname('ignore_private', room_from) ignore = self.core.config.get_by_tabname('ignore_private', room_from)
if ignore and not sent: if ignore and not sent:
await self.core.events.trigger_async('ignored_private', message, tab) await self.core.events.trigger_async('ignored_private', message, tab)
msg = config.get_by_tabname('private_auto_response', room_from) msg = self.core.config.get_by_tabname('private_auto_response', room_from)
if msg and body: if msg and body:
self.core.xmpp.send_message( self.core.xmpp.send_message(
mto=jid.full, mbody=msg, mtype='chat') mto=jid.full, mbody=msg, mtype='chat')
@ -540,8 +540,8 @@ class HandlerCore:
else: else:
await tab.handle_message(message) await tab.handle_message(message)
if not sent and 'private' in config.getstr('beep_on').split(): if not sent and 'private' in self.core.config.getstr('beep_on').split():
if not config.get_by_tabname('disable_beep', jid.full): if not self.core.config.get_by_tabname('disable_beep', jid.full):
curses.beep() curses.beep()
if tab is self.core.tabs.current_tab: if tab is self.core.tabs.current_tab:
self.core.refresh_window() self.core.refresh_window()
@ -589,7 +589,8 @@ class HandlerCore:
tab.refresh_info_header() tab.refresh_info_header()
self.core.doupdate() self.core.doupdate()
else: else:
_composing_tab_state(tab, state) show = self.core.config.getstr('show_composing_tabs')
_composing_tab_state(tab, state, show)
self.core.refresh_tab_win() self.core.refresh_tab_win()
return True return True
@ -607,7 +608,8 @@ class HandlerCore:
tab.refresh_info_header() tab.refresh_info_header()
self.core.doupdate() self.core.doupdate()
else: else:
_composing_tab_state(tab, state) show = self.core.config.getstr('show_composing_tabs')
_composing_tab_state(tab, state, show)
self.core.refresh_tab_win() self.core.refresh_tab_win()
async def on_chatstate_groupchat_conversation(self, message: Message, state: str): async def on_chatstate_groupchat_conversation(self, message: Message, state: str):
@ -626,7 +628,8 @@ class HandlerCore:
tab.input.refresh() tab.input.refresh()
self.core.doupdate() self.core.doupdate()
else: else:
_composing_tab_state(tab, state) show = self.core.config.getstr('show_composing_tabs')
_composing_tab_state(tab, state, show)
self.core.refresh_tab_win() self.core.refresh_tab_win()
@staticmethod @staticmethod
@ -904,13 +907,13 @@ class HandlerCore:
""" """
When we are disconnected from remote server When we are disconnected from remote server
""" """
if 'disconnect' in config.getstr('beep_on').split(): if 'disconnect' in self.core.config.getstr('beep_on').split():
curses.beep() curses.beep()
# Stop the ping plugin. It would try to send stanza on regular basis # Stop the ping plugin. It would try to send stanza on regular basis
self.core.xmpp.plugin['xep_0199'].disable_keepalive() self.core.xmpp.plugin['xep_0199'].disable_keepalive()
msg_typ = 'Error' if not self.core.legitimate_disconnect else 'Info' msg_typ = 'Error' if not self.core.legitimate_disconnect else 'Info'
self.core.information("Disconnected from server%s." % (event and ": %s" % event or ""), msg_typ) self.core.information("Disconnected from server%s." % (event and ": %s" % event or ""), msg_typ)
if self.core.legitimate_disconnect or not config.getbool( if self.core.legitimate_disconnect or not self.core.config.getbool(
'auto_reconnect'): 'auto_reconnect'):
return return
if (self.core.last_stream_error if (self.core.last_stream_error
@ -975,7 +978,7 @@ class HandlerCore:
self.core.xmpp.get_roster() self.core.xmpp.get_roster()
roster.update_contact_groups(self.core.xmpp.boundjid.bare) roster.update_contact_groups(self.core.xmpp.boundjid.bare)
# send initial presence # send initial presence
if config.getbool('send_initial_presence'): if self.core.config.getbool('send_initial_presence'):
pres = self.core.xmpp.make_presence() pres = self.core.xmpp.make_presence()
pres['show'] = self.core.status.show pres['show'] = self.core.status.show
pres['status'] = self.core.status.message pres['status'] = self.core.status.message
@ -985,7 +988,7 @@ class HandlerCore:
# join all the available bookmarks. As of yet, this is just the local ones # join all the available bookmarks. As of yet, this is just the local ones
self.core.join_initial_rooms(self.core.bookmarks.local()) self.core.join_initial_rooms(self.core.bookmarks.local())
if config.getbool('enable_user_nick'): if self.core.config.getbool('enable_user_nick'):
self.core.xmpp.plugin['xep_0172'].publish_nick( self.core.xmpp.plugin['xep_0172'].publish_nick(
nick=self.core.own_nick, callback=dumb_callback) nick=self.core.own_nick, callback=dumb_callback)
asyncio.create_task(self.core.xmpp.plugin['xep_0115'].update_caps()) asyncio.create_task(self.core.xmpp.plugin['xep_0115'].update_caps())
@ -1243,7 +1246,7 @@ class HandlerCore:
'New certificate accepted:\nnew: %s\nold: %s' % 'New certificate accepted:\nnew: %s\nold: %s' %
(old_cert, new_cert), 'Info') (old_cert, new_cert), 'Info')
log.debug('Setting certificate to %s', new_cert) log.debug('Setting certificate to %s', new_cert)
if not config.silent_set('certificate', new_cert): if not self.core.config.silent_set('certificate', new_cert):
self.core.information('Unable to write in the config file', self.core.information('Unable to write in the config file',
'Error') 'Error')
else: else:
@ -1278,14 +1281,14 @@ class HandlerCore:
""" """
Check the server certificate using the slixmpp ssl_cert event Check the server certificate using the slixmpp ssl_cert event
""" """
if config.getbool('ignore_certificate'): if self.core.config.getbool('ignore_certificate'):
return return
cert = config.getstr('certificate') cert = self.core.config.getstr('certificate')
# update the cert representation when it uses the old one # update the cert representation when it uses the old one
if cert and ':' not in cert: if cert and ':' not in cert:
cert = ':'.join( cert = ':'.join(
i + j for i, j in zip(cert[::2], cert[1::2])).upper() i + j for i, j in zip(cert[::2], cert[1::2])).upper()
config.set_and_save('certificate', cert) self.core.config.set_and_save('certificate', cert)
der = ssl.PEM_cert_to_DER_cert(pem) der = ssl.PEM_cert_to_DER_cert(pem)
asn1 = pyasn1.codec.der.decoder.decode( asn1 = pyasn1.codec.der.decoder.decode(
@ -1306,7 +1309,7 @@ class HandlerCore:
log.debug( log.debug(
'Current hash is cert hash, moving to SPKI hash (%s)', 'Current hash is cert hash, moving to SPKI hash (%s)',
spki_found_cert) spki_found_cert)
config.set_and_save('certificate', spki_found_cert) self.core.config.set_and_save('certificate', spki_found_cert)
return return
elif spki_found_cert == cert: elif spki_found_cert == cert:
return return
@ -1314,7 +1317,7 @@ class HandlerCore:
self._ssl_pop_tab(cert, spki_found_cert) self._ssl_pop_tab(cert, spki_found_cert)
else: else:
log.debug('First time. Setting certificate to %s', spki_found_cert) log.debug('First time. Setting certificate to %s', spki_found_cert)
if not config.silent_set('certificate', spki_found_cert): if not self.core.config.silent_set('certificate', spki_found_cert):
self.core.information('Unable to write in the config file', self.core.information('Unable to write in the config file',
'Error') 'Error')
@ -1408,7 +1411,7 @@ class HandlerCore:
self.core.close_tab() self.core.close_tab()
def _composing_tab_state(tab, state): def _composing_tab_state(tab, state, show_composing_tabs):
""" """
Set a tab state to or from the "composing" state Set a tab state to or from the "composing" state
according to the config and the current tab state according to the config and the current tab state
@ -1422,8 +1425,7 @@ def _composing_tab_state(tab, state):
else: else:
return # should not happen return # should not happen
show = config.getstr('show_composing_tabs').lower() show = show_composing_tabs.lower() in values
show = show in values
if tab.state != 'composing' and state == 'composing': if tab.state != 'composing' and state == 'composing':
if show: if show: