def send_dmail_text(self, subject, message_text): if message_text.startswith("from: "): p0 = message_text.find('\n') m_from_asymkey = rsakey.RsaKey(\ privdata=base58.decode(message_text[6:p0])) p0 += 1 else: p0 = 0 m_from_asymkey = None m_dest_ids = [] while message_text.startswith("to: ", p0): p1 = message_text.find('\n') m_dest_enc = message_text[p0+4:p1] m_dest_id, sig_bits = mutil.decode_key(m_dest_enc) m_dest_ids.append((m_dest_enc, m_dest_id, sig_bits)) p0 = p1 + 1 date = mutil.utc_datetime() if message_text[p0] == '\n': p0 += 1 message_text = message_text[p0:] storing_nodes = yield from self.send_dmail(\ m_from_asymkey, m_dest_ids, subject, date, message_text) return storing_nodes
def send_dmail(self, from_asymkey, recipients, subject, date,\ message_text): assert from_asymkey is None or type(from_asymkey) is rsakey.RsaKey assert type(recipients) is list, type(recipients) assert not date or type(date) is datetime if type(message_text) is str: message_text = message_text.encode() if not date: date = mutil.utc_datetime() dmail = Dmail() dmail.sender_pubkey = from_asymkey.asbytes() if from_asymkey else b"" dmail.subject = subject dmail.date = mutil.format_iso_datetime(date) if message_text: part = DmailPart() part.mime_type = "text/plain" part.data = message_text dmail.parts.append(part) storing_nodes =\ yield from self._send_dmail(dmail, from_asymkey, recipients) return storing_nodes
def send_dmail_text(self, subject, message_text): if message_text.startswith("from: "): p0 = message_text.find('\n') m_from_asymkey = rsakey.RsaKey(\ privdata=base58.decode(message_text[6:p0])) p0 += 1 else: p0 = 0 m_from_asymkey = None m_dest_ids = [] while message_text.startswith("to: ", p0): p1 = message_text.find('\n') m_dest_enc = message_text[p0 + 4:p1] m_dest_id, sig_bits = mutil.decode_key(m_dest_enc) m_dest_ids.append((m_dest_enc, m_dest_id, sig_bits)) p0 = p1 + 1 date = mutil.utc_datetime() if message_text[p0] == '\n': p0 += 1 message_text = message_text[p0:] storing_nodes = 0 for dest_id in m_dest_ids: storing_nodes += yield from self.send_dmail(\ m_from_asymkey, dest_id, subject, date, message_text) return storing_nodes
def dbcall(dbpeer): with self.node.db.open_session() as sess: self.node.db.lock_table(sess, Peer) dbpeer = sess.query(Peer).get(dbpeer.id) if dbpeer.connected: if log.isEnabledFor(logging.DEBUG): log.debug(\ "Peer [{}] connected to us in the mean time."\ .format(dbpeer.id)) return False dbpeer.connected = True dbpeer.last_connect_attempt = mutil.utc_datetime() sess.commit() return True
def dbcall(): with self.node.db.open_session() as sess: grace = mutil.utc_datetime() - timedelta(minutes=5) q = sess.query(Peer)\ .filter(Peer.distance == distance,\ Peer.connected == False,\ or_(Peer.last_connect_attempt == None,\ Peer.last_connect_attempt < grace))\ .order_by(func.random())\ .limit(bucket_needs) r = q.all() sess.expunge_all() return r
def send_dmail(self, from_asymkey, destination_addr, subject, date,\ message_text): assert from_asymkey is None or type(from_asymkey) is rsakey.RsaKey assert type(destination_addr) in (list, tuple, bytes, bytearray, str),\ type(destination_addr) assert not date or type(date) is datetime addr, rsite =\ yield from self.fetch_recipient_dmail_site(destination_addr) if not rsite: return False if rsite.root["ssm"] != _dh_method_name: raise DmailException("Unsupported ss method [{}]."\ .format(rsite.root["ssm"])) if type(message_text) is str: message_text = message_text.encode() if not date: date = mutil.utc_datetime() dmail = Dmail() dmail.sender_pubkey = from_asymkey.asbytes() if from_asymkey else b"" dmail.destination_addr = addr dmail.subject = subject dmail.date = mutil.format_iso_datetime(date) if message_text: part = DmailPart() part.mime_type = "text/plain" part.data = message_text dmail.parts.append(part) dmail_bytes = dmail.encode() if from_asymkey: signature = from_asymkey.calc_rsassa_pss_sig(dmail_bytes) dmail_bytes += sshtype.encodeBinary(signature) storing_nodes = yield from\ self._send_dmail(from_asymkey, rsite, dmail_bytes, signature) return storing_nodes
def dbcall(): with dispatcher.node.db.open_session() as sess: # Immediately delete messages that we sent. q = ( sess.query(DmailMessage) .filter(DmailMessage.address.has(DmailAddress.site_key == addr_site_key)) .filter(DmailMessage.hidden == True) .filter(DmailMessage.destination_dmail_key != None) ) q.delete(synchronize_session=False) # Mark messages that we received for deletion later. We can't # actually delete them until we no longer check the target_id they # came from, else we will pick them up again. q = ( sess.query(DmailMessage) .filter(DmailMessage.address.has(DmailAddress.site_key == addr_site_key)) .filter(DmailMessage.hidden == True) ) msgs = q.all() for msg in msgs: msg.tags.clear() msg.sender_dmail_key = None msg.destination_dmail_key = None msg.destination_significant_bits = None msg.subject = "" msg.date = mutil.utc_datetime() msg.parts.clear() msg.read = False msg.hidden = True msg.deleted = True sess.commit()
def _read_dmail_post(dispatcher, data): charset = dispatcher.handler.headers["Content-Type"] if charset: p0 = charset.find("charset=") if p0 > -1: p0 += 8 p1 = charset.find(" ", p0 + 8) if p1 == -1: p1 = charset.find(";", p0 + 8) if p1 > -1: charset = charset[p0:p1].strip() else: charset = charset[p0:].strip() if log.isEnabledFor(logging.DEBUG): log.debug("Form charset=[{}].".format(charset)) else: charset = "UTF-8" qs = data.decode(charset) dd = parse_qs(qs, keep_blank_values=True) if log.isEnabledFor(logging.DEBUG): log.debug("dd=[{}].".format(dd)) if not dispatcher.check_csrf_token(dd["csrf_token"][0]): return None, None dm = DmailMessage() subject = dd.get("subject") if subject: dm.subject = subject[0] else: dm.subject = "" sender_dmail_id = dd.get("sender") if sender_dmail_id: sender_dmail_id = sender_dmail_id[0] if log.isEnabledFor(logging.DEBUG): log.debug("sender_dmail_id=[{}].".format(sender_dmail_id)) if sender_dmail_id and sender_dmail_id != "": sender_dmail_id = int(sender_dmail_id) dmail_address = yield from _load_dmail_address(dispatcher, sender_dmail_id) dm.address = dmail_address dm.sender_valid = True dm.sender_dmail_key = dm.address.site_key if not dm.address: owner_if_anon = dd.get("owner_if_anon") if owner_if_anon and owner_if_anon[0]: dmail_address = yield from _load_dmail_address(dispatcher, owner_if_anon[0]) dm.address = dmail_address if dm.address: dm.sender_valid = True else: dm.sender_valid = False # sender_asymkey = rsakey.RsaKey(\ # privdata=dmail_address.site_privatekey)\ # if dmail_address else None # else: # sender_asymkey = None dest_addr_enc = dd.get("destination") if dest_addr_enc: dm.destination_dmail_key, dm.destination_significant_bits = mutil.decode_key(dest_addr_enc[0]) # mbase32.decode(dest_addr_enc[0]) # dispatcher.send_error("You must specify a destination.", 400) content = dd.get("content") if content: dp = DmailPart() dp.mime_type = "text/plain" dp.data = content[0].encode() dm.parts.append(dp) dm.date = mutil.utc_datetime() dm.hidden = False dm.read = True dm.deleted = False submit = dd.get("submit") if submit: return dm, submit[0] else: return dm, None