2015-09-28 07:36:25 +00:00
|
|
|
from __future__ import print_function
|
2015-07-25 01:04:15 +00:00
|
|
|
import sys, json
|
2015-06-21 02:03:10 +00:00
|
|
|
from twisted.internet import reactor
|
2015-07-25 01:04:15 +00:00
|
|
|
from .transcribe import Wormhole
|
2015-06-21 02:03:10 +00:00
|
|
|
from .. import public_relay
|
|
|
|
|
|
|
|
APPID = "lothar.com/wormhole/text-xfer"
|
|
|
|
|
2015-07-25 01:04:15 +00:00
|
|
|
w = Wormhole(APPID, public_relay.RENDEZVOUS_RELAY)
|
2015-06-21 02:03:10 +00:00
|
|
|
|
|
|
|
if sys.argv[1] == "send-text":
|
|
|
|
message = sys.argv[2]
|
2015-07-25 01:04:15 +00:00
|
|
|
data = json.dumps({"message": message}).encode("utf-8")
|
2015-06-21 02:03:10 +00:00
|
|
|
d = w.get_code()
|
|
|
|
def _got_code(code):
|
2015-09-28 07:36:25 +00:00
|
|
|
print("code is:", code)
|
2015-07-25 01:04:15 +00:00
|
|
|
return w.get_data(data)
|
2015-06-21 02:03:10 +00:00
|
|
|
d.addCallback(_got_code)
|
2015-07-25 01:04:15 +00:00
|
|
|
def _got_data(them_bytes):
|
|
|
|
them_d = json.loads(them_bytes.decode("utf-8"))
|
|
|
|
if them_d["message"] == "ok":
|
2015-09-28 07:36:25 +00:00
|
|
|
print("text sent")
|
2015-07-25 01:04:15 +00:00
|
|
|
else:
|
2015-09-28 07:36:25 +00:00
|
|
|
print("error sending text: %r" % (them_d,))
|
2015-06-21 02:03:10 +00:00
|
|
|
d.addCallback(_got_data)
|
|
|
|
elif sys.argv[1] == "receive-text":
|
|
|
|
code = sys.argv[2]
|
|
|
|
w.set_code(code)
|
2015-07-25 01:04:15 +00:00
|
|
|
data = json.dumps({"message": "ok"}).encode("utf-8")
|
|
|
|
d = w.get_data(data)
|
|
|
|
def _got_data(them_bytes):
|
|
|
|
them_d = json.loads(them_bytes.decode("utf-8"))
|
|
|
|
if "error" in them_d:
|
2015-09-28 07:36:25 +00:00
|
|
|
print("ERROR: " + them_d["error"], file=sys.stderr)
|
2015-07-25 01:04:15 +00:00
|
|
|
return 1
|
2015-09-28 07:36:25 +00:00
|
|
|
print(them_d["message"])
|
2015-06-21 02:03:10 +00:00
|
|
|
d.addCallback(_got_data)
|
|
|
|
else:
|
|
|
|
raise ValueError("bad command")
|
|
|
|
d.addCallback(lambda _: reactor.stop())
|
|
|
|
reactor.run()
|