refactoring: renamed sat package to libervia.backend

This commit is contained in:
Goffi 2023-06-02 11:49:51 +02:00
commit d2685ba11b
11 changed files with 1293 additions and 1293 deletions

View file

View file

@ -0,0 +1,534 @@
#!/usr/bin/env python3
# Libervia: an XMPP client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
try:
from xdg import BaseDirectory
from os.path import expanduser, realpath
except ImportError:
BaseDirectory = None
from os.path import dirname
from typing import Final
from libervia import backend
class Const(object):
## Application ##
APP_NAME = "Libervia"
APP_COMPONENT = "backend"
APP_NAME_ALT = "Libervia"
APP_NAME_FILE = "libervia"
APP_NAME_FULL = f"{APP_NAME} ({APP_COMPONENT})"
APP_VERSION = (
backend.__version__
) # Please add 'D' at the end of version in sat/VERSION for dev versions
APP_RELEASE_NAME = "La Ruche"
APP_URL = "https://libervia.org"
## Runtime ##
PLUGIN_EXT = "py"
HISTORY_SKIP = "skip"
## Main config ##
DEFAULT_BRIDGE = "dbus"
## Protocol ##
XMPP_C2S_PORT = 5222
XMPP_MAX_RETRIES = None
# default port used on Prosody, may differ on other servers
XMPP_COMPONENT_PORT = 5347
## Parameters ##
NO_SECURITY_LIMIT = -1 #  FIXME: to rename
SECURITY_LIMIT_MAX = 0
INDIVIDUAL = "individual"
GENERAL = "general"
# General parameters
HISTORY_LIMIT = "History"
SHOW_OFFLINE_CONTACTS = "Offline contacts"
SHOW_EMPTY_GROUPS = "Empty groups"
# Parameters related to connection
FORCE_SERVER_PARAM = "Force server"
FORCE_PORT_PARAM = "Force port"
# Parameters related to encryption
PROFILE_PASS_PATH = ("General", "Password")
MEMORY_CRYPTO_NAMESPACE = "crypto" # for the private persistent binary dict
MEMORY_CRYPTO_KEY = "personal_key"
# Parameters for static blog pages
# FIXME: blog constants should not be in core constants
STATIC_BLOG_KEY = "Blog page"
STATIC_BLOG_PARAM_TITLE = "Title"
STATIC_BLOG_PARAM_BANNER = "Banner"
STATIC_BLOG_PARAM_KEYWORDS = "Keywords"
STATIC_BLOG_PARAM_DESCRIPTION = "Description"
## Menus ##
MENU_GLOBAL = "GLOBAL"
MENU_ROOM = "ROOM"
MENU_SINGLE = "SINGLE"
MENU_JID_CONTEXT = "JID_CONTEXT"
MENU_ROSTER_JID_CONTEXT = "ROSTER_JID_CONTEXT"
MENU_ROSTER_GROUP_CONTEXT = "MENU_ROSTER_GROUP_CONTEXT"
MENU_ROOM_OCCUPANT_CONTEXT = "MENU_ROOM_OCCUPANT_CONTEXT"
## Profile and entities ##
PROF_KEY_NONE = "@NONE@"
PROF_KEY_DEFAULT = "@DEFAULT@"
PROF_KEY_ALL = "@ALL@"
ENTITY_ALL = "@ALL@"
ENTITY_ALL_RESOURCES = "@ALL_RESOURCES@"
ENTITY_MAIN_RESOURCE = "@MAIN_RESOURCE@"
ENTITY_CAP_HASH = "CAP_HASH"
ENTITY_TYPE = "type"
ENTITY_TYPE_MUC = "MUC"
## Roster jids selection ##
PUBLIC = "PUBLIC"
ALL = (
"ALL"
) # ALL means all known contacts, while PUBLIC means everybody, known or not
GROUP = "GROUP"
JID = "JID"
## Messages ##
MESS_TYPE_INFO = "info"
MESS_TYPE_CHAT = "chat"
MESS_TYPE_ERROR = "error"
MESS_TYPE_GROUPCHAT = "groupchat"
MESS_TYPE_HEADLINE = "headline"
MESS_TYPE_NORMAL = "normal"
MESS_TYPE_AUTO = "auto" # magic value to let the backend guess the type
MESS_TYPE_STANDARD = (
MESS_TYPE_CHAT,
MESS_TYPE_ERROR,
MESS_TYPE_GROUPCHAT,
MESS_TYPE_HEADLINE,
MESS_TYPE_NORMAL,
)
MESS_TYPE_ALL = MESS_TYPE_STANDARD + (MESS_TYPE_INFO, MESS_TYPE_AUTO)
MESS_EXTRA_INFO = "info_type"
EXTRA_INFO_DECR_ERR = "DECRYPTION_ERROR"
EXTRA_INFO_ENCR_ERR = "ENCRYPTION_ERROR"
# encryption is a key for plugins
MESS_KEY_ENCRYPTION: Final = "ENCRYPTION"
# encrypted is a key for frontends
MESS_KEY_ENCRYPTED = "encrypted"
MESS_KEY_TRUSTED = "trusted"
# File encryption algorithms
ENC_AES_GCM = "AES-GCM"
## Chat ##
CHAT_ONE2ONE = "one2one"
CHAT_GROUP = "group"
## Presence ##
PRESENCE_UNAVAILABLE = "unavailable"
PRESENCE_SHOW_AWAY = "away"
PRESENCE_SHOW_CHAT = "chat"
PRESENCE_SHOW_DND = "dnd"
PRESENCE_SHOW_XA = "xa"
PRESENCE_SHOW = "show"
PRESENCE_STATUSES = "statuses"
PRESENCE_STATUSES_DEFAULT = "default"
PRESENCE_PRIORITY = "priority"
## Common namespaces ##
NS_XML = "http://www.w3.org/XML/1998/namespace"
NS_CLIENT = "jabber:client"
NS_COMPONENT = "jabber:component:accept"
NS_STREAM = (NS_CLIENT, NS_COMPONENT)
NS_FORWARD = "urn:xmpp:forward:0"
NS_DELAY = "urn:xmpp:delay"
NS_XHTML = "http://www.w3.org/1999/xhtml"
## Common XPath ##
IQ_GET = '/iq[@type="get"]'
IQ_SET = '/iq[@type="set"]'
## Directories ##
# directory for components specific data
COMPONENTS_DIR = "components"
CACHE_DIR = "cache"
# files in file dir are stored for long term
# files dir is global, i.e. for all profiles
FILES_DIR = "files"
# FILES_LINKS_DIR is a directory where files owned by a specific profile
# are linked to the global files directory. This way the directory can be
#  shared per profiles while keeping global directory where identical files
# shared between different profiles are not duplicated.
FILES_LINKS_DIR = "files_links"
# FILES_TMP_DIR is where profile's partially transfered files are put.
# Once transfer is completed, they are moved to FILES_DIR
FILES_TMP_DIR = "files_tmp"
## Templates ##
TEMPLATE_TPL_DIR = "templates"
TEMPLATE_THEME_DEFAULT = "default"
TEMPLATE_STATIC_DIR = "static"
# templates i18n
KEY_LANG = "lang"
KEY_THEME = "theme"
## Plugins ##
# PLUGIN_INFO keys
# XXX: we use PI instead of PLUG_INFO which would normally be used
# to make the header more readable
PI_NAME = "name"
PI_IMPORT_NAME = "import_name"
PI_MAIN = "main"
PI_HANDLER = "handler"
PI_TYPE = (
"type"
) #  FIXME: should be types, and should handle single unicode type or tuple of types (e.g. "blog" and "import")
PI_MODES = "modes"
PI_PROTOCOLS = "protocols"
PI_DEPENDENCIES = "dependencies"
PI_RECOMMENDATIONS = "recommendations"
PI_DESCRIPTION = "description"
PI_USAGE = "usage"
# Types
PLUG_TYPE_XEP = "XEP"
PLUG_TYPE_MISC = "MISC"
PLUG_TYPE_EXP = "EXP"
PLUG_TYPE_SEC = "SEC"
PLUG_TYPE_SYNTAXE = "SYNTAXE"
PLUG_TYPE_PUBSUB = "PUBSUB"
PLUG_TYPE_BLOG = "BLOG"
PLUG_TYPE_IMPORT = "IMPORT"
PLUG_TYPE_ENTRY_POINT = "ENTRY_POINT"
# Modes
PLUG_MODE_CLIENT = "client"
PLUG_MODE_COMPONENT = "component"
PLUG_MODE_DEFAULT = (PLUG_MODE_CLIENT,)
PLUG_MODE_BOTH = (PLUG_MODE_CLIENT, PLUG_MODE_COMPONENT)
# names of widely used plugins
TEXT_CMDS = "TEXT-COMMANDS"
# PubSub event categories
PS_PEP = "PEP"
PS_MICROBLOG = "MICROBLOG"
# PubSub
PS_PUBLISH = "publish"
PS_RETRACT = "retract" # used for items
PS_DELETE = "delete" # used for nodes
PS_PURGE = "purge" # used for nodes
PS_ITEM = "item"
PS_ITEMS = "items" # Can contain publish and retract items
PS_EVENTS = (PS_ITEMS, PS_DELETE, PS_PURGE)
## MESSAGE/NOTIFICATION LEVELS ##
LVL_INFO = "info"
LVL_WARNING = "warning"
LVL_ERROR = "error"
## XMLUI ##
XMLUI_WINDOW = "window"
XMLUI_POPUP = "popup"
XMLUI_FORM = "form"
XMLUI_PARAM = "param"
XMLUI_DIALOG = "dialog"
XMLUI_DIALOG_CONFIRM = "confirm"
XMLUI_DIALOG_MESSAGE = "message"
XMLUI_DIALOG_NOTE = "note"
XMLUI_DIALOG_FILE = "file"
XMLUI_DATA_ANSWER = "answer"
XMLUI_DATA_CANCELLED = "cancelled"
XMLUI_DATA_TYPE = "type"
XMLUI_DATA_MESS = "message"
XMLUI_DATA_LVL = "level"
XMLUI_DATA_LVL_INFO = LVL_INFO
XMLUI_DATA_LVL_WARNING = LVL_WARNING
XMLUI_DATA_LVL_ERROR = LVL_ERROR
XMLUI_DATA_LVL_DEFAULT = XMLUI_DATA_LVL_INFO
XMLUI_DATA_LVLS = (XMLUI_DATA_LVL_INFO, XMLUI_DATA_LVL_WARNING, XMLUI_DATA_LVL_ERROR)
XMLUI_DATA_BTNS_SET = "buttons_set"
XMLUI_DATA_BTNS_SET_OKCANCEL = "ok/cancel"
XMLUI_DATA_BTNS_SET_YESNO = "yes/no"
XMLUI_DATA_BTNS_SET_DEFAULT = XMLUI_DATA_BTNS_SET_OKCANCEL
XMLUI_DATA_FILETYPE = "filetype"
XMLUI_DATA_FILETYPE_FILE = "file"
XMLUI_DATA_FILETYPE_DIR = "dir"
XMLUI_DATA_FILETYPE_DEFAULT = XMLUI_DATA_FILETYPE_FILE
## Logging ##
LOG_LVL_DEBUG = "DEBUG"
LOG_LVL_INFO = "INFO"
LOG_LVL_WARNING = "WARNING"
LOG_LVL_ERROR = "ERROR"
LOG_LVL_CRITICAL = "CRITICAL"
LOG_LEVELS = (
LOG_LVL_DEBUG,
LOG_LVL_INFO,
LOG_LVL_WARNING,
LOG_LVL_ERROR,
LOG_LVL_CRITICAL,
)
LOG_BACKEND_STANDARD = "standard"
LOG_BACKEND_TWISTED = "twisted"
LOG_BACKEND_BASIC = "basic"
LOG_BACKEND_CUSTOM = "custom"
LOG_BASE_LOGGER = "root"
LOG_TWISTED_LOGGER = "twisted"
LOG_OPT_SECTION = "DEFAULT" # section of sat.conf where log options should be
LOG_OPT_PREFIX = "log_"
# (option_name, default_value) tuples
LOG_OPT_COLORS = (
"colors",
"true",
) # true for auto colors, force to have colors even if stdout is not a tty, false for no color
LOG_OPT_TAINTS_DICT = (
"levels_taints_dict",
{
LOG_LVL_DEBUG: ("cyan",),
LOG_LVL_INFO: (),
LOG_LVL_WARNING: ("yellow",),
LOG_LVL_ERROR: ("red", "blink", r"/!\ ", "blink_off"),
LOG_LVL_CRITICAL: ("bold", "red", "Guru Meditation ", "normal_weight"),
},
)
LOG_OPT_LEVEL = ("level", "info")
LOG_OPT_FORMAT = ("fmt", "%(message)s") # similar to logging format.
LOG_OPT_LOGGER = ("logger", "") # regex to filter logger name
LOG_OPT_OUTPUT_SEP = "//"
LOG_OPT_OUTPUT_DEFAULT = "default"
LOG_OPT_OUTPUT_MEMORY = "memory"
LOG_OPT_OUTPUT_MEMORY_LIMIT = 300
LOG_OPT_OUTPUT_FILE = "file" # file is implicit if only output
LOG_OPT_OUTPUT = (
"output",
LOG_OPT_OUTPUT_SEP + LOG_OPT_OUTPUT_DEFAULT,
) # //default = normal output (stderr or a file with twistd), path/to/file for a file (must be the first if used), //memory for memory (options can be put in parenthesis, e.g.: //memory(500) for a 500 lines memory)
## action constants ##
META_TYPE_FILE = "file"
META_TYPE_CALL = "call"
META_TYPE_OVERWRITE = "overwrite"
META_TYPE_NOT_IN_ROSTER_LEAK = "not_in_roster_leak"
META_SUBTYPE_CALL_AUDIO = "audio"
META_SUBTYPE_CALL_VIDEO = "video"
## HARD-CODED ACTIONS IDS (generated with uuid.uuid4) ##
AUTHENTICATE_PROFILE_ID = "b03bbfa8-a4ae-4734-a248-06ce6c7cf562"
CHANGE_XMPP_PASSWD_ID = "878b9387-de2b-413b-950f-e424a147bcd0"
## Text values ##
BOOL_TRUE = "true"
BOOL_FALSE = "false"
## Special values used in bridge methods calls ##
HISTORY_LIMIT_DEFAULT = -1
HISTORY_LIMIT_NONE = -2
## Progress error special values ##
PROGRESS_ERROR_DECLINED = "declined" #  session has been declined by peer user
PROGRESS_ERROR_FAILED = "failed" #  something went wrong with the session
## Files ##
FILE_TYPE_DIRECTORY = "directory"
FILE_TYPE_FILE = "file"
# when filename can't be found automatically, this one will be used
FILE_DEFAULT_NAME = "unnamed"
## Permissions management ##
ACCESS_PERM_READ = "read"
ACCESS_PERM_WRITE = "write"
ACCESS_PERMS = {ACCESS_PERM_READ, ACCESS_PERM_WRITE}
ACCESS_TYPE_PUBLIC = "public"
ACCESS_TYPE_WHITELIST = "whitelist"
ACCESS_TYPES = (ACCESS_TYPE_PUBLIC, ACCESS_TYPE_WHITELIST)
## Common data keys ##
KEY_THUMBNAILS = "thumbnails"
KEY_PROGRESS_ID = "progress_id"
KEY_ATTACHMENTS = "attachments"
KEY_ATTACHMENTS_MEDIA_TYPE = "media_type"
KEY_ATTACHMENTS_PREVIEW = "preview"
KEY_ATTACHMENTS_RESIZE = "resize"
## Common extra keys/values ##
KEY_ORDER_BY = "order_by"
KEY_USE_CACHE = "use_cache"
KEY_DECRYPT = "decrypt"
ORDER_BY_CREATION = 'creation'
ORDER_BY_MODIFICATION = 'modification'
# internationalisation
DEFAULT_LOCALE = "en_GB"
## Command Line ##
# Exit codes used by CLI applications
EXIT_OK = 0
EXIT_ERROR = 1 # generic error, when nothing else match
EXIT_BAD_ARG = 2 # arguments given by user are bad
EXIT_BRIDGE_ERROR = 3 # can't connect to bridge
EXIT_BRIDGE_ERRBACK = 4 # something went wrong when calling a bridge method
EXIT_BACKEND_NOT_FOUND = 5 # can't find backend with this bride
EXIT_NOT_FOUND = 16 # an item required by a command was not found
EXIT_DATA_ERROR = 17 # data needed for a command is invalid
EXIT_MISSING_FEATURE = 18 # a needed plugin or feature is not available
EXIT_CONFLICT = 19 # an item already exists
EXIT_USER_CANCELLED = 20 # user cancelled action
EXIT_INTERNAL_ERROR = 111 # unexpected error
EXIT_FILE_NOT_EXE = (
126
) # a file to be executed was found, but it was not an executable utility (cf. man 1 exit)
EXIT_CMD_NOT_FOUND = 127 # a utility to be executed was not found (cf. man 1 exit)
EXIT_CMD_ERROR = 127 # a utility to be executed returned an error exit code
EXIT_SIGNAL_INT = 128 # a command was interrupted by a signal (cf. man 1 exit)
## Misc ##
SAVEFILE_DATABASE = APP_NAME_FILE + ".db"
IQ_SET = '/iq[@type="set"]'
ENV_PREFIX = "SAT_" # Prefix used for environment variables
IGNORE = "ignore"
NO_LIMIT = -1 # used in bridge when a integer value is expected
DEFAULT_MAX_AGE = 1209600 # default max age of cached files, in seconds
STANZA_NAMES = ("iq", "message", "presence")
# Stream Hooks
STREAM_HOOK_SEND = "send"
STREAM_HOOK_RECEIVE = "receive"
@classmethod
def LOG_OPTIONS(cls):
"""Return options checked for logs"""
# XXX: we use a classmethod so we can use Const inheritance to change default options
return (
cls.LOG_OPT_COLORS,
cls.LOG_OPT_TAINTS_DICT,
cls.LOG_OPT_LEVEL,
cls.LOG_OPT_FORMAT,
cls.LOG_OPT_LOGGER,
cls.LOG_OPT_OUTPUT,
)
@classmethod
def bool(cls, value: str) -> bool:
"""@return (bool): bool value for associated constant"""
assert isinstance(value, str)
return value.lower() in (cls.BOOL_TRUE, "1", "yes", "on")
@classmethod
def bool_const(cls, value: bool) -> str:
"""@return (str): constant associated to bool value"""
assert isinstance(value, bool)
return cls.BOOL_TRUE if value else cls.BOOL_FALSE
## Configuration ##
if (
BaseDirectory
): # skipped when xdg module is not available (should not happen in backend)
if "org.libervia.cagou" in BaseDirectory.__file__:
# FIXME: hack to make config read from the right location on Android
# TODO: fix it in a more proper way
# we need to use Android API to get downloads directory
import os.path
from jnius import autoclass
# we don't want the very verbose jnius log when we are in DEBUG level
import logging
logging.getLogger('jnius').setLevel(logging.WARNING)
logging.getLogger('jnius.reflect').setLevel(logging.WARNING)
Environment = autoclass("android.os.Environment")
BaseDirectory = None
Const.DEFAULT_CONFIG = {
"local_dir": "/data/data/org.libervia.cagou/app",
"media_dir": "/data/data/org.libervia.cagou/files/app/media",
# FIXME: temporary location for downloads, need to call API properly
"downloads_dir": os.path.join(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
).getAbsolutePath(),
Const.APP_NAME_FILE,
),
"pid_dir": "%(local_dir)s",
"log_dir": "%(local_dir)s",
}
Const.CONFIG_FILES = [
"/data/data/org.libervia.cagou/files/app/android/"
+ Const.APP_NAME_FILE
+ ".conf"
]
else:
import os
# we use parent of "sat" module dir as last config path, this is useful for
# per instance configurations (e.g. a dev instance and a main instance)
root_dir = dirname(dirname(backend.__file__)) + '/'
Const.CONFIG_PATHS = (
# /etc/_sat.conf is used for system-related settings (e.g. when media_dir
# is set by the distribution and has not reason to change, or in a Docker
# image)
["/etc/_", "/etc/", "~/", "~/."]
+ [
"{}/".format(path)
for path in list(BaseDirectory.load_config_paths(Const.APP_NAME_FILE))
]
# this is to handle legacy sat.conf
+ [
"{}/".format(path)
for path in list(BaseDirectory.load_config_paths("sat"))
]
+ [root_dir]
)
# on recent versions of Flatpak, FLATPAK_ID is set at run time
# it seems that this is not the case on older versions,
# but FLATPAK_SANDBOX_DIR seems set then
if os.getenv('FLATPAK_ID') or os.getenv('FLATPAK_SANDBOX_DIR'):
# for Flatpak, the conf can't be set in /etc or $HOME, so we have
# to add /app
Const.CONFIG_PATHS.append('/app/')
## Configuration ##
Const.DEFAULT_CONFIG = {
"media_dir": "/usr/share/" + Const.APP_NAME_FILE + "/media",
"local_dir": BaseDirectory.save_data_path(Const.APP_NAME_FILE),
"downloads_dir": "~/Downloads/" + Const.APP_NAME_FILE,
"pid_dir": "%(local_dir)s",
"log_dir": "%(local_dir)s",
}
# List of the configuration filenames sorted by ascending priority
Const.CONFIG_FILES = [
realpath(expanduser(path) + Const.APP_NAME_FILE + ".conf")
for path in Const.CONFIG_PATHS
] + [
# legacy sat.conf
realpath(expanduser(path) + "sat.conf")
for path in Const.CONFIG_PATHS
]

View file

@ -0,0 +1,66 @@
#!/usr/bin/env python3
# Libervia types
# Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import namedtuple
from typing import Dict, Callable, Optional
from typing_extensions import TypedDict
from twisted.words.protocols.jabber import jid as t_jid
from twisted.words.protocols.jabber import xmlstream
from twisted.words.xish import domish
class SatXMPPEntity:
profile: str
jid: t_jid.JID
is_component: bool
server_jid: t_jid.JID
IQ: Callable[[Optional[str], Optional[int]], xmlstream.IQ]
EncryptionPlugin = namedtuple("EncryptionPlugin", ("instance",
"name",
"namespace",
"priority",
"directed"))
class EncryptionSession(TypedDict):
plugin: EncryptionPlugin
# Incomplete types built through observation rather than code inspection.
MessageDataExtra = TypedDict(
"MessageDataExtra",
{ "encrypted": bool, "origin_id": str },
total=False
)
MessageData = TypedDict("MessageData", {
"from": t_jid.JID,
"to": t_jid.JID,
"uid": str,
"message": Dict[str, str],
"subject": Dict[str, str],
"type": str,
"timestamp": float,
"extra": MessageDataExtra,
"ENCRYPTION": EncryptionSession,
"xml": domish.Element
}, total=False)

View file

@ -0,0 +1,158 @@
#!/usr/bin/env python3
# SàT Exceptions
# Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class ProfileUnknownError(Exception):
pass
class ProfileNotInCacheError(Exception):
pass
class ProfileNotSetError(Exception):
"""This error raises when no profile has been set (value @NONE@ is found, but it should have been replaced)"""
class ProfileConnected(Exception):
"""This error is raised when trying to delete a connected profile."""
class ProfileNotConnected(Exception):
pass
class ProfileKeyUnknown(Exception):
pass
class ClientTypeError(Exception):
"""This code is not allowed for this type of client (i.e. component or not)"""
class UnknownEntityError(Exception):
pass
class UnknownGroupError(Exception):
pass
class MissingModule(Exception):
# Used to indicate when a plugin dependence is not found
# it's nice to indicate when to find the dependence in argument string
pass
class MissingPlugin(Exception):
"""A SàT plugin needed for a feature/method is missing"""
pass
class NotFound(Exception):
pass
class ConfigError(Exception):
pass
class DataError(Exception):
pass
class ExternalRequestError(Exception):
"""Request to third party server failed"""
class ConflictError(Exception):
pass
class TimeOutError(Exception):
pass
class CancelError(Exception):
pass
class InternalError(Exception):
pass
class FeatureNotFound(
Exception
): # a disco feature/identity which is needed is not present
pass
class BridgeInitError(Exception):
pass
class BridgeExceptionNoService(Exception):
pass
class DatabaseError(Exception):
pass
class PasswordError(Exception):
pass
class PermissionError(Exception):
pass
class ParsingError(ValueError):
pass
class EncryptionError(Exception):
"""Invalid encryption"""
pass
# Something which need to be done is not available yet
class NotReady(Exception):
pass
class NetworkError(Exception):
"""Something is wrong with a request (e.g. HTTP(S))"""
class InvalidCertificate(Exception):
"""A TLS certificate is not valid"""
pass
class CommandException(RuntimeError):
"""An external command failed
stdout and stderr will be attached to the Exception
"""
def __init__(self, msg, stdout, stderr):
super(CommandException, self).__init__(msg)
self.stdout = stdout
self.stderr = stderr

View file

@ -0,0 +1,50 @@
#!/usr/bin/env python3
# SAT: a jabber client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Callable, cast
from libervia.backend.core.log import getLogger
log = getLogger(__name__)
try:
import gettext
_ = gettext.translation("libervia_backend", "i18n", fallback=True).gettext
_translators = {None: gettext.NullTranslations()}
def language_switch(lang=None):
if not lang in _translators:
_translators[lang] = gettext.translation(
"libervia_backendt", languages=[lang], fallback=True
)
_translators[lang].install()
except ImportError:
log.warning("gettext support disabled")
_ = cast(Callable[[str], str], lambda msg: msg) # Libervia doesn't support gettext
def language_switch(lang=None):
pass
D_ = cast(Callable[[str], str], lambda msg: msg) # used for deferred translations

View file

@ -0,0 +1,247 @@
#!/usr/bin/env python3
# Libervia: an XMPP client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Script launching SàT backend"""
import sys
import os
import argparse
from pathlib import Path
from configparser import ConfigParser
from twisted.application import app
from twisted.python import usage
from libervia.backend.core.constants import Const as C
class LiberviaLogger(app.AppLogger):
def start(self, application):
# logging is initialised by sat.core.log_config via the Twisted plugin, nothing
# to do here
self._initialLog()
def stop(self):
pass
class Launcher:
APP_NAME=C.APP_NAME
APP_NAME_FILE=C.APP_NAME_FILE
@property
def NOT_RUNNING_MSG(self):
return f"{self.APP_NAME} is *NOT* running"
def cmd_no_subparser(self, args):
"""Command launched by default"""
args.extra_args = []
self.cmd_background(args)
def cmd_background(self, args):
self.run_twistd(args)
def cmd_foreground(self, args):
self.run_twistd(args, twistd_opts=['--nodaemon'])
def cmd_debug(self, args):
self.run_twistd(args, twistd_opts=['--debug'])
def cmd_stop(self, args):
import signal
import time
config = self.get_config()
pid_file = self.get_pid_file(config)
if not pid_file.is_file():
print(self.NOT_RUNNING_MSG)
sys.exit(0)
try:
pid = int(pid_file.read_text())
except Exception as e:
print(f"Can't read PID file at {pid_file}: {e}")
# we use the same exit code as DATA_ERROR in jp
sys.exit(17)
print(f"Terminating {self.APP_NAME}…")
os.kill(pid, signal.SIGTERM)
kill_started = time.time()
state = "init"
import errno
while True:
try:
os.kill(pid, 0)
except OSError as e:
if e.errno == errno.ESRCH:
break
elif e.errno == errno.EPERM:
print(f"Can't kill {self.APP_NAME}, the process is owned by an other user", file=sys.stderr)
sys.exit(18)
else:
raise e
time.sleep(0.2)
now = time.time()
if state == 'init' and now - kill_started > 5:
if state == 'init':
state = 'waiting'
print(f"Still waiting for {self.APP_NAME} to be terminated…")
elif state == 'waiting' and now - kill_started > 10:
state == 'killing'
print("Waiting for too long, we kill the process")
os.kill(pid, signal.SIGKILL)
sys.exit(1)
sys.exit(0)
def cmd_status(self, args):
config = self.get_config()
pid_file = self.get_pid_file(config)
if pid_file.is_file():
import errno
try:
pid = int(pid_file.read_text())
except Exception as e:
print(f"Can't read PID file at {pid_file}: {e}")
# we use the same exit code as DATA_ERROR in jp
sys.exit(17)
# we check if there is a process
# inspired by https://stackoverflow.com/a/568285 and https://stackoverflow.com/a/6940314
try:
os.kill(pid, 0)
except OSError as e:
if e.errno == errno.ESRCH:
running = False
elif e.errno == errno.EPERM:
print("Process {pid} is run by an other user")
running = True
else:
running = True
if running:
print(f"{self.APP_NAME} is running (pid: {pid})")
sys.exit(0)
else:
print(f"{self.NOT_RUNNING_MSG}, but a pid file is present (bad exit ?): {pid_file}")
sys.exit(2)
else:
print(self.NOT_RUNNING_MSG)
sys.exit(1)
def parse_args(self):
parser = argparse.ArgumentParser(description=f"Launch {self.APP_NAME} backend")
parser.set_defaults(cmd=self.cmd_no_subparser)
subparsers = parser.add_subparsers()
extra_help = f"arguments to pass to {self.APP_NAME} service"
bg_parser = subparsers.add_parser(
'background',
aliases=['bg'],
help=f"run {self.APP_NAME} backend in background (as a daemon)")
bg_parser.add_argument('extra_args', nargs=argparse.REMAINDER, help=extra_help)
bg_parser.set_defaults(cmd=self.cmd_background)
fg_parser = subparsers.add_parser(
'foreground',
aliases=['fg'],
help=f"run {self.APP_NAME} backend in foreground")
fg_parser.add_argument('extra_args', nargs=argparse.REMAINDER, help=extra_help)
fg_parser.set_defaults(cmd=self.cmd_foreground)
dbg_parser = subparsers.add_parser(
'debug',
aliases=['dbg'],
help=f"run {self.APP_NAME} backend in debug mode")
dbg_parser.add_argument('extra_args', nargs=argparse.REMAINDER, help=extra_help)
dbg_parser.set_defaults(cmd=self.cmd_debug)
stop_parser = subparsers.add_parser(
'stop',
help=f"stop running {self.APP_NAME} backend")
stop_parser.set_defaults(cmd=self.cmd_stop)
status_parser = subparsers.add_parser(
'status',
help=f"indicate if {self.APP_NAME} backend is running")
status_parser.set_defaults(cmd=self.cmd_status)
return parser.parse_args()
def get_config(self):
config = ConfigParser(defaults=C.DEFAULT_CONFIG)
try:
config.read(C.CONFIG_FILES)
except Exception as e:
print (rf"/!\ Can't read main config! {e}")
sys.exit(1)
return config
def get_pid_file(self, config):
pid_dir = Path(config.get('DEFAULT', 'pid_dir')).expanduser()
return pid_dir / f"{self.APP_NAME_FILE}.pid"
def run_twistd(self, args, twistd_opts=None):
"""Run twistd settings options with args"""
from twisted.python.runtime import platformType
if platformType == "win32":
from twisted.scripts._twistw import (ServerOptions,
WindowsApplicationRunner as app_runner)
else:
from twisted.scripts._twistd_unix import (ServerOptions,
UnixApplicationRunner as app_runner)
app_runner.loggerFactory = LiberviaLogger
server_options = ServerOptions()
config = self.get_config()
pid_file = self.get_pid_file(config)
log_dir = Path(config.get('DEFAULT', 'log_dir')).expanduser()
log_file = log_dir / f"{self.APP_NAME_FILE}.log"
server_opts = [
'--no_save',
'--pidfile', str(pid_file),
'--logfile', str(log_file),
]
if twistd_opts is not None:
server_opts.extend(twistd_opts)
server_opts.append(self.APP_NAME_FILE)
if args.extra_args:
try:
args.extra_args.remove('--')
except ValueError:
pass
server_opts.extend(args.extra_args)
try:
server_options.parseOptions(server_opts)
except usage.error as ue:
print(server_options)
print("%s: %s" % (sys.argv[0], ue))
sys.exit(1)
else:
runner = app_runner(server_options)
runner.run()
if runner._exitSignal is not None:
app._exitWithSignal(runner._exitSignal)
try:
sys.exit(app._exitCode)
except AttributeError:
pass
@classmethod
def run(cls):
args = cls().parse_args()
args.cmd(args)
if __name__ == '__main__':
Launcher.run()

View file

@ -0,0 +1,426 @@
#!/usr/bin/env python3
# Libervia: an XMPP client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""High level logging functions"""
# XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed.
# TODO: change formatting from "%s" style to "{}" when moved to Python 3
from typing import TYPE_CHECKING, Any, Optional, Dict
if TYPE_CHECKING:
from logging import _ExcInfoType
else:
_ExcInfoType = Any
from libervia.backend.core.constants import Const as C
from libervia.backend.tools.common.ansi import ANSI as A
from libervia.backend.core import exceptions
import traceback
backend = None
_loggers: Dict[str, "Logger"] = {}
handlers = {}
COLOR_START = '%(color_start)s'
COLOR_END = '%(color_end)s'
class Filtered(Exception):
pass
class Logger:
"""High level logging class"""
fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER)
filter_name = None # filter to call
post_treat = None
def __init__(self, name):
if isinstance(name, Logger):
self.copy(name)
else:
self._name = name
def copy(self, other):
"""Copy values from other Logger"""
self.fmt = other.fmt
self.Filter_name = other.fmt
self.post_treat = other.post_treat
self._name = other._name
def add_traceback(self, message):
tb = traceback.format_exc()
return message + "\n==== traceback ====\n" + tb
def out(
self,
message: object,
level: Optional[str] = None,
exc_info: _ExcInfoType = False,
**kwargs
) -> None:
"""Actually log the message
@param message: formatted message
"""
if exc_info:
message = self.add_traceback(message)
print(message)
def log(
self,
level: str,
message: object,
exc_info: _ExcInfoType = False,
**kwargs
) -> None:
"""Print message
@param level: one of C.LOG_LEVELS
@param message: message to format and print
"""
if exc_info:
message = self.add_traceback(message)
try:
formatted = self.format(level, message)
if self.post_treat is None:
self.out(formatted, level, **kwargs)
else:
self.out(self.post_treat(level, formatted), level, **kwargs)
except Filtered:
pass
def format(self, level: str, message: object) -> object:
"""Format message according to Logger.fmt
@param level: one of C.LOG_LEVELS
@param message: message to format
@return: formatted message
@raise: Filtered when the message must not be logged
"""
if self.fmt is None and self.filter_name is None:
return message
record = {'name': self._name,
'message': message,
'levelname': level,
}
try:
if not self.filter_name.dict_filter(record):
raise Filtered
except (AttributeError, TypeError): # XXX: TypeError is here because of a pyjamas bug which need to be fixed (TypeError is raised instead of AttributeError)
if self.filter_name is not None:
raise ValueError("Bad filter: filters must have a .filter method")
try:
return self.fmt % record
except TypeError:
return message
except KeyError as e:
if e.args[0] == 'profile':
# XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production
record['profile'] = configure_cls[backend].get_profile()
return self.fmt % record
else:
raise e
def debug(self, msg: object, **kwargs) -> None:
self.log(C.LOG_LVL_DEBUG, msg, **kwargs)
def info(self, msg: object, **kwargs) -> None:
self.log(C.LOG_LVL_INFO, msg, **kwargs)
def warning(self, msg: object, **kwargs) -> None:
self.log(C.LOG_LVL_WARNING, msg, **kwargs)
def error(self, msg: object, **kwargs) -> None:
self.log(C.LOG_LVL_ERROR, msg, **kwargs)
def critical(self, msg: object, **kwargs) -> None:
self.log(C.LOG_LVL_CRITICAL, msg, **kwargs)
def exception(self, msg: object, exc_info=True, **kwargs) -> None:
self.log(C.LOG_LVL_ERROR, msg, exc_info=exc_info, **kwargs)
class FilterName(object):
"""Filter on logger name according to a regex"""
def __init__(self, name_re):
"""Initialise name filter
@param name_re: regular expression used to filter names (using search and not match)
"""
assert name_re
import re
self.name_re = re.compile(name_re)
def filter(self, record):
if self.name_re.search(record.name) is not None:
return 1
return 0
def dict_filter(self, dict_record):
"""Filter using a dictionary record
@param dict_record: dictionary with at list a key "name" with logger name
@return: True if message should be logged
"""
class LogRecord(object):
pass
log_record = LogRecord()
log_record.name = dict_record['name']
return self.filter(log_record) == 1
class ConfigureBase:
LOGGER_CLASS = Logger
# True if color location is specified in fmt (with COLOR_START)
_color_location = False
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False,
levels_taints_dict=None, force_colors=False, backend_data=None):
"""Configure a backend
@param level: one of C.LOG_LEVELS
@param fmt: format string, pretty much as in std logging.
Accept the following keywords (maybe more depending on backend):
- "message"
- "levelname"
- "name" (logger name)
@param logger: if set, use it as a regular expression to filter on logger name.
Use search to match expression, so ^ or $ can be necessary.
@param colors: if True use ANSI colors to show log levels
@param force_colors: if True ANSI colors are used even if stdout is not a tty
"""
self.backend_data = backend_data
self.pre_treatment()
self.configure_level(level)
self.configure_format(fmt)
self.configure_output(output)
self.configure_logger(logger)
self.configure_colors(colors, force_colors, levels_taints_dict)
self.post_treatment()
self.update_current_logger()
def update_current_logger(self):
"""update existing logger to the class needed for this backend"""
if self.LOGGER_CLASS is None:
return
for name, logger in list(_loggers.items()):
_loggers[name] = self.LOGGER_CLASS(logger)
def pre_treatment(self):
pass
def configure_level(self, level):
if level is not None:
# we deactivate methods below level
level_idx = C.LOG_LEVELS.index(level)
def dev_null(self, msg):
pass
for _level in C.LOG_LEVELS[:level_idx]:
setattr(Logger, _level.lower(), dev_null)
def configure_format(self, fmt):
if fmt is not None:
if fmt != '%(message)s': # %(message)s is the same as None
Logger.fmt = fmt
if COLOR_START in fmt:
ConfigureBase._color_location = True
if fmt.find(COLOR_END,fmt.rfind(COLOR_START))<0:
# color_start not followed by an end, we add it
Logger.fmt += COLOR_END
def configure_output(self, output):
if output is not None:
if output != C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT:
# TODO: manage other outputs
raise NotImplementedError("Basic backend only manage default output yet")
def configure_logger(self, logger):
if logger:
Logger.filter_name = FilterName(logger)
def configure_colors(self, colors, force_colors, levels_taints_dict):
if colors:
# if color are used, we need to handle levels_taints_dict
for level in list(levels_taints_dict.keys()):
# we wants levels in uppercase to correspond to contstants
levels_taints_dict[level.upper()] = levels_taints_dict[level]
taints = self.__class__.taints = {}
for level in C.LOG_LEVELS:
# we want use values and use constant value as default
taint_list = levels_taints_dict.get(level, C.LOG_OPT_TAINTS_DICT[1][level])
ansi_list = []
for elt in taint_list:
elt = elt.upper()
try:
ansi = getattr(A, 'FG_{}'.format(elt))
except AttributeError:
try:
ansi = getattr(A, elt)
except AttributeError:
# we use raw string if element is unknown
ansi = elt
ansi_list.append(ansi)
taints[level] = ''.join(ansi_list)
def post_treatment(self):
pass
def manage_outputs(self, outputs_raw):
""" Parse output option in a backend agnostic way, and fill handlers consequently
@param outputs_raw: output option as enterred in environment variable or in configuration
"""
if not outputs_raw:
return
outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP)
global handlers
if len(outputs) == 1:
handlers[C.LOG_OPT_OUTPUT_FILE] = [outputs.pop()]
for output in outputs:
if not output:
continue
if output[-1] == ')':
# we have options
opt_begin = output.rfind('(')
options = output[opt_begin+1:-1]
output = output[:opt_begin]
else:
options = None
if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, C.LOG_OPT_OUTPUT_MEMORY):
raise ValueError("Invalid output [%s]" % output)
if output == C.LOG_OPT_OUTPUT_DEFAULT:
# no option for defaut handler
handlers[output] = None
elif output == C.LOG_OPT_OUTPUT_FILE:
if not options:
ValueError("{handler} output need a path as option" .format(handle=output))
handlers.setdefault(output, []).append(options)
options = None # option are parsed, we can empty them
elif output == C.LOG_OPT_OUTPUT_MEMORY:
# we have memory handler, option can be the len limit or None
try:
limit = int(options)
options = None # option are parsed, we can empty them
except (TypeError, ValueError):
limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT
handlers[output] = limit
if options: # we should not have unparsed options
raise ValueError("options [{options}] are not supported for {handler} output".format(options=options, handler=output))
@staticmethod
def memory_get(size=None):
"""Return buffered logs
@param size: number of logs to return
"""
raise NotImplementedError
@classmethod
def ansi_colors(cls, level, message):
"""Colorise message depending on level for terminals
@param level: one of C.LOG_LEVELS
@param message: formatted message to log
@return: message with ANSI escape codes for coloration
"""
try:
start = cls.taints[level]
except KeyError:
start = ''
if cls._color_location:
return message % {'color_start': start,
'color_end': A.RESET}
else:
return '%s%s%s' % (start, message, A.RESET)
@staticmethod
def get_profile():
"""Try to find profile value using introspection"""
raise NotImplementedError
class ConfigureCustom(ConfigureBase):
LOGGER_CLASS = None
def __init__(self, logger_class, *args, **kwargs):
ConfigureCustom.LOGGER_CLASS = logger_class
configure_cls = { None: ConfigureBase,
C.LOG_BACKEND_CUSTOM: ConfigureCustom
} # XXX: (key: backend, value: Configure subclass) must be filled when new backend are added
def configure(backend_, **options):
"""Configure logging behaviour
@param backend: can be:
C.LOG_BACKEND_BASIC: use a basic print based logging
C.LOG_BACKEND_CUSTOM: use a given Logger subclass
"""
global backend
if backend is not None:
raise exceptions.InternalError("Logging can only be configured once")
backend = backend_
try:
configure_class = configure_cls[backend]
except KeyError:
raise ValueError("unknown backend [{}]".format(backend))
if backend == C.LOG_BACKEND_CUSTOM:
logger_class = options.pop('logger_class')
configure_class(logger_class, **options)
else:
configure_class(**options)
def memory_get(size=None):
if not C.LOG_OPT_OUTPUT_MEMORY in handlers:
raise ValueError('memory output is not used')
return configure_cls[backend].memory_get(size)
def getLogger(name=C.LOG_BASE_LOGGER) -> Logger:
try:
logger_class = configure_cls[backend].LOGGER_CLASS
except KeyError:
raise ValueError("This method should not be called with backend [{}]".format(backend))
return _loggers.setdefault(name, logger_class(name))
_root_logger = getLogger()
def debug(msg, **kwargs):
_root_logger.debug(msg, **kwargs)
def info(msg, **kwargs):
_root_logger.info(msg, **kwargs)
def warning(msg, **kwargs):
_root_logger.warning(msg, **kwargs)
def error(msg, **kwargs):
_root_logger.error(msg, **kwargs)
def critical(msg, **kwargs):
_root_logger.critical(msg, **kwargs)

View file

@ -0,0 +1,411 @@
#!/usr/bin/env python3
# Libervia: an XMPP client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""High level logging functions"""
# XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed.
from libervia.backend.core.constants import Const as C
from libervia.backend.core import log
class TwistedLogger(log.Logger):
colors = True
force_colors = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
from twisted.logger import Logger
self.twisted_log = Logger()
def out(self, message, level=None, **kwargs):
"""Actually log the message
@param message: formatted message
"""
self.twisted_log.emit(
level=self.level_map[level],
format=message,
sat_logged=True,
**kwargs,
)
class ConfigureBasic(log.ConfigureBase):
def configure_colors(self, colors, force_colors, levels_taints_dict):
super(ConfigureBasic, self).configure_colors(
colors, force_colors, levels_taints_dict
)
if colors:
import sys
try:
isatty = sys.stdout.isatty()
except AttributeError:
isatty = False
# FIXME: isatty should be tested on each handler, not globaly
if (force_colors or isatty):
# we need colors
log.Logger.post_treat = lambda logger, level, message: self.ansi_colors(
level, message
)
elif force_colors:
raise ValueError("force_colors can't be used if colors is False")
@staticmethod
def get_profile():
"""Try to find profile value using introspection"""
import inspect
stack = inspect.stack()
current_path = stack[0][1]
for frame_data in stack[:-1]:
if frame_data[1] != current_path:
if (
log.backend == C.LOG_BACKEND_STANDARD
and "/logging/__init__.py" in frame_data[1]
):
continue
break
frame = frame_data[0]
args = inspect.getargvalues(frame)
try:
profile = args.locals.get("profile") or args.locals["profile_key"]
except (TypeError, KeyError):
try:
try:
profile = args.locals["self"].profile
except AttributeError:
try:
profile = args.locals["self"].parent.profile
except AttributeError:
profile = args.locals[
"self"
].host.profile # used in quick_frontend for single profile configuration
except Exception:
# we can't find profile, we return an empty value
profile = ""
return profile
class ConfigureTwisted(ConfigureBasic):
LOGGER_CLASS = TwistedLogger
def pre_treatment(self):
from twisted import logger
global logger
self.level_map = {
C.LOG_LVL_DEBUG: logger.LogLevel.debug,
C.LOG_LVL_INFO: logger.LogLevel.info,
C.LOG_LVL_WARNING: logger.LogLevel.warn,
C.LOG_LVL_ERROR: logger.LogLevel.error,
C.LOG_LVL_CRITICAL: logger.LogLevel.critical,
}
self.LOGGER_CLASS.level_map = self.level_map
def configure_level(self, level):
self.level = self.level_map[level]
def configure_output(self, output):
import sys
from twisted.python import logfile
self.log_publisher = logger.LogPublisher()
if output is None:
output = C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT
self.manage_outputs(output)
if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers:
if self.backend_data is None:
raise ValueError(
"You must pass options as backend_data with Twisted backend"
)
options = self.backend_data
log_file = logfile.LogFile.fromFullPath(options['logfile'])
self.log_publisher.addObserver(
logger.FileLogObserver(log_file, self.text_formatter))
# we also want output to stdout if we are in debug or nodaemon mode
if options.get("nodaemon", False) or options.get("debug", False):
self.log_publisher.addObserver(
logger.FileLogObserver(sys.stdout, self.text_formatter))
if C.LOG_OPT_OUTPUT_FILE in log.handlers:
for path in log.handlers[C.LOG_OPT_OUTPUT_FILE]:
log_file = (
sys.stdout if path == "-" else logfile.LogFile.fromFullPath(path)
)
self.log_publisher.addObserver(
logger.FileLogObserver(log_file, self.text_formatter))
if C.LOG_OPT_OUTPUT_MEMORY in log.handlers:
raise NotImplementedError(
"Memory observer is not implemented in Twisted backend"
)
def configure_colors(self, colors, force_colors, levels_taints_dict):
super(ConfigureTwisted, self).configure_colors(
colors, force_colors, levels_taints_dict
)
self.LOGGER_CLASS.colors = colors
self.LOGGER_CLASS.force_colors = force_colors
if force_colors and not colors:
raise ValueError("colors must be True if force_colors is True")
def post_treatment(self):
"""Install twistedObserver which manage non SàT logs"""
# from twisted import logger
import sys
filtering_obs = logger.FilteringLogObserver(
observer=self.log_publisher,
predicates=[
logger.LogLevelFilterPredicate(self.level),
]
)
logger.globalLogBeginner.beginLoggingTo([filtering_obs])
def text_formatter(self, event):
if event.get('sat_logged', False):
timestamp = ''.join([logger.formatTime(event.get("log_time", None)), " "])
return f"{timestamp}{event.get('log_format', '')}\n"
else:
eventText = logger.eventAsText(
event, includeSystem=True)
if not eventText:
return None
return eventText.replace("\n", "\n\t") + "\n"
class ConfigureStandard(ConfigureBasic):
def __init__(
self,
level=None,
fmt=None,
output=None,
logger=None,
colors=False,
levels_taints_dict=None,
force_colors=False,
backend_data=None,
):
if fmt is None:
fmt = C.LOG_OPT_FORMAT[1]
if output is None:
output = C.LOG_OPT_OUTPUT[1]
super(ConfigureStandard, self).__init__(
level,
fmt,
output,
logger,
colors,
levels_taints_dict,
force_colors,
backend_data,
)
def pre_treatment(self):
"""We use logging methods directly, instead of using Logger"""
import logging
log.getLogger = logging.getLogger
log.debug = logging.debug
log.info = logging.info
log.warning = logging.warning
log.error = logging.error
log.critical = logging.critical
def configure_level(self, level):
if level is None:
level = C.LOG_LVL_DEBUG
self.level = level
def configure_format(self, fmt):
super(ConfigureStandard, self).configure_format(fmt)
import logging
class SatFormatter(logging.Formatter):
"""Formatter which manage SàT specificities"""
_format = fmt
_with_profile = "%(profile)s" in fmt
def __init__(self, can_colors=False):
super(SatFormatter, self).__init__(self._format)
self.can_colors = can_colors
def format(self, record):
if self._with_profile:
record.profile = ConfigureStandard.get_profile()
do_color = self.with_colors and (self.can_colors or self.force_colors)
if ConfigureStandard._color_location:
# we copy raw formatting strings for color_*
# as formatting is handled in ansi_colors in this case
if do_color:
record.color_start = log.COLOR_START
record.color_end = log.COLOR_END
else:
record.color_start = record.color_end = ""
s = super(SatFormatter, self).format(record)
if do_color:
s = ConfigureStandard.ansi_colors(record.levelname, s)
return s
self.formatterClass = SatFormatter
def configure_output(self, output):
self.manage_outputs(output)
def configure_logger(self, logger):
self.name_filter = log.FilterName(logger) if logger else None
def configure_colors(self, colors, force_colors, levels_taints_dict):
super(ConfigureStandard, self).configure_colors(
colors, force_colors, levels_taints_dict
)
self.formatterClass.with_colors = colors
self.formatterClass.force_colors = force_colors
if not colors and force_colors:
raise ValueError("force_colors can't be used if colors is False")
def _add_handler(self, root_logger, hdlr, can_colors=False):
hdlr.setFormatter(self.formatterClass(can_colors))
root_logger.addHandler(hdlr)
root_logger.setLevel(self.level)
if self.name_filter is not None:
hdlr.addFilter(self.name_filter)
def post_treatment(self):
import logging
root_logger = logging.getLogger()
if len(root_logger.handlers) == 0:
for handler, options in list(log.handlers.items()):
if handler == C.LOG_OPT_OUTPUT_DEFAULT:
hdlr = logging.StreamHandler()
try:
can_colors = hdlr.stream.isatty()
except AttributeError:
can_colors = False
self._add_handler(root_logger, hdlr, can_colors=can_colors)
elif handler == C.LOG_OPT_OUTPUT_MEMORY:
from logging.handlers import BufferingHandler
class SatMemoryHandler(BufferingHandler):
def emit(self, record):
super(SatMemoryHandler, self).emit(self.format(record))
hdlr = SatMemoryHandler(options)
log.handlers[
handler
] = (
hdlr
) # we keep a reference to the handler to read the buffer later
self._add_handler(root_logger, hdlr, can_colors=False)
elif handler == C.LOG_OPT_OUTPUT_FILE:
import os.path
for path in options:
hdlr = logging.FileHandler(os.path.expanduser(path))
self._add_handler(root_logger, hdlr, can_colors=False)
else:
raise ValueError("Unknown handler type")
else:
root_logger.warning("Handlers already set on root logger")
@staticmethod
def memory_get(size=None):
"""Return buffered logs
@param size: number of logs to return
"""
mem_handler = log.handlers[C.LOG_OPT_OUTPUT_MEMORY]
return (
log_msg for log_msg in mem_handler.buffer[size if size is None else -size :]
)
log.configure_cls[C.LOG_BACKEND_BASIC] = ConfigureBasic
log.configure_cls[C.LOG_BACKEND_TWISTED] = ConfigureTwisted
log.configure_cls[C.LOG_BACKEND_STANDARD] = ConfigureStandard
def configure(backend, **options):
"""Configure logging behaviour
@param backend: can be:
C.LOG_BACKEND_STANDARD: use standard logging module
C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer)
C.LOG_BACKEND_BASIC: use a basic print based logging
C.LOG_BACKEND_CUSTOM: use a given Logger subclass
"""
return log.configure(backend, **options)
def _parse_options(options):
"""Parse string options as given in conf or environment variable, and return expected python value
@param options (dict): options with (key: name, value: string value)
"""
COLORS = C.LOG_OPT_COLORS[0]
LEVEL = C.LOG_OPT_LEVEL[0]
if COLORS in options:
if options[COLORS].lower() in ("1", "true"):
options[COLORS] = True
elif options[COLORS] == "force":
options[COLORS] = True
options["force_colors"] = True
else:
options[COLORS] = False
if LEVEL in options:
level = options[LEVEL].upper()
if level not in C.LOG_LEVELS:
level = C.LOG_LVL_INFO
options[LEVEL] = level
def sat_configure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
"""Configure logging system for SàT, can be used by frontends
logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
@param backend: backend to use, it can be:
- C.LOG_BACKEND_BASIC: print based backend
- C.LOG_BACKEND_TWISTED: Twisted logging backend
- C.LOG_BACKEND_STANDARD: standard logging backend
@param const: Const class to use instead of sat.core.constants.Const (mainly used to change default values)
"""
if const is not None:
global C
C = const
log.C = const
from libervia.backend.tools import config
import os
log_conf = {}
sat_conf = config.parse_main_conf()
for opt_name, opt_default in C.LOG_OPTIONS():
try:
log_conf[opt_name] = os.environ[
"".join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))
]
except KeyError:
log_conf[opt_name] = config.config_get(
sat_conf, C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name, opt_default
)
_parse_options(log_conf)
configure(backend, backend_data=backend_data, **log_conf)

View file

@ -0,0 +1,156 @@
import copy
from twisted.words.protocols.jabber import xmlstream, sasl, client as tclient, jid
from wokkel import client
from libervia.backend.core.constants import Const as C
from libervia.backend.core.log import getLogger
log = getLogger(__name__)
"""This module applies monkey patches to Twisted and Wokkel
First part handle certificate validation during XMPP connectionand are temporary
(until merged upstream).
Second part add a trigger point to send and onElement method of XmlStream
"""
## certificate validation patches
class XMPPClient(client.XMPPClient):
def __init__(self, jid, password, host=None, port=5222,
tls_required=True, configurationForTLS=None):
self.jid = jid
self.domain = jid.host.encode('idna')
self.host = host
self.port = port
factory = HybridClientFactory(
jid, password, tls_required=tls_required,
configurationForTLS=configurationForTLS)
client.StreamManager.__init__(self, factory)
def HybridClientFactory(jid, password, tls_required=True, configurationForTLS=None):
a = HybridAuthenticator(jid, password, tls_required, configurationForTLS)
return xmlstream.XmlStreamFactory(a)
class HybridAuthenticator(client.HybridAuthenticator):
res_binding = True
def __init__(self, jid, password, tls_required=True, configurationForTLS=None):
xmlstream.ConnectAuthenticator.__init__(self, jid.host)
self.jid = jid
self.password = password
self.tls_required = tls_required
self.configurationForTLS = configurationForTLS
def associateWithStream(self, xs):
xmlstream.ConnectAuthenticator.associateWithStream(self, xs)
tlsInit = xmlstream.TLSInitiatingInitializer(
xs, required=self.tls_required, configurationForTLS=self.configurationForTLS)
xs.initializers = [client.client.CheckVersionInitializer(xs),
tlsInit,
CheckAuthInitializer(xs, self.res_binding)]
# XmlStream triggers
class XmlStream(xmlstream.XmlStream):
"""XmlStream which allows to add hooks"""
def __init__(self, authenticator):
xmlstream.XmlStream.__init__(self, authenticator)
# hooks at this level should not modify content
# so it's not needed to handle priority as with triggers
self._onElementHooks = []
self._sendHooks = []
def add_hook(self, hook_type, callback):
"""Add a send or receive hook"""
conflict_msg = f"Hook conflict: can't add {hook_type} hook {callback}"
if hook_type == C.STREAM_HOOK_RECEIVE:
if callback not in self._onElementHooks:
self._onElementHooks.append(callback)
else:
log.warning(conflict_msg)
elif hook_type == C.STREAM_HOOK_SEND:
if callback not in self._sendHooks:
self._sendHooks.append(callback)
else:
log.warning(conflict_msg)
else:
raise ValueError(f"Invalid hook type: {hook_type}")
def onElement(self, element):
for hook in self._onElementHooks:
hook(element)
xmlstream.XmlStream.onElement(self, element)
def send(self, obj):
for hook in self._sendHooks:
hook(obj)
xmlstream.XmlStream.send(self, obj)
# Binding activation (needed for stream management, XEP-0198)
class CheckAuthInitializer(client.CheckAuthInitializer):
def __init__(self, xs, res_binding):
super(CheckAuthInitializer, self).__init__(xs)
self.res_binding = res_binding
def initialize(self):
# XXX: modification of client.CheckAuthInitializer which has optional
# resource binding, and which doesn't do deprecated
# SessionInitializer
if (sasl.NS_XMPP_SASL, 'mechanisms') in self.xmlstream.features:
inits = [(sasl.SASLInitiatingInitializer, True)]
if self.res_binding:
inits.append((tclient.BindInitializer, True)),
for initClass, required in inits:
init = initClass(self.xmlstream)
init.required = required
self.xmlstream.initializers.append(init)
elif (tclient.NS_IQ_AUTH_FEATURE, 'auth') in self.xmlstream.features:
self.xmlstream.initializers.append(
tclient.IQAuthInitializer(self.xmlstream))
else:
raise Exception("No available authentication method found")
# jid fix
def internJID(jidstring):
"""
Return interned JID.
@rtype: L{JID}
"""
# XXX: this interJID return a copy of the cached jid
# this avoid modification of cached jid as JID is mutable
# TODO: propose this upstream
if jidstring in jid.__internJIDs:
return copy.copy(jid.__internJIDs[jidstring])
else:
j = jid.JID(jidstring)
jid.__internJIDs[jidstring] = j
return copy.copy(j)
def apply():
# certificate validation
client.XMPPClient = XMPPClient
# XmlStream triggers
xmlstream.XmlStreamFactory.protocol = XmlStream
# jid fix
jid.internJID = internJID

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff