Python 3 port:
/!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
This commit is contained in:
parent
d21a6a660f
commit
fbfe96c505
8 changed files with 6336 additions and 6398 deletions
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -23,7 +23,7 @@ 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 pickle as pickle
|
||||
import mimetypes
|
||||
import os.path
|
||||
import time
|
||||
|
|
@ -42,9 +42,9 @@ class Cache(object):
|
|||
self.profile = profile
|
||||
path_elts = [host.memory.getConfig("", "local_dir"), C.CACHE_DIR]
|
||||
if profile:
|
||||
path_elts.extend([u"profiles", regex.pathEscape(profile)])
|
||||
path_elts.extend(["profiles", regex.pathEscape(profile)])
|
||||
else:
|
||||
path_elts.append(u"common")
|
||||
path_elts.append("common")
|
||||
self.cache_dir = os.path.join(*path_elts)
|
||||
|
||||
if not os.path.exists(self.cache_dir):
|
||||
|
|
@ -55,11 +55,11 @@ class Cache(object):
|
|||
|
||||
@param filename(unicode): cached file name (cache data or actual file)
|
||||
"""
|
||||
if not filename or u"/" in filename:
|
||||
if not filename or "/" in filename:
|
||||
log.error(
|
||||
u"invalid char found in file name, hack attempt? name:{}".format(filename)
|
||||
"invalid char found in file name, hack attempt? name:{}".format(filename)
|
||||
)
|
||||
raise exceptions.DataError(u"Invalid char found")
|
||||
raise exceptions.DataError("Invalid char found")
|
||||
return os.path.join(self.cache_dir, filename)
|
||||
|
||||
def getMetadata(self, uid):
|
||||
|
|
@ -73,7 +73,7 @@ class Cache(object):
|
|||
|
||||
uid = uid.strip()
|
||||
if not uid:
|
||||
raise exceptions.InternalError(u"uid must not be empty")
|
||||
raise exceptions.InternalError("uid must not be empty")
|
||||
cache_url = self.getPath(uid)
|
||||
if not os.path.exists(cache_url):
|
||||
return None
|
||||
|
|
@ -82,20 +82,20 @@ class Cache(object):
|
|||
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))
|
||||
log.warning("can't read cache at {}".format(cache_url))
|
||||
return None
|
||||
except pickle.UnpicklingError:
|
||||
log.warning(u"invalid cache found at {}".format(cache_url))
|
||||
log.warning("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))
|
||||
log.warning("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)
|
||||
"removing expired cache (expired for {}s)".format(time.time() - eol)
|
||||
)
|
||||
return None
|
||||
|
||||
|
|
@ -135,11 +135,11 @@ class Cache(object):
|
|||
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)
|
||||
"can't find extension for MIME type {}".format(mime_type)
|
||||
)
|
||||
ext = DEFAULT_EXT
|
||||
elif ext == u".jpe":
|
||||
ext = u".jpg"
|
||||
elif ext == ".jpe":
|
||||
ext = ".jpg"
|
||||
else:
|
||||
ext = DEFAULT_EXT
|
||||
mime_type = None
|
||||
|
|
@ -147,10 +147,10 @@ class Cache(object):
|
|||
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,
|
||||
"source": source,
|
||||
"filename": filename,
|
||||
"eol": int(time.time()) + max_age,
|
||||
"mime_type": mime_type,
|
||||
}
|
||||
file_path = self.getPath(filename)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -92,7 +92,7 @@ class BlockCipher(object):
|
|||
# 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)
|
||||
d.addCallback(lambda text: text if text else None)
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
|
|
@ -114,11 +114,12 @@ class BlockCipher(object):
|
|||
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)
|
||||
return s + (bs - len(s) % bs) * (chr(bs - len(s) % bs)).encode('utf-8')
|
||||
|
||||
@classmethod
|
||||
def unpad(self, s):
|
||||
"""Method from http://stackoverflow.com/a/12525165"""
|
||||
s = s.decode('utf-8')
|
||||
return s[0 : -ord(s[-1])]
|
||||
|
||||
|
||||
|
|
@ -136,7 +137,7 @@ class PasswordHasher(object):
|
|||
@return: Deferred: base-64 encoded str
|
||||
"""
|
||||
if leave_empty and password == "":
|
||||
return succeed(password)
|
||||
return succeed(b"")
|
||||
salt = (
|
||||
b64decode(salt)[: PasswordHasher.SALT_LEN]
|
||||
if salt
|
||||
|
|
@ -146,6 +147,11 @@ class PasswordHasher(object):
|
|||
d.addCallback(lambda hashed: b64encode(salt + hashed))
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
def compare_hash(cls, hashed_attempt, hashed):
|
||||
assert isinstance(hashed, bytes)
|
||||
return hashed_attempt == hashed
|
||||
|
||||
@classmethod
|
||||
def verify(cls, attempt, hashed):
|
||||
"""Verify a password attempt.
|
||||
|
|
@ -156,5 +162,5 @@ class PasswordHasher(object):
|
|||
"""
|
||||
leave_empty = hashed == ""
|
||||
d = PasswordHasher.hash(attempt, hashed, leave_empty)
|
||||
d.addCallback(lambda hashed_attempt: hashed_attempt == hashed)
|
||||
d.addCallback(cls.compare_hash, hashed=hashed.encode('utf-8'))
|
||||
return d
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -50,11 +50,11 @@ class ByteIdentity(object):
|
|||
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 ""
|
||||
self.name = identity.name.encode("utf-8") if identity.name else b""
|
||||
self.lang = lang.encode("utf-8") if lang is not None else b""
|
||||
|
||||
def __str__(self):
|
||||
return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
|
||||
def __bytes__(self):
|
||||
return b"%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
|
||||
|
||||
|
||||
class HashManager(object):
|
||||
|
|
@ -74,7 +74,7 @@ class HashManager(object):
|
|||
|
||||
def __setitem__(self, hash_, disco_info):
|
||||
if hash_ in self.hashes:
|
||||
log.debug(u"ignoring hash set: it is already known")
|
||||
log.debug("ignoring hash set: it is already known")
|
||||
return
|
||||
self.hashes[hash_] = disco_info
|
||||
self.persistent[hash_] = disco_info.toElement().toXml()
|
||||
|
|
@ -84,19 +84,19 @@ class HashManager(object):
|
|||
|
||||
def load(self):
|
||||
def fillHashes(hashes):
|
||||
for hash_, xml in hashes.iteritems():
|
||||
for hash_, xml in hashes.items():
|
||||
element = xml_tools.ElementParser()(xml)
|
||||
disco_info = disco.DiscoInfo.fromElement(element)
|
||||
if not disco_info.features and not disco_info.identities:
|
||||
log.warning(
|
||||
_(
|
||||
u"no feature/identity found in disco element (hash: {cap_hash}), ignoring: {xml}"
|
||||
"no feature/identity found in disco element (hash: {cap_hash}), ignoring: {xml}"
|
||||
).format(cap_hash=hash_, xml=xml)
|
||||
)
|
||||
else:
|
||||
self.hashes[hash_] = disco_info
|
||||
|
||||
log.info(u"Disco hashes loaded")
|
||||
log.info("Disco hashes loaded")
|
||||
|
||||
d = self.persistent.load()
|
||||
d.addCallback(fillHashes)
|
||||
|
|
@ -116,7 +116,7 @@ class Discovery(object):
|
|||
return self.hashes.load()
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def hasFeature(self, client, feature, jid_=None, node=u""):
|
||||
def hasFeature(self, client, feature, jid_=None, node=""):
|
||||
"""Tell if an entity has the required feature
|
||||
|
||||
@param feature: feature namespace
|
||||
|
|
@ -128,7 +128,7 @@ class Discovery(object):
|
|||
defer.returnValue(feature in disco_infos.features)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def checkFeature(self, client, feature, jid_=None, node=u""):
|
||||
def checkFeature(self, client, feature, jid_=None, node=""):
|
||||
"""Like hasFeature, but raise an exception is feature is not Found
|
||||
|
||||
@param feature: feature namespace
|
||||
|
|
@ -142,7 +142,7 @@ class Discovery(object):
|
|||
raise failure.Failure(exceptions.FeatureNotFound)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def checkFeatures(self, client, features, jid_=None, identity=None, node=u""):
|
||||
def checkFeatures(self, client, features, jid_=None, identity=None, node=""):
|
||||
"""Like checkFeature, but check several features at once, and check also identity
|
||||
|
||||
@param features(iterable[unicode]): features to check
|
||||
|
|
@ -159,7 +159,7 @@ class Discovery(object):
|
|||
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):
|
||||
def getInfos(self, client, jid_=None, node="", 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
|
||||
|
|
@ -188,16 +188,16 @@ class Discovery(object):
|
|||
|
||||
def infosEb(fail):
|
||||
if fail.check(defer.CancelledError):
|
||||
reason = u"request time-out"
|
||||
reason = "request time-out"
|
||||
fail = failure.Failure(exceptions.TimeOutError(fail.message))
|
||||
else:
|
||||
try:
|
||||
reason = unicode(fail.value)
|
||||
reason = str(fail.value)
|
||||
except AttributeError:
|
||||
reason = unicode(fail)
|
||||
reason = str(fail)
|
||||
|
||||
log.warning(
|
||||
u"Error while requesting disco infos from {jid}: {reason}".format(
|
||||
"Error while requesting disco infos from {jid}: {reason}".format(
|
||||
jid=jid_.full(), reason=reason
|
||||
)
|
||||
)
|
||||
|
|
@ -218,7 +218,7 @@ class Discovery(object):
|
|||
return defer.succeed(disco_infos)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def getItems(self, client, jid_=None, node=u"", use_cache=True):
|
||||
def getItems(self, client, jid_=None, node="", 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
|
||||
|
|
@ -236,12 +236,12 @@ class Discovery(object):
|
|||
items = self.host.memory.getEntityData(
|
||||
jid_, ["DISCO_ITEMS"], client.profile
|
||||
)["DISCO_ITEMS"]
|
||||
log.debug(u"[%s] disco items are in cache" % jid_.full())
|
||||
log.debug("[%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())
|
||||
log.debug("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
|
||||
|
|
@ -251,7 +251,7 @@ class Discovery(object):
|
|||
items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
|
||||
except StanzaError as e:
|
||||
log.warning(
|
||||
u"Error while requesting items for {jid}: {reason}".format(
|
||||
"Error while requesting items for {jid}: {reason}".format(
|
||||
jid=jid_.full(), reason=e.condition
|
||||
)
|
||||
)
|
||||
|
|
@ -262,7 +262,7 @@ class Discovery(object):
|
|||
def _infosEb(self, failure_, entity_jid):
|
||||
failure_.trap(StanzaError)
|
||||
log.warning(
|
||||
_(u"Error while requesting [%(jid)s]: %(error)s")
|
||||
_("Error while requesting [%(jid)s]: %(error)s")
|
||||
% {"jid": entity_jid.full(), "error": failure_.getErrorMessage()}
|
||||
)
|
||||
|
||||
|
|
@ -326,7 +326,7 @@ class Discovery(object):
|
|||
|
||||
def infosCb(infos, entity):
|
||||
if entity is None:
|
||||
log.warning(_(u"received an item without jid"))
|
||||
log.warning(_("received an item without jid"))
|
||||
return
|
||||
if identity is not None and identity not in infos.identities:
|
||||
return
|
||||
|
|
@ -367,8 +367,8 @@ class Discovery(object):
|
|||
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("<")
|
||||
s.append(bytes(identity))
|
||||
s.append(b"<")
|
||||
# features
|
||||
byte_features = [
|
||||
service.encode("utf-8")
|
||||
|
|
@ -378,32 +378,32 @@ class Discovery(object):
|
|||
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("<")
|
||||
s.append(b"<")
|
||||
|
||||
# extensions
|
||||
ext = services.extensions.values()
|
||||
ext = list(services.extensions.values())
|
||||
ext.sort(key=lambda f: f.formNamespace.encode('utf-8'))
|
||||
for extension in ext:
|
||||
s.append(extension.formNamespace.encode('utf-8'))
|
||||
s.append("<")
|
||||
s.append(b"<")
|
||||
fields = extension.fieldList
|
||||
fields.sort(key=lambda f: f.var.encode('utf-8'))
|
||||
for field in fields:
|
||||
s.append(field.var.encode('utf-8'))
|
||||
s.append("<")
|
||||
s.append(b"<")
|
||||
values = [v.encode('utf-8') for v in field.values]
|
||||
values.sort()
|
||||
for value in values:
|
||||
s.append(value)
|
||||
s.append("<")
|
||||
s.append(b"<")
|
||||
|
||||
cap_hash = b64encode(sha1("".join(s)).digest())
|
||||
log.debug(_(u"Capability hash generated: [{cap_hash}]").format(cap_hash=cap_hash))
|
||||
cap_hash = b64encode(sha1(b"".join(s)).digest()).decode('utf-8')
|
||||
log.debug(_("Capability hash generated: [{cap_hash}]").format(cap_hash=cap_hash))
|
||||
return cap_hash
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _discoInfos(
|
||||
self, entity_jid_s, node=u"", use_cache=True, profile_key=C.PROF_KEY_NONE
|
||||
self, entity_jid_s, node="", use_cache=True, profile_key=C.PROF_KEY_NONE
|
||||
):
|
||||
""" Discovery method for the bridge
|
||||
@param entity_jid_s: entity we want to discover
|
||||
|
|
@ -417,7 +417,7 @@ class Discovery(object):
|
|||
disco_infos = yield self.getInfos(client, entity, node, use_cache)
|
||||
extensions = {}
|
||||
# FIXME: should extensions be serialised using tools.common.data_format?
|
||||
for form_type, form in disco_infos.extensions.items():
|
||||
for form_type, form in list(disco_infos.extensions.items()):
|
||||
fields = []
|
||||
for field in form.fieldList:
|
||||
data = {"type": field.fieldType}
|
||||
|
|
@ -427,7 +427,7 @@ class Discovery(object):
|
|||
data[attr] = value
|
||||
|
||||
values = [field.value] if field.value is not None else field.values
|
||||
if field.fieldType == u"boolean":
|
||||
if field.fieldType == "boolean":
|
||||
values = [C.boolConst(v) for v in values]
|
||||
fields.append((data, values))
|
||||
|
||||
|
|
@ -436,7 +436,7 @@ class Discovery(object):
|
|||
defer.returnValue((
|
||||
disco_infos.features,
|
||||
[(cat, type_, name or "")
|
||||
for (cat, type_), name in disco_infos.identities.items()],
|
||||
for (cat, type_), name in list(disco_infos.identities.items())],
|
||||
extensions))
|
||||
|
||||
def items2tuples(self, disco_items):
|
||||
|
|
@ -447,13 +447,13 @@ class Discovery(object):
|
|||
"""
|
||||
for item in disco_items:
|
||||
if not item.entity:
|
||||
log.warning(_(u"invalid item (no jid)"))
|
||||
log.warning(_("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
|
||||
self, entity_jid_s, node="", use_cache=True, profile_key=C.PROF_KEY_NONE
|
||||
):
|
||||
""" Discovery method for the bridge
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -91,7 +91,7 @@ class EncryptionHandler(object):
|
|||
directed=directed)
|
||||
cls.plugins.append(plugin)
|
||||
cls.plugins.sort(key=lambda p: p.priority)
|
||||
log.info(_(u"Encryption plugin registered: {name}").format(name=name))
|
||||
log.info(_("Encryption plugin registered: {name}").format(name=name))
|
||||
|
||||
@classmethod
|
||||
def getPlugins(cls):
|
||||
|
|
@ -103,7 +103,7 @@ class EncryptionHandler(object):
|
|||
return next(p for p in cls.plugins if p.namespace == namespace)
|
||||
except StopIteration:
|
||||
raise exceptions.NotFound(_(
|
||||
u"Can't find requested encryption plugin: {namespace}").format(
|
||||
"Can't find requested encryption plugin: {namespace}").format(
|
||||
namespace=namespace))
|
||||
|
||||
@classmethod
|
||||
|
|
@ -123,7 +123,7 @@ class EncryptionHandler(object):
|
|||
if p.name.lower() == name.lower():
|
||||
return p.namespace
|
||||
raise exceptions.NotFound(_(
|
||||
u"Can't find a plugin with the name \"{name}\".".format(
|
||||
"Can't find a plugin with the name \"{name}\".".format(
|
||||
name=name)))
|
||||
|
||||
def getBridgeData(self, session):
|
||||
|
|
@ -133,12 +133,12 @@ class EncryptionHandler(object):
|
|||
@return (unicode): serialized data for bridge
|
||||
"""
|
||||
if session is None:
|
||||
return u''
|
||||
plugin = session[u'plugin']
|
||||
return ''
|
||||
plugin = session['plugin']
|
||||
bridge_data = {'name': plugin.name,
|
||||
'namespace': plugin.namespace}
|
||||
if u'directed_devices' in session:
|
||||
bridge_data[u'directed_devices'] = session[u'directed_devices']
|
||||
if 'directed_devices' in session:
|
||||
bridge_data['directed_devices'] = session['directed_devices']
|
||||
|
||||
return data_format.serialise(bridge_data)
|
||||
|
||||
|
|
@ -151,7 +151,7 @@ class EncryptionHandler(object):
|
|||
try:
|
||||
start_encryption = plugin.instance.startEncryption
|
||||
except AttributeError:
|
||||
log.debug(u"No startEncryption method found for {plugin}".format(
|
||||
log.debug("No startEncryption method found for {plugin}".format(
|
||||
plugin = plugin.namespace))
|
||||
return defer.succeed(None)
|
||||
else:
|
||||
|
|
@ -167,7 +167,7 @@ class EncryptionHandler(object):
|
|||
try:
|
||||
stop_encryption = plugin.instance.stopEncryption
|
||||
except AttributeError:
|
||||
log.debug(u"No stopEncryption method found for {plugin}".format(
|
||||
log.debug("No stopEncryption method found for {plugin}".format(
|
||||
plugin = plugin.namespace))
|
||||
return defer.succeed(None)
|
||||
else:
|
||||
|
|
@ -187,8 +187,8 @@ class EncryptionHandler(object):
|
|||
it will be replaced by the new one
|
||||
"""
|
||||
if not self.plugins:
|
||||
raise exceptions.NotFound(_(u"No encryption plugin is registered, "
|
||||
u"an encryption session can't be started"))
|
||||
raise exceptions.NotFound(_("No encryption plugin is registered, "
|
||||
"an encryption session can't be started"))
|
||||
|
||||
if namespace is None:
|
||||
plugin = self.plugins[0]
|
||||
|
|
@ -198,10 +198,10 @@ class EncryptionHandler(object):
|
|||
bare_jid = entity.userhostJID()
|
||||
if bare_jid in self._sessions:
|
||||
# we have already an encryption session with this contact
|
||||
former_plugin = self._sessions[bare_jid][u"plugin"]
|
||||
former_plugin = self._sessions[bare_jid]["plugin"]
|
||||
if former_plugin.namespace == namespace:
|
||||
log.info(_(u"Session with {bare_jid} is already encrypted with {name}. "
|
||||
u"Nothing to do.").format(
|
||||
log.info(_("Session with {bare_jid} is already encrypted with {name}. "
|
||||
"Nothing to do.").format(
|
||||
bare_jid=bare_jid, name=former_plugin.name))
|
||||
return
|
||||
|
||||
|
|
@ -211,8 +211,8 @@ class EncryptionHandler(object):
|
|||
del self._sessions[bare_jid]
|
||||
yield self._stopEncryption(former_plugin, entity)
|
||||
else:
|
||||
msg = (_(u"Session with {bare_jid} is already encrypted with {name}. "
|
||||
u"Please stop encryption session before changing algorithm.")
|
||||
msg = (_("Session with {bare_jid} is already encrypted with {name}. "
|
||||
"Please stop encryption session before changing algorithm.")
|
||||
.format(bare_jid=bare_jid, name=plugin.name))
|
||||
log.warning(msg)
|
||||
raise exceptions.ConflictError(msg)
|
||||
|
|
@ -223,34 +223,34 @@ class EncryptionHandler(object):
|
|||
entity.resource = self.host.memory.getMainResource(self.client, entity)
|
||||
if not entity.resource:
|
||||
raise exceptions.NotFound(
|
||||
_(u"No resource found for {destinee}, can't encrypt with {name}")
|
||||
_("No resource found for {destinee}, can't encrypt with {name}")
|
||||
.format(destinee=entity.full(), name=plugin.name))
|
||||
log.info(_(u"No resource specified to encrypt with {name}, using "
|
||||
u"{destinee}.").format(destinee=entity.full(),
|
||||
log.info(_("No resource specified to encrypt with {name}, using "
|
||||
"{destinee}.").format(destinee=entity.full(),
|
||||
name=plugin.name))
|
||||
# indicate that we encrypt only for some devices
|
||||
directed_devices = data[u'directed_devices'] = [entity.resource]
|
||||
directed_devices = data['directed_devices'] = [entity.resource]
|
||||
elif entity.resource:
|
||||
raise ValueError(_(u"{name} encryption must be used with bare jids."))
|
||||
raise ValueError(_("{name} encryption must be used with bare jids."))
|
||||
|
||||
yield self._startEncryption(plugin, entity)
|
||||
self._sessions[entity.userhostJID()] = data
|
||||
log.info(_(u"Encryption session has been set for {entity_jid} with "
|
||||
u"{encryption_name}").format(
|
||||
log.info(_("Encryption session has been set for {entity_jid} with "
|
||||
"{encryption_name}").format(
|
||||
entity_jid=entity.full(), encryption_name=plugin.name))
|
||||
self.host.bridge.messageEncryptionStarted(
|
||||
entity.full(),
|
||||
self.getBridgeData(data),
|
||||
self.client.profile)
|
||||
msg = D_(u"Encryption session started: your messages with {destinee} are "
|
||||
u"now end to end encrypted using {name} algorithm.").format(
|
||||
msg = D_("Encryption session started: your messages with {destinee} are "
|
||||
"now end to end encrypted using {name} algorithm.").format(
|
||||
destinee=entity.full(), name=plugin.name)
|
||||
directed_devices = data.get(u'directed_devices')
|
||||
directed_devices = data.get('directed_devices')
|
||||
if directed_devices:
|
||||
msg += u"\n" + D_(u"Message are encrypted only for {nb_devices} device(s): "
|
||||
u"{devices_list}.").format(
|
||||
msg += "\n" + D_("Message are encrypted only for {nb_devices} device(s): "
|
||||
"{devices_list}.").format(
|
||||
nb_devices=len(directed_devices),
|
||||
devices_list = u', '.join(directed_devices))
|
||||
devices_list = ', '.join(directed_devices))
|
||||
|
||||
self.client.feedback(bare_jid, msg)
|
||||
|
||||
|
|
@ -266,29 +266,29 @@ class EncryptionHandler(object):
|
|||
session = self.getSession(entity.userhostJID())
|
||||
if not session:
|
||||
raise failure.Failure(
|
||||
exceptions.NotFound(_(u"There is no encryption session with this "
|
||||
u"entity.")))
|
||||
exceptions.NotFound(_("There is no encryption session with this "
|
||||
"entity.")))
|
||||
plugin = session['plugin']
|
||||
if namespace is not None and plugin.namespace != namespace:
|
||||
raise exceptions.InternalError(_(
|
||||
u"The encryption session is not run with the expected plugin: encrypted "
|
||||
u"with {current_name} and was expecting {expected_name}").format(
|
||||
current_name=session[u'plugin'].namespace,
|
||||
"The encryption session is not run with the expected plugin: encrypted "
|
||||
"with {current_name} and was expecting {expected_name}").format(
|
||||
current_name=session['plugin'].namespace,
|
||||
expected_name=namespace))
|
||||
if entity.resource:
|
||||
try:
|
||||
directed_devices = session[u'directed_devices']
|
||||
directed_devices = session['directed_devices']
|
||||
except KeyError:
|
||||
raise exceptions.NotFound(_(
|
||||
u"There is a session for the whole entity (i.e. all devices of the "
|
||||
u"entity), not a directed one. Please use bare jid if you want to "
|
||||
u"stop the whole encryption with this entity."))
|
||||
"There is a session for the whole entity (i.e. all devices of the "
|
||||
"entity), not a directed one. Please use bare jid if you want to "
|
||||
"stop the whole encryption with this entity."))
|
||||
|
||||
try:
|
||||
directed_devices.remove(entity.resource)
|
||||
except ValueError:
|
||||
raise exceptions.NotFound(_(u"There is no directed session with this "
|
||||
u"entity."))
|
||||
raise exceptions.NotFound(_("There is no directed session with this "
|
||||
"entity."))
|
||||
else:
|
||||
if not directed_devices:
|
||||
# if we have no more directed device sessions,
|
||||
|
|
@ -302,7 +302,7 @@ class EncryptionHandler(object):
|
|||
del self._sessions[entity.userhostJID()]
|
||||
yield self._stopEncryption(plugin, entity)
|
||||
|
||||
log.info(_(u"encryption session stopped with entity {entity}").format(
|
||||
log.info(_("encryption session stopped with entity {entity}").format(
|
||||
entity=entity.full()))
|
||||
self.host.bridge.messageEncryptionStopped(
|
||||
entity.full(),
|
||||
|
|
@ -310,9 +310,9 @@ class EncryptionHandler(object):
|
|||
'namespace': plugin.namespace,
|
||||
},
|
||||
self.client.profile)
|
||||
msg = D_(u"Encryption session finished: your messages with {destinee} are "
|
||||
u"NOT end to end encrypted anymore.\nYour server administrators or "
|
||||
u"{destinee} server administrators will be able to read them.").format(
|
||||
msg = D_("Encryption session finished: your messages with {destinee} are "
|
||||
"NOT end to end encrypted anymore.\nYour server administrators or "
|
||||
"{destinee} server administrators will be able to read them.").format(
|
||||
destinee=entity.full())
|
||||
|
||||
self.client.feedback(entity, msg)
|
||||
|
|
@ -326,7 +326,7 @@ class EncryptionHandler(object):
|
|||
None if there is not encryption for this session with this jid
|
||||
"""
|
||||
if entity.resource:
|
||||
raise ValueError(u"Full jid given when expecting bare jid")
|
||||
raise ValueError("Full jid given when expecting bare jid")
|
||||
return self._sessions.get(entity)
|
||||
|
||||
def getTrustUI(self, entity_jid, namespace=None):
|
||||
|
|
@ -346,7 +346,7 @@ class EncryptionHandler(object):
|
|||
session = self.getSession(entity_jid)
|
||||
if not session:
|
||||
raise exceptions.NotFound(
|
||||
u"No encryption session currently active for {entity_jid}"
|
||||
"No encryption session currently active for {entity_jid}"
|
||||
.format(entity_jid=entity_jid.full()))
|
||||
plugin = session['plugin']
|
||||
else:
|
||||
|
|
@ -355,7 +355,7 @@ class EncryptionHandler(object):
|
|||
get_trust_ui = plugin.instance.getTrustUI
|
||||
except AttributeError:
|
||||
raise NotImplementedError(
|
||||
u"Encryption plugin doesn't handle trust management UI")
|
||||
"Encryption plugin doesn't handle trust management UI")
|
||||
else:
|
||||
return defer.maybeDeferred(get_trust_ui, self.client, entity_jid)
|
||||
|
||||
|
|
@ -364,32 +364,32 @@ class EncryptionHandler(object):
|
|||
@classmethod
|
||||
def _importMenus(cls, host):
|
||||
host.importMenu(
|
||||
(D_(u"Encryption"), D_(u"unencrypted (plain text)")),
|
||||
(D_("Encryption"), D_("unencrypted (plain text)")),
|
||||
partial(cls._onMenuUnencrypted, host=host),
|
||||
security_limit=0,
|
||||
help_string=D_(u"End encrypted session"),
|
||||
help_string=D_("End encrypted session"),
|
||||
type_=C.MENU_SINGLE,
|
||||
)
|
||||
for plg in cls.getPlugins():
|
||||
host.importMenu(
|
||||
(D_(u"Encryption"), plg.name),
|
||||
(D_("Encryption"), plg.name),
|
||||
partial(cls._onMenuName, host=host, plg=plg),
|
||||
security_limit=0,
|
||||
help_string=D_(u"Start {name} session").format(name=plg.name),
|
||||
help_string=D_("Start {name} session").format(name=plg.name),
|
||||
type_=C.MENU_SINGLE,
|
||||
)
|
||||
host.importMenu(
|
||||
(D_(u"Encryption"), D_(u"⛨ {name} trust").format(name=plg.name)),
|
||||
(D_("Encryption"), D_("⛨ {name} trust").format(name=plg.name)),
|
||||
partial(cls._onMenuTrust, host=host, plg=plg),
|
||||
security_limit=0,
|
||||
help_string=D_(u"Manage {name} trust").format(name=plg.name),
|
||||
help_string=D_("Manage {name} trust").format(name=plg.name),
|
||||
type_=C.MENU_SINGLE,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _onMenuUnencrypted(cls, data, host, profile):
|
||||
client = host.getClient(profile)
|
||||
peer_jid = jid.JID(data[u'jid']).userhostJID()
|
||||
peer_jid = jid.JID(data['jid']).userhostJID()
|
||||
d = client.encryption.stop(peer_jid)
|
||||
d.addCallback(lambda __: {})
|
||||
return d
|
||||
|
|
@ -397,7 +397,7 @@ class EncryptionHandler(object):
|
|||
@classmethod
|
||||
def _onMenuName(cls, data, host, plg, profile):
|
||||
client = host.getClient(profile)
|
||||
peer_jid = jid.JID(data[u'jid'])
|
||||
peer_jid = jid.JID(data['jid'])
|
||||
if not plg.directed:
|
||||
peer_jid = peer_jid.userhostJID()
|
||||
d = client.encryption.start(peer_jid, plg.namespace, replace=True)
|
||||
|
|
@ -408,9 +408,9 @@ class EncryptionHandler(object):
|
|||
@defer.inlineCallbacks
|
||||
def _onMenuTrust(cls, data, host, plg, profile):
|
||||
client = host.getClient(profile)
|
||||
peer_jid = jid.JID(data[u'jid']).userhostJID()
|
||||
peer_jid = jid.JID(data['jid']).userhostJID()
|
||||
ui = yield client.encryption.getTrustUI(peer_jid, plg.namespace)
|
||||
defer.returnValue({u'xmlui': ui.toXml()})
|
||||
defer.returnValue({'xmlui': ui.toXml()})
|
||||
|
||||
## Triggers ##
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -26,7 +26,7 @@ log = getLogger(__name__)
|
|||
import os.path
|
||||
import copy
|
||||
from collections import namedtuple
|
||||
from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
|
||||
from configparser import SafeConfigParser, NoOptionError, NoSectionError
|
||||
from uuid import uuid4
|
||||
from twisted.python import failure
|
||||
from twisted.internet import defer, reactor, error
|
||||
|
|
@ -76,7 +76,7 @@ class Sessions(object):
|
|||
session_id = str(uuid4())
|
||||
elif session_id in self._sessions:
|
||||
raise exceptions.ConflictError(
|
||||
u"Session id {} is already used".format(session_id)
|
||||
"Session id {} is already used".format(session_id)
|
||||
)
|
||||
timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
|
||||
if session_data is None:
|
||||
|
|
@ -99,9 +99,9 @@ class Sessions(object):
|
|||
pass
|
||||
del self._sessions[session_id]
|
||||
log.debug(
|
||||
u"Session {} purged{}".format(
|
||||
"Session {} purged{}".format(
|
||||
session_id,
|
||||
u" (profile {})".format(profile) if profile is not None else u"",
|
||||
" (profile {})".format(profile) if profile is not None else "",
|
||||
)
|
||||
)
|
||||
|
||||
|
|
@ -147,10 +147,10 @@ class Sessions(object):
|
|||
self._purgeSession(session_id)
|
||||
|
||||
def keys(self):
|
||||
return self._sessions.keys()
|
||||
return list(self._sessions.keys())
|
||||
|
||||
def iterkeys(self):
|
||||
return self._sessions.iterkeys()
|
||||
return iter(self._sessions.keys())
|
||||
|
||||
|
||||
class ProfileSessions(Sessions):
|
||||
|
|
@ -165,7 +165,7 @@ class ProfileSessions(Sessions):
|
|||
@return: a list containing the sessions ids
|
||||
"""
|
||||
ret = []
|
||||
for session_id in self._sessions.iterkeys():
|
||||
for session_id in self._sessions.keys():
|
||||
try:
|
||||
timer, session_data, profile_set = self._sessions[session_id]
|
||||
except ValueError:
|
||||
|
|
@ -245,7 +245,7 @@ def fixLocalDir(silent=True):
|
|||
if not silent:
|
||||
log.warning(
|
||||
_(
|
||||
u"A database has been found in the default local_dir for previous versions (< 0.5)"
|
||||
"A database has been found in the default local_dir for previous versions (< 0.5)"
|
||||
)
|
||||
)
|
||||
tools_config.fixConfigOption("", "local_dir", old_default, silent)
|
||||
|
|
@ -306,10 +306,10 @@ class Memory(object):
|
|||
if os.path.exists(filename):
|
||||
try:
|
||||
self.params.load_xml(filename)
|
||||
log.debug(_(u"Parameters loaded from file: %s") % filename)
|
||||
log.debug(_("Parameters loaded from file: %s") % filename)
|
||||
return True
|
||||
except Exception as e:
|
||||
log.error(_(u"Can't load parameters from file: %s") % e)
|
||||
log.error(_("Can't load parameters from file: %s") % e)
|
||||
return False
|
||||
|
||||
def save_xml(self, filename):
|
||||
|
|
@ -324,10 +324,10 @@ class Memory(object):
|
|||
filename = os.path.expanduser(filename)
|
||||
try:
|
||||
self.params.save_xml(filename)
|
||||
log.debug(_(u"Parameters saved to file: %s") % filename)
|
||||
log.debug(_("Parameters saved to file: %s") % filename)
|
||||
return True
|
||||
except Exception as e:
|
||||
log.error(_(u"Can't save parameters to file: %s") % e)
|
||||
log.error(_("Can't save parameters to file: %s") % e)
|
||||
return False
|
||||
|
||||
def load(self):
|
||||
|
|
@ -356,7 +356,7 @@ class Memory(object):
|
|||
def createSession(__):
|
||||
"""Called once params are loaded."""
|
||||
self._entities_cache[profile] = {}
|
||||
log.info(u"[{}] Profile session started".format(profile))
|
||||
log.info("[{}] Profile session started".format(profile))
|
||||
return False
|
||||
|
||||
def backendInitialised(__):
|
||||
|
|
@ -392,13 +392,13 @@ class Memory(object):
|
|||
@param profile: %(doc_profile)s
|
||||
"""
|
||||
if self.host.isConnected(profile):
|
||||
log.debug(u"Disconnecting profile because of session stop")
|
||||
log.debug("Disconnecting profile because of session stop")
|
||||
self.host.disconnect(profile)
|
||||
self.auth_sessions.profileDelUnique(profile)
|
||||
try:
|
||||
self._entities_cache[profile]
|
||||
except KeyError:
|
||||
log.warning(u"Profile was not in cache")
|
||||
log.warning("Profile was not in cache")
|
||||
|
||||
def _isSessionStarted(self, profile_key):
|
||||
return self.isSessionStarted(self.getProfileName(profile_key))
|
||||
|
|
@ -428,10 +428,10 @@ class Memory(object):
|
|||
|
||||
def check_result(result):
|
||||
if not result:
|
||||
log.warning(u"Authentication failure of profile {}".format(profile))
|
||||
log.warning("Authentication failure of profile {}".format(profile))
|
||||
raise failure.Failure(
|
||||
exceptions.PasswordError(
|
||||
u"The provided profile password doesn't match."
|
||||
"The provided profile password doesn't match."
|
||||
)
|
||||
)
|
||||
if (
|
||||
|
|
@ -460,7 +460,7 @@ class Memory(object):
|
|||
self.auth_sessions.newSession(
|
||||
{C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile
|
||||
)
|
||||
log.debug(u"auth session created for profile %s" % profile)
|
||||
log.debug("auth session created for profile %s" % profile)
|
||||
|
||||
d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load()
|
||||
d.addCallback(lambda data: BlockCipher.decrypt(key, data[C.MEMORY_CRYPTO_KEY]))
|
||||
|
|
@ -476,7 +476,7 @@ class Memory(object):
|
|||
except KeyError:
|
||||
log.error(
|
||||
_(
|
||||
u"Trying to purge roster status cache for a profile not in memory: [%s]"
|
||||
"Trying to purge roster status cache for a profile not in memory: [%s]"
|
||||
)
|
||||
% profile
|
||||
)
|
||||
|
|
@ -489,7 +489,7 @@ class Memory(object):
|
|||
@return (list[unicode]): selected profiles
|
||||
"""
|
||||
if not clients and not components:
|
||||
log.warning(_(u"requesting no profiles at all"))
|
||||
log.warning(_("requesting no profiles at all"))
|
||||
return []
|
||||
profiles = self.storage.getProfilesList()
|
||||
if clients and components:
|
||||
|
|
@ -533,20 +533,20 @@ class Memory(object):
|
|||
@raise exceptions.NotFound: component is not a known plugin import name
|
||||
"""
|
||||
if not name:
|
||||
raise ValueError(u"Empty profile name")
|
||||
raise ValueError("Empty profile name")
|
||||
if name[0] == "@":
|
||||
raise ValueError(u"A profile name can't start with a '@'")
|
||||
raise ValueError("A profile name can't start with a '@'")
|
||||
if "\n" in name:
|
||||
raise ValueError(u"A profile name can't contain line feed ('\\n')")
|
||||
raise ValueError("A profile name can't contain line feed ('\\n')")
|
||||
|
||||
if name in self._entities_cache:
|
||||
raise exceptions.ConflictError(u"A session for this profile exists")
|
||||
raise exceptions.ConflictError("A session for this profile exists")
|
||||
|
||||
if component:
|
||||
if not component in self.host.plugins:
|
||||
raise exceptions.NotFound(
|
||||
_(
|
||||
u"Can't find component {component} entry point".format(
|
||||
"Can't find component {component} entry point".format(
|
||||
component=component
|
||||
)
|
||||
)
|
||||
|
|
@ -664,7 +664,7 @@ class Memory(object):
|
|||
|
||||
def _getPresenceStatuses(self, profile_key):
|
||||
ret = self.getPresenceStatuses(profile_key)
|
||||
return {entity.full(): data for entity, data in ret.iteritems()}
|
||||
return {entity.full(): data for entity, data in ret.items()}
|
||||
|
||||
def getPresenceStatuses(self, profile_key):
|
||||
"""Get all the presence statuses of a profile
|
||||
|
|
@ -676,8 +676,8 @@ class Memory(object):
|
|||
profile_cache = self._getProfileCache(client)
|
||||
entities_presence = {}
|
||||
|
||||
for entity_jid, entity_data in profile_cache.iteritems():
|
||||
for resource, resource_data in entity_data.iteritems():
|
||||
for entity_jid, entity_data in profile_cache.items():
|
||||
for resource, resource_data in entity_data.items():
|
||||
full_jid = copy.copy(entity_jid)
|
||||
full_jid.resource = resource
|
||||
try:
|
||||
|
|
@ -736,7 +736,7 @@ class Memory(object):
|
|||
entity_data = profile_cache[entity_jid.userhostJID()]
|
||||
except KeyError:
|
||||
raise exceptions.UnknownEntityError(
|
||||
u"Entity {} not in cache".format(entity_jid)
|
||||
"Entity {} not in cache".format(entity_jid)
|
||||
)
|
||||
resources = set(entity_data.keys())
|
||||
resources.discard(None)
|
||||
|
|
@ -758,7 +758,7 @@ class Memory(object):
|
|||
try:
|
||||
presence_data = self.getEntityDatum(full_jid, "presence", client.profile)
|
||||
except KeyError:
|
||||
log.debug(u"Can't get presence data for {}".format(full_jid))
|
||||
log.debug("Can't get presence data for {}".format(full_jid))
|
||||
else:
|
||||
if presence_data.show != C.PRESENCE_UNAVAILABLE:
|
||||
available.append(resource)
|
||||
|
|
@ -787,7 +787,7 @@ class Memory(object):
|
|||
try:
|
||||
resources = self.getAllResources(client, entity_jid)
|
||||
except exceptions.UnknownEntityError:
|
||||
log.warning(u"Entity is not in cache, we can't find any resource")
|
||||
log.warning("Entity is not in cache, we can't find any resource")
|
||||
return None
|
||||
priority_resources = []
|
||||
for resource in resources:
|
||||
|
|
@ -796,13 +796,13 @@ class Memory(object):
|
|||
try:
|
||||
presence_data = self.getEntityDatum(full_jid, "presence", client.profile)
|
||||
except KeyError:
|
||||
log.debug(u"No presence information for {}".format(full_jid))
|
||||
log.debug("No presence information for {}".format(full_jid))
|
||||
continue
|
||||
priority_resources.append((resource, presence_data.priority))
|
||||
try:
|
||||
return max(priority_resources, key=lambda res_tuple: res_tuple[1])[0]
|
||||
except ValueError:
|
||||
log.warning(u"No resource found at all for {}".format(entity_jid))
|
||||
log.warning("No resource found at all for {}".format(entity_jid))
|
||||
return None
|
||||
|
||||
## Entities data ##
|
||||
|
|
@ -835,8 +835,8 @@ class Memory(object):
|
|||
"""
|
||||
profile_cache = self._getProfileCache(client)
|
||||
# we construct a list of all known full jids (bare jid of entities x resources)
|
||||
for bare_jid, entity_data in profile_cache.iteritems():
|
||||
for resource in entity_data.iterkeys():
|
||||
for bare_jid, entity_data in profile_cache.items():
|
||||
for resource in entity_data.keys():
|
||||
if resource is None:
|
||||
continue
|
||||
full_jid = copy.copy(bare_jid)
|
||||
|
|
@ -871,9 +871,9 @@ class Memory(object):
|
|||
|
||||
entity_data[key] = value
|
||||
if key in self._key_signals and not silent:
|
||||
if not isinstance(value, basestring):
|
||||
if not isinstance(value, str):
|
||||
log.error(
|
||||
u"Setting a non string value ({}) for a key ({}) which has a signal flag".format(
|
||||
"Setting a non string value ({}) for a key ({}) which has a signal flag".format(
|
||||
value, key
|
||||
)
|
||||
)
|
||||
|
|
@ -905,7 +905,7 @@ class Memory(object):
|
|||
entity_data = profile_cache[jid_.userhostJID()][jid_.resource]
|
||||
except KeyError:
|
||||
raise exceptions.UnknownEntityError(
|
||||
u"Entity {} not in cache".format(jid_)
|
||||
"Entity {} not in cache".format(jid_)
|
||||
)
|
||||
try:
|
||||
del entity_data[key]
|
||||
|
|
@ -919,7 +919,7 @@ class Memory(object):
|
|||
ret = self.getEntitiesData(
|
||||
[jid.JID(jid_) for jid_ in entities_jids], keys_list, profile_key
|
||||
)
|
||||
return {jid_.full(): data for jid_, data in ret.iteritems()}
|
||||
return {jid_.full(): data for jid_, data in ret.items()}
|
||||
|
||||
def getEntitiesData(self, entities_jids, keys_list=None, profile_key=C.PROF_KEY_NONE):
|
||||
"""Get a list of cached values for several entities at once
|
||||
|
|
@ -961,8 +961,8 @@ class Memory(object):
|
|||
continue
|
||||
ret_data[entity.full()] = fillEntityData(entity_cache_data, keys_list)
|
||||
else:
|
||||
for bare_jid, data in profile_cache.iteritems():
|
||||
for resource, entity_cache_data in data.iteritems():
|
||||
for bare_jid, data in profile_cache.items():
|
||||
for resource, entity_cache_data in data.items():
|
||||
full_jid = copy.copy(bare_jid)
|
||||
full_jid.resource = resource
|
||||
ret_data[full_jid] = fillEntityData(entity_cache_data)
|
||||
|
|
@ -987,7 +987,7 @@ class Memory(object):
|
|||
entity_data = profile_cache[entity_jid.userhostJID()][entity_jid.resource]
|
||||
except KeyError:
|
||||
raise exceptions.UnknownEntityError(
|
||||
u"Entity {} not in cache (was requesting {})".format(
|
||||
"Entity {} not in cache (was requesting {})".format(
|
||||
entity_jid, keys_list
|
||||
)
|
||||
)
|
||||
|
|
@ -1030,14 +1030,14 @@ class Memory(object):
|
|||
del profile_cache[entity_jid]
|
||||
except KeyError:
|
||||
raise exceptions.UnknownEntityError(
|
||||
u"Entity {} not in cache".format(entity_jid)
|
||||
"Entity {} not in cache".format(entity_jid)
|
||||
)
|
||||
else:
|
||||
try:
|
||||
del profile_cache[entity_jid.userhostJID()][entity_jid.resource]
|
||||
except KeyError:
|
||||
raise exceptions.UnknownEntityError(
|
||||
u"Entity {} not in cache".format(entity_jid)
|
||||
"Entity {} not in cache".format(entity_jid)
|
||||
)
|
||||
|
||||
## Encryption ##
|
||||
|
|
@ -1103,7 +1103,7 @@ class Memory(object):
|
|||
|
||||
def done(__):
|
||||
log.debug(
|
||||
_(u"Personal data (%(ns)s, %(key)s) has been successfuly encrypted")
|
||||
_("Personal data (%(ns)s, %(key)s) has been successfuly encrypted")
|
||||
% {"ns": C.MEMORY_CRYPTO_NAMESPACE, "key": data_key}
|
||||
)
|
||||
|
||||
|
|
@ -1225,21 +1225,21 @@ class Memory(object):
|
|||
# the owner has all rights
|
||||
return
|
||||
if not C.ACCESS_PERMS.issuperset(perms_to_check):
|
||||
raise exceptions.InternalError(_(u"invalid permission"))
|
||||
raise exceptions.InternalError(_("invalid permission"))
|
||||
|
||||
for perm in perms_to_check:
|
||||
# we check each perm and raise PermissionError as soon as one condition is not valid
|
||||
# we must never return here, we only return after the loop if nothing was blocking the access
|
||||
try:
|
||||
perm_data = file_data[u"access"][perm]
|
||||
perm_type = perm_data[u"type"]
|
||||
perm_data = file_data["access"][perm]
|
||||
perm_type = perm_data["type"]
|
||||
except KeyError:
|
||||
raise exceptions.PermissionError()
|
||||
if perm_type == C.ACCESS_TYPE_PUBLIC:
|
||||
continue
|
||||
elif perm_type == C.ACCESS_TYPE_WHITELIST:
|
||||
try:
|
||||
jids = perm_data[u"jids"]
|
||||
jids = perm_data["jids"]
|
||||
except KeyError:
|
||||
raise exceptions.PermissionError()
|
||||
if peer_jid.full() in jids:
|
||||
|
|
@ -1248,7 +1248,7 @@ class Memory(object):
|
|||
raise exceptions.PermissionError()
|
||||
else:
|
||||
raise exceptions.InternalError(
|
||||
_(u"unknown access type: {type}").format(type=perm_type)
|
||||
_("unknown access type: {type}").format(type=perm_type)
|
||||
)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
|
|
@ -1257,7 +1257,7 @@ class Memory(object):
|
|||
current = file_data
|
||||
while True:
|
||||
self.checkFilePermission(current, peer_jid, perms_to_check)
|
||||
parent = current[u"parent"]
|
||||
parent = current["parent"]
|
||||
if not parent:
|
||||
break
|
||||
files_data = yield self.getFile(
|
||||
|
|
@ -1266,7 +1266,7 @@ class Memory(object):
|
|||
try:
|
||||
current = files_data[0]
|
||||
except IndexError:
|
||||
raise exceptions.DataError(u"Missing parent")
|
||||
raise exceptions.DataError("Missing parent")
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _getParentDir(
|
||||
|
|
@ -1283,15 +1283,15 @@ class Memory(object):
|
|||
# if path is set, we have to retrieve parent directory of the file(s) from it
|
||||
if parent is not None:
|
||||
raise exceptions.ConflictError(
|
||||
_(u"You can't use path and parent at the same time")
|
||||
_("You can't use path and parent at the same time")
|
||||
)
|
||||
path_elts = filter(None, path.split(u"/"))
|
||||
if {u"..", u"."}.intersection(path_elts):
|
||||
raise ValueError(_(u'".." or "." can\'t be used in path'))
|
||||
path_elts = [_f for _f in path.split("/") if _f]
|
||||
if {"..", "."}.intersection(path_elts):
|
||||
raise ValueError(_('".." or "." can\'t be used in path'))
|
||||
|
||||
# we retrieve all directories from path until we get the parent container
|
||||
# non existing directories will be created
|
||||
parent = u""
|
||||
parent = ""
|
||||
for idx, path_elt in enumerate(path_elts):
|
||||
directories = yield self.storage.getFiles(
|
||||
client,
|
||||
|
|
@ -1306,12 +1306,12 @@ class Memory(object):
|
|||
# from this point, directories don't exist anymore, we have to create them
|
||||
elif len(directories) > 1:
|
||||
raise exceptions.InternalError(
|
||||
_(u"Several directories found, this should not happen")
|
||||
_("Several directories found, this should not happen")
|
||||
)
|
||||
else:
|
||||
directory = directories[0]
|
||||
self.checkFilePermission(directory, peer_jid, perms_to_check)
|
||||
parent = directory[u"id"]
|
||||
parent = directory["id"]
|
||||
defer.returnValue((parent, []))
|
||||
|
||||
@defer.inlineCallbacks
|
||||
|
|
@ -1357,8 +1357,8 @@ class Memory(object):
|
|||
"""
|
||||
if peer_jid is None and perms_to_check or perms_to_check is None and peer_jid:
|
||||
raise exceptions.InternalError(
|
||||
u"if you want to disable permission check, both peer_jid and "
|
||||
u"perms_to_check must be None"
|
||||
"if you want to disable permission check, both peer_jid and "
|
||||
"perms_to_check must be None"
|
||||
)
|
||||
if owner is not None:
|
||||
owner = owner.userhostJID()
|
||||
|
|
@ -1378,7 +1378,7 @@ class Memory(object):
|
|||
try:
|
||||
parent_data = parent_data[0]
|
||||
except IndexError:
|
||||
raise exceptions.DataError(u"mising parent")
|
||||
raise exceptions.DataError("mising parent")
|
||||
yield self.checkPermissionToRoot(
|
||||
client, parent_data, peer_jid, perms_to_check
|
||||
)
|
||||
|
|
@ -1414,7 +1414,7 @@ class Memory(object):
|
|||
|
||||
@defer.inlineCallbacks
|
||||
def setFile(
|
||||
self, client, name, file_id=None, version=u"", parent=None, path=None,
|
||||
self, client, name, file_id=None, version="", parent=None, path=None,
|
||||
type_=C.FILE_TYPE_FILE, file_hash=None, hash_algo=None, size=None,
|
||||
namespace=None, mime_type=None, created=None, modified=None, owner=None,
|
||||
access=None, extra=None, peer_jid=None, perms_to_check=(C.ACCESS_PERM_WRITE,)
|
||||
|
|
@ -1481,7 +1481,7 @@ class Memory(object):
|
|||
if type_ == C.FILE_TYPE_DIRECTORY:
|
||||
if any(version, file_hash, size, mime_type):
|
||||
raise ValueError(
|
||||
u"version, file_hash, size and mime_type can't be set for a directory"
|
||||
"version, file_hash, size and mime_type can't be set for a directory"
|
||||
)
|
||||
if owner is not None:
|
||||
owner = owner.userhostJID()
|
||||
|
|
@ -1498,7 +1498,7 @@ class Memory(object):
|
|||
client,
|
||||
name=new_dir,
|
||||
file_id=new_dir_id,
|
||||
version=u"",
|
||||
version="",
|
||||
parent=parent,
|
||||
type_=C.FILE_TYPE_DIRECTORY,
|
||||
namespace=namespace,
|
||||
|
|
@ -1509,7 +1509,7 @@ class Memory(object):
|
|||
)
|
||||
parent = new_dir_id
|
||||
elif parent is None:
|
||||
parent = u""
|
||||
parent = ""
|
||||
|
||||
yield self.storage.setFile(
|
||||
client,
|
||||
|
|
@ -1552,35 +1552,35 @@ class Memory(object):
|
|||
@param files_path(unicode): path of the directory containing the actual files
|
||||
@param file_data(dict): data of the file to delete
|
||||
"""
|
||||
if file_data[u'owner'] != peer_jid:
|
||||
if file_data['owner'] != peer_jid:
|
||||
raise exceptions.PermissionError(
|
||||
u"file {file_name} can't be deleted, {peer_jid} is not the owner"
|
||||
.format(file_name=file_data[u'name'], peer_jid=peer_jid.full()))
|
||||
if file_data[u'type'] == C.FILE_TYPE_DIRECTORY:
|
||||
sub_files = yield self.getFiles(client, peer_jid, parent=file_data[u'id'])
|
||||
"file {file_name} can't be deleted, {peer_jid} is not the owner"
|
||||
.format(file_name=file_data['name'], peer_jid=peer_jid.full()))
|
||||
if file_data['type'] == C.FILE_TYPE_DIRECTORY:
|
||||
sub_files = yield self.getFiles(client, peer_jid, parent=file_data['id'])
|
||||
if sub_files and not recursive:
|
||||
raise exceptions.DataError(_(u"Can't delete directory, it is not empty"))
|
||||
raise exceptions.DataError(_("Can't delete directory, it is not empty"))
|
||||
# we first delete the sub-files
|
||||
for sub_file_data in sub_files:
|
||||
yield self._deleteFile(client, peer_jid, recursive, sub_file_data)
|
||||
# then the directory itself
|
||||
yield self.storage.fileDelete(file_data[u'id'])
|
||||
elif file_data[u'type'] == C.FILE_TYPE_FILE:
|
||||
log.info(_(u"deleting file {name} with hash {file_hash}").format(
|
||||
name=file_data[u'name'], file_hash=file_data[u'file_hash']))
|
||||
yield self.storage.fileDelete(file_data[u'id'])
|
||||
yield self.storage.fileDelete(file_data['id'])
|
||||
elif file_data['type'] == C.FILE_TYPE_FILE:
|
||||
log.info(_("deleting file {name} with hash {file_hash}").format(
|
||||
name=file_data['name'], file_hash=file_data['file_hash']))
|
||||
yield self.storage.fileDelete(file_data['id'])
|
||||
references = yield self.getFiles(
|
||||
client, peer_jid, file_hash=file_data[u'file_hash'])
|
||||
client, peer_jid, file_hash=file_data['file_hash'])
|
||||
if references:
|
||||
log.debug(u"there are still references to the file, we keep it")
|
||||
log.debug("there are still references to the file, we keep it")
|
||||
else:
|
||||
file_path = os.path.join(files_path, file_data[u'file_hash'])
|
||||
log.info(_(u"no reference left to {file_path}, deleting").format(
|
||||
file_path = os.path.join(files_path, file_data['file_hash'])
|
||||
log.info(_("no reference left to {file_path}, deleting").format(
|
||||
file_path=file_path))
|
||||
os.unlink(file_path)
|
||||
else:
|
||||
raise exceptions.InternalError(u'Unexpected file type: {file_type}'
|
||||
.format(file_type=file_data[u'type']))
|
||||
raise exceptions.InternalError('Unexpected file type: {file_type}'
|
||||
.format(file_type=file_data['type']))
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def fileDelete(self, client, peer_jid, file_id, recursive=False):
|
||||
|
|
@ -1595,11 +1595,11 @@ class Memory(object):
|
|||
# should be checked too
|
||||
files_data = yield self.getFiles(client, peer_jid, file_id)
|
||||
if not files_data:
|
||||
raise exceptions.NotFound(u"Can't find the file with id {file_id}".format(
|
||||
raise exceptions.NotFound("Can't find the file with id {file_id}".format(
|
||||
file_id=file_id))
|
||||
file_data = files_data[0]
|
||||
if file_data[u"type"] != C.FILE_TYPE_DIRECTORY and recursive:
|
||||
raise ValueError(u"recursive can only be set for directories")
|
||||
if file_data["type"] != C.FILE_TYPE_DIRECTORY and recursive:
|
||||
raise ValueError("recursive can only be set for directories")
|
||||
files_path = self.host.getLocalPath(None, C.FILES_DIR, profile=False)
|
||||
yield self._deleteFile(client, peer_jid, recursive, files_path, file_data)
|
||||
|
||||
|
|
@ -1618,6 +1618,6 @@ class Memory(object):
|
|||
try:
|
||||
presence_data = self.getEntityDatum(entity_jid, "presence", client.profile)
|
||||
except KeyError:
|
||||
log.debug(u"No presence information for {}".format(entity_jid))
|
||||
log.debug("No presence information for {}".format(entity_jid))
|
||||
return False
|
||||
return presence_data.show != C.PRESENCE_UNAVAILABLE
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -56,7 +56,7 @@ class Params(object):
|
|||
|
||||
# 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"""
|
||||
default_xml = """
|
||||
<params>
|
||||
<general>
|
||||
</general>
|
||||
|
|
@ -80,20 +80,20 @@ class Params(object):
|
|||
</individual>
|
||||
</params>
|
||||
""" % {
|
||||
u"category_general": D_(u"General"),
|
||||
u"category_connection": D_(u"Connection"),
|
||||
u"history_param": C.HISTORY_LIMIT,
|
||||
u"history_label": D_(u"Chat history limit"),
|
||||
u"show_offline_contacts": C.SHOW_OFFLINE_CONTACTS,
|
||||
u"show_offline_contacts_label": D_(u"Show offline contacts"),
|
||||
u"show_empty_groups": C.SHOW_EMPTY_GROUPS,
|
||||
u"show_empty_groups_label": D_(u"Show empty groups"),
|
||||
u"force_server_param": C.FORCE_SERVER_PARAM,
|
||||
u"force_port_param": C.FORCE_PORT_PARAM,
|
||||
u"new_account_label": D_(u"Register new account"),
|
||||
u"autoconnect_label": D_(u"Connect on frontend startup"),
|
||||
u"autodisconnect_label": D_(u"Disconnect on frontend closure"),
|
||||
u"check_certificate_label": D_(u"Check certificate (don't uncheck if unsure)"),
|
||||
"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"),
|
||||
"check_certificate_label": D_("Check certificate (don't uncheck if unsure)"),
|
||||
}
|
||||
|
||||
def load_default_params(self):
|
||||
|
|
@ -158,7 +158,7 @@ class Params(object):
|
|||
del self.params[profile]
|
||||
except KeyError:
|
||||
log.error(
|
||||
_(u"Trying to purge cache of a profile not in memory: [%s]") % profile
|
||||
_("Trying to purge cache of a profile not in memory: [%s]") % profile
|
||||
)
|
||||
|
||||
def save_xml(self, filename):
|
||||
|
|
@ -238,7 +238,7 @@ class Params(object):
|
|||
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)
|
||||
log.error(_("Trying to access an unknown profile (%s)") % profile_key)
|
||||
raise exceptions.ProfileUnknownError(profile_key)
|
||||
return profile_key
|
||||
|
||||
|
|
@ -294,7 +294,7 @@ class Params(object):
|
|||
if (
|
||||
len(cat_node.childNodes) == to_remove_count
|
||||
): # remove empty category
|
||||
for __ in xrange(0, to_remove_count):
|
||||
for __ in range(0, to_remove_count):
|
||||
to_remove.pop()
|
||||
to_remove.append(cat_node)
|
||||
for node in to_remove:
|
||||
|
|
@ -333,7 +333,7 @@ class Params(object):
|
|||
if not app:
|
||||
log.warning(
|
||||
_(
|
||||
u"Trying to register frontends parameters with no specified app: aborted"
|
||||
"Trying to register frontends parameters with no specified app: aborted"
|
||||
)
|
||||
)
|
||||
return
|
||||
|
|
@ -342,14 +342,14 @@ class Params(object):
|
|||
if app in self.frontends_cache:
|
||||
log.debug(
|
||||
_(
|
||||
u"Trying to register twice frontends parameters for %(app)s: aborted"
|
||||
"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})
|
||||
log.debug("Frontends parameters registered for %(app)s" % {"app": app})
|
||||
|
||||
def __default_ok(self, value, name, category):
|
||||
# FIXME: will not work with individual parameters
|
||||
|
|
@ -357,7 +357,7 @@ class Params(object):
|
|||
|
||||
def __default_ko(self, failure, name, category):
|
||||
log.error(
|
||||
_(u"Can't determine default value for [%(category)s/%(name)s]: %(reason)s")
|
||||
_("Can't determine default value for [%(category)s/%(name)s]: %(reason)s")
|
||||
% {"category": category, "name": name, "reason": str(failure.value)}
|
||||
)
|
||||
|
||||
|
|
@ -380,7 +380,7 @@ class Params(object):
|
|||
if not node:
|
||||
log.error(
|
||||
_(
|
||||
u"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
|
||||
"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
|
||||
)
|
||||
% {"name": name, "category": category}
|
||||
)
|
||||
|
|
@ -443,14 +443,14 @@ class Params(object):
|
|||
if len(selected) == 0:
|
||||
log.error(
|
||||
_(
|
||||
u"Parameter (%(cat)s, %(param)s) of type list has no default option!"
|
||||
"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!"
|
||||
"Parameter (%(cat)s, %(param)s) of type list has more than one default option!"
|
||||
)
|
||||
% {"cat": cat, "param": param}
|
||||
)
|
||||
|
|
@ -468,7 +468,7 @@ class Params(object):
|
|||
jids[idx] = jid.JID(value)
|
||||
except (RuntimeError, jid.InvalidFormat, AttributeError):
|
||||
log.warning(
|
||||
u"Incorrect jid value found in jids list: [{}]".format(value)
|
||||
"Incorrect jid value found in jids list: [{}]".format(value)
|
||||
)
|
||||
to_delete.append(value)
|
||||
for value in to_delete:
|
||||
|
|
@ -564,7 +564,7 @@ class Params(object):
|
|||
if not node:
|
||||
log.error(
|
||||
_(
|
||||
u"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
|
||||
"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
|
||||
)
|
||||
% {"name": name, "category": category}
|
||||
)
|
||||
|
|
@ -630,7 +630,7 @@ class Params(object):
|
|||
if not node:
|
||||
log.error(
|
||||
_(
|
||||
u"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
|
||||
"Requested param [%(name)s] in category [%(category)s] doesn't exist !"
|
||||
)
|
||||
% {"name": name, "category": category}
|
||||
)
|
||||
|
|
@ -639,7 +639,7 @@ class Params(object):
|
|||
if not self.checkSecurityLimit(node[1], security_limit):
|
||||
log.warning(
|
||||
_(
|
||||
u"Trying to get parameter '%(param)s' in category '%(cat)s' without authorization!!!"
|
||||
"Trying to get parameter '%(param)s' in category '%(cat)s' without authorization!!!"
|
||||
% {"param": name, "cat": category}
|
||||
)
|
||||
)
|
||||
|
|
@ -697,7 +697,7 @@ class Params(object):
|
|||
name = param_node.getAttribute("name")
|
||||
if not name:
|
||||
log.warning(
|
||||
u"ignoring attribute without name: {}".format(
|
||||
"ignoring attribute without name: {}".format(
|
||||
param_node.toxml()
|
||||
)
|
||||
)
|
||||
|
|
@ -850,7 +850,7 @@ class Params(object):
|
|||
AttributeError,
|
||||
):
|
||||
log.warning(
|
||||
u"Incorrect jid value found in jids list: [{}]".format(
|
||||
"Incorrect jid value found in jids list: [{}]".format(
|
||||
jid_
|
||||
)
|
||||
)
|
||||
|
|
@ -982,13 +982,13 @@ class Params(object):
|
|||
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"))
|
||||
log.error(_("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)")
|
||||
_("Requesting an unknown parameter (%(category)s/%(name)s)")
|
||||
% {"category": category, "name": name}
|
||||
)
|
||||
return defer.succeed(None)
|
||||
|
|
@ -996,7 +996,7 @@ class Params(object):
|
|||
if not self.checkSecurityLimit(node[1], security_limit):
|
||||
log.warning(
|
||||
_(
|
||||
u"Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!"
|
||||
"Trying to set parameter '%(param)s' in category '%(cat)s' without authorization!!!"
|
||||
% {"param": name, "cat": category}
|
||||
)
|
||||
)
|
||||
|
|
@ -1012,7 +1012,7 @@ class Params(object):
|
|||
except ValueError:
|
||||
log.debug(
|
||||
_(
|
||||
u"Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
|
||||
"Trying to set parameter '%(param)s' in category '%(cat)s' with an non-integer value"
|
||||
% {"param": name, "cat": category}
|
||||
)
|
||||
)
|
||||
|
|
@ -1051,7 +1051,7 @@ class Params(object):
|
|||
assert profile_key != C.PROF_KEY_NONE
|
||||
|
||||
if type_ == "button":
|
||||
log.debug(u"Clicked param button %s" % node.toxml())
|
||||
log.debug("Clicked param button %s" % node.toxml())
|
||||
return defer.succeed(None)
|
||||
elif type_ == "password":
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -63,10 +63,10 @@ class PersistentDict(object):
|
|||
return d
|
||||
|
||||
def iteritems(self):
|
||||
return self._cache.iteritems()
|
||||
return iter(self._cache.items())
|
||||
|
||||
def items(self):
|
||||
return self._cache.items()
|
||||
return list(self._cache.items())
|
||||
|
||||
def __repr__(self):
|
||||
return self._cache.__repr__()
|
||||
|
|
@ -98,8 +98,8 @@ class PersistentDict(object):
|
|||
def __hash__(self):
|
||||
return self._cache.__hash__()
|
||||
|
||||
def __nonzero__(self):
|
||||
return self._cache.__len__()
|
||||
def __bool__(self):
|
||||
return self._cache.__len__() != 0
|
||||
|
||||
def __contains__(self, key):
|
||||
return self._cache.__contains__(key)
|
||||
|
|
@ -149,7 +149,7 @@ class PersistentBinaryDict(PersistentDict):
|
|||
|
||||
|
||||
class LazyPersistentBinaryDict(PersistentBinaryDict):
|
||||
ur"""PersistentBinaryDict which get key/value when needed
|
||||
r"""PersistentBinaryDict which get key/value when needed
|
||||
|
||||
This Persistent need more database access, it is suitable for largest data,
|
||||
to save memory.
|
||||
|
|
@ -160,7 +160,7 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
|
|||
|
||||
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"))
|
||||
log.warning(_("Calling load on LazyPersistentBinaryDict while it's not needed"))
|
||||
|
||||
def iteritems(self):
|
||||
raise NotImplementedError
|
||||
|
|
@ -196,9 +196,9 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
|
|||
raise NotImplementedError
|
||||
|
||||
def __hash__(self):
|
||||
return hash(unicode(self.__class__) + self.namespace + (self.profile or u''))
|
||||
return hash(str(self.__class__) + self.namespace + (self.profile or ''))
|
||||
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def __contains__(self, key):
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# SAT: a jabber client
|
||||
|
|
@ -31,7 +31,7 @@ from collections import OrderedDict
|
|||
import sys
|
||||
import re
|
||||
import os.path
|
||||
import cPickle as pickle
|
||||
import pickle as pickle
|
||||
import hashlib
|
||||
import sqlite3
|
||||
import json
|
||||
|
|
@ -152,12 +152,12 @@ class ConnectionPool(adbapi.ConnectionPool):
|
|||
# Sqlite integration, probably with high level library
|
||||
retry -= 1
|
||||
if retry == 0:
|
||||
log.error(_(u'too many db tries, we abandon! Error message: {msg}\n'
|
||||
u'query was {query}'
|
||||
.format(msg=e, query=u' '.join([unicode(a) for a in args]))))
|
||||
log.error(_('too many db tries, we abandon! Error message: {msg}\n'
|
||||
'query was {query}'
|
||||
.format(msg=e, query=' '.join([str(a) for a in args]))))
|
||||
raise e
|
||||
log.warning(
|
||||
_(u'exception while running query, retrying ({try_}): {msg}').format(
|
||||
_('exception while running query, retrying ({try_}): {msg}').format(
|
||||
try_ = 6 - retry,
|
||||
msg = e))
|
||||
kw['query_retry'] = retry
|
||||
|
|
@ -175,14 +175,14 @@ class ConnectionPool(adbapi.ConnectionPool):
|
|||
retry -= 1
|
||||
if retry == 0:
|
||||
log.error(
|
||||
_(u'too many interaction tries, we abandon! Error message: {msg}\n'
|
||||
u'interaction method was: {interaction}\n'
|
||||
u'interaction arguments were: {args}'
|
||||
_('too many interaction tries, we abandon! Error message: {msg}\n'
|
||||
'interaction method was: {interaction}\n'
|
||||
'interaction arguments were: {args}'
|
||||
.format(msg=e, interaction=interaction,
|
||||
args=u', '.join([unicode(a) for a in args]))))
|
||||
args=', '.join([str(a) for a in args]))))
|
||||
raise e
|
||||
log.warning(
|
||||
_(u'exception while running interaction, retrying ({try_}): {msg}')
|
||||
_('exception while running interaction, retrying ({try_}): {msg}')
|
||||
.format(try_ = 4 - retry, msg = e))
|
||||
kw['interaction_retry'] = retry
|
||||
return self._runInteraction(interaction, *args, **kw)
|
||||
|
|
@ -204,7 +204,7 @@ class SqliteStorage(object):
|
|||
if new_base: # the dir may not exist if it's not the XDG recommended one
|
||||
dir_ = os.path.dirname(db_filename)
|
||||
if not os.path.exists(dir_):
|
||||
os.makedirs(dir_, 0700)
|
||||
os.makedirs(dir_, 0o700)
|
||||
|
||||
def foreignKeysOn(sqlite):
|
||||
sqlite.execute('PRAGMA foreign_keys = ON')
|
||||
|
|
@ -240,7 +240,7 @@ class SqliteStorage(object):
|
|||
|
||||
if statements is None:
|
||||
return defer.succeed(None)
|
||||
log.debug(u"\n===== COMMITTING STATEMENTS =====\n%s\n============\n\n" % '\n'.join(statements))
|
||||
log.debug("\n===== COMMITTING STATEMENTS =====\n%s\n============\n\n" % '\n'.join(statements))
|
||||
d = self.dbpool.runInteraction(self._updateDb, tuple(statements))
|
||||
return d
|
||||
|
||||
|
|
@ -270,7 +270,7 @@ class SqliteStorage(object):
|
|||
|
||||
def getProfilesList(self):
|
||||
""""Return list of all registered profiles"""
|
||||
return self.profiles.keys()
|
||||
return list(self.profiles.keys())
|
||||
|
||||
def hasProfile(self, profile_name):
|
||||
"""return True if profile_name exists
|
||||
|
|
@ -283,13 +283,13 @@ class SqliteStorage(object):
|
|||
try:
|
||||
return self.profiles[profile_name] in self.components
|
||||
except KeyError:
|
||||
raise exceptions.NotFound(u"the requested profile doesn't exists")
|
||||
raise exceptions.NotFound("the requested profile doesn't exists")
|
||||
|
||||
def getEntryPoint(self, profile_name):
|
||||
try:
|
||||
return self.components[self.profiles[profile_name]]
|
||||
except KeyError:
|
||||
raise exceptions.NotFound(u"the requested profile doesn't exists or is not a component")
|
||||
raise exceptions.NotFound("the requested profile doesn't exists or is not a component")
|
||||
|
||||
def createProfile(self, name, component=None):
|
||||
"""Create a new profile
|
||||
|
|
@ -326,7 +326,7 @@ class SqliteStorage(object):
|
|||
@return: deferred triggered once profile is actually deleted
|
||||
"""
|
||||
def deletionError(failure_):
|
||||
log.error(_(u"Can't delete profile [%s]") % name)
|
||||
log.error(_("Can't delete profile [%s]") % name)
|
||||
return failure_
|
||||
|
||||
def delete(txn):
|
||||
|
|
@ -359,7 +359,7 @@ class SqliteStorage(object):
|
|||
for param in result:
|
||||
category, name, value = param
|
||||
params_gen[(category, name)] = value
|
||||
log.debug(_(u"loading general parameters from database"))
|
||||
log.debug(_("loading general parameters from database"))
|
||||
return self.dbpool.runQuery("SELECT category,name,value FROM param_gen").addCallback(fillParams)
|
||||
|
||||
def loadIndParams(self, params_ind, profile):
|
||||
|
|
@ -374,7 +374,7 @@ class SqliteStorage(object):
|
|||
for param in result:
|
||||
category, name, value = param
|
||||
params_ind[(category, name)] = value
|
||||
log.debug(_(u"loading individual parameters from database"))
|
||||
log.debug(_("loading individual parameters from database"))
|
||||
d = self.dbpool.runQuery("SELECT category,name,value FROM param_ind WHERE profile_id=?", (self.profiles[profile], ))
|
||||
d.addCallback(fillParams)
|
||||
return d
|
||||
|
|
@ -399,7 +399,7 @@ class SqliteStorage(object):
|
|||
@param value: value to set
|
||||
@return: deferred"""
|
||||
d = self.dbpool.runQuery("REPLACE INTO param_gen(category,name,value) VALUES (?,?,?)", (category, name, value))
|
||||
d.addErrback(lambda ignore: log.error(_(u"Can't set general parameter (%(category)s/%(name)s) in database" % {"category": category, "name": name})))
|
||||
d.addErrback(lambda ignore: log.error(_("Can't set general parameter (%(category)s/%(name)s) in database" % {"category": category, "name": name})))
|
||||
return d
|
||||
|
||||
def setIndParam(self, category, name, value, profile):
|
||||
|
|
@ -412,7 +412,7 @@ class SqliteStorage(object):
|
|||
@return: deferred
|
||||
"""
|
||||
d = self.dbpool.runQuery("REPLACE INTO param_ind(category,name,profile_id,value) VALUES (?,?,?,?)", (category, name, self.profiles[profile], value))
|
||||
d.addErrback(lambda ignore: log.error(_(u"Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category": category, "name": name, "profile": profile})))
|
||||
d.addErrback(lambda ignore: log.error(_("Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category": category, "name": name, "profile": profile})))
|
||||
return d
|
||||
|
||||
## History
|
||||
|
|
@ -423,14 +423,14 @@ class SqliteStorage(object):
|
|||
uid = data['uid']
|
||||
d_list = []
|
||||
for key in ('message', 'subject'):
|
||||
for lang, value in data[key].iteritems():
|
||||
for lang, value in data[key].items():
|
||||
d = self.dbpool.runQuery(
|
||||
"INSERT INTO {key}(history_uid, {key}, language) VALUES (?,?,?)"
|
||||
.format(key=key),
|
||||
(uid, value, lang or None))
|
||||
d.addErrback(lambda __: log.error(
|
||||
_(u"Can't save following {key} in history (uid: {uid}, lang:{lang}):"
|
||||
u" {value}").format(
|
||||
_("Can't save following {key} in history (uid: {uid}, lang:{lang}):"
|
||||
" {value}").format(
|
||||
key=key, uid=uid, lang=lang, value=value)))
|
||||
d_list.append(d)
|
||||
try:
|
||||
|
|
@ -443,8 +443,8 @@ class SqliteStorage(object):
|
|||
"INSERT INTO thread(history_uid, thread_id, parent_id) VALUES (?,?,?)",
|
||||
(uid, thread, thread_parent))
|
||||
d.addErrback(lambda __: log.error(
|
||||
_(u"Can't save following thread in history (uid: {uid}): thread: "
|
||||
u"{thread}), parent:{parent}").format(
|
||||
_("Can't save following thread in history (uid: {uid}): thread: "
|
||||
"{thread}), parent:{parent}").format(
|
||||
uid=uid, thread=thread, parent=thread_parent)))
|
||||
d_list.append(d)
|
||||
return defer.DeferredList(d_list)
|
||||
|
|
@ -453,24 +453,24 @@ class SqliteStorage(object):
|
|||
failure_.trap(sqlite3.IntegrityError)
|
||||
sqlite_msg = failure_.value.args[0]
|
||||
if "UNIQUE constraint failed" in sqlite_msg:
|
||||
log.debug(u"message {} is already in history, not storing it again"
|
||||
log.debug("message {} is already in history, not storing it again"
|
||||
.format(data['uid']))
|
||||
if 'received_timestamp' not in data:
|
||||
log.warning(
|
||||
u"duplicate message is not delayed, this is maybe a bug: data={}"
|
||||
"duplicate message is not delayed, this is maybe a bug: data={}"
|
||||
.format(data))
|
||||
# we cancel message to avoid sending duplicate message to frontends
|
||||
raise failure.Failure(exceptions.CancelError("Cancelled duplicated message"))
|
||||
else:
|
||||
log.error(u"Can't store message in history: {}".format(failure_))
|
||||
log.error("Can't store message in history: {}".format(failure_))
|
||||
|
||||
def _logHistoryError(self, failure_, from_jid, to_jid, data):
|
||||
if failure_.check(exceptions.CancelError):
|
||||
# we propagate CancelError to avoid sending message to frontends
|
||||
raise failure_
|
||||
log.error(_(
|
||||
u"Can't save following message in history: from [{from_jid}] to [{to_jid}] "
|
||||
u"(uid: {uid})")
|
||||
"Can't save following message in history: from [{from_jid}] to [{to_jid}] "
|
||||
"(uid: {uid})")
|
||||
.format(from_jid=from_jid.full(), to_jid=to_jid.full(), uid=data['uid']))
|
||||
|
||||
def addToHistory(self, data, profile):
|
||||
|
|
@ -478,14 +478,14 @@ class SqliteStorage(object):
|
|||
|
||||
@param data(dict): message data as build by SatMessageProtocol.onMessage
|
||||
"""
|
||||
extra = pickle.dumps({k: v for k, v in data['extra'].iteritems()
|
||||
extra = pickle.dumps({k: v for k, v in data['extra'].items()
|
||||
if k not in NOT_IN_EXTRA}, 0)
|
||||
from_jid = data['from']
|
||||
to_jid = data['to']
|
||||
d = self.dbpool.runQuery(
|
||||
u"INSERT INTO history(uid, stanza_id, update_uid, profile_id, source, dest, "
|
||||
u"source_res, dest_res, timestamp, received_timestamp, type, extra) VALUES "
|
||||
u"(?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||
"INSERT INTO history(uid, stanza_id, update_uid, profile_id, source, dest, "
|
||||
"source_res, dest_res, timestamp, received_timestamp, type, extra) VALUES "
|
||||
"(?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||
(data['uid'], data['extra'].get('stanza_id'), data['extra'].get('update_uid'),
|
||||
self.profiles[profile], data['from'].userhost(), to_jid.userhost(),
|
||||
from_jid.resource, to_jid.resource, data['timestamp'],
|
||||
|
|
@ -508,7 +508,7 @@ class SqliteStorage(object):
|
|||
if uid != current['uid']:
|
||||
# new message
|
||||
try:
|
||||
extra = pickle.loads(str(extra or ""))
|
||||
extra = pickle.loads(extra or b"")
|
||||
except EOFError:
|
||||
extra = {}
|
||||
current = {
|
||||
|
|
@ -543,8 +543,8 @@ class SqliteStorage(object):
|
|||
else:
|
||||
if thread_parent is not None:
|
||||
log.error(
|
||||
u"Database inconsistency: thread parent without thread (uid: "
|
||||
u"{uid}, thread_parent: {parent})"
|
||||
"Database inconsistency: thread parent without thread (uid: "
|
||||
"{uid}, thread_parent: {parent})"
|
||||
.format(uid=uid, parent=thread_parent))
|
||||
|
||||
return result
|
||||
|
|
@ -575,7 +575,7 @@ class SqliteStorage(object):
|
|||
if limit == 0:
|
||||
return defer.succeed([])
|
||||
|
||||
query_parts = [u"SELECT uid, stanza_id, update_uid, source, dest, source_res, dest_res, timestamp, received_timestamp,\
|
||||
query_parts = ["SELECT uid, stanza_id, update_uid, source, dest, source_res, dest_res, timestamp, received_timestamp,\
|
||||
type, extra, message, message.language, subject, subject.language, thread_id, thread.parent_id\
|
||||
FROM history LEFT JOIN message ON history.uid = message.history_uid\
|
||||
LEFT JOIN subject ON history.uid=subject.history_uid\
|
||||
|
|
@ -587,8 +587,8 @@ class SqliteStorage(object):
|
|||
values.append(jid_.userhost())
|
||||
if jid_.resource:
|
||||
values.append(jid_.resource)
|
||||
return u'({type_}=? AND {type_}_res=?)'.format(type_=type_)
|
||||
return u'{type_}=?'.format(type_=type_)
|
||||
return '({type_}=? AND {type_}_res=?)'.format(type_=type_)
|
||||
return '{type_}=?'.format(type_=type_)
|
||||
|
||||
if not from_jid and not to_jid:
|
||||
# not jid specified, we want all one2one communications
|
||||
|
|
@ -598,15 +598,15 @@ class SqliteStorage(object):
|
|||
# we only have one jid specified, we check all messages
|
||||
# from or to this jid
|
||||
jid_ = from_jid or to_jid
|
||||
query_parts.append(u"AND ({source} OR {dest})".format(
|
||||
source=test_jid(u'source', jid_),
|
||||
dest=test_jid(u'dest' , jid_)))
|
||||
query_parts.append("AND ({source} OR {dest})".format(
|
||||
source=test_jid('source', jid_),
|
||||
dest=test_jid('dest' , jid_)))
|
||||
else:
|
||||
# we have 2 jids specified, we check all communications between
|
||||
# those 2 jids
|
||||
query_parts.append(
|
||||
u"AND (({source_from} AND {dest_to}) "
|
||||
u"OR ({source_to} AND {dest_from}))".format(
|
||||
"AND (({source_from} AND {dest_to}) "
|
||||
"OR ({source_to} AND {dest_from}))".format(
|
||||
source_from=test_jid('source', from_jid),
|
||||
dest_to=test_jid('dest', to_jid),
|
||||
source_to=test_jid('source', to_jid),
|
||||
|
|
@ -619,47 +619,47 @@ class SqliteStorage(object):
|
|||
q.append(test_jid('source', from_jid))
|
||||
if to_jid is not None:
|
||||
q.append(test_jid('dest', to_jid))
|
||||
query_parts.append(u"AND " + u" AND ".join(q))
|
||||
query_parts.append("AND " + " AND ".join(q))
|
||||
|
||||
if filters:
|
||||
if u'timestamp_start' in filters:
|
||||
query_parts.append(u"AND timestamp>= ?")
|
||||
values.append(float(filters[u'timestamp_start']))
|
||||
if u'body' in filters:
|
||||
if 'timestamp_start' in filters:
|
||||
query_parts.append("AND timestamp>= ?")
|
||||
values.append(float(filters['timestamp_start']))
|
||||
if 'body' in filters:
|
||||
# TODO: use REGEXP (function to be defined) instead of GLOB: https://www.sqlite.org/lang_expr.html
|
||||
query_parts.append(u"AND message LIKE ?")
|
||||
values.append(u"%{}%".format(filters['body']))
|
||||
if u'search' in filters:
|
||||
query_parts.append(u"AND (message LIKE ? OR source_res LIKE ?)")
|
||||
values.extend([u"%{}%".format(filters['search'])] * 2)
|
||||
if u'types' in filters:
|
||||
query_parts.append("AND message LIKE ?")
|
||||
values.append("%{}%".format(filters['body']))
|
||||
if 'search' in filters:
|
||||
query_parts.append("AND (message LIKE ? OR source_res LIKE ?)")
|
||||
values.extend(["%{}%".format(filters['search'])] * 2)
|
||||
if 'types' in filters:
|
||||
types = filters['types'].split()
|
||||
query_parts.append(u"AND type IN ({})".format(u','.join("?"*len(types))))
|
||||
query_parts.append("AND type IN ({})".format(','.join("?"*len(types))))
|
||||
values.extend(types)
|
||||
if u'not_types' in filters:
|
||||
if 'not_types' in filters:
|
||||
types = filters['not_types'].split()
|
||||
query_parts.append(u"AND type NOT IN ({})".format(u','.join("?"*len(types))))
|
||||
query_parts.append("AND type NOT IN ({})".format(','.join("?"*len(types))))
|
||||
values.extend(types)
|
||||
if u'last_stanza_id' in filters:
|
||||
if 'last_stanza_id' in filters:
|
||||
# this request get the last message with a "stanza_id" that we
|
||||
# have in history. This is mainly used to retrieve messages sent
|
||||
# while we were offline, using MAM (XEP-0313).
|
||||
if (filters[u'last_stanza_id'] is not True
|
||||
if (filters['last_stanza_id'] is not True
|
||||
or limit != 1):
|
||||
raise ValueError(u"Unexpected values for last_stanza_id filter")
|
||||
query_parts.append(u"AND stanza_id IS NOT NULL")
|
||||
raise ValueError("Unexpected values for last_stanza_id filter")
|
||||
query_parts.append("AND stanza_id IS NOT NULL")
|
||||
|
||||
|
||||
# timestamp may be identical for 2 close messages (specially when delay is
|
||||
# used) that's why we order ties by received_timestamp
|
||||
# We'll reverse the order in sqliteHistoryToList
|
||||
# we use DESC here so LIMIT keep the last messages
|
||||
query_parts.append(u"ORDER BY timestamp DESC, history.received_timestamp DESC")
|
||||
query_parts.append("ORDER BY timestamp DESC, history.received_timestamp DESC")
|
||||
if limit is not None:
|
||||
query_parts.append(u"LIMIT ?")
|
||||
query_parts.append("LIMIT ?")
|
||||
values.append(limit)
|
||||
|
||||
d = self.dbpool.runQuery(u" ".join(query_parts), values)
|
||||
d = self.dbpool.runQuery(" ".join(query_parts), values)
|
||||
d.addCallback(self.sqliteHistoryToList)
|
||||
d.addCallback(self.listDict2listTuple)
|
||||
return d
|
||||
|
|
@ -668,32 +668,41 @@ class SqliteStorage(object):
|
|||
|
||||
def _privateDataEb(self, failure_, operation, namespace, key=None, profile=None):
|
||||
"""generic errback for data queries"""
|
||||
log.error(_(u"Can't {operation} data in database for namespace {namespace}{and_key}{for_profile}: {msg}").format(
|
||||
log.error(_("Can't {operation} data in database for namespace {namespace}{and_key}{for_profile}: {msg}").format(
|
||||
operation = operation,
|
||||
namespace = namespace,
|
||||
and_key = (u" and key " + key) if key is not None else u"",
|
||||
for_profile = (u' [' + profile + u']') if profile is not None else u'',
|
||||
and_key = (" and key " + key) if key is not None else "",
|
||||
for_profile = (' [' + profile + ']') if profile is not None else '',
|
||||
msg = failure_))
|
||||
|
||||
def _load_pickle(self, v):
|
||||
# FIXME: workaround for Python 3 port, some pickled data are byte while other are strings
|
||||
try:
|
||||
return pickle.loads(v)
|
||||
except TypeError:
|
||||
data = pickle.loads(v.encode('utf-8'))
|
||||
log.warning(f"encoding issue in pickled data: {data}")
|
||||
return data
|
||||
|
||||
def _generateDataDict(self, query_result, binary):
|
||||
if binary:
|
||||
return {k: pickle.loads(str(v)) for k,v in query_result}
|
||||
return {k: self._load_pickle(v) for k,v in query_result}
|
||||
else:
|
||||
return dict(query_result)
|
||||
|
||||
def _getPrivateTable(self, binary, profile):
|
||||
"""Get table to use for private values"""
|
||||
table = [u'private']
|
||||
table = ['private']
|
||||
|
||||
if profile is None:
|
||||
table.append(u'gen')
|
||||
table.append('gen')
|
||||
else:
|
||||
table.append(u'ind')
|
||||
table.append('ind')
|
||||
|
||||
if binary:
|
||||
table.append(u'bin')
|
||||
table.append('bin')
|
||||
|
||||
return u'_'.join(table)
|
||||
return '_'.join(table)
|
||||
|
||||
def getPrivates(self, namespace, keys=None, binary=False, profile=None):
|
||||
"""Get private value(s) from databases
|
||||
|
|
@ -706,27 +715,27 @@ class SqliteStorage(object):
|
|||
None to use general values
|
||||
@return (dict[unicode, object]): gotten keys/values
|
||||
"""
|
||||
log.debug(_(u"getting {type}{binary} private values from database for namespace {namespace}{keys}".format(
|
||||
type = u"general" if profile is None else "individual",
|
||||
binary = u" binary" if binary else u"",
|
||||
log.debug(_("getting {type}{binary} private values from database for namespace {namespace}{keys}".format(
|
||||
type = "general" if profile is None else "individual",
|
||||
binary = " binary" if binary else "",
|
||||
namespace = namespace,
|
||||
keys = u" with keys {}".format(u", ".join(keys)) if keys is not None else u"")))
|
||||
keys = " with keys {}".format(", ".join(keys)) if keys is not None else "")))
|
||||
table = self._getPrivateTable(binary, profile)
|
||||
query_parts = [u"SELECT key,value FROM", table, "WHERE namespace=?"]
|
||||
query_parts = ["SELECT key,value FROM", table, "WHERE namespace=?"]
|
||||
args = [namespace]
|
||||
|
||||
if keys is not None:
|
||||
placeholders = u','.join(len(keys) * u'?')
|
||||
query_parts.append(u'AND key IN (' + placeholders + u')')
|
||||
placeholders = ','.join(len(keys) * '?')
|
||||
query_parts.append('AND key IN (' + placeholders + ')')
|
||||
args.extend(keys)
|
||||
|
||||
if profile is not None:
|
||||
query_parts.append(u'AND profile_id=?')
|
||||
query_parts.append('AND profile_id=?')
|
||||
args.append(self.profiles[profile])
|
||||
|
||||
d = self.dbpool.runQuery(u" ".join(query_parts), args)
|
||||
d = self.dbpool.runQuery(" ".join(query_parts), args)
|
||||
d.addCallback(self._generateDataDict, binary)
|
||||
d.addErrback(self._privateDataEb, u"get", namespace, profile=profile)
|
||||
d.addErrback(self._privateDataEb, "get", namespace, profile=profile)
|
||||
return d
|
||||
|
||||
def setPrivateValue(self, namespace, key, value, binary=False, profile=None):
|
||||
|
|
@ -741,7 +750,7 @@ class SqliteStorage(object):
|
|||
if None, it's a general value
|
||||
"""
|
||||
table = self._getPrivateTable(binary, profile)
|
||||
query_values_names = [u'namespace', u'key', u'value']
|
||||
query_values_names = ['namespace', 'key', 'value']
|
||||
query_values = [namespace, key]
|
||||
|
||||
if binary:
|
||||
|
|
@ -750,14 +759,14 @@ class SqliteStorage(object):
|
|||
query_values.append(value)
|
||||
|
||||
if profile is not None:
|
||||
query_values_names.append(u'profile_id')
|
||||
query_values_names.append('profile_id')
|
||||
query_values.append(self.profiles[profile])
|
||||
|
||||
query_parts = [u"REPLACE INTO", table, u'(', u','.join(query_values_names), u')',
|
||||
u"VALUES (", u",".join(u'?'*len(query_values_names)), u')']
|
||||
query_parts = ["REPLACE INTO", table, '(', ','.join(query_values_names), ')',
|
||||
"VALUES (", ",".join('?'*len(query_values_names)), ')']
|
||||
|
||||
d = self.dbpool.runQuery(u" ".join(query_parts), query_values)
|
||||
d.addErrback(self._privateDataEb, u"set", namespace, key, profile=profile)
|
||||
d = self.dbpool.runQuery(" ".join(query_parts), query_values)
|
||||
d.addErrback(self._privateDataEb, "set", namespace, key, profile=profile)
|
||||
return d
|
||||
|
||||
def delPrivateValue(self, namespace, key, binary=False, profile=None):
|
||||
|
|
@ -770,13 +779,13 @@ class SqliteStorage(object):
|
|||
if None, it's a general value
|
||||
"""
|
||||
table = self._getPrivateTable(binary, profile)
|
||||
query_parts = [u"DELETE FROM", table, u"WHERE namespace=? AND key=?"]
|
||||
query_parts = ["DELETE FROM", table, "WHERE namespace=? AND key=?"]
|
||||
args = [namespace, key]
|
||||
if profile is not None:
|
||||
query_parts.append(u"AND profile_id=?")
|
||||
query_parts.append("AND profile_id=?")
|
||||
args.append(self.profiles[profile])
|
||||
d = self.dbpool.runQuery(u" ".join(query_parts), args)
|
||||
d.addErrback(self._privateDataEb, u"delete", namespace, key, profile=profile)
|
||||
d = self.dbpool.runQuery(" ".join(query_parts), args)
|
||||
d.addErrback(self._privateDataEb, "delete", namespace, key, profile=profile)
|
||||
return d
|
||||
|
||||
def delPrivateNamespace(self, namespace, binary=False, profile=None):
|
||||
|
|
@ -787,19 +796,19 @@ class SqliteStorage(object):
|
|||
Params are the same as for delPrivateValue
|
||||
"""
|
||||
table = self._getPrivateTable(binary, profile)
|
||||
query_parts = [u"DELETE FROM", table, u"WHERE namespace=?"]
|
||||
query_parts = ["DELETE FROM", table, "WHERE namespace=?"]
|
||||
args = [namespace]
|
||||
if profile is not None:
|
||||
query_parts.append(u"AND profile_id=?")
|
||||
query_parts.append("AND profile_id=?")
|
||||
args.append(self.profiles[profile])
|
||||
d = self.dbpool.runQuery(u" ".join(query_parts), args)
|
||||
d.addErrback(self._privateDataEb, u"delete namespace", namespace, profile=profile)
|
||||
d = self.dbpool.runQuery(" ".join(query_parts), args)
|
||||
d.addErrback(self._privateDataEb, "delete namespace", namespace, profile=profile)
|
||||
return d
|
||||
|
||||
## Files
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def getFiles(self, client, file_id=None, version=u'', parent=None, type_=None,
|
||||
def getFiles(self, client, file_id=None, version='', parent=None, type_=None,
|
||||
file_hash=None, hash_algo=None, name=None, namespace=None, mime_type=None,
|
||||
owner=None, access=None, projection=None, unique=False):
|
||||
"""retrieve files with with given filters
|
||||
|
|
@ -831,45 +840,45 @@ class SqliteStorage(object):
|
|||
args = [self.profiles[client.profile]]
|
||||
|
||||
if file_id is not None:
|
||||
filters.append(u'id=?')
|
||||
filters.append('id=?')
|
||||
args.append(file_id)
|
||||
if version is not None:
|
||||
filters.append(u'version=?')
|
||||
filters.append('version=?')
|
||||
args.append(version)
|
||||
if parent is not None:
|
||||
filters.append(u'parent=?')
|
||||
filters.append('parent=?')
|
||||
args.append(parent)
|
||||
if type_ is not None:
|
||||
filters.append(u'type=?')
|
||||
filters.append('type=?')
|
||||
args.append(type_)
|
||||
if file_hash is not None:
|
||||
filters.append(u'file_hash=?')
|
||||
filters.append('file_hash=?')
|
||||
args.append(file_hash)
|
||||
if hash_algo is not None:
|
||||
filters.append(u'hash_algo=?')
|
||||
filters.append('hash_algo=?')
|
||||
args.append(hash_algo)
|
||||
if name is not None:
|
||||
filters.append(u'name=?')
|
||||
filters.append('name=?')
|
||||
args.append(name)
|
||||
if namespace is not None:
|
||||
filters.append(u'namespace=?')
|
||||
filters.append('namespace=?')
|
||||
args.append(namespace)
|
||||
if mime_type is not None:
|
||||
filters.append(u'mime_type=?')
|
||||
filters.append('mime_type=?')
|
||||
args.append(mime_type)
|
||||
if owner is not None:
|
||||
filters.append(u'owner=?')
|
||||
filters.append('owner=?')
|
||||
args.append(owner.full())
|
||||
if access is not None:
|
||||
raise NotImplementedError('Access check is not implemented yet')
|
||||
# a JSON comparison is needed here
|
||||
|
||||
filters = u' AND '.join(filters)
|
||||
filters = ' AND '.join(filters)
|
||||
query_parts.append(filters)
|
||||
query = u' '.join(query_parts)
|
||||
query = ' '.join(query_parts)
|
||||
|
||||
result = yield self.dbpool.runQuery(query, args)
|
||||
files_data = [dict(zip(projection, row)) for row in result]
|
||||
files_data = [dict(list(zip(projection, row))) for row in result]
|
||||
to_parse = {'access', 'extra'}.intersection(projection)
|
||||
to_filter = {'owner'}.intersection(projection)
|
||||
if to_parse or to_filter:
|
||||
|
|
@ -882,7 +891,7 @@ class SqliteStorage(object):
|
|||
file_data['owner'] = jid.JID(owner)
|
||||
defer.returnValue(files_data)
|
||||
|
||||
def setFile(self, client, name, file_id, version=u'', parent=None, type_=C.FILE_TYPE_FILE,
|
||||
def setFile(self, client, name, file_id, version='', parent=None, type_=C.FILE_TYPE_FILE,
|
||||
file_hash=None, hash_algo=None, size=None, namespace=None, mime_type=None,
|
||||
created=None, modified=None, owner=None, access=None, extra=None):
|
||||
"""set a file metadata
|
||||
|
|
@ -921,12 +930,12 @@ class SqliteStorage(object):
|
|||
json.dumps(access) if access else None,
|
||||
json.dumps(extra) if extra else None,
|
||||
self.profiles[client.profile]))
|
||||
d.addErrback(lambda failure: log.error(_(u"Can't save file metadata for [{profile}]: {reason}".format(profile=client.profile, reason=failure))))
|
||||
d.addErrback(lambda failure: log.error(_("Can't save file metadata for [{profile}]: {reason}".format(profile=client.profile, reason=failure))))
|
||||
return d
|
||||
|
||||
def _fileUpdate(self, cursor, file_id, column, update_cb):
|
||||
query = 'SELECT {column} FROM files where id=?'.format(column=column)
|
||||
for i in xrange(5):
|
||||
for i in range(5):
|
||||
cursor.execute(query, [file_id])
|
||||
try:
|
||||
older_value_raw = cursor.fetchone()[0]
|
||||
|
|
@ -951,9 +960,9 @@ class SqliteStorage(object):
|
|||
else:
|
||||
if cursor.rowcount == 1:
|
||||
break;
|
||||
log.warning(_(u"table not updated, probably due to race condition, trying again ({tries})").format(tries=i+1))
|
||||
log.warning(_("table not updated, probably due to race condition, trying again ({tries})").format(tries=i+1))
|
||||
else:
|
||||
log.error(_(u"Can't update file table"))
|
||||
log.error(_("Can't update file table"))
|
||||
|
||||
def fileUpdate(self, file_id, column, update_cb):
|
||||
"""Update a column value using a method to avoid race conditions
|
||||
|
|
@ -1072,17 +1081,17 @@ class Updater(object):
|
|||
update_raw = yield self.update2raw(update_data, True)
|
||||
defer.returnValue(update_raw)
|
||||
else:
|
||||
log.error(_(u"schema version is up-to-date, but local schema differ from expected current schema"))
|
||||
log.error(_("schema version is up-to-date, but local schema differ from expected current schema"))
|
||||
update_data = self.generateUpdateData(local_sch, current_sch, True)
|
||||
update_raw = yield self.update2raw(update_data)
|
||||
log.warning(_(u"Here are the commands that should fix the situation, use at your own risk (do a backup before modifying database), you can go to SàT's MUC room at sat@chat.jabberfr.org for help\n### SQL###\n%s\n### END SQL ###\n") % u'\n'.join("%s;" % statement for statement in update_raw))
|
||||
log.warning(_("Here are the commands that should fix the situation, use at your own risk (do a backup before modifying database), you can go to SàT's MUC room at sat@chat.jabberfr.org for help\n### SQL###\n%s\n### END SQL ###\n") % '\n'.join("%s;" % statement for statement in update_raw))
|
||||
raise exceptions.DatabaseError("Database mismatch")
|
||||
else:
|
||||
if local_version > CURRENT_DB_VERSION:
|
||||
log.error(_(
|
||||
u"You database version is higher than the one used in this SàT "
|
||||
u"version, are you using several version at the same time? We "
|
||||
u"can't run SàT with this database."))
|
||||
"You database version is higher than the one used in this SàT "
|
||||
"version, are you using several version at the same time? We "
|
||||
"can't run SàT with this database."))
|
||||
sys.exit(1)
|
||||
|
||||
# Database is not up-to-date, we'll do the update
|
||||
|
|
@ -1091,7 +1100,7 @@ class Updater(object):
|
|||
else:
|
||||
log.info(_("Database schema has changed, local database will be updated"))
|
||||
update_raw = []
|
||||
for version in xrange(local_version + 1, CURRENT_DB_VERSION + 1):
|
||||
for version in range(local_version + 1, CURRENT_DB_VERSION + 1):
|
||||
try:
|
||||
update_data = DATABASE_SCHEMAS[version]
|
||||
except KeyError:
|
||||
|
|
@ -1150,17 +1159,17 @@ class Updater(object):
|
|||
ret = []
|
||||
assert isinstance(data, tuple)
|
||||
for table, col_data in data:
|
||||
assert isinstance(table, basestring)
|
||||
assert isinstance(table, str)
|
||||
assert isinstance(col_data, tuple)
|
||||
for cols in col_data:
|
||||
if isinstance(cols, tuple):
|
||||
assert all([isinstance(c, basestring) for c in cols])
|
||||
indexed_cols = u','.join(cols)
|
||||
elif isinstance(cols, basestring):
|
||||
assert all([isinstance(c, str) for c in cols])
|
||||
indexed_cols = ','.join(cols)
|
||||
elif isinstance(cols, str):
|
||||
indexed_cols = cols
|
||||
else:
|
||||
raise exceptions.InternalError(u"unexpected index columns value")
|
||||
index_name = table + u'__' + indexed_cols.replace(u',', u'_')
|
||||
raise exceptions.InternalError("unexpected index columns value")
|
||||
index_name = table + '__' + indexed_cols.replace(',', '_')
|
||||
ret.append(Updater.INDEX_SQL % (index_name, table, indexed_cols))
|
||||
return ret
|
||||
|
||||
|
|
@ -1173,7 +1182,7 @@ class Updater(object):
|
|||
@return: hash as string
|
||||
"""
|
||||
hash_ = hashlib.sha1()
|
||||
tables = data.keys()
|
||||
tables = list(data.keys())
|
||||
tables.sort()
|
||||
|
||||
def stmnts2str(stmts):
|
||||
|
|
@ -1181,7 +1190,9 @@ class Updater(object):
|
|||
|
||||
for table in tables:
|
||||
col_defs, col_constr = data[table]
|
||||
hash_.update("%s:%s:%s" % (table, stmnts2str(col_defs), stmnts2str(col_constr)))
|
||||
hash_.update(
|
||||
("%s:%s:%s" % (table, stmnts2str(col_defs), stmnts2str(col_constr)))
|
||||
.encode('utf-8'))
|
||||
return hash_.digest()
|
||||
|
||||
def rawStatements2data(self, raw_statements):
|
||||
|
|
@ -1324,7 +1335,7 @@ class Updater(object):
|
|||
|
||||
def update_v8(self):
|
||||
"""Update database from v7 to v8 (primary keys order changes + indexes)"""
|
||||
log.info(u"Database update to v8")
|
||||
log.info("Database update to v8")
|
||||
statements = ["PRAGMA foreign_keys = OFF"]
|
||||
|
||||
# here is a copy of create and index data, we can't use "current" table
|
||||
|
|
@ -1357,11 +1368,11 @@ class Updater(object):
|
|||
schema = {table: create[table]}
|
||||
cols = [d.split()[0] for d in schema[table][0]]
|
||||
statements.extend(Updater.createData2Raw(schema))
|
||||
statements.append(u"INSERT INTO {table}({cols}) "
|
||||
u"SELECT {cols} FROM {table}_old".format(
|
||||
statements.append("INSERT INTO {table}({cols}) "
|
||||
"SELECT {cols} FROM {table}_old".format(
|
||||
table=table,
|
||||
cols=u','.join(cols)))
|
||||
statements.append(u"DROP TABLE {}_old".format(table))
|
||||
cols=','.join(cols)))
|
||||
statements.append("DROP TABLE {}_old".format(table))
|
||||
|
||||
statements.extend(Updater.indexData2Raw(index))
|
||||
statements.append("PRAGMA foreign_keys = ON")
|
||||
|
|
@ -1370,48 +1381,48 @@ class Updater(object):
|
|||
@defer.inlineCallbacks
|
||||
def update_v7(self):
|
||||
"""Update database from v6 to v7 (history unique constraint change)"""
|
||||
log.info(u"Database update to v7, this may be long depending on your history "
|
||||
u"size, please be patient.")
|
||||
log.info("Database update to v7, this may be long depending on your history "
|
||||
"size, please be patient.")
|
||||
|
||||
log.info(u"Some cleaning first")
|
||||
log.info("Some cleaning first")
|
||||
# we need to fix duplicate stanza_id, as it can result in conflicts with the new schema
|
||||
# normally database should not contain any, but better safe than sorry.
|
||||
rows = yield self.dbpool.runQuery(
|
||||
u"SELECT stanza_id, COUNT(*) as c FROM history WHERE stanza_id is not NULL "
|
||||
u"GROUP BY stanza_id HAVING c>1")
|
||||
"SELECT stanza_id, COUNT(*) as c FROM history WHERE stanza_id is not NULL "
|
||||
"GROUP BY stanza_id HAVING c>1")
|
||||
if rows:
|
||||
count = sum([r[1] for r in rows]) - len(rows)
|
||||
log.info(u"{count} duplicate stanzas found, cleaning".format(count=count))
|
||||
log.info("{count} duplicate stanzas found, cleaning".format(count=count))
|
||||
for stanza_id, count in rows:
|
||||
log.info(u"cleaning duplicate stanza {stanza_id}".format(stanza_id=stanza_id))
|
||||
log.info("cleaning duplicate stanza {stanza_id}".format(stanza_id=stanza_id))
|
||||
row_uids = yield self.dbpool.runQuery(
|
||||
"SELECT uid FROM history WHERE stanza_id = ? LIMIT ?",
|
||||
(stanza_id, count-1))
|
||||
uids = [r[0] for r in row_uids]
|
||||
yield self.dbpool.runQuery(
|
||||
"DELETE FROM history WHERE uid IN ({})".format(u",".join(u"?"*len(uids))),
|
||||
"DELETE FROM history WHERE uid IN ({})".format(",".join("?"*len(uids))),
|
||||
uids)
|
||||
|
||||
def deleteInfo(txn):
|
||||
# with foreign_keys on, the delete takes ages, so we deactivate it here
|
||||
# the time to delete info messages from history.
|
||||
txn.execute("PRAGMA foreign_keys = OFF")
|
||||
txn.execute(u"DELETE FROM message WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
u"type='info')")
|
||||
txn.execute(u"DELETE FROM subject WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
u"type='info')")
|
||||
txn.execute(u"DELETE FROM thread WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
u"type='info')")
|
||||
txn.execute(u"DELETE FROM message WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
u"type='info')")
|
||||
txn.execute(u"DELETE FROM history WHERE type='info'")
|
||||
txn.execute("DELETE FROM message WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
"type='info')")
|
||||
txn.execute("DELETE FROM subject WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
"type='info')")
|
||||
txn.execute("DELETE FROM thread WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
"type='info')")
|
||||
txn.execute("DELETE FROM message WHERE history_uid IN (SELECT uid FROM history WHERE "
|
||||
"type='info')")
|
||||
txn.execute("DELETE FROM history WHERE type='info'")
|
||||
# not sure that is is necessary to reactivate here, but in doubt…
|
||||
txn.execute("PRAGMA foreign_keys = ON")
|
||||
|
||||
log.info(u'Deleting "info" messages (this can take a while)')
|
||||
log.info('Deleting "info" messages (this can take a while)')
|
||||
yield self.dbpool.runInteraction(deleteInfo)
|
||||
|
||||
log.info(u"Cleaning done")
|
||||
log.info("Cleaning done")
|
||||
|
||||
# we have to rename table we will replace
|
||||
# tables referencing history need to be replaced to, else reference would
|
||||
|
|
@ -1423,68 +1434,68 @@ class Updater(object):
|
|||
yield self.dbpool.runQuery("ALTER TABLE thread RENAME TO thread_old")
|
||||
|
||||
# history
|
||||
query = (u"CREATE TABLE history (uid TEXT PRIMARY KEY, stanza_id TEXT, "
|
||||
u"update_uid TEXT, profile_id INTEGER, source TEXT, dest TEXT, "
|
||||
u"source_res TEXT, dest_res TEXT, timestamp DATETIME NOT NULL, "
|
||||
u"received_timestamp DATETIME, type TEXT, extra BLOB, "
|
||||
u"FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE, "
|
||||
u"FOREIGN KEY(type) REFERENCES message_types(type), "
|
||||
u"UNIQUE (profile_id, stanza_id, source, dest))")
|
||||
query = ("CREATE TABLE history (uid TEXT PRIMARY KEY, stanza_id TEXT, "
|
||||
"update_uid TEXT, profile_id INTEGER, source TEXT, dest TEXT, "
|
||||
"source_res TEXT, dest_res TEXT, timestamp DATETIME NOT NULL, "
|
||||
"received_timestamp DATETIME, type TEXT, extra BLOB, "
|
||||
"FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE, "
|
||||
"FOREIGN KEY(type) REFERENCES message_types(type), "
|
||||
"UNIQUE (profile_id, stanza_id, source, dest))")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
# message
|
||||
query = (u"CREATE TABLE message (id INTEGER PRIMARY KEY ASC, history_uid INTEGER"
|
||||
u", message TEXT, language TEXT, FOREIGN KEY(history_uid) REFERENCES "
|
||||
u"history(uid) ON DELETE CASCADE)")
|
||||
query = ("CREATE TABLE message (id INTEGER PRIMARY KEY ASC, history_uid INTEGER"
|
||||
", message TEXT, language TEXT, FOREIGN KEY(history_uid) REFERENCES "
|
||||
"history(uid) ON DELETE CASCADE)")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
# subject
|
||||
query = (u"CREATE TABLE subject (id INTEGER PRIMARY KEY ASC, history_uid INTEGER"
|
||||
u", subject TEXT, language TEXT, FOREIGN KEY(history_uid) REFERENCES "
|
||||
u"history(uid) ON DELETE CASCADE)")
|
||||
query = ("CREATE TABLE subject (id INTEGER PRIMARY KEY ASC, history_uid INTEGER"
|
||||
", subject TEXT, language TEXT, FOREIGN KEY(history_uid) REFERENCES "
|
||||
"history(uid) ON DELETE CASCADE)")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
# thread
|
||||
query = (u"CREATE TABLE thread (id INTEGER PRIMARY KEY ASC, history_uid INTEGER"
|
||||
u", thread_id TEXT, parent_id TEXT, FOREIGN KEY(history_uid) REFERENCES "
|
||||
u"history(uid) ON DELETE CASCADE)")
|
||||
query = ("CREATE TABLE thread (id INTEGER PRIMARY KEY ASC, history_uid INTEGER"
|
||||
", thread_id TEXT, parent_id TEXT, FOREIGN KEY(history_uid) REFERENCES "
|
||||
"history(uid) ON DELETE CASCADE)")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
log.info(u"Now transfering old data to new tables, please be patient.")
|
||||
log.info("Now transfering old data to new tables, please be patient.")
|
||||
|
||||
log.info(u"\nTransfering table history")
|
||||
query = (u"INSERT INTO history (uid, stanza_id, update_uid, profile_id, source, "
|
||||
u"dest, source_res, dest_res, timestamp, received_timestamp, type, extra"
|
||||
u") SELECT uid, stanza_id, update_uid, profile_id, source, dest, "
|
||||
u"source_res, dest_res, timestamp, received_timestamp, type, extra "
|
||||
u"FROM history_old")
|
||||
log.info("\nTransfering table history")
|
||||
query = ("INSERT INTO history (uid, stanza_id, update_uid, profile_id, source, "
|
||||
"dest, source_res, dest_res, timestamp, received_timestamp, type, extra"
|
||||
") SELECT uid, stanza_id, update_uid, profile_id, source, dest, "
|
||||
"source_res, dest_res, timestamp, received_timestamp, type, extra "
|
||||
"FROM history_old")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
log.info(u"\nTransfering table message")
|
||||
query = (u"INSERT INTO message (id, history_uid, message, language) SELECT id, "
|
||||
u"history_uid, message, language FROM message_old")
|
||||
log.info("\nTransfering table message")
|
||||
query = ("INSERT INTO message (id, history_uid, message, language) SELECT id, "
|
||||
"history_uid, message, language FROM message_old")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
log.info(u"\nTransfering table subject")
|
||||
query = (u"INSERT INTO subject (id, history_uid, subject, language) SELECT id, "
|
||||
u"history_uid, subject, language FROM subject_old")
|
||||
log.info("\nTransfering table subject")
|
||||
query = ("INSERT INTO subject (id, history_uid, subject, language) SELECT id, "
|
||||
"history_uid, subject, language FROM subject_old")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
log.info(u"\nTransfering table thread")
|
||||
query = (u"INSERT INTO thread (id, history_uid, thread_id, parent_id) SELECT id"
|
||||
u", history_uid, thread_id, parent_id FROM thread_old")
|
||||
log.info("\nTransfering table thread")
|
||||
query = ("INSERT INTO thread (id, history_uid, thread_id, parent_id) SELECT id"
|
||||
", history_uid, thread_id, parent_id FROM thread_old")
|
||||
yield self.dbpool.runQuery(query)
|
||||
|
||||
log.info(u"\nRemoving old tables")
|
||||
log.info("\nRemoving old tables")
|
||||
# because of foreign keys, tables referencing history_old
|
||||
# must be deleted first
|
||||
yield self.dbpool.runQuery("DROP TABLE thread_old")
|
||||
yield self.dbpool.runQuery("DROP TABLE subject_old")
|
||||
yield self.dbpool.runQuery("DROP TABLE message_old")
|
||||
yield self.dbpool.runQuery("DROP TABLE history_old")
|
||||
log.info(u"\nReducing database size (this can take a while)")
|
||||
log.info("\nReducing database size (this can take a while)")
|
||||
yield self.dbpool.runQuery("VACUUM")
|
||||
log.info(u"Database update done :)")
|
||||
log.info("Database update done :)")
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def update_v3(self):
|
||||
|
|
@ -1494,7 +1505,7 @@ class Updater(object):
|
|||
# big database for tests. If issues are happening, we can cut it
|
||||
# in smaller transactions using LIMIT and by deleting already updated
|
||||
# messages
|
||||
log.info(u"Database update to v3, this may take a while")
|
||||
log.info("Database update to v3, this may take a while")
|
||||
|
||||
# we need to fix duplicate timestamp, as it can result in conflicts with the new schema
|
||||
rows = yield self.dbpool.runQuery("SELECT timestamp, COUNT(*) as c FROM history GROUP BY timestamp HAVING c>1")
|
||||
|
|
@ -1506,10 +1517,10 @@ class Updater(object):
|
|||
for idx, (id_,) in enumerate(ids_rows):
|
||||
fixed.append(id_)
|
||||
yield self.dbpool.runQuery("UPDATE history SET timestamp=? WHERE id=?", (float(timestamp) + idx * 0.001, id_))
|
||||
log.info(u"fixed messages with ids {}".format(u', '.join([unicode(id_) for id_ in fixed])))
|
||||
log.info("fixed messages with ids {}".format(', '.join([str(id_) for id_ in fixed])))
|
||||
|
||||
def historySchema(txn):
|
||||
log.info(u"History schema update")
|
||||
log.info("History schema update")
|
||||
txn.execute("ALTER TABLE history RENAME TO tmp_sat_update")
|
||||
txn.execute("CREATE TABLE history (uid TEXT PRIMARY KEY, update_uid TEXT, profile_id INTEGER, source TEXT, dest TEXT, source_res TEXT, dest_res TEXT, timestamp DATETIME NOT NULL, received_timestamp DATETIME, type TEXT, extra BLOB, FOREIGN KEY(profile_id) REFERENCES profiles(id) ON DELETE CASCADE, FOREIGN KEY(type) REFERENCES message_types(type), UNIQUE (profile_id, timestamp, source, dest, source_res, dest_res))")
|
||||
txn.execute("INSERT INTO history (uid, profile_id, source, dest, source_res, dest_res, timestamp, type, extra) SELECT id, profile_id, source, dest, source_res, dest_res, timestamp, type, extra FROM tmp_sat_update")
|
||||
|
|
@ -1517,17 +1528,17 @@ class Updater(object):
|
|||
yield self.dbpool.runInteraction(historySchema)
|
||||
|
||||
def newTables(txn):
|
||||
log.info(u"Creating new tables")
|
||||
log.info("Creating new tables")
|
||||
txn.execute("CREATE TABLE message (id INTEGER PRIMARY KEY ASC, history_uid INTEGER, message TEXT, language TEXT, FOREIGN KEY(history_uid) REFERENCES history(uid) ON DELETE CASCADE)")
|
||||
txn.execute("CREATE TABLE thread (id INTEGER PRIMARY KEY ASC, history_uid INTEGER, thread_id TEXT, parent_id TEXT, FOREIGN KEY(history_uid) REFERENCES history(uid) ON DELETE CASCADE)")
|
||||
txn.execute("CREATE TABLE subject (id INTEGER PRIMARY KEY ASC, history_uid INTEGER, subject TEXT, language TEXT, FOREIGN KEY(history_uid) REFERENCES history(uid) ON DELETE CASCADE)")
|
||||
|
||||
yield self.dbpool.runInteraction(newTables)
|
||||
|
||||
log.info(u"inserting new message type")
|
||||
log.info("inserting new message type")
|
||||
yield self.dbpool.runQuery("INSERT INTO message_types VALUES (?)", ('info',))
|
||||
|
||||
log.info(u"messages update")
|
||||
log.info("messages update")
|
||||
rows = yield self.dbpool.runQuery("SELECT id, timestamp, message, extra FROM tmp_sat_update")
|
||||
total = len(rows)
|
||||
|
||||
|
|
@ -1545,7 +1556,7 @@ class Updater(object):
|
|||
except EOFError:
|
||||
extra = {}
|
||||
except Exception:
|
||||
log.warning(u"Can't handle extra data for message id {}, ignoring it".format(id_))
|
||||
log.warning("Can't handle extra data for message id {}, ignoring it".format(id_))
|
||||
extra = {}
|
||||
|
||||
queries.append(("INSERT INTO message(history_uid, message) VALUES (?,?)", (id_, message)))
|
||||
|
|
@ -1556,9 +1567,9 @@ class Updater(object):
|
|||
pass
|
||||
else:
|
||||
try:
|
||||
subject = subject.decode('utf-8')
|
||||
subject = subject
|
||||
except UnicodeEncodeError:
|
||||
log.warning(u"Error while decoding subject, ignoring it")
|
||||
log.warning("Error while decoding subject, ignoring it")
|
||||
del extra['subject']
|
||||
else:
|
||||
queries.append(("INSERT INTO subject(history_uid, subject) VALUES (?,?)", (id_, subject)))
|
||||
|
|
@ -1597,7 +1608,7 @@ class Updater(object):
|
|||
try:
|
||||
id_ = result[0][0]
|
||||
except IndexError:
|
||||
log.error(u"Profile of id %d is referenced in 'param_ind' but it doesn't exist!" % profile_id)
|
||||
log.error("Profile of id %d is referenced in 'param_ind' but it doesn't exist!" % profile_id)
|
||||
return defer.succeed(None)
|
||||
|
||||
sat_password = xmpp_password
|
||||
|
|
|
|||
Loading…
Reference in a new issue