2020-09-06 06:50:23 +00:00
# internal imports
import os , sys ; sys . path . append ( os . path . abspath ( os . path . join ( os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , ' .. ' ) ) , ' .. ' ) ) )
from komrade import *
2020-09-06 19:41:17 +00:00
from komrade . backend import *
2020-09-08 15:14:48 +00:00
from komrade . backend . phonelines import *
2020-09-06 06:50:23 +00:00
### ACTUAL PHONE CONNECTIONS
2020-09-06 19:24:36 +00:00
class TheTelephone ( Operator ) :
2020-09-06 06:50:23 +00:00
"""
API client class for Caller to interact with The Operator .
"""
2020-09-07 17:27:46 +00:00
def __init__ ( self , caller = None ) :
2020-09-09 14:38:37 +00:00
super ( ) . __init__ ( name = TELEPHONE_NAME )
2020-09-06 22:40:53 +00:00
self . caller = caller
2020-09-12 17:00:06 +00:00
from komrade . backend . phonelines import check_phonelines
keychain = check_phonelines ( ) [ TELEPHONE_NAME ]
self . _keychain = self . load_keychain_from_bytes ( keychain )
2020-09-09 14:38:37 +00:00
2020-09-06 06:50:23 +00:00
2020-09-12 18:18:37 +00:00
def send_and_receive ( self , msg_d , * * y ) :
2020-09-12 19:00:33 +00:00
msg_b = msg_d [ " msg " ]
msg_b64 = b64encode ( msg_b )
2020-09-09 15:21:13 +00:00
msg_b64_str = msg_b64 . decode ( )
2020-09-12 19:00:33 +00:00
self . log ( f ' But because I am @Telephone, and @Operator communicates only with me directly, \n I will omit all metadata and send only the encrypted packcage: \n \n { msg_b64_str } ' )
2020-09-10 14:04:32 +00:00
2020-09-12 19:00:33 +00:00
# seal for transport
msg_b64_str_esc = msg_b64_str . replace ( ' / ' , ' _ ' )
2020-09-10 14:04:32 +00:00
2020-09-09 15:21:13 +00:00
# dial the operator
2020-09-08 15:36:01 +00:00
URL = OPERATOR_API_URL + msg_b64_str_esc + ' / '
2020-09-06 06:50:23 +00:00
self . log ( " DIALING THE OPERATOR: " , URL )
2020-09-09 15:21:13 +00:00
phonecall = komrade_request ( URL )
if phonecall . status_code != 200 :
self . log ( ' !! error in request ' , phonecall . status_code , phonecall . text )
return
# response back from Operator!
2020-09-09 20:25:13 +00:00
resp_msg_b64_str = phonecall . text
2020-09-10 11:14:21 +00:00
# self.log('resp_msg_b64_str',resp_msg_b64_str)
2020-09-09 20:25:13 +00:00
resp_msg_b64 = resp_msg_b64_str . encode ( )
resp_msg_b = b64decode ( resp_msg_b64 )
2020-09-09 18:31:36 +00:00
# unseal
2020-09-09 22:43:50 +00:00
resp_msg_obj = self . unseal_msg ( resp_msg_b )
# res = resp_msg_b_unsealed
2020-09-09 22:50:22 +00:00
self . log ( ' unsealed resp_msg_obj ' , resp_msg_obj )
2020-09-09 22:43:50 +00:00
2020-09-09 23:01:08 +00:00
# decrypt
2020-09-10 08:02:03 +00:00
# resp_msg_obj.decrypt()
# self.log('returning decrypted form:',resp_msg_obj)
2020-09-09 22:57:52 +00:00
return resp_msg_obj
2020-09-09 22:50:22 +00:00
# return self.pronto_pronto(resp_msg_obj)
2020-09-09 15:21:13 +00:00
2020-09-07 11:23:10 +00:00
2020-09-12 14:32:03 +00:00
def ring_ring ( self , msg , * * y ) :
2020-09-09 14:38:37 +00:00
return super ( ) . ring_ring (
msg ,
to_whom = self . op ,
2020-09-12 14:32:03 +00:00
get_resp_from = self . send_and_receive ,
* * y
2020-09-08 11:23:41 +00:00
)
2020-09-09 14:38:37 +00:00
2020-09-06 06:50:23 +00:00
def test_call ( ) :
2020-09-08 15:14:48 +00:00
phone = TheTelephone ( )
2020-09-12 17:00:06 +00:00
pprint ( phone . keychain ( ) )
2020-09-08 15:14:48 +00:00
# caller = Caller('marx33') #Caller('marx')
2020-09-06 06:50:23 +00:00
# caller.boot(create=True)
# print(caller.keychain())
2020-09-06 19:59:14 +00:00
# phone = TheTelephone()
2020-09-09 18:31:36 +00:00
# req_json = {'_route':'forge_new_keys','name':name, 'pubkey_is_public':pubkey_is_public}}
2020-09-06 08:39:31 +00:00
# req_json_s = jsonify(req_json)
# res = phone.req({'forge_new_keys':{'name':'marx', 'pubkey_is_public':True}})
# print(res)
2020-09-07 07:41:17 +00:00
# asyncio.run(caller.get_new_keys())
2020-09-08 15:14:48 +00:00
# x=caller.get_new_keys(passphrase='1869')
2020-09-06 06:50:23 +00:00
2020-09-08 15:14:48 +00:00
# print('YEAH COOL',x)
2020-09-06 11:48:35 +00:00
2020-09-06 06:50:23 +00:00
## main
if __name__ == ' __main__ ' : test_call ( )