Commit 08f48b0c authored by mathieui's avatar mathieui

Move get_error_message to common

parent e1720c8e
...@@ -505,3 +505,75 @@ def to_utc(time: datetime) -> datetime: ...@@ -505,3 +505,75 @@ def to_utc(time: datetime) -> datetime:
time = time.replace(tzinfo=tzone).astimezone(tz=timezone.utc) time = time.replace(tzinfo=tzone).astimezone(tz=timezone.utc)
# Return an offset-naive datetime # Return an offset-naive datetime
return time.replace(tzinfo=None) return time.replace(tzinfo=None)
# http://xmpp.org/extensions/xep-0045.html#errorstatus
ERROR_AND_STATUS_CODES = {
'401': 'A password is required',
'403': 'Permission denied',
'404': 'The room doesn’t exist',
'405': 'Your are not allowed to create a new room',
'406': 'A reserved nick must be used',
'407': 'You are not in the member list',
'409': 'This nickname is already in use or has been reserved',
'503': 'The maximum number of users has been reached',
}
# http://xmpp.org/extensions/xep-0086.html
DEPRECATED_ERRORS = {
'302': 'Redirect',
'400': 'Bad request',
'401': 'Not authorized',
'402': 'Payment required',
'403': 'Forbidden',
'404': 'Not found',
'405': 'Not allowed',
'406': 'Not acceptable',
'407': 'Registration required',
'408': 'Request timeout',
'409': 'Conflict',
'500': 'Internal server error',
'501': 'Feature not implemented',
'502': 'Remote server error',
'503': 'Service unavailable',
'504': 'Remote server timeout',
'510': 'Disconnected',
}
def get_error_message(stanza: Message, deprecated: bool = False) -> str:
"""
Takes a stanza of the form <message type='error'><error/></message>
and return a well formed string containing error information
"""
sender = stanza['from']
msg = stanza['error']['type']
condition = stanza['error']['condition']
code = stanza['error']['code']
body = stanza['error']['text']
if not body:
if deprecated:
if code in DEPRECATED_ERRORS:
body = DEPRECATED_ERRORS[code]
else:
body = condition or 'Unknown error'
else:
if code in ERROR_AND_STATUS_CODES:
body = ERROR_AND_STATUS_CODES[code]
else:
body = condition or 'Unknown error'
if code:
message = '%(from)s: %(code)s - %(msg)s: %(body)s' % {
'from': sender,
'msg': msg,
'body': body,
'code': code
}
else:
message = '%(from)s: %(msg)s: %(body)s' % {
'from': sender,
'msg': msg,
'body': body
}
return message
...@@ -45,6 +45,7 @@ from poezio import timed_events ...@@ -45,6 +45,7 @@ from poezio import timed_events
from poezio import windows from poezio import windows
from poezio.bookmarks import BookmarkList from poezio.bookmarks import BookmarkList
from poezio.common import get_error_message
from poezio.config import config, firstrun from poezio.config import config, firstrun
from poezio.contact import Contact, Resource from poezio.contact import Contact, Resource
from poezio.daemon import Executor from poezio.daemon import Executor
...@@ -66,8 +67,6 @@ from poezio.core.handlers import HandlerCore ...@@ -66,8 +67,6 @@ from poezio.core.handlers import HandlerCore
from poezio.core.structs import ( from poezio.core.structs import (
Command, Command,
Status, Status,
DEPRECATED_ERRORS,
ERROR_AND_STATUS_CODES,
POSSIBLE_SHOW, POSSIBLE_SHOW,
) )
...@@ -989,42 +988,6 @@ class Core: ...@@ -989,42 +988,6 @@ class Core:
for jid in jids: for jid in jids:
self.invite(jid, room) self.invite(jid, room)
def get_error_message(self, stanza, deprecated: bool = False):
"""
Takes a stanza of the form <message type='error'><error/></message>
and return a well formed string containing error information
"""
sender = stanza['from']
msg = stanza['error']['type']
condition = stanza['error']['condition']
code = stanza['error']['code']
body = stanza['error']['text']
if not body:
if deprecated:
if code in DEPRECATED_ERRORS:
body = DEPRECATED_ERRORS[code]
else:
body = condition or 'Unknown error'
else:
if code in ERROR_AND_STATUS_CODES:
body = ERROR_AND_STATUS_CODES[code]
else:
body = condition or 'Unknown error'
if code:
message = '%(from)s: %(code)s - %(msg)s: %(body)s' % {
'from': sender,
'msg': msg,
'body': body,
'code': code
}
else:
message = '%(from)s: %(msg)s: %(body)s' % {
'from': sender,
'msg': msg,
'body': body
}
return message
####################### Tab logic-related things ############################## ####################### Tab logic-related things ##############################
### Tab getters ### ### Tab getters ###
...@@ -2125,7 +2088,7 @@ class Core: ...@@ -2125,7 +2088,7 @@ class Core:
tab = self.tabs.by_name_and_class(room_name, tabs.MucTab) tab = self.tabs.by_name_and_class(room_name, tabs.MucTab)
if not tab: if not tab:
return return
error_message = self.get_error_message(error) error_message = get_error_message(error)
tab.add_message( tab.add_message(
Message( Message(
error_message, error_message,
......
...@@ -31,7 +31,7 @@ from poezio import pep ...@@ -31,7 +31,7 @@ from poezio import pep
from poezio import tabs 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 safeJID from poezio.common import safeJID, get_error_message
from poezio.config import config, get_image_cache from poezio.config import config, 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
...@@ -304,7 +304,7 @@ class HandlerCore: ...@@ -304,7 +304,7 @@ class HandlerCore:
if jid_from.full == jid_from.bare: if jid_from.full == jid_from.bare:
self.core.room_error(message, jid_from.bare) self.core.room_error(message, jid_from.bare)
else: else:
text = self.core.get_error_message(message) text = get_error_message(message)
p_tab = self.core.tabs.by_name_and_class( p_tab = self.core.tabs.by_name_and_class(
jid_from.full, tabs.PrivateTab) jid_from.full, tabs.PrivateTab)
if p_tab: if p_tab:
...@@ -313,7 +313,7 @@ class HandlerCore: ...@@ -313,7 +313,7 @@ class HandlerCore:
self.core.information(text, 'Error') self.core.information(text, 'Error')
return return
tab = self.core.get_conversation_by_jid(message['from'], create=False) tab = self.core.get_conversation_by_jid(message['from'], create=False)
error_msg = self.core.get_error_message(message, deprecated=True) error_msg = get_error_message(message, deprecated=True)
if not tab: if not tab:
self.core.information(error_msg, 'Error') self.core.information(error_msg, 'Error')
return return
...@@ -1749,7 +1749,7 @@ class HandlerCore: ...@@ -1749,7 +1749,7 @@ class HandlerCore:
def adhoc_error(self, iq, adhoc_session): def adhoc_error(self, iq, adhoc_session):
self.core.xmpp.plugin['xep_0050'].terminate_command(adhoc_session) self.core.xmpp.plugin['xep_0050'].terminate_command(adhoc_session)
error_message = self.core.get_error_message(iq) error_message = get_error_message(iq)
self.core.information( self.core.information(
"An error occurred while executing the command: %s" % "An error occurred while executing the command: %s" %
(error_message), 'Error') (error_message), 'Error')
......
...@@ -5,43 +5,10 @@ from dataclasses import dataclass ...@@ -5,43 +5,10 @@ from dataclasses import dataclass
from typing import Any, Callable, List, Dict from typing import Any, Callable, List, Dict
__all__ = [ __all__ = [
'ERROR_AND_STATUS_CODES', 'DEPRECATED_ERRORS', 'POSSIBLE_SHOW', 'Status', 'POSSIBLE_SHOW', 'Status',
'Command', 'Completion' 'Command', 'Completion'
] ]
# http://xmpp.org/extensions/xep-0045.html#errorstatus
ERROR_AND_STATUS_CODES = {
'401': 'A password is required',
'403': 'Permission denied',
'404': 'The room doesn’t exist',
'405': 'Your are not allowed to create a new room',
'406': 'A reserved nick must be used',
'407': 'You are not in the member list',
'409': 'This nickname is already in use or has been reserved',
'503': 'The maximum number of users has been reached',
}
# http://xmpp.org/extensions/xep-0086.html
DEPRECATED_ERRORS = {
'302': 'Redirect',
'400': 'Bad request',
'401': 'Not authorized',
'402': 'Payment required',
'403': 'Forbidden',
'404': 'Not found',
'405': 'Not allowed',
'406': 'Not acceptable',
'407': 'Registration required',
'408': 'Request timeout',
'409': 'Conflict',
'500': 'Internal server error',
'501': 'Feature not implemented',
'502': 'Remote server error',
'503': 'Service unavailable',
'504': 'Remote server timeout',
'510': 'Disconnected',
}
POSSIBLE_SHOW = { POSSIBLE_SHOW = {
'available': None, 'available': None,
'chat': 'chat', 'chat': 'chat',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment