/ docs / example_receiver.py
example_receiver.py
 1  import RNS
 2  import LXMF
 3  import time
 4  
 5  required_stamp_cost = 8
 6  enforce_stamps = False
 7  
 8  def delivery_callback(message):
 9    global my_lxmf_destination, router
10    time_string      = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.timestamp))
11    signature_string = "Signature is invalid, reason undetermined"
12    if message.signature_validated:
13      signature_string = "Validated"
14    else:
15      if message.unverified_reason == LXMF.LXMessage.SIGNATURE_INVALID:
16        signature_string = "Invalid signature"
17      if message.unverified_reason == LXMF.LXMessage.SOURCE_UNKNOWN:
18        signature_string = "Cannot verify, source is unknown"
19  
20    if message.stamp_valid:
21      stamp_string = "Validated"
22    else:
23      stamp_string = "Invalid"
24  
25    RNS.log("\t+--- LXMF Delivery ---------------------------------------------")
26    RNS.log("\t| Source hash            : "+RNS.prettyhexrep(message.source_hash))
27    RNS.log("\t| Source instance        : "+str(message.get_source()))
28    RNS.log("\t| Destination hash       : "+RNS.prettyhexrep(message.destination_hash))
29    RNS.log("\t| Destination instance   : "+str(message.get_destination()))
30    RNS.log("\t| Transport Encryption   : "+str(message.transport_encryption))
31    RNS.log("\t| Timestamp              : "+time_string)
32    RNS.log("\t| Title                  : "+str(message.title_as_string()))
33    RNS.log("\t| Content                : "+str(message.content_as_string()))
34    RNS.log("\t| Fields                 : "+str(message.fields))
35    if message.ratchet_id:
36      RNS.log("\t| Ratchet                : "+str(RNS.Identity._get_ratchet_id(message.ratchet_id)))
37    RNS.log("\t| Message signature      : "+signature_string)
38    RNS.log("\t| Stamp                  : "+stamp_string)
39    RNS.log("\t+---------------------------------------------------------------")
40  
41    # Optionally, send a reply
42    # source = my_lxmf_destination
43    # dest = message.source
44    # lxm = LXMF.LXMessage(dest, source, "Reply", None, desired_method=LXMF.LXMessage.DIRECT, include_ticket=True)
45    # router.handle_outbound(lxm)
46  
47  r = RNS.Reticulum()
48  
49  router = LXMF.LXMRouter(storagepath="./tmp1", enforce_stamps=enforce_stamps)
50  identity = RNS.Identity()
51  my_lxmf_destination = router.register_delivery_identity(identity, display_name="Anonymous Peer", stamp_cost=required_stamp_cost)
52  router.register_delivery_callback(delivery_callback)
53  
54  RNS.log("Ready to receive on: "+RNS.prettyhexrep(my_lxmf_destination.hash))
55  
56  
57  # You can set a propagation node address to test receiving
58  # messages from a propagation node, instead of directly
59  
60  # router.set_outbound_propagation_node(bytes.fromhex("e75d9b6a69f82b48b6077cf2242d7499"))
61  
62  
63  # This loop allows you to execute various actions for testing
64  # and experimenting with the example scripts.
65  while True:
66    input()
67    RNS.log("Announcing lxmf.delivery destination...")
68    router.announce(my_lxmf_destination.hash)
69  
70    # input()
71    # RNS.log("Requesting messages from propagation node...")
72    # router.request_messages_from_propagation_node(identity)