core, setup: huge cleaning:
- moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
This commit is contained in:
parent
4afd2ff26f
commit
80ef13612b
8 changed files with 42 additions and 269 deletions
0
sat/memory/__init__.py
Normal file
0
sat/memory/__init__.py
Normal file
153
sat/memory/cache.py
Normal file
153
sat/memory/cache.py
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
# Copyright (C) 2009-2018 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 sat.core.log import getLogger
|
||||
log = getLogger(__name__)
|
||||
from sat.tools.common import regex
|
||||
from sat.core import exceptions
|
||||
from sat.core.constants import Const as C
|
||||
import cPickle as pickle
|
||||
import mimetypes
|
||||
import os.path
|
||||
import time
|
||||
|
||||
DEFAULT_EXT = '.raw'
|
||||
|
||||
|
||||
class Cache(object):
|
||||
"""generic file caching"""
|
||||
|
||||
def __init__(self, host, profile):
|
||||
"""
|
||||
@param profile(unicode, None): ame of the profile to set the cache for
|
||||
if None, the cache will be common for all profiles
|
||||
"""
|
||||
self.profile = profile
|
||||
path_elts = [host.memory.getConfig('', 'local_dir'), C.CACHE_DIR]
|
||||
if profile:
|
||||
path_elts.extend([u'profiles',regex.pathEscape(profile)])
|
||||
else:
|
||||
path_elts.append(u'common')
|
||||
self.cache_dir = os.path.join(*path_elts)
|
||||
|
||||
if not os.path.exists(self.cache_dir):
|
||||
os.makedirs(self.cache_dir)
|
||||
|
||||
def getPath(self, filename):
|
||||
"""return cached file URL
|
||||
|
||||
@param filename(unicode): cached file name (cache data or actual file)
|
||||
"""
|
||||
if not filename or u'/' in filename:
|
||||
log.error(u"invalid char found in file name, hack attempt? name:{}".format(filename))
|
||||
raise exceptions.DataError(u"Invalid char found")
|
||||
return os.path.join(self.cache_dir, filename)
|
||||
|
||||
def getMetadata(self, uid):
|
||||
"""retrieve metadata for cached data
|
||||
|
||||
@param uid(unicode): unique identifier of file
|
||||
@return (dict, None): metadata with following keys:
|
||||
see [cacheData] for data details, an additional "path" key is the full path to cached file.
|
||||
None if file is not in cache (or cache is invalid)
|
||||
"""
|
||||
|
||||
uid = uid.strip()
|
||||
if not uid:
|
||||
raise exceptions.InternalError(u"uid must not be empty")
|
||||
cache_url = self.getPath(uid)
|
||||
if not os.path.exists(cache_url):
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(cache_url, 'rb') as f:
|
||||
cache_data = pickle.load(f)
|
||||
except IOError:
|
||||
log.warning(u"can't read cache at {}".format(cache_url))
|
||||
return None
|
||||
except pickle.UnpicklingError:
|
||||
log.warning(u'invalid cache found at {}'.format(cache_url))
|
||||
return None
|
||||
|
||||
try:
|
||||
eol = cache_data['eol']
|
||||
except KeyError:
|
||||
log.warning(u'no End Of Life found for cached file {}'.format(uid))
|
||||
eol = 0
|
||||
if eol < time.time():
|
||||
log.debug(u"removing expired cache (expired for {}s)".format(
|
||||
time.time() - eol))
|
||||
return None
|
||||
|
||||
cache_data['path'] = self.getPath(cache_data['filename'])
|
||||
return cache_data
|
||||
|
||||
def getFilePath(self, uid):
|
||||
"""retrieve absolute path to file
|
||||
|
||||
@param uid(unicode): unique identifier of file
|
||||
@return (unicode, None): absolute path to cached file
|
||||
None if file is not in cache (or cache is invalid)
|
||||
"""
|
||||
metadata = self.getMetadata(uid)
|
||||
if metadata is not None:
|
||||
return metadata['path']
|
||||
|
||||
def cacheData(self, source, uid, mime_type=None, max_age=None, filename=None):
|
||||
"""create cache metadata and file object to use for actual data
|
||||
|
||||
@param source(unicode): source of the cache (should be plugin's import_name)
|
||||
@param uid(unicode): an identifier of the file which must be unique
|
||||
@param mime_type(unicode): MIME type of the file to cache
|
||||
it will be used notably to guess file extension
|
||||
@param max_age(int, None): maximum age in seconds
|
||||
the cache metadata will have an "eol" (end of life)
|
||||
None to use default value
|
||||
0 to ignore cache (file will be re-downloaded on each access)
|
||||
@param filename: if not None, will be used as filename
|
||||
else one will be generated from uid and guessed extension
|
||||
@return(file): file object opened in write mode
|
||||
you have to close it yourself (hint: use with statement)
|
||||
"""
|
||||
cache_url = self.getPath(uid)
|
||||
if filename is None:
|
||||
if mime_type:
|
||||
ext = mimetypes.guess_extension(mime_type, strict=False)
|
||||
if ext is None:
|
||||
log.warning(u"can't find extension for MIME type {}".format(mime_type))
|
||||
ext = DEFAULT_EXT
|
||||
elif ext == u'.jpe':
|
||||
ext = u'.jpg'
|
||||
else:
|
||||
ext = DEFAULT_EXT
|
||||
mime_type = None
|
||||
filename = uid + ext
|
||||
if max_age is None:
|
||||
max_age = C.DEFAULT_MAX_AGE
|
||||
cache_data = {u'source': source,
|
||||
u'filename': filename,
|
||||
u'eol': int(time.time()) + max_age,
|
||||
u'mime_type': mime_type,
|
||||
}
|
||||
file_path = self.getPath(filename)
|
||||
|
||||
with open(cache_url, 'wb') as f:
|
||||
pickle.dump(cache_data, f, protocol=2)
|
||||
|
||||
return open(file_path, 'wb')
|
||||
145
sat/memory/crypto.py
Normal file
145
sat/memory/crypto.py
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
# Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
|
||||
# Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.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 Crypto.Cipher import AES
|
||||
from Crypto.Protocol.KDF import PBKDF2
|
||||
except ImportError:
|
||||
raise Exception("PyCrypto is not installed.")
|
||||
|
||||
from os import urandom
|
||||
from base64 import b64encode, b64decode
|
||||
from twisted.internet.threads import deferToThread
|
||||
from twisted.internet.defer import succeed
|
||||
|
||||
|
||||
class BlockCipher(object):
|
||||
|
||||
BLOCK_SIZE = AES.block_size # 16 bits
|
||||
MAX_KEY_SIZE = AES.key_size[-1] # 32 bits = AES-256
|
||||
IV_SIZE = BLOCK_SIZE # initialization vector size, 16 bits
|
||||
|
||||
@classmethod
|
||||
def encrypt(cls, key, text, leave_empty=True):
|
||||
"""Encrypt a message.
|
||||
|
||||
Based on http://stackoverflow.com/a/12525165
|
||||
|
||||
@param key (unicode): the encryption key
|
||||
@param text (unicode): the text to encrypt
|
||||
@param leave_empty (bool): if True, empty text will be returned "as is"
|
||||
@return: Deferred: base-64 encoded str
|
||||
"""
|
||||
if leave_empty and text == '':
|
||||
return succeed(text)
|
||||
iv = BlockCipher.getRandomKey()
|
||||
key = key.encode('utf-8')
|
||||
key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
|
||||
cipher = AES.new(key, AES.MODE_CFB, iv)
|
||||
d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode('utf-8')))
|
||||
d.addCallback(lambda ciphertext: b64encode(iv + ciphertext))
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
def decrypt(cls, key, ciphertext, leave_empty=True):
|
||||
"""Decrypt a message.
|
||||
|
||||
Based on http://stackoverflow.com/a/12525165
|
||||
|
||||
@param key (unicode): the decryption key
|
||||
@param ciphertext (base-64 encoded str): the text to decrypt
|
||||
@param leave_empty (bool): if True, empty ciphertext will be returned "as is"
|
||||
@return: Deferred: str or None if the password could not be decrypted
|
||||
"""
|
||||
if leave_empty and ciphertext == '':
|
||||
return succeed('')
|
||||
ciphertext = b64decode(ciphertext)
|
||||
iv, ciphertext = ciphertext[:BlockCipher.IV_SIZE], ciphertext[BlockCipher.IV_SIZE:]
|
||||
key = key.encode('utf-8')
|
||||
key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
|
||||
cipher = AES.new(key, AES.MODE_CFB, iv)
|
||||
d = deferToThread(cipher.decrypt, ciphertext)
|
||||
d.addCallback(lambda text: BlockCipher.unpad(text))
|
||||
# XXX: cipher.decrypt gives no way to make the distinction between
|
||||
# a decrypted empty value and a decryption failure... both return
|
||||
# the empty value. Fortunately, we detect empty passwords beforehand
|
||||
# thanks to the "leave_empty" parameter which is used by default.
|
||||
d.addCallback(lambda text: text.decode('utf-8') if text else None)
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
def getRandomKey(cls, size=None, base64=False):
|
||||
"""Return a random key suitable for block cipher encryption.
|
||||
|
||||
Note: a good value for the key length is to make it as long as the block size.
|
||||
|
||||
@param size: key length in bytes, positive or null (default: BlockCipher.IV_SIZE)
|
||||
@param base64: if True, encode the result to base-64
|
||||
@return: str (eventually base-64 encoded)
|
||||
"""
|
||||
if size is None or size < 0:
|
||||
size = BlockCipher.IV_SIZE
|
||||
key = urandom(size)
|
||||
return b64encode(key) if base64 else key
|
||||
|
||||
@classmethod
|
||||
def pad(self, s):
|
||||
"""Method from http://stackoverflow.com/a/12525165"""
|
||||
bs = BlockCipher.BLOCK_SIZE
|
||||
return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
|
||||
|
||||
@classmethod
|
||||
def unpad(self, s):
|
||||
"""Method from http://stackoverflow.com/a/12525165"""
|
||||
return s[0:-ord(s[-1])]
|
||||
|
||||
|
||||
class PasswordHasher(object):
|
||||
|
||||
SALT_LEN = 16 # 128 bits
|
||||
|
||||
@classmethod
|
||||
def hash(cls, password, salt=None, leave_empty=True):
|
||||
"""Hash a password.
|
||||
|
||||
@param password (str): the password to hash
|
||||
@param salt (base-64 encoded str): if not None, use the given salt instead of a random value
|
||||
@param leave_empty (bool): if True, empty password will be returned "as is"
|
||||
@return: Deferred: base-64 encoded str
|
||||
"""
|
||||
if leave_empty and password == '':
|
||||
return succeed(password)
|
||||
salt = b64decode(salt)[:PasswordHasher.SALT_LEN] if salt else urandom(PasswordHasher.SALT_LEN)
|
||||
d = deferToThread(PBKDF2, password, salt)
|
||||
d.addCallback(lambda hashed: b64encode(salt + hashed))
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
def verify(cls, attempt, hashed):
|
||||
"""Verify a password attempt.
|
||||
|
||||
@param attempt (str): the attempt to check
|
||||
@param hashed (str): the hash of the password
|
||||
@return: Deferred: boolean
|
||||
"""
|
||||
leave_empty = hashed == ''
|
||||
d = PasswordHasher.hash(attempt, hashed, leave_empty)
|
||||
d.addCallback(lambda hashed_attempt: hashed_attempt == hashed)
|
||||
return d
|
||||
389
sat/memory/disco.py
Executable file
389
sat/memory/disco.py
Executable file
|
|
@ -0,0 +1,389 @@
|
|||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
# Copyright (C) 2009-2018 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 sat.core.i18n import _
|
||||
from sat.core import exceptions
|
||||
from sat.core.log import getLogger
|
||||
log = getLogger(__name__)
|
||||
from twisted.words.protocols.jabber import jid
|
||||
from twisted.words.protocols.jabber.error import StanzaError
|
||||
from twisted.internet import defer
|
||||
from twisted.internet import reactor
|
||||
from twisted.python import failure
|
||||
from sat.core.constants import Const as C
|
||||
from sat.tools import xml_tools
|
||||
from sat.memory import persistent
|
||||
from wokkel import disco
|
||||
from base64 import b64encode
|
||||
from hashlib import sha1
|
||||
|
||||
|
||||
TIMEOUT = 15
|
||||
CAP_HASH_ERROR = 'ERROR'
|
||||
|
||||
class HashGenerationError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ByteIdentity(object):
|
||||
"""This class manage identity as bytes (needed for i;octet sort), it is used for the hash generation"""
|
||||
|
||||
def __init__(self, identity, lang=None):
|
||||
assert isinstance(identity, disco.DiscoIdentity)
|
||||
self.category = identity.category.encode('utf-8')
|
||||
self.idType = identity.type.encode('utf-8')
|
||||
self.name = identity.name.encode('utf-8') if identity.name else ''
|
||||
self.lang = lang.encode('utf-8') if lang is not None else ''
|
||||
|
||||
def __str__(self):
|
||||
return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
|
||||
|
||||
|
||||
class HashManager(object):
|
||||
"""map object which manage hashes
|
||||
|
||||
persistent storage is update when a new hash is added
|
||||
"""
|
||||
|
||||
def __init__(self, persistent):
|
||||
self.hashes = {
|
||||
CAP_HASH_ERROR: disco.DiscoInfo(), # used when we can't get disco infos
|
||||
}
|
||||
self.persistent = persistent
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.hashes[key]
|
||||
|
||||
def __setitem__(self, hash_, disco_info):
|
||||
if hash_ in self.hashes:
|
||||
log.debug(u"ignoring hash set: it is already known")
|
||||
return
|
||||
self.hashes[hash_] = disco_info
|
||||
self.persistent[hash_] = disco_info.toElement().toXml()
|
||||
|
||||
def __contains__(self, hash_):
|
||||
return self.hashes.__contains__(hash_)
|
||||
|
||||
def load(self):
|
||||
def fillHashes(hashes):
|
||||
for hash_, xml in hashes.iteritems():
|
||||
element = xml_tools.ElementParser()(xml)
|
||||
self.hashes[hash_] = disco.DiscoInfo.fromElement(element)
|
||||
log.info(u"Disco hashes loaded")
|
||||
d = self.persistent.load()
|
||||
d.addCallback(fillHashes)
|
||||
return d
|
||||
|
||||
|
||||
class Discovery(object):
|
||||
""" Manage capabilities of entities """
|
||||
|
||||
def __init__(self, host):
|
||||
self.host = host
|
||||
# TODO: remove legacy hashes
|
||||
|
||||
def load(self):
|
||||
"""Load persistent hashes"""
|
||||
self.hashes = HashManager(persistent.PersistentDict("disco"))
|
||||
return self.hashes.load()
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def hasFeature(self, client, feature, jid_=None, node=u''):
|
||||
"""Tell if an entity has the required feature
|
||||
|
||||
@param feature: feature namespace
|
||||
@param jid_: jid of the target, or None for profile's server
|
||||
@param node(unicode): optional node to use for disco request
|
||||
@return: a Deferred which fire a boolean (True if feature is available)
|
||||
"""
|
||||
disco_infos = yield self.getInfos(client, jid_, node)
|
||||
defer.returnValue(feature in disco_infos.features)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def checkFeature(self, client, feature, jid_=None, node=u''):
|
||||
"""Like hasFeature, but raise an exception is feature is not Found
|
||||
|
||||
@param feature: feature namespace
|
||||
@param jid_: jid of the target, or None for profile's server
|
||||
@param node(unicode): optional node to use for disco request
|
||||
|
||||
@raise: exceptions.FeatureNotFound
|
||||
"""
|
||||
disco_infos = yield self.getInfos(client, jid_, node)
|
||||
if not feature in disco_infos.features:
|
||||
raise failure.Failure(exceptions.FeatureNotFound)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def checkFeatures(self, client, features, jid_=None, identity=None, node=u''):
|
||||
"""Like checkFeature, but check several features at once, and check also identity
|
||||
|
||||
@param features(iterable[unicode]): features to check
|
||||
@param jid_(jid.JID): jid of the target, or None for profile's server
|
||||
@param node(unicode): optional node to use for disco request
|
||||
@param identity(None, tuple(unicode, unicode): if not None, the entity must have an identity with this (category, type) tuple
|
||||
|
||||
@raise: exceptions.FeatureNotFound
|
||||
"""
|
||||
disco_infos = yield self.getInfos(client, jid_, node)
|
||||
if not set(features).issubset(disco_infos.features):
|
||||
raise failure.Failure(exceptions.FeatureNotFound())
|
||||
|
||||
if identity is not None and identity not in disco_infos.identities:
|
||||
raise failure.Failure(exceptions.FeatureNotFound())
|
||||
|
||||
def getInfos(self, client, jid_=None, node=u'', use_cache=True):
|
||||
"""get disco infos from jid_, filling capability hash if needed
|
||||
|
||||
@param jid_: jid of the target, or None for profile's server
|
||||
@param node(unicode): optional node to use for disco request
|
||||
@param use_cache(bool): if True, use cached data if available
|
||||
@return: a Deferred which fire disco.DiscoInfo
|
||||
"""
|
||||
if jid_ is None:
|
||||
jid_ = jid.JID(client.jid.host)
|
||||
try:
|
||||
cap_hash = self.host.memory.getEntityData(jid_, [C.ENTITY_CAP_HASH], client.profile)[C.ENTITY_CAP_HASH]
|
||||
if not use_cache:
|
||||
# we ignore cache, so we pretend we haven't found it
|
||||
raise KeyError
|
||||
except (KeyError, exceptions.UnknownEntityError):
|
||||
# capability hash is not available, we'll compute one
|
||||
def infosCb(disco_infos):
|
||||
cap_hash = self.generateHash(disco_infos)
|
||||
self.hashes[cap_hash] = disco_infos
|
||||
self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile)
|
||||
return disco_infos
|
||||
def infosEb(fail):
|
||||
if fail.check(defer.CancelledError):
|
||||
reason = u"request time-out"
|
||||
else:
|
||||
try:
|
||||
reason = unicode(fail.value)
|
||||
except AttributeError:
|
||||
reason = unicode(fail)
|
||||
log.warning(u"Error while requesting disco infos from {jid}: {reason}".format(jid=jid_.full(), reason=reason))
|
||||
self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, CAP_HASH_ERROR, profile_key=client.profile)
|
||||
disco_infos = self.hashes[CAP_HASH_ERROR]
|
||||
return disco_infos
|
||||
d = client.disco.requestInfo(jid_, nodeIdentifier=node)
|
||||
d.addCallback(infosCb)
|
||||
d.addErrback(infosEb)
|
||||
return d
|
||||
else:
|
||||
disco_infos = self.hashes[cap_hash]
|
||||
return defer.succeed(disco_infos)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def getItems(self, client, jid_=None, node=u'', use_cache=True):
|
||||
"""get disco items from jid_, cache them for our own server
|
||||
|
||||
@param jid_(jid.JID): jid of the target, or None for profile's server
|
||||
@param node(unicode): optional node to use for disco request
|
||||
@param use_cache(bool): if True, use cached data if available
|
||||
@return: a Deferred which fire disco.DiscoItems
|
||||
"""
|
||||
server_jid = jid.JID(client.jid.host)
|
||||
if jid_ is None:
|
||||
jid_ = server_jid
|
||||
|
||||
if jid_ == server_jid and not node:
|
||||
# we cache items only for our own server and if node is not set
|
||||
try:
|
||||
items = self.host.memory.getEntityData(jid_, ["DISCO_ITEMS"], client.profile)["DISCO_ITEMS"]
|
||||
log.debug(u"[%s] disco items are in cache" % jid_.full())
|
||||
if not use_cache:
|
||||
# we ignore cache, so we pretend we haven't found it
|
||||
raise KeyError
|
||||
except (KeyError, exceptions.UnknownEntityError):
|
||||
log.debug(u"Caching [%s] disco items" % jid_.full())
|
||||
items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
|
||||
self.host.memory.updateEntityData(jid_, "DISCO_ITEMS", items, profile_key=client.profile)
|
||||
else:
|
||||
try:
|
||||
items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
|
||||
except StanzaError as e:
|
||||
log.warning(u"Error while requesting items for {jid}: {reason}"
|
||||
.format(jid=jid_.full(), reason=e.condition))
|
||||
items = disco.DiscoItems()
|
||||
|
||||
defer.returnValue(items)
|
||||
|
||||
|
||||
def _infosEb(self, failure_, entity_jid):
|
||||
failure_.trap(StanzaError)
|
||||
log.warning(_(u"Error while requesting [%(jid)s]: %(error)s") % {'jid': entity_jid.full(),
|
||||
'error': failure_.getErrorMessage()})
|
||||
|
||||
def findServiceEntity(self, client, category, type_, jid_=None):
|
||||
"""Helper method to find first available entity from findServiceEntities
|
||||
|
||||
args are the same as for [findServiceEntities]
|
||||
@return (jid.JID, None): found entity
|
||||
"""
|
||||
d = self.host.findServiceEntities(client, "pubsub", "service")
|
||||
d.addCallback(lambda entities: entities.pop() if entities else None)
|
||||
return d
|
||||
|
||||
def findServiceEntities(self, client, category, type_, jid_=None):
|
||||
"""Return all available items of an entity which correspond to (category, type_)
|
||||
|
||||
@param category: identity's category
|
||||
@param type_: identitiy's type
|
||||
@param jid_: the jid of the target server (None for profile's server)
|
||||
@return: a set of found entities
|
||||
@raise defer.CancelledError: the request timed out
|
||||
"""
|
||||
found_entities = set()
|
||||
|
||||
def infosCb(infos, entity_jid):
|
||||
if (category, type_) in infos.identities:
|
||||
found_entities.add(entity_jid)
|
||||
|
||||
def gotItems(items):
|
||||
defers_list = []
|
||||
for item in items:
|
||||
info_d = self.getInfos(client, item.entity)
|
||||
info_d.addCallbacks(infosCb, self._infosEb, [item.entity], None, [item.entity])
|
||||
defers_list.append(info_d)
|
||||
return defer.DeferredList(defers_list)
|
||||
|
||||
d = self.getItems(client, jid_)
|
||||
d.addCallback(gotItems)
|
||||
d.addCallback(lambda dummy: found_entities)
|
||||
reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
|
||||
return d
|
||||
|
||||
def findFeaturesSet(self, client, features, identity=None, jid_=None):
|
||||
"""Return entities (including jid_ and its items) offering features
|
||||
|
||||
@param features: iterable of features which must be present
|
||||
@param identity(None, tuple(unicode, unicode)): if not None, accept only this (category/type) identity
|
||||
@param jid_: the jid of the target server (None for profile's server)
|
||||
@param profile: %(doc_profile)s
|
||||
@return: a set of found entities
|
||||
"""
|
||||
if jid_ is None:
|
||||
jid_ = jid.JID(client.jid.host)
|
||||
features = set(features)
|
||||
found_entities = set()
|
||||
|
||||
def infosCb(infos, entity):
|
||||
if entity is None:
|
||||
log.warning(_(u'received an item without jid'))
|
||||
return
|
||||
if identity is not None and identity not in infos.identities:
|
||||
return
|
||||
if features.issubset(infos.features):
|
||||
found_entities.add(entity)
|
||||
|
||||
def gotItems(items):
|
||||
defer_list = []
|
||||
for entity in [jid_] + [item.entity for item in items]:
|
||||
infos_d = self.getInfos(client, entity)
|
||||
infos_d.addCallbacks(infosCb, self._infosEb, [entity], None, [entity])
|
||||
defer_list.append(infos_d)
|
||||
return defer.DeferredList(defer_list)
|
||||
|
||||
d = self.getItems(client, jid_)
|
||||
d.addCallback(gotItems)
|
||||
d.addCallback(lambda dummy: found_entities)
|
||||
reactor.callLater(TIMEOUT, d.cancel) # FIXME: one bad service make a general timeout
|
||||
return d
|
||||
|
||||
def generateHash(self, services):
|
||||
""" Generate a unique hash for given service
|
||||
|
||||
hash algorithm is the one described in XEP-0115
|
||||
@param services: iterable of disco.DiscoIdentity/disco.DiscoFeature, as returned by discoHandler.info
|
||||
|
||||
"""
|
||||
s = []
|
||||
byte_identities = [ByteIdentity(service) for service in services if isinstance(service, disco.DiscoIdentity)] # FIXME: lang must be managed here
|
||||
byte_identities.sort(key=lambda i: i.lang)
|
||||
byte_identities.sort(key=lambda i: i.idType)
|
||||
byte_identities.sort(key=lambda i: i.category)
|
||||
for identity in byte_identities:
|
||||
s.append(str(identity))
|
||||
s.append('<')
|
||||
byte_features = [service.encode('utf-8') for service in services if isinstance(service, disco.DiscoFeature)]
|
||||
byte_features.sort() # XXX: the default sort has the same behaviour as the requested RFC 4790 i;octet sort
|
||||
for feature in byte_features:
|
||||
s.append(feature)
|
||||
s.append('<')
|
||||
#TODO: manage XEP-0128 data form here
|
||||
cap_hash = b64encode(sha1(''.join(s)).digest())
|
||||
log.debug(_(u'Capability hash generated: [%s]') % cap_hash)
|
||||
return cap_hash
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _discoInfos(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
|
||||
""" Discovery method for the bridge
|
||||
@param entity_jid_s: entity we want to discover
|
||||
@param use_cache(bool): if True, use cached data if available
|
||||
@param node(unicode): optional node to use
|
||||
|
||||
@return: list of tuples
|
||||
"""
|
||||
client = self.host.getClient(profile_key)
|
||||
entity = jid.JID(entity_jid_s)
|
||||
disco_infos = yield self.getInfos(client, entity, node, use_cache)
|
||||
extensions = {}
|
||||
for form_type, form in disco_infos.extensions.items():
|
||||
fields = []
|
||||
for field in form.fieldList:
|
||||
data = {'type': field.fieldType}
|
||||
for attr in ('var', 'label', 'desc'):
|
||||
value = getattr(field, attr)
|
||||
if value is not None:
|
||||
data[attr] = value
|
||||
|
||||
values = [field.value] if field.value is not None else field.values
|
||||
fields.append((data, values))
|
||||
|
||||
extensions[form_type or ""] = fields
|
||||
|
||||
defer.returnValue((disco_infos.features,
|
||||
[(cat, type_, name or '') for (cat, type_), name in disco_infos.identities.items()],
|
||||
extensions))
|
||||
|
||||
def items2tuples(self, disco_items):
|
||||
"""convert disco items to tuple of strings
|
||||
|
||||
@param disco_items(iterable[disco.DiscoItem]): items
|
||||
@return G(tuple[unicode,unicode,unicode]): serialised items
|
||||
"""
|
||||
for item in disco_items:
|
||||
if not item.entity:
|
||||
log.warning(_(u"invalid item (no jid)"))
|
||||
continue
|
||||
yield (item.entity.full(), item.nodeIdentifier or '', item.name or '')
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _discoItems(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
|
||||
""" Discovery method for the bridge
|
||||
|
||||
@param entity_jid_s: entity we want to discover
|
||||
@param node(unicode): optional node to use
|
||||
@param use_cache(bool): if True, use cached data if available
|
||||
@return: list of tuples"""
|
||||
client = self.host.getClient(profile_key)
|
||||
entity = jid.JID(entity_jid_s)
|
||||
disco_items = yield self.getItems(client, entity, node, use_cache)
|
||||
ret = list(self.items2tuples(disco_items))
|
||||
defer.returnValue(ret)
|
||||
1326
sat/memory/memory.py
Normal file
1326
sat/memory/memory.py
Normal file
File diff suppressed because it is too large
Load diff
945
sat/memory/params.py
Normal file
945
sat/memory/params.py
Normal file
|
|
@ -0,0 +1,945 @@
|
|||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
# Copyright (C) 2009-2018 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 sat.core.i18n import _, D_
|
||||
|
||||
from sat.core import exceptions
|
||||
from sat.core.constants import Const as C
|
||||
from sat.memory.crypto import BlockCipher, PasswordHasher
|
||||
from xml.dom import minidom, NotFoundErr
|
||||
from sat.core.log import getLogger
|
||||
log = getLogger(__name__)
|
||||
from twisted.internet import defer
|
||||
from twisted.python.failure import Failure
|
||||
from twisted.words.xish import domish
|
||||
from twisted.words.protocols.jabber import jid
|
||||
from sat.tools.xml_tools import paramsXML2XMLUI, getText
|
||||
|
||||
# TODO: params should be rewritten using Twisted directly instead of minidom
|
||||
# general params should be linked to sat.conf and kept synchronised
|
||||
# this need an overall simplification to make maintenance easier
|
||||
|
||||
|
||||
def createJidElts(jids):
|
||||
"""Generator which return <jid/> elements from jids
|
||||
|
||||
@param jids(iterable[id.jID]): jids to use
|
||||
@return (generator[domish.Element]): <jid/> elements
|
||||
"""
|
||||
for jid_ in jids:
|
||||
jid_elt = domish.Element((None, 'jid'))
|
||||
jid_elt.addContent(jid_.full())
|
||||
yield jid_elt
|
||||
|
||||
|
||||
class Params(object):
|
||||
"""This class manage parameters with xml"""
|
||||
### TODO: add desciption in params
|
||||
|
||||
#TODO: when priority is changed, a new presence stanza must be emitted
|
||||
#TODO: int type (Priority should be int instead of string)
|
||||
default_xml = u"""
|
||||
<params>
|
||||
<general>
|
||||
</general>
|
||||
<individual>
|
||||
<category name="General" label="%(category_general)s">
|
||||
<param name="Password" value="" type="password" />
|
||||
<param name="%(history_param)s" label="%(history_label)s" value="20" constraint="0;100" type="int" security="0" />
|
||||
<param name="%(show_offline_contacts)s" label="%(show_offline_contacts_label)s" value="false" type="bool" security="0" />
|
||||
<param name="%(show_empty_groups)s" label="%(show_empty_groups_label)s" value="true" type="bool" security="0" />
|
||||
</category>
|
||||
<category name="Connection" label="%(category_connection)s">
|
||||
<param name="JabberID" value="name@example.org" type="string" security="10" />
|
||||
<param name="Password" value="" type="password" security="10" />
|
||||
<param name="Priority" value="50" type="int" constraint="-128;127" security="10" />
|
||||
<param name="%(force_server_param)s" value="" type="string" security="50" />
|
||||
<param name="%(force_port_param)s" value="" type="int" constraint="1;65535" security="50" />
|
||||
<param name="autoconnect" label="%(autoconnect_label)s" value="true" type="bool" security="50" />
|
||||
<param name="autodisconnect" label="%(autodisconnect_label)s" value="false" type="bool" security="50" />
|
||||
</category>
|
||||
</individual>
|
||||
</params>
|
||||
""" % {
|
||||
'category_general': D_("General"),
|
||||
'category_connection': D_("Connection"),
|
||||
'history_param': C.HISTORY_LIMIT,
|
||||
'history_label': D_('Chat history limit'),
|
||||
'show_offline_contacts': C.SHOW_OFFLINE_CONTACTS,
|
||||
'show_offline_contacts_label': D_('Show offline contacts'),
|
||||
'show_empty_groups': C.SHOW_EMPTY_GROUPS,
|
||||
'show_empty_groups_label': D_('Show empty groups'),
|
||||
'force_server_param': C.FORCE_SERVER_PARAM,
|
||||
'force_port_param': C.FORCE_PORT_PARAM,
|
||||
'new_account_label': D_("Register new account"),
|
||||
'autoconnect_label': D_('Connect on frontend startup'),
|
||||
'autodisconnect_label': D_('Disconnect on frontend closure'),
|
||||
}
|
||||
|
||||
def load_default_params(self):
|
||||
self.dom = minidom.parseString(Params.default_xml.encode('utf-8'))
|
||||
|
||||
def _mergeParams(self, source_node, dest_node):
|
||||
"""Look for every node in source_node and recursively copy them to dest if they don't exists"""
|
||||
|
||||
def getNodesMap(children):
|
||||
ret = {}
|
||||
for child in children:
|
||||
if child.nodeType == child.ELEMENT_NODE:
|
||||
ret[(child.tagName, child.getAttribute('name'))] = child
|
||||
return ret
|
||||
source_map = getNodesMap(source_node.childNodes)
|
||||
dest_map = getNodesMap(dest_node.childNodes)
|
||||
source_set = set(source_map.keys())
|
||||
dest_set = set(dest_map.keys())
|
||||
to_add = source_set.difference(dest_set)
|
||||
|
||||
for node_key in to_add:
|
||||
dest_node.appendChild(source_map[node_key].cloneNode(True))
|
||||
|
||||
to_recurse = source_set - to_add
|
||||
for node_key in to_recurse:
|
||||
self._mergeParams(source_map[node_key], dest_map[node_key])
|
||||
|
||||
def load_xml(self, xml_file):
|
||||
"""Load parameters template from xml file"""
|
||||
self.dom = minidom.parse(xml_file)
|
||||
default_dom = minidom.parseString(Params.default_xml.encode('utf-8'))
|
||||
self._mergeParams(default_dom.documentElement, self.dom.documentElement)
|
||||
|
||||
def loadGenParams(self):
|
||||
"""Load general parameters data from storage
|
||||
|
||||
@return: deferred triggered once params are loaded
|
||||
"""
|
||||
return self.storage.loadGenParams(self.params_gen)
|
||||
|
||||
def loadIndParams(self, profile, cache=None):
|
||||
"""Load individual parameters
|
||||
|
||||
set self.params cache or a temporary cache
|
||||
@param profile: profile to load (*must exist*)
|
||||
@param cache: if not None, will be used to store the value, as a short time cache
|
||||
@return: deferred triggered once params are loaded
|
||||
"""
|
||||
if cache is None:
|
||||
self.params[profile] = {}
|
||||
return self.storage.loadIndParams(self.params[profile] if cache is None else cache, profile)
|
||||
|
||||
def purgeProfile(self, profile):
|
||||
"""Remove cache data of a profile
|
||||
|
||||
@param profile: %(doc_profile)s
|
||||
"""
|
||||
try:
|
||||
del self.params[profile]
|
||||
except KeyError:
|
||||
log.error(_(u"Trying to purge cache of a profile not in memory: [%s]") % profile)
|
||||
|
||||
def save_xml(self, filename):
|
||||
"""Save parameters template to xml file"""
|
||||
with open(filename, 'wb') as xml_file:
|
||||
xml_file.write(self.dom.toxml('utf-8'))
|
||||
|
||||
def __init__(self, host, storage):
|
||||
log.debug("Parameters init")
|
||||
self.host = host
|
||||
self.storage = storage
|
||||
self.default_profile = None
|
||||
self.params = {}
|
||||
self.params_gen = {}
|
||||
|
||||
def createProfile(self, profile, component):
|
||||
"""Create a new profile
|
||||
|
||||
@param profile(unicode): name of the profile
|
||||
@param component(unicode): entry point if profile is a component
|
||||
@param callback: called when the profile actually exists in database and memory
|
||||
@return: a Deferred instance
|
||||
"""
|
||||
if self.storage.hasProfile(profile):
|
||||
log.info(_('The profile name already exists'))
|
||||
return defer.fail(Failure(exceptions.ConflictError))
|
||||
if not self.host.trigger.point("ProfileCreation", profile):
|
||||
return defer.fail(Failure(exceptions.CancelError))
|
||||
return self.storage.createProfile(profile, component or None)
|
||||
|
||||
def asyncDeleteProfile(self, profile, force=False):
|
||||
"""Delete an existing profile
|
||||
|
||||
@param profile: name of the profile
|
||||
@param force: force the deletion even if the profile is connected.
|
||||
To be used for direct calls only (not through the bridge).
|
||||
@return: a Deferred instance
|
||||
"""
|
||||
if not self.storage.hasProfile(profile):
|
||||
log.info(_('Trying to delete an unknown profile'))
|
||||
return defer.fail(Failure(exceptions.ProfileUnknownError(profile)))
|
||||
if self.host.isConnected(profile):
|
||||
if force:
|
||||
self.host.disconnect(profile)
|
||||
else:
|
||||
log.info(_("Trying to delete a connected profile"))
|
||||
return defer.fail(Failure(exceptions.ProfileConnected))
|
||||
return self.storage.deleteProfile(profile)
|
||||
|
||||
def getProfileName(self, profile_key, return_profile_keys=False):
|
||||
"""return profile according to profile_key
|
||||
|
||||
@param profile_key: profile name or key which can be
|
||||
C.PROF_KEY_ALL for all profiles
|
||||
C.PROF_KEY_DEFAULT for default profile
|
||||
@param return_profile_keys: if True, return unmanaged profile keys (like C.PROF_KEY_ALL). This keys must be managed by the caller
|
||||
@return: requested profile name
|
||||
@raise exceptions.ProfileUnknownError: profile doesn't exists
|
||||
@raise exceptions.ProfileNotSetError: if C.PROF_KEY_NONE is used
|
||||
"""
|
||||
if profile_key == '@DEFAULT@':
|
||||
default = self.host.memory.memory_data.get('Profile_default')
|
||||
if not default:
|
||||
log.info(_('No default profile, returning first one'))
|
||||
try:
|
||||
default = self.host.memory.memory_data['Profile_default'] = self.storage.getProfilesList()[0]
|
||||
except IndexError:
|
||||
log.info(_('No profile exist yet'))
|
||||
raise exceptions.ProfileUnknownError(profile_key)
|
||||
return default # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists
|
||||
elif profile_key == C.PROF_KEY_NONE:
|
||||
raise exceptions.ProfileNotSetError
|
||||
elif return_profile_keys and profile_key in [C.PROF_KEY_ALL]:
|
||||
return profile_key # this value must be managed by the caller
|
||||
if not self.storage.hasProfile(profile_key):
|
||||
log.error(_(u'Trying to access an unknown profile (%s)') % profile_key)
|
||||
raise exceptions.ProfileUnknownError(profile_key)
|
||||
return profile_key
|
||||
|
||||
def __get_unique_node(self, parent, tag, name):
|
||||
"""return node with given tag
|
||||
|
||||
@param parent: parent of nodes to check (e.g. documentElement)
|
||||
@param tag: tag to check (e.g. "category")
|
||||
@param name: name to check (e.g. "JID")
|
||||
@return: node if it exist or None
|
||||
"""
|
||||
for node in parent.childNodes:
|
||||
if node.nodeName == tag and node.getAttribute("name") == name:
|
||||
#the node already exists
|
||||
return node
|
||||
#the node is new
|
||||
return None
|
||||
|
||||
def updateParams(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=''):
|
||||
"""import xml in parameters, update if the param already exists
|
||||
|
||||
If security_limit is specified and greater than -1, the parameters
|
||||
that have a security level greater than security_limit are skipped.
|
||||
@param xml: parameters in xml form
|
||||
@param security_limit: -1 means no security, 0 is the maximum security then the higher the less secure
|
||||
@param app: name of the frontend registering the parameters or empty value
|
||||
"""
|
||||
# TODO: should word with domish.Element
|
||||
src_parent = minidom.parseString(xml.encode('utf-8')).documentElement
|
||||
|
||||
def pre_process_app_node(src_parent, security_limit, app):
|
||||
"""Parameters that are registered from a frontend must be checked"""
|
||||
to_remove = []
|
||||
for type_node in src_parent.childNodes:
|
||||
if type_node.nodeName != C.INDIVIDUAL:
|
||||
to_remove.append(type_node) # accept individual parameters only
|
||||
continue
|
||||
for cat_node in type_node.childNodes:
|
||||
if cat_node.nodeName != 'category':
|
||||
to_remove.append(cat_node)
|
||||
continue
|
||||
to_remove_count = 0 # count the params to be removed from current category
|
||||
for node in cat_node.childNodes:
|
||||
if node.nodeName != "param" or not self.checkSecurityLimit(node, security_limit):
|
||||
to_remove.append(node)
|
||||
to_remove_count += 1
|
||||
continue
|
||||
node.setAttribute('app', app)
|
||||
if len(cat_node.childNodes) == to_remove_count: # remove empty category
|
||||
for dummy in xrange(0, to_remove_count):
|
||||
to_remove.pop()
|
||||
to_remove.append(cat_node)
|
||||
for node in to_remove:
|
||||
node.parentNode.removeChild(node)
|
||||
|
||||
def import_node(tgt_parent, src_parent):
|
||||
for child in src_parent.childNodes:
|
||||
if child.nodeName == '#text':
|
||||
continue
|
||||
node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name"))
|
||||
if not node: # The node is new
|
||||
tgt_parent.appendChild(child.cloneNode(True))
|
||||
else:
|
||||
if child.nodeName == "param":
|
||||
# The child updates an existing parameter, we replace the node
|
||||
tgt_parent.replaceChild(child, node)
|
||||
else:
|
||||
# the node already exists, we recurse 1 more level
|
||||
import_node(node, child)
|
||||
|
||||
if app:
|
||||
pre_process_app_node(src_parent, security_limit, app)
|
||||
import_node(self.dom.documentElement, src_parent)
|
||||
|
||||
def paramsRegisterApp(self, xml, security_limit, app):
|
||||
"""Register frontend's specific parameters
|
||||
|
||||
If security_limit is specified and greater than -1, the parameters
|
||||
that have a security level greater than security_limit are skipped.
|
||||
@param xml: XML definition of the parameters to be added
|
||||
@param security_limit: -1 means no security, 0 is the maximum security then the higher the less secure
|
||||
@param app: name of the frontend registering the parameters
|
||||
"""
|
||||
if not app:
|
||||
log.warning(_(u"Trying to register frontends parameters with no specified app: aborted"))
|
||||
return
|
||||
if not hasattr(self, "frontends_cache"):
|
||||
self.frontends_cache = []
|
||||
if app in self.frontends_cache:
|
||||
log.debug(_(u"Trying to register twice frontends parameters for %(app)s: aborted" % {"app": app}))
|
||||
return
|
||||
self.frontends_cache.append(app)
|
||||
self.updateParams(xml, security_limit, app)
|
||||
log.debug(u"Frontends parameters registered for %(app)s" % {'app': app})
|
||||
|
||||
def __default_ok(self, value, name, category):
|
||||
#FIXME: will not work with individual parameters
|
||||
self.setParam(name, value, category)
|
||||
|
||||
def __default_ko(self, failure, name, category):
|
||||
log.error(_(u"Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category': category, 'name': name, 'reason': str(failure.value)})
|
||||
|
||||
def setDefault(self, name, category, callback, errback=None):
|
||||
"""Set default value of parameter
|
||||
|
||||
'default_cb' attibute of parameter must be set to 'yes'
|
||||
@param name: name of the parameter
|
||||
@param category: category of the parameter
|
||||
@param callback: must return a string with the value (use deferred if needed)
|
||||
@param errback: must manage the error with args failure, name, category
|
||||
"""
|
||||
#TODO: send signal param update if value changed
|
||||
#TODO: manage individual paramaters
|
||||
log.debug ("setDefault called for %(category)s/%(name)s" % {"category": category, "name": name})
|
||||
node = self._getParamNode(name, category, '@ALL@')
|
||||
if not node:
|
||||
log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
|
||||
return
|
||||
if node[1].getAttribute('default_cb') == 'yes':
|
||||
# del node[1].attributes['default_cb'] # default_cb is not used anymore as a flag to know if we have to set the default value,
|
||||
# and we can still use it later e.g. to call a generic setDefault method
|
||||
value = self._getParam(category, name, C.GENERAL)
|
||||
if value is None: # no value set by the user: we have the default value
|
||||
log.debug ("Default value to set, using callback")
|
||||
d = defer.maybeDeferred(callback)
|
||||
d.addCallback(self.__default_ok, name, category)
|
||||
d.addErrback(errback or self.__default_ko, name, category)
|
||||
|
||||
def _getAttr_internal(self, node, attr, value):
|
||||
"""Get attribute value.
|
||||
|
||||
/!\ This method would return encrypted password values.
|
||||
|
||||
@param node: XML param node
|
||||
@param attr: name of the attribute to get (e.g.: 'value' or 'type')
|
||||
@param value: user defined value
|
||||
@return: value (can be str, bool, int, list, None)
|
||||
"""
|
||||
if attr == 'value':
|
||||
value_to_use = value if value is not None else node.getAttribute(attr) # we use value (user defined) if it exist, else we use node's default value
|
||||
if node.getAttribute('type') == 'bool':
|
||||
return C.bool(value_to_use)
|
||||
if node.getAttribute('type') == 'int':
|
||||
return int(value_to_use)
|
||||
elif node.getAttribute('type') == 'list':
|
||||
if not value_to_use: # no user defined value, take default value from the XML
|
||||
options = [option for option in node.childNodes if option.nodeName == 'option']
|
||||
selected = [option for option in options if option.getAttribute('selected') == 'true']
|
||||
cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name')
|
||||
if len(selected) == 1:
|
||||
value_to_use = selected[0].getAttribute('value')
|
||||
log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") %
|
||||
{'cat': cat, 'param': param, 'value': value_to_use})
|
||||
return value_to_use
|
||||
if len(selected) == 0:
|
||||
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param})
|
||||
else:
|
||||
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param})
|
||||
raise exceptions.DataError
|
||||
elif node.getAttribute('type') == 'jids_list':
|
||||
if value_to_use:
|
||||
jids = value_to_use.split('\t') # FIXME: it's not good to use tabs as separator !
|
||||
else: # no user defined value, take default value from the XML
|
||||
jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")]
|
||||
to_delete = []
|
||||
for idx, value in enumerate(jids):
|
||||
try:
|
||||
jids[idx] = jid.JID(value)
|
||||
except (RuntimeError, jid.InvalidFormat, AttributeError):
|
||||
log.warning(u"Incorrect jid value found in jids list: [{}]".format(value))
|
||||
to_delete.append(value)
|
||||
for value in to_delete:
|
||||
jids.remove(value)
|
||||
return jids
|
||||
return value_to_use
|
||||
return node.getAttribute(attr)
|
||||
|
||||
def _getAttr(self, node, attr, value):
|
||||
"""Get attribute value (synchronous).
|
||||
|
||||
/!\ This method can not be used to retrieve password values.
|
||||
@param node: XML param node
|
||||
@param attr: name of the attribute to get (e.g.: 'value' or 'type')
|
||||
@param value: user defined value
|
||||
@return (unicode, bool, int, list): value to retrieve
|
||||
"""
|
||||
if attr == 'value' and node.getAttribute('type') == 'password':
|
||||
raise exceptions.InternalError('To retrieve password values, use _asyncGetAttr instead of _getAttr')
|
||||
return self._getAttr_internal(node, attr, value)
|
||||
|
||||
def _asyncGetAttr(self, node, attr, value, profile=None):
|
||||
"""Get attribute value.
|
||||
|
||||
Profile passwords are returned hashed (if not empty),
|
||||
other passwords are returned decrypted (if not empty).
|
||||
@param node: XML param node
|
||||
@param attr: name of the attribute to get (e.g.: 'value' or 'type')
|
||||
@param value: user defined value
|
||||
@param profile: %(doc_profile)s
|
||||
@return (unicode, bool, int, list): Deferred value to retrieve
|
||||
"""
|
||||
value = self._getAttr_internal(node, attr, value)
|
||||
if attr != 'value' or node.getAttribute('type') != 'password':
|
||||
return defer.succeed(value)
|
||||
param_cat = node.parentNode.getAttribute('name')
|
||||
param_name = node.getAttribute('name')
|
||||
if ((param_cat, param_name) == C.PROFILE_PASS_PATH) or not value:
|
||||
return defer.succeed(value) # profile password and empty passwords are returned "as is"
|
||||
if not profile:
|
||||
raise exceptions.ProfileNotSetError('The profile is needed to decrypt a password')
|
||||
d = self.host.memory.decryptValue(value, profile)
|
||||
|
||||
def gotPlainPassword(password):
|
||||
if password is None: # empty value means empty password, None means decryption failure
|
||||
raise exceptions.InternalError(_('The stored password could not be decrypted!'))
|
||||
return password
|
||||
|
||||
return d.addCallback(gotPlainPassword)
|
||||
|
||||
def __type_to_string(self, result):
|
||||
""" convert result to string, according to its type """
|
||||
if isinstance(result, bool):
|
||||
return "true" if result else "false"
|
||||
elif isinstance(result, int):
|
||||
return str(result)
|
||||
return result
|
||||
|
||||
def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE):
|
||||
""" Same as getParamA but for bridge: convert non string value to string """
|
||||
return self.__type_to_string(self.getParamA(name, category, attr, profile_key=profile_key))
|
||||
|
||||
def getParamA(self, name, category, attr="value", use_default=True, profile_key=C.PROF_KEY_NONE):
|
||||
"""Helper method to get a specific attribute.
|
||||
|
||||
/!\ This method would return encrypted password values,
|
||||
to get the plain values you have to use _asyncGetParamA.
|
||||
@param name: name of the parameter
|
||||
@param category: category of the parameter
|
||||
@param attr: name of the attribute (default: "value")
|
||||
@parm use_default(bool): if True and attr=='value', return default value if not set
|
||||
else return None if not set
|
||||
@param profile: owner of the param (@ALL@ for everyone)
|
||||
@return: attribute
|
||||
"""
|
||||
# FIXME: looks really dirty and buggy, need to be reviewed/refactored
|
||||
# FIXME: security_limit is not managed here !
|
||||
node = self._getParamNode(name, category)
|
||||
if not node:
|
||||
log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
|
||||
raise exceptions.NotFound
|
||||
|
||||
if attr == 'value' and node[1].getAttribute('type') == 'password':
|
||||
raise exceptions.InternalError('To retrieve password values, use asyncGetParamA instead of getParamA')
|
||||
|
||||
if node[0] == C.GENERAL:
|
||||
value = self._getParam(category, name, C.GENERAL)
|
||||
if value is None and attr=='value' and not use_default:
|
||||
return value
|
||||
return self._getAttr(node[1], attr, value)
|
||||
|
||||
assert node[0] == C.INDIVIDUAL
|
||||
|
||||
profile = self.getProfileName(profile_key)
|
||||
if not profile:
|
||||
log.error(_('Requesting a param for an non-existant profile'))
|
||||
raise exceptions.ProfileUnknownError(profile_key)
|
||||
|
||||
if profile not in self.params:
|
||||
log.error(_('Requesting synchronous param for not connected profile'))
|
||||
raise exceptions.ProfileNotConnected(profile)
|
||||
|
||||
if attr == "value":
|
||||
value = self._getParam(category, name, profile=profile)
|
||||
if value is None and attr=='value' and not use_default:
|
||||
return value
|
||||
return self._getAttr(node[1], attr, value)
|
||||
|
||||
def asyncGetStringParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
|
||||
d = self.asyncGetParamA(name, category, attr, security_limit, profile_key)
|
||||
d.addCallback(self.__type_to_string)
|
||||
return d
|
||||
|
||||
def asyncGetParamA(self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
|
||||
"""Helper method to get a specific attribute.
|
||||
|
||||
@param name: name of the parameter
|
||||
@param category: category of the parameter
|
||||
@param attr: name of the attribute (default: "value")
|
||||
@param profile: owner of the param (@ALL@ for everyone)
|
||||
@return (defer.Deferred): parameter value, with corresponding type (bool, int, list, etc)
|
||||
"""
|
||||
node = self._getParamNode(name, category)
|
||||
if not node:
|
||||
log.error(_(u"Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
|
||||
raise ValueError("Requested param doesn't exist")
|
||||
|
||||
if not self.checkSecurityLimit(node[1], security_limit):
|
||||
log.warning(_(u"Trying to get parameter '%(param)s' in category '%(cat)s' without authorization!!!"
|
||||
% {'param': name, 'cat': category}))
|
||||
raise exceptions.PermissionError
|
||||
|
||||
if node[0] == C.GENERAL:
|
||||
value = self._getParam(category, name, C.GENERAL)
|
||||
return self._asyncGetAttr(node[1], attr, value)
|
||||
|
||||
assert node[0] == C.INDIVIDUAL
|
||||
|
||||
profile = self.getProfileName(profile_key)
|
||||
if not profile:
|
||||
raise exceptions.InternalError(_('Requesting a param for a non-existant profile'))
|
||||
|
||||
if attr != "value":
|
||||
return defer.succeed(node[1].getAttribute(attr))
|
||||
try:
|
||||
value = self._getParam(category, name, profile=profile)
|
||||
return self._asyncGetAttr(node[1], attr, value, profile)
|
||||
except exceptions.ProfileNotInCacheError:
|
||||
#We have to ask data to the storage manager
|
||||
d = self.storage.getIndParam(category, name, profile)
|
||||
return d.addCallback(lambda value: self._asyncGetAttr(node[1], attr, value, profile))
|
||||
|
||||
def asyncGetParamsValuesFromCategory(self, category, security_limit, profile_key):
|
||||
"""Get all parameters "attribute" for a category
|
||||
|
||||
@param category(unicode): the desired category
|
||||
@param security_limit(int): NO_SECURITY_LIMIT (-1) to return all the params.
|
||||
Otherwise sole the params which have a security level defined *and*
|
||||
lower or equal to the specified value are returned.
|
||||
@param profile_key: %(doc_profile_key)s
|
||||
@return (dict): key: param name, value: param value (converted to string if needed)
|
||||
"""
|
||||
#TODO: manage category of general type (without existant profile)
|
||||
profile = self.getProfileName(profile_key)
|
||||
if not profile:
|
||||
log.error(_("Asking params for inexistant profile"))
|
||||
return ""
|
||||
|
||||
def setValue(value, ret, name):
|
||||
ret[name] = value
|
||||
|
||||
def returnCategoryXml(prof_xml):
|
||||
ret = {}
|
||||
names_d_list = []
|
||||
for category_node in prof_xml.getElementsByTagName("category"):
|
||||
if category_node.getAttribute("name") == category:
|
||||
for param_node in category_node.getElementsByTagName("param"):
|
||||
name = param_node.getAttribute('name')
|
||||
if not name:
|
||||
log.warning(u"ignoring attribute without name: {}".format(param_node.toxml()))
|
||||
continue
|
||||
d = self.asyncGetStringParamA(name, category, security_limit=security_limit, profile_key=profile)
|
||||
d.addCallback(setValue, ret, name)
|
||||
names_d_list.append(d)
|
||||
break
|
||||
|
||||
prof_xml.unlink()
|
||||
dlist = defer.gatherResults(names_d_list)
|
||||
dlist.addCallback(lambda dummy: ret)
|
||||
return ret
|
||||
|
||||
d = self._constructProfileXml(security_limit, '', profile)
|
||||
return d.addCallback(returnCategoryXml)
|
||||
|
||||
def _getParam(self, category, name, type_=C.INDIVIDUAL, cache=None, profile=C.PROF_KEY_NONE):
|
||||
"""Return the param, or None if it doesn't exist
|
||||
|
||||
@param category: param category
|
||||
@param name: param name
|
||||
@param type_: GENERAL or INDIVIDUAL
|
||||
@param cache: temporary cache, to use when profile is not logged
|
||||
@param profile: the profile name (not profile key, i.e. name and not something like @DEFAULT@)
|
||||
@return: param value or None if it doesn't exist
|
||||
"""
|
||||
if type_ == C.GENERAL:
|
||||
if (category, name) in self.params_gen:
|
||||
return self.params_gen[(category, name)]
|
||||
return None # This general param has the default value
|
||||
assert type_ == C.INDIVIDUAL
|
||||
if profile == C.PROF_KEY_NONE:
|
||||
raise exceptions.ProfileNotSetError
|
||||
if profile in self.params:
|
||||
cache = self.params[profile] # if profile is in main cache, we use it,
|
||||
# ignoring the temporary cache
|
||||
elif cache is None: # else we use the temporary cache if it exists, or raise an exception
|
||||
raise exceptions.ProfileNotInCacheError
|
||||
if (category, name) not in cache:
|
||||
return None
|
||||
return cache[(category, name)]
|
||||
|
||||
def _constructProfileXml(self, security_limit, app, profile):
|
||||
"""Construct xml for asked profile, filling values when needed
|
||||
|
||||
/!\ as noticed in doc, don't forget to unlink the minidom.Document
|
||||
@param security_limit: NO_SECURITY_LIMIT (-1) to return all the params.
|
||||
Otherwise sole the params which have a security level defined *and*
|
||||
lower or equal to the specified value are returned.
|
||||
@param app: name of the frontend requesting the parameters, or '' to get all parameters
|
||||
@param profile: profile name (not key !)
|
||||
@return: a deferred that fire a minidom.Document of the profile xml (cf warning above)
|
||||
"""
|
||||
|
||||
def checkNode(node):
|
||||
"""Check the node against security_limit and app"""
|
||||
return self.checkSecurityLimit(node, security_limit) and self.checkApp(node, app)
|
||||
|
||||
def constructProfile(ignore, profile_cache):
|
||||
# init the result document
|
||||
prof_xml = minidom.parseString('<params/>')
|
||||
cache = {}
|
||||
|
||||
for type_node in self.dom.documentElement.childNodes:
|
||||
if type_node.nodeName != C.GENERAL and type_node.nodeName != C.INDIVIDUAL:
|
||||
continue
|
||||
# we use all params, general and individual
|
||||
for cat_node in type_node.childNodes:
|
||||
if cat_node.nodeName != 'category':
|
||||
continue
|
||||
category = cat_node.getAttribute('name')
|
||||
dest_params = {} # result (merged) params for category
|
||||
if category not in cache:
|
||||
# we make a copy for the new xml
|
||||
cache[category] = dest_cat = cat_node.cloneNode(True)
|
||||
to_remove = []
|
||||
for node in dest_cat.childNodes:
|
||||
if node.nodeName != "param":
|
||||
continue
|
||||
if not checkNode(node):
|
||||
to_remove.append(node)
|
||||
continue
|
||||
dest_params[node.getAttribute('name')] = node
|
||||
for node in to_remove:
|
||||
dest_cat.removeChild(node)
|
||||
new_node = True
|
||||
else:
|
||||
# It's not a new node, we use the previously cloned one
|
||||
dest_cat = cache[category]
|
||||
new_node = False
|
||||
params = cat_node.getElementsByTagName("param")
|
||||
|
||||
for param_node in params:
|
||||
# we have to merge new params (we are parsing individual parameters, we have to add them
|
||||
# to the previously parsed general ones)
|
||||
name = param_node.getAttribute('name')
|
||||
if not checkNode(param_node):
|
||||
continue
|
||||
if name not in dest_params:
|
||||
# this is reached when a previous category exists
|
||||
dest_params[name] = param_node.cloneNode(True)
|
||||
dest_cat.appendChild(dest_params[name])
|
||||
|
||||
profile_value = self._getParam(category,
|
||||
name, type_node.nodeName,
|
||||
cache=profile_cache, profile=profile)
|
||||
if profile_value is not None:
|
||||
# there is a value for this profile, we must change the default
|
||||
if dest_params[name].getAttribute('type') == 'list':
|
||||
for option in dest_params[name].getElementsByTagName("option"):
|
||||
if option.getAttribute('value') == profile_value:
|
||||
option.setAttribute('selected', 'true')
|
||||
else:
|
||||
try:
|
||||
option.removeAttribute('selected')
|
||||
except NotFoundErr:
|
||||
pass
|
||||
elif dest_params[name].getAttribute('type') == 'jids_list':
|
||||
jids = profile_value.split('\t')
|
||||
for jid_elt in dest_params[name].getElementsByTagName("jid"):
|
||||
dest_params[name].removeChild(jid_elt) # remove all default
|
||||
for jid_ in jids: # rebuilt the children with use values
|
||||
try:
|
||||
jid.JID(jid_)
|
||||
except (RuntimeError, jid.InvalidFormat, AttributeError):
|
||||
log.warning(u"Incorrect jid value found in jids list: [{}]".format(jid_))
|
||||
else:
|
||||
jid_elt = prof_xml.createElement('jid')
|
||||
jid_elt.appendChild(prof_xml.createTextNode(jid_))
|
||||
dest_params[name].appendChild(jid_elt)
|
||||
else:
|
||||
dest_params[name].setAttribute('value', profile_value)
|
||||
if new_node:
|
||||
prof_xml.documentElement.appendChild(dest_cat)
|
||||
|
||||
to_remove = []
|
||||
for cat_node in prof_xml.documentElement.childNodes:
|
||||
# we remove empty categories
|
||||
if cat_node.getElementsByTagName("param").length == 0:
|
||||
to_remove.append(cat_node)
|
||||
for node in to_remove:
|
||||
prof_xml.documentElement.removeChild(node)
|
||||
return prof_xml
|
||||
|
||||
if profile in self.params:
|
||||
d = defer.succeed(None)
|
||||
profile_cache = self.params[profile]
|
||||
else:
|
||||
#profile is not in cache, we load values in a short time cache
|
||||
profile_cache = {}
|
||||
d = self.loadIndParams(profile, profile_cache)
|
||||
|
||||
return d.addCallback(constructProfile, profile_cache)
|
||||
|
||||
def getParamsUI(self, security_limit, app, profile_key):
|
||||
"""
|
||||
@param security_limit: NO_SECURITY_LIMIT (-1) to return all the params.
|
||||
Otherwise sole the params which have a security level defined *and*
|
||||
lower or equal to the specified value are returned.
|
||||
@param app: name of the frontend requesting the parameters, or '' to get all parameters
|
||||
@param profile_key: Profile key which can be either a magic (eg: @DEFAULT@) or the name of an existing profile.
|
||||
@return: a SàT XMLUI for parameters
|
||||
"""
|
||||
profile = self.getProfileName(profile_key)
|
||||
if not profile:
|
||||
log.error(_("Asking params for inexistant profile"))
|
||||
return ""
|
||||
d = self.getParams(security_limit, app, profile)
|
||||
return d.addCallback(lambda param_xml: paramsXML2XMLUI(param_xml))
|
||||
|
||||
def getParams(self, security_limit, app, profile_key):
|
||||
"""Construct xml for asked profile, take params xml as skeleton
|
||||
|
||||
@param security_limit: NO_SECURITY_LIMIT (-1) to return all the params.
|
||||
Otherwise sole the params which have a security level defined *and*
|
||||
lower or equal to the specified value are returned.
|
||||
@param app: name of the frontend requesting the parameters, or '' to get all parameters
|
||||
@param profile_key: Profile key which can be either a magic (eg: @DEFAULT@) or the name of an existing profile.
|
||||
@return: XML of parameters
|
||||
"""
|
||||
profile = self.getProfileName(profile_key)
|
||||
if not profile:
|
||||
log.error(_("Asking params for inexistant profile"))
|
||||
return defer.succeed("")
|
||||
|
||||
def returnXML(prof_xml):
|
||||
return_xml = prof_xml.toxml()
|
||||
prof_xml.unlink()
|
||||
return '\n'.join((line for line in return_xml.split('\n') if line))
|
||||
|
||||
return self._constructProfileXml(security_limit, app, profile).addCallback(returnXML)
|
||||
|
||||
def _getParamNode(self, name, category, type_="@ALL@"): # FIXME: is type_ useful ?
|
||||
"""Return a node from the param_xml
|
||||
@param name: name of the node
|
||||
@param category: category of the node
|
||||
@param type_: keyword for search:
|
||||
@ALL@ search everywhere
|
||||
@GENERAL@ only search in general type
|
||||
@INDIVIDUAL@ only search in individual type
|
||||
@return: a tuple (node type, node) or None if not found"""
|
||||
|
||||
for type_node in self.dom.documentElement.childNodes:
|
||||
if (((type_ == "@ALL@" or type_ == "@GENERAL@") and type_node.nodeName == C.GENERAL)
|
||||
or ((type_ == "@ALL@" or type_ == "@INDIVIDUAL@") and type_node.nodeName == C.INDIVIDUAL)):
|
||||
for node in type_node.getElementsByTagName('category'):
|
||||
if node.getAttribute("name") == category:
|
||||
params = node.getElementsByTagName("param")
|
||||
for param in params:
|
||||
if param.getAttribute("name") == name:
|
||||
return (type_node.nodeName, param)
|
||||
return None
|
||||
|
||||
def getParamsCategories(self):
|
||||
"""return the categories availables"""
|
||||
categories = []
|
||||
for cat in self.dom.getElementsByTagName("category"):
|
||||
name = cat.getAttribute("name")
|
||||
if name not in categories:
|
||||
categories.append(cat.getAttribute("name"))
|
||||
return categories
|
||||
|
||||
def setParam(self, name, value, category, security_limit=C.NO_SECURITY_LIMIT, profile_key=C.PROF_KEY_NONE):
|
||||
"""Set a parameter, return None if the parameter is not in param xml.
|
||||
|
||||
Parameter of type 'password' that are not the SàT profile password are
|
||||
stored encrypted (if not empty). The profile password is stored hashed
|
||||
(if not empty).
|
||||
|
||||
@param name (str): the parameter name
|
||||
@param value (str): the new value
|
||||
@param category (str): the parameter category
|
||||
@param security_limit (int)
|
||||
@param profile_key (str): %(doc_profile_key)s
|
||||
@return: a deferred None value when everything is done
|
||||
"""
|
||||
# FIXME: setParam should accept the right type for value, not only str !
|
||||
if profile_key != C.PROF_KEY_NONE:
|
||||
profile = self.getProfileName(profile_key)
|
||||
if not profile:
|
||||
log.error(_(u'Trying to set parameter for an unknown profile'))
|
||||
raise exceptions.ProfileUnknownError(profile_key)
|
||||
|
||||
node = self._getParamNode(name, category, '@ALL@')
|
||||
if not node:
|
||||
log.error(_(u'Requesting an unknown parameter (%(category)s/%(name)s)')
|
||||
% {'category': category, 'name': name})
|
||||
return defer.succeed(None)
|
||||
|
||||
if not self.checkSecurityLimit(node[1], security_limit):
|
||||
log.warning(_(u"Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!"
|
||||
% {'param': name, 'cat': category}))
|
||||
return defer.succeed(None)
|
||||
|
||||
type_ = node[1].getAttribute("type")
|
||||
if type_ == 'int':
|
||||
if not value: # replace with the default value (which might also be '')
|
||||
value = node[1].getAttribute("value")
|
||||
else:
|
||||
try:
|
||||
int(value)
|
||||
except ValueError:
|
||||
log.debug(_(u"Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
|
||||
% {'param': name, 'cat': category}))
|
||||
return defer.succeed(None)
|
||||
if node[1].hasAttribute("constraint"):
|
||||
constraint = node[1].getAttribute("constraint")
|
||||
try:
|
||||
min_, max_ = [int(limit) for limit in constraint.split(";")]
|
||||
except ValueError:
|
||||
raise exceptions.InternalError("Invalid integer parameter constraint: %s" % constraint)
|
||||
value = str(min(max(int(value), min_), max_))
|
||||
|
||||
|
||||
log.info(_("Setting parameter (%(category)s, %(name)s) = %(value)s") %
|
||||
{'category': category, 'name': name, 'value': value if type_ != 'password' else '********'})
|
||||
|
||||
if node[0] == C.GENERAL:
|
||||
self.params_gen[(category, name)] = value
|
||||
self.storage.setGenParam(category, name, value)
|
||||
for profile in self.storage.getProfilesList():
|
||||
if self.host.memory.isSessionStarted(profile):
|
||||
self.host.bridge.paramUpdate(name, value, category, profile)
|
||||
self.host.trigger.point("paramUpdateTrigger", name, value, category, node[0], profile)
|
||||
return defer.succeed(None)
|
||||
|
||||
assert node[0] == C.INDIVIDUAL
|
||||
assert profile_key != C.PROF_KEY_NONE
|
||||
|
||||
if type_ == "button":
|
||||
log.debug(u"Clicked param button %s" % node.toxml())
|
||||
return defer.succeed(None)
|
||||
elif type_ == "password":
|
||||
try:
|
||||
personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
|
||||
except TypeError:
|
||||
raise exceptions.InternalError(_('Trying to encrypt a password while the personal key is undefined!'))
|
||||
if (category, name) == C.PROFILE_PASS_PATH:
|
||||
# using 'value' as the encryption key to encrypt another encryption key... could be confusing!
|
||||
d = self.host.memory.encryptPersonalData(data_key=C.MEMORY_CRYPTO_KEY,
|
||||
data_value=personal_key,
|
||||
crypto_key=value,
|
||||
profile=profile)
|
||||
d.addCallback(lambda dummy: PasswordHasher.hash(value)) # profile password is hashed (empty value stays empty)
|
||||
elif value: # other non empty passwords are encrypted with the personal key
|
||||
d = BlockCipher.encrypt(personal_key, value)
|
||||
else:
|
||||
d = defer.succeed(value)
|
||||
else:
|
||||
d = defer.succeed(value)
|
||||
|
||||
def gotFinalValue(value):
|
||||
if self.host.memory.isSessionStarted(profile):
|
||||
self.params[profile][(category, name)] = value
|
||||
self.host.bridge.paramUpdate(name, value, category, profile)
|
||||
self.host.trigger.point("paramUpdateTrigger", name, value, category, node[0], profile)
|
||||
return self.storage.setIndParam(category, name, value, profile)
|
||||
else:
|
||||
raise exceptions.ProfileNotConnected
|
||||
|
||||
d.addCallback(gotFinalValue)
|
||||
return d
|
||||
|
||||
def _getNodesOfTypes(self, attr_type, node_type="@ALL@"):
|
||||
"""Return all the nodes matching the given types.
|
||||
|
||||
TODO: using during the dev but not anymore... remove if not needed
|
||||
|
||||
@param attr_type (str): the attribute type (string, text, password, bool, int, button, list)
|
||||
@param node_type (str): keyword for filtering:
|
||||
@ALL@ search everywhere
|
||||
@GENERAL@ only search in general type
|
||||
@INDIVIDUAL@ only search in individual type
|
||||
@return: dict{tuple: node}: a dict {key, value} where:
|
||||
- key is a couple (attribute category, attribute name)
|
||||
- value is a node
|
||||
"""
|
||||
ret = {}
|
||||
for type_node in self.dom.documentElement.childNodes:
|
||||
if (((node_type == "@ALL@" or node_type == "@GENERAL@") and type_node.nodeName == C.GENERAL) or
|
||||
((node_type == "@ALL@" or node_type == "@INDIVIDUAL@") and type_node.nodeName == C.INDIVIDUAL)):
|
||||
for cat_node in type_node.getElementsByTagName('category'):
|
||||
cat = cat_node.getAttribute('name')
|
||||
params = cat_node.getElementsByTagName("param")
|
||||
for param in params:
|
||||
if param.getAttribute("type") == attr_type:
|
||||
ret[(cat, param.getAttribute("name"))] = param
|
||||
return ret
|
||||
|
||||
def checkSecurityLimit(self, node, security_limit):
|
||||
"""Check the given node against the given security limit.
|
||||
The value NO_SECURITY_LIMIT (-1) means that everything is allowed.
|
||||
@return: True if this node can be accessed with the given security limit.
|
||||
"""
|
||||
if security_limit < 0:
|
||||
return True
|
||||
if node.hasAttribute("security"):
|
||||
if int(node.getAttribute("security")) <= security_limit:
|
||||
return True
|
||||
return False
|
||||
|
||||
def checkApp(self, node, app):
|
||||
"""Check the given node against the given app.
|
||||
@param node: parameter node
|
||||
@param app: name of the frontend requesting the parameters, or '' to get all parameters
|
||||
@return: True if this node concerns the given app.
|
||||
"""
|
||||
if not app or not node.hasAttribute("app"):
|
||||
return True
|
||||
return node.getAttribute("app") == app
|
||||
233
sat/memory/persistent.py
Normal file
233
sat/memory/persistent.py
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
#!/usr/bin/env python2
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
# Copyright (C) 2009-2018 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 sat.core.i18n import _
|
||||
from sat.core.log import getLogger
|
||||
log = getLogger(__name__)
|
||||
|
||||
|
||||
class MemoryNotInitializedError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PersistentDict(object):
|
||||
r"""A dictionary which save persistently each value assigned
|
||||
|
||||
/!\ be careful, each assignment means a database write
|
||||
/!\ Memory must be initialised before loading/setting value with instances of this class"""
|
||||
storage = None
|
||||
binary = False
|
||||
|
||||
def __init__(self, namespace, profile=None):
|
||||
"""
|
||||
|
||||
@param namespace: unique namespace for this dictionary
|
||||
@param profile(unicode, None): profile which *MUST* exists, or None for general values
|
||||
"""
|
||||
if not self.storage:
|
||||
log.error(_("PersistentDict can't be used before memory initialisation"))
|
||||
raise MemoryNotInitializedError
|
||||
self._cache = None
|
||||
self.namespace = namespace
|
||||
self.profile = profile
|
||||
|
||||
def _setCache(self, data):
|
||||
self._cache = data
|
||||
|
||||
def load(self):
|
||||
"""Load persistent data from storage.
|
||||
|
||||
need to be called before any other operation
|
||||
@return: defers the PersistentDict instance itself
|
||||
"""
|
||||
d = self.storage.getPrivates(self.namespace, binary=self.binary, profile=self.profile)
|
||||
d.addCallback(self._setCache)
|
||||
d.addCallback(lambda dummy: self)
|
||||
return d
|
||||
|
||||
def iteritems(self):
|
||||
return self._cache.iteritems()
|
||||
|
||||
def items(self):
|
||||
return self._cache.items()
|
||||
|
||||
def __repr__(self):
|
||||
return self._cache.__repr__()
|
||||
|
||||
def __str__(self):
|
||||
return self._cache.__str__()
|
||||
|
||||
def __lt__(self, other):
|
||||
return self._cache.__lt__(other)
|
||||
|
||||
def __le__(self, other):
|
||||
return self._cache.__le__(other)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self._cache.__eq__(other)
|
||||
|
||||
def __ne__(self, other):
|
||||
return self._cache.__ne__(other)
|
||||
|
||||
def __gt__(self, other):
|
||||
return self._cache.__gt__(other)
|
||||
|
||||
def __ge__(self, other):
|
||||
return self._cache.__ge__(other)
|
||||
|
||||
def __cmp__(self, other):
|
||||
return self._cache.__cmp__(other)
|
||||
|
||||
def __hash__(self):
|
||||
return self._cache.__hash__()
|
||||
|
||||
def __nonzero__(self):
|
||||
return self._cache.__len__()
|
||||
|
||||
def __contains__(self, key):
|
||||
return self._cache.__contains__(key)
|
||||
|
||||
def __iter__(self):
|
||||
return self._cache.__iter__()
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._cache.__getitem__(key)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.storage.setPrivateValue(self.namespace, key, value, self.binary, self.profile)
|
||||
return self._cache.__setitem__(key, value)
|
||||
|
||||
def __delitem__(self, key):
|
||||
self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile)
|
||||
return self._cache.__delitem__(key)
|
||||
|
||||
def get(self, key, default=None):
|
||||
return self._cache.get(key, default)
|
||||
|
||||
def setdefault(self, key, default):
|
||||
try:
|
||||
return self._cache[key]
|
||||
except:
|
||||
self.__setitem__(key, default)
|
||||
return default
|
||||
|
||||
def force(self, name):
|
||||
"""Force saving of an attribute to storage
|
||||
|
||||
@return: deferred fired when data is actually saved
|
||||
"""
|
||||
return self.storage.setPrivateValue(self.namespace, name, self._cache[name], self.binary, self.profile)
|
||||
|
||||
|
||||
class PersistentBinaryDict(PersistentDict):
|
||||
"""Persistent dict where value can be any python data (instead of string only)"""
|
||||
binary = True
|
||||
|
||||
|
||||
class LazyPersistentBinaryDict(PersistentBinaryDict):
|
||||
ur"""PersistentBinaryDict which get key/value when needed
|
||||
|
||||
This Persistent need more database access, it is suitable for largest data,
|
||||
to save memory.
|
||||
/!\ most of methods return a Deferred
|
||||
"""
|
||||
# TODO: missing methods should be implemented using database access
|
||||
# TODO: a cache would be useful (which is deleted after a timeout)
|
||||
|
||||
def load(self):
|
||||
# we show a warning as calling load on LazyPersistentBinaryDict sounds like a code mistake
|
||||
log.warning(_(u"Calling load on LazyPersistentBinaryDict while it's not needed"))
|
||||
|
||||
def iteritems(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def items(self):
|
||||
return self.storage.getPrivates(self.namespace, binary=self.binary, profile=self.profile)
|
||||
|
||||
def __repr__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def __str__(self):
|
||||
return "LazyPersistentBinaryDict (namespace: {})".format(self.namespace)
|
||||
|
||||
def __lt__(self, other):
|
||||
raise NotImplementedError
|
||||
|
||||
def __le__(self, other):
|
||||
raise NotImplementedError
|
||||
|
||||
def __eq__(self, other):
|
||||
raise NotImplementedError
|
||||
|
||||
def __ne__(self, other):
|
||||
raise NotImplementedError
|
||||
|
||||
def __gt__(self, other):
|
||||
raise NotImplementedError
|
||||
|
||||
def __ge__(self, other):
|
||||
raise NotImplementedError
|
||||
|
||||
def __cmp__(self, other):
|
||||
raise NotImplementedError
|
||||
|
||||
def __hash__(self):
|
||||
return hash(unicode(self.__class__) + self.namespace + (self.profile or u''))
|
||||
|
||||
def __nonzero__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def __contains__(self, key):
|
||||
raise NotImplementedError
|
||||
|
||||
def __iter__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def __getitem__(self, key):
|
||||
"""get the value as a Deferred"""
|
||||
d = self.storage.getPrivates(self.namespace, keys=[key], binary=self.binary, profile=self.profile)
|
||||
d.addCallback(lambda data: data[key])
|
||||
return d
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.storage.setPrivateValue(self.namespace, key, value, self.binary, self.profile)
|
||||
|
||||
def __delitem__(self, key):
|
||||
self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile)
|
||||
|
||||
def _valueOrDefault(self, value, default):
|
||||
if value is None:
|
||||
return default
|
||||
return value
|
||||
|
||||
def get(self, key, default=None):
|
||||
d = self.__getitem__(key)
|
||||
d.addCallback(self._valueOrDefault)
|
||||
return d
|
||||
|
||||
def setdefault(self, key, default):
|
||||
raise NotImplementedError
|
||||
|
||||
def force(self, name, value):
|
||||
"""Force saving of an attribute to storage
|
||||
|
||||
@param value(object): value is needed for LazyPersistentBinaryDict
|
||||
@return: deferred fired when data is actually saved
|
||||
"""
|
||||
return self.storage.setPrivateValue(self.namespace, name, value, self.binary, self.profile)
|
||||
1295
sat/memory/sqlite.py
Normal file
1295
sat/memory/sqlite.py
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue