LXMF/docs/example_receiver.py

72 lines
2.9 KiB
Python
Raw Normal View History

2024-01-03 06:45:18 -05:00
import RNS
import LXMF
import time
2024-09-06 14:34:45 -04:00
required_stamp_cost = 8
enforce_stamps = False
2024-01-03 06:45:18 -05:00
def delivery_callback(message):
global my_lxmf_destination, router
2024-01-03 06:45:18 -05:00
time_string = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.timestamp))
signature_string = "Signature is invalid, reason undetermined"
if message.signature_validated:
signature_string = "Validated"
else:
if message.unverified_reason == LXMF.LXMessage.SIGNATURE_INVALID:
signature_string = "Invalid signature"
if message.unverified_reason == LXMF.LXMessage.SOURCE_UNKNOWN:
signature_string = "Cannot verify, source is unknown"
2024-09-06 14:34:45 -04:00
if message.stamp_valid:
stamp_string = "Validated"
else:
2024-09-06 14:34:45 -04:00
stamp_string = "Invalid"
2024-01-03 06:45:18 -05:00
RNS.log("\t+--- LXMF Delivery ---------------------------------------------")
RNS.log("\t| Source hash : "+RNS.prettyhexrep(message.source_hash))
RNS.log("\t| Source instance : "+str(message.get_source()))
RNS.log("\t| Destination hash : "+RNS.prettyhexrep(message.destination_hash))
RNS.log("\t| Destination instance : "+str(message.get_destination()))
RNS.log("\t| Transport Encryption : "+str(message.transport_encryption))
RNS.log("\t| Timestamp : "+time_string)
RNS.log("\t| Title : "+message.title_as_string())
RNS.log("\t| Content : "+message.content_as_string())
RNS.log("\t| Fields : "+str(message.fields))
2024-09-08 20:12:27 -04:00
if message.ratchet_id:
RNS.log("\t| Ratchet : "+str(RNS.Identity._get_ratchet_id(message.ratchet_id)))
2024-01-03 06:45:18 -05:00
RNS.log("\t| Message signature : "+signature_string)
RNS.log("\t| Stamp : "+stamp_string)
2024-01-03 06:45:18 -05:00
RNS.log("\t+---------------------------------------------------------------")
# Optionally, send a reply
# source = my_lxmf_destination
# dest = message.source
# lxm = LXMF.LXMessage(dest, source, "Reply", None, desired_method=LXMF.LXMessage.DIRECT, include_ticket=True)
# router.handle_outbound(lxm)
2024-01-03 06:45:18 -05:00
r = RNS.Reticulum()
2024-09-06 14:34:45 -04:00
router = LXMF.LXMRouter(storagepath="./tmp1", enforce_stamps=enforce_stamps)
2024-01-03 06:45:18 -05:00
identity = RNS.Identity()
my_lxmf_destination = router.register_delivery_identity(identity, display_name="Anonymous Peer", stamp_cost=required_stamp_cost)
2024-01-03 06:45:18 -05:00
router.register_delivery_callback(delivery_callback)
RNS.log("Ready to receive on: "+RNS.prettyhexrep(my_lxmf_destination.hash))
2024-09-04 12:57:51 -04:00
# You can set a propagation node address to test receiving
# messages from a propagation node, instead of directly
# router.set_outbound_propagation_node(bytes.fromhex("e75d9b6a69f82b48b6077cf2242d7499"))
# This loop allows you to execute various actions for testing
# and experimenting with the example scripts.
while True:
input()
RNS.log("Announcing lxmf.delivery destination...")
2024-09-06 14:34:45 -04:00
router.announce(my_lxmf_destination.hash)
2024-09-04 12:57:51 -04:00
# input()
# RNS.log("Requesting messages from propagation node...")
# router.request_messages_from_propagation_node(identity)