def _do_store(self, stanza, expire=None): global dbpool receipt = xmlstream2.extract_receipt(stanza, 'request') if receipt: # this is indeed generated by server :) msgId = receipt['id'] else: # WARNING stanza id must be server generated msgId = stanza['id'] args = ( msgId, util.jid_to_userid(jid.JID(stanza['from'])), util.jid_to_userid(jid.JID(stanza['to'])), stanza.getAttribute('type'), stanza.toXml().encode('utf-8').decode('utf-8'), int(time.time()*1e3), expire ) # for presence we want to overwrite old requests if stanza.name == 'presence': op = 'REPLACE' else: op = 'INSERT' return dbpool.runOperation('%s INTO stanzas_%s (id, sender, recipient, type, content, timestamp, expire_timestamp) VALUES(?, ?, ?, ?, ?, ?, ?)' % (op, stanza.name, ), args)
def store(self, stanza, network, delayed=False, reuseId=None, expire=None): receipt = xmlstream2.extract_receipt(stanza, 'request') if not receipt: if reuseId is not None: _id = reuseId else: _id = util.rand_str(30, util.CHARSBOX_AZN_LOWERCASE) else: _id = receipt['id'] # cancel any previous delayed call self._cancel_pending(_id) # WARNING using deepcopy is not safe if delayed: # delay our call self._pending_offline[_id] = (reactor.callLater( self.OFFLINE_STORE_DELAY, self._store, stanza=deepcopy(stanza), network=network, _id=_id, expire=expire), stanza, (network, _id, expire)) return _id else: return self._store(deepcopy(stanza), network, _id, expire)
def _do_store(self, stanza, expire=None): global dbpool receipt = xmlstream2.extract_receipt(stanza, "request") if receipt: # this is indeed generated by server :) msgId = receipt["id"] else: # WARNING stanza id must be server generated msgId = stanza["id"] args = ( msgId, util.jid_to_userid(jid.JID(stanza["from"])), util.jid_to_userid(jid.JID(stanza["to"])), stanza.getAttribute("type"), stanza.toXml().encode("utf-8").decode("utf-8"), int(time.time() * 1e3), expire, ) # for presence we want to overwrite old requests if stanza.name == "presence": op = "REPLACE" else: op = "INSERT" return dbpool.runOperation( "%s INTO stanzas_%s (id, sender, recipient, type, content, timestamp, expire_timestamp) VALUES(?, ?, ?, ?, ?, ?, ?)" % (op, stanza.name), args, )
def store(self, stanza, network, delayed=False, reuseId=None, expire=None): receipt = xmlstream2.extract_receipt(stanza, "request") if not receipt: if reuseId is not None: _id = reuseId else: _id = util.rand_str(30, util.CHARSBOX_AZN_LOWERCASE) else: _id = receipt["id"] # cancel any previous delayed call self._cancel_pending(_id) if delayed: # delay our call self._pending_offline[_id] = ( reactor.callLater( self.OFFLINE_STORE_DELAY, self._store, stanza=stanza, network=network, _id=_id, expire=expire ), stanza, (network, _id, expire), ) return _id else: return self._store(stanza, network, _id, expire)
def _store(self, stanza, network, _id, expire): # remove ourselves from pending if not self._exiting: try: del self._pending_offline[_id] except: pass # WARNING using deepcopy is not safe from copy import deepcopy stanza = deepcopy(stanza) # if no receipt request is found, generate a unique id for the message receipt = xmlstream2.extract_receipt(stanza, "request") if not receipt: if _id: stanza["id"] = _id else: stanza["id"] = util.rand_str(30, util.CHARSBOX_AZN_LOWERCASE) # store message for bare network JID jid_to = jid.JID(stanza["to"]) # WARNING this is actually useless jid_to.host = network stanza["to"] = jid_to.userhost() # sender JID should be a network JID jid_from = jid.JID(stanza["from"]) # WARNING this is actually useless jid_from.host = network stanza["from"] = jid_from.full() try: del stanza["origin"] except KeyError: pass # safe uri for persistance stanza.uri = stanza.defaultUri = sm.C2SManager.namespace log.debug("storing offline message for %s" % (stanza["to"],)) try: d = self._do_store(stanza, expire) if self._exiting: return d except: # TODO log this import traceback traceback.print_exc() return stanza["id"]
def _store(self, stanza, network, _id, expire): # remove ourselves from pending if not self._exiting: try: del self._pending_offline[_id] except: pass # WARNING using deepcopy is not safe from copy import deepcopy stanza = deepcopy(stanza) # if no receipt request is found, generate a unique id for the message receipt = xmlstream2.extract_receipt(stanza, 'request') if not receipt: if _id: stanza['id'] = _id else: stanza['id'] = util.rand_str(30, util.CHARSBOX_AZN_LOWERCASE) # store message for bare network JID jid_to = jid.JID(stanza['to']) # WARNING this is actually useless jid_to.host = network stanza['to'] = jid_to.userhost() # sender JID should be a network JID jid_from = jid.JID(stanza['from']) # WARNING this is actually useless jid_from.host = network stanza['from'] = jid_from.full() try: del stanza['origin'] except KeyError: pass # safe uri for persistance stanza.uri = stanza.defaultUri = sm.C2SManager.namespace log.debug("storing offline message for %s" % (stanza['to'], )) try: d = self._do_store(stanza, expire) if self._exiting: return d except: # TODO log this import traceback traceback.print_exc() return stanza['id']