2020-09-08 11:23:41 +00:00
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 *
from komrade . backend import *
2020-09-11 14:35:47 +00:00
from komrade . backend . keymaker import *
2020-09-08 11:23:41 +00:00
2020-09-14 06:02:17 +00:00
class KomradeX ( Caller ) :
2020-09-08 11:23:41 +00:00
2020-09-14 08:25:55 +00:00
def __init__ ( self , name = None , pubkey = None ) :
super ( ) . __init__ ( name = name )
2020-09-14 06:02:17 +00:00
# self.log(f'booted komrade with {name} and {passphrase} and\n\n{dict_format(self.keychain())}')
2020-09-13 19:03:29 +00:00
# if SHOW_STATUS:
# from komrade.cli import CLI
# self.cli = CLI(name=name, komrade=self)
2020-09-13 19:26:55 +00:00
self . boot ( create = False )
2020-09-13 19:04:43 +00:00
# self.name=name
# pass
2020-09-08 12:11:13 +00:00
2020-09-13 18:55:14 +00:00
def boot ( self , create = False , ping = False ) :
2020-09-13 18:47:25 +00:00
# Do I already have my keys?
# yes? -- login
2020-09-08 12:11:13 +00:00
2020-09-13 18:47:25 +00:00
keys = self . keychain ( )
2020-09-14 09:28:56 +00:00
# self.log(f'booting {self}!',dict_format(keys))
2020-09-14 07:58:26 +00:00
2020-09-13 18:47:25 +00:00
if keys . get ( ' pubkey ' ) and keys . get ( ' privkey ' ) :
2020-09-14 06:20:02 +00:00
# self.log('already booted! @'+self.name)
2020-09-13 18:47:25 +00:00
return True
2020-09-09 14:38:37 +00:00
2020-09-13 18:55:14 +00:00
if self . exists_locally_as_account ( ) :
self . log ( f ' this account (@ { self . name } ) can be logged into ' )
return self . login ( )
elif self . exists_locally_as_contact ( ) :
self . log ( f ' this account (@ { self . name } ) is a contact ' )
return #pass #???
elif ping and self . exists_on_server ( ) :
self . log ( f ' this account exists on server. introduce? ' )
return
elif create :
self . log ( ' account is free: register? ' )
return self . register ( )
2020-09-09 14:38:37 +00:00
2020-09-14 04:43:59 +00:00
def exists_locally ( self ) :
return bool ( self . pubkey )
2020-09-09 17:34:19 +00:00
def exists_locally_as_contact ( self ) :
2020-09-14 04:43:59 +00:00
return bool ( self . pubkey ) and not bool ( self . privkey )
2020-09-09 17:34:19 +00:00
2020-09-13 18:55:14 +00:00
def exists_locally_as_account ( self ) :
2020-09-14 04:43:59 +00:00
return bool ( self . pubkey ) and bool ( self . privkey_encr )
2020-09-09 17:34:19 +00:00
def exists_on_server ( self ) :
2020-09-10 21:32:59 +00:00
answer = self . phone . ring_ring ( {
2020-09-09 18:31:36 +00:00
' _route ' : ' does_username_exist ' ,
' name ' : self . name
} )
2020-09-09 22:38:57 +00:00
self . log ( ' answer?? ' , answer )
2020-09-10 08:08:50 +00:00
return answer
2020-09-08 11:23:41 +00:00
2020-09-16 14:47:51 +00:00
def register ( self , name = None , passphrase = None , is_group = None , show_intro = 0 , show_body = True , logfunc = None ) :
2020-09-14 06:02:17 +00:00
# global PHONEBOOK
2020-09-13 14:15:06 +00:00
# print('got name:',name)
2020-09-12 13:11:39 +00:00
## Defaults
2020-09-10 17:52:07 +00:00
if name and not self . name : self . name = name
if not name and self . name : name = self . name
2020-09-13 13:57:06 +00:00
# if not name and not self.name: name=''
2020-09-13 14:15:06 +00:00
# print('got name',name)
2020-09-16 14:47:51 +00:00
if not logfunc : logfunc = self . log
2020-09-14 04:43:59 +00:00
# already have it?
if self . exists_locally_as_account ( ) :
2020-09-14 06:20:02 +00:00
return { ' success ' : False , ' status ' : ' You have already created this account. ' }
2020-09-14 04:43:59 +00:00
if self . exists_locally_as_contact ( ) :
2020-09-14 06:20:02 +00:00
return { ' success ' : False , ' status ' : ' This is already a contact of yours ' }
2020-09-14 04:43:59 +00:00
2020-09-12 13:11:39 +00:00
## 1) Have name?
2020-09-16 15:10:19 +00:00
clear_screen ( )
2020-09-13 13:57:06 +00:00
tolog = ' '
2020-09-11 14:35:47 +00:00
if SHOW_STATUS and show_intro :
2020-09-13 13:57:06 +00:00
self . name = name = self . cli . status_keymaker_part1 ( name )
2020-09-12 13:17:54 +00:00
elif not name :
2020-09-13 13:57:06 +00:00
self . name = name = input ( ' \n Hello, this is Komrade @ ' )
2020-09-16 15:08:57 +00:00
logfunc ( ' I would like to sign up for the socialist network revolution. ' , flush = True , komrade_name = name , pause = True )
2020-09-16 12:44:18 +00:00
# do_pause()
2020-09-13 13:57:06 +00:00
else :
2020-09-16 15:19:38 +00:00
logfunc ( f ' Hello, this is Komrade @ { name } . \n \n I would like to sign up for the socialist network revolution. ' , pause = True , komrade_name = name )
2020-09-16 12:44:18 +00:00
# do_pause()
2020-09-13 13:57:06 +00:00
2020-09-16 12:44:18 +00:00
# clear_screen()
2020-09-16 15:19:38 +00:00
logfunc ( f ' Excellent. But to communicate with komrades securely, you must first cut your public & private encryption keys. ' , pause = True , clear = True )
2020-09-13 13:57:06 +00:00
# do_pause()
2020-09-12 13:11:39 +00:00
## 2) Make pub public/private keys
2020-09-11 14:35:47 +00:00
keypair = KomradeAsymmetricKey ( )
pubkey , privkey = keypair . pubkey_obj , keypair . privkey_obj
2020-09-16 15:56:47 +00:00
logfunc ( f ' I have cut for you a private and public asymmetric key pair, using the iron-clad Elliptic curve algorithm: \n \n (1) { pubkey } \n \n (2) { privkey } { ART_KEY_PAIR } ' , clear = True , pause = True )
2020-09-16 16:15:45 +00:00
self . _keychain [ ' pubkey ' ] = pubkey
self . _keychain [ ' privkey ' ] = privkey
self . log ( ' My keychain now looks like: ' , dict_format ( self . keychain ( ) ) )
2020-09-11 14:35:47 +00:00
2020-09-16 15:54:45 +00:00
### PUBLIC KEY
qr_str = self . qr_str ( pubkey . data_b64 )
2020-09-16 16:01:04 +00:00
logfunc ( f ' (1) You may store your public key both on your device hardware, as well as share it with anyone you wish: \n \n { pubkey . data_b64_s } \n \n It will also be stored as a QR code on your device: \n { qr_str } ' , pause = True , clear = True )
2020-09-16 16:15:45 +00:00
logfunc ( ' You must also register your username and public key with Komrade @Operator on the remote server. \n \n Shall Komrade @Telephone send them over? ' , pause = False , clear = False ) #),dict_format(data,tab=2),pause=True)
ok_to_send = input ( f ' Komrade @ { name } : [Y/n] ' )
2020-09-16 15:54:45 +00:00
if ok_to_send . strip ( ) . lower ( ) == ' n ' :
logfunc ( ' Cancelling registration. ' )
return
## CALL OP WITH PUBKEY
resp_msg_d = self . ring_ring (
{
' name ' : name ,
' pubkey ' : pubkey . data ,
} ,
route = ' register_new_user '
)
2020-09-16 16:15:45 +00:00
print ( )
logfunc ( resp_msg_d . get ( ' status ' ) , komrade_name = ' Operator ' , pause = True )
2020-09-16 15:54:45 +00:00
if not resp_msg_d . get ( ' success ' ) :
2020-09-16 16:15:45 +00:00
logfunc ( ' Cancelling registration. ' , pause = True , clear = True )
2020-09-16 15:54:45 +00:00
return
2020-09-16 16:26:43 +00:00
# clear_screen()
logfunc ( ' Great. Komrade @Operator knows who you are now, because they have your name and public key on file (and nothing else!). ' , pause = True , clear = True )
2020-09-16 15:54:45 +00:00
2020-09-16 16:26:43 +00:00
logfunc ( f " (2) Your PRIVATE key, on the other hand, will be stored only on your device hardware. " , pause = True )
logfunc ( ''' But your private key is so sensitive we ' ll even encrypt it before storing it. ''' , pause = True , use_prefix = False )
2020-09-16 15:54:45 +00:00
2020-09-16 16:26:43 +00:00
2020-09-16 15:54:45 +00:00
2020-09-12 13:11:39 +00:00
## 3) Have passphrase?
2020-09-11 17:17:21 +00:00
if SHOW_STATUS and not passphrase :
2020-09-12 14:32:03 +00:00
passphrase = self . cli . status_keymaker_part2 ( name , passphrase , pubkey , privkey , hasher , self )
2020-09-11 14:35:47 +00:00
else :
2020-09-13 05:33:37 +00:00
if not passphrase : passphrase = DEBUG_DEFAULT_PASSPHRASE
2020-09-13 05:39:38 +00:00
while not passphrase :
2020-09-16 16:26:43 +00:00
# logfunc('Please type a password:',use_prefix=False)
logfunc ( ''' Please enter a memorable password to generate a (symmetric AES) encryption key with: ''' , use_prefix = True )
2020-09-16 15:25:03 +00:00
passphrase = getpass ( f ' \n Komrade @ { self . name } : ' )
2020-09-13 07:50:15 +00:00
clear_screen ( )
2020-09-12 13:11:39 +00:00
## 4) Get hashed password
2020-09-16 15:54:45 +00:00
2020-09-12 14:32:03 +00:00
passhash = hasher ( passphrase )
2020-09-16 15:54:45 +00:00
privkey_decr = KomradeSymmetricKeyWithPassphrase ( passhash = passhash )
logfunc ( f ''' Let ' s beef up your password by running it through a SHA-256 hashing algorithm: \n \n { make_key_discreet_str ( passhash ) } ''' , pause = True , clear = False )
2020-09-11 17:17:21 +00:00
privkey_encr = privkey_decr . encrypt ( privkey . data )
2020-09-12 07:55:23 +00:00
privkey_encr_obj = KomradeEncryptedAsymmetricPrivateKey ( privkey_encr )
2020-09-16 16:15:45 +00:00
self . _keychain [ ' privkey_encr ' ] = privkey_encr_obj
self . log ( ' My keychain now looks like v2: ' , dict_format ( self . keychain ( ) ) )
2020-09-12 13:17:54 +00:00
2020-09-16 15:54:45 +00:00
logfunc ( f " With that password we then encrypt your asymmetric private key: \n \n [Encrypted Private Key] \n ( { make_key_discreet_str ( privkey_encr_obj . data_b64 ) } ) " , pause = True , clear = False )
logfunc ( ' Only this encrypted version is stored. ' , pause = True , clear = True )
2020-09-16 16:15:45 +00:00
2020-09-12 13:17:54 +00:00
2020-09-14 04:43:59 +00:00
# More narration?
if SHOW_STATUS : self . cli . status_keymaker_part3 ( privkey , privkey_decr , privkey_encr , passphrase )
2020-09-10 21:32:59 +00:00
2020-09-13 10:57:06 +00:00
2020-09-13 09:42:22 +00:00
self . name = resp_msg_d . get ( ' name ' )
2020-09-13 13:04:44 +00:00
pubkey_b = resp_msg_d . get ( ' pubkey ' )
2020-09-14 04:43:59 +00:00
assert pubkey_b == pubkey . data
2020-09-14 06:35:40 +00:00
uri_id = pubkey . data_b64
2020-09-13 13:04:44 +00:00
sec_login = resp_msg_d . get ( ' secret_login ' )
2020-09-13 09:42:22 +00:00
2020-09-16 14:47:51 +00:00
logfunc ( f ''' Now saving name and public key on local device: ''' )
2020-09-14 06:35:40 +00:00
self . crypt_keys . set ( name , pubkey_b , prefix = ' /pubkey/ ' )
self . crypt_keys . set ( uri_id , name , prefix = ' /name/ ' )
2020-09-14 04:43:59 +00:00
self . crypt_keys . set ( uri_id , sec_login , prefix = ' /secret_login/ ' )
2020-09-10 09:34:34 +00:00
2020-09-14 08:25:55 +00:00
# store privkey pieces
self . crypt_keys . set ( uri_id , privkey_encr_obj . data , prefix = ' /privkey_encr/ ' )
# just to show we used a passphrase -->
self . crypt_keys . set ( uri_id , KomradeSymmetricKeyWithPassphrase . __name__ , prefix = ' /privkey_decr/ ' )
2020-09-13 09:42:22 +00:00
# save qr too:
2020-09-14 04:43:59 +00:00
self . save_uri_as_qrcode ( uri_id )
2020-09-13 10:51:01 +00:00
# self.log(f'saved public key as QR code to:\n {fnfn}\n\n{qr_str}')
2020-09-13 09:43:15 +00:00
2020-09-13 14:18:36 +00:00
return resp_msg_d
2020-09-13 09:42:22 +00:00
# done!
2020-09-16 15:25:03 +00:00
logfunc ( f ' Congratulations. Welcome, Komrade { self } . ' , pause = True , clear = True )
2020-09-16 15:25:11 +00:00
# self.help()
2020-09-08 15:14:48 +00:00
2020-09-13 11:18:04 +00:00
@property
def secret_login ( self ) :
return self . crypt_keys . get (
2020-09-13 12:31:33 +00:00
self . pubkey . data_b64 ,
prefix = ' /secret_login/ '
2020-09-13 11:18:04 +00:00
)
2020-09-13 12:27:45 +00:00
def login ( self , passphrase = None ) :
2020-09-14 07:58:26 +00:00
# what keys do I have?
keys = self . keychain ( )
# check hardware
if not ' pubkey ' in keys :
emsg = ''' Login impossible. I do not have this komrade ' s public key, much less private one. '''
# self.log()
return { ' success ' : False , ' status ' : emsg }
if not ' privkey_encr ' in keys :
emsg = ''' Login impossible. I do not have this komrade ' s private key on this hardware. '''
self . log ( emsg )
return { ' success ' : False , ' status ' : emsg }
2020-09-13 11:18:04 +00:00
2020-09-14 07:58:26 +00:00
if not ' privkey ' in keys :
emsg = ''' Login failed. Private key did not unlock from passphrase. '''
return { ' success ' : False , ' status ' : emsg }
2020-09-13 11:18:04 +00:00
# compose message
msg = {
' name ' : self . name ,
2020-09-14 07:58:26 +00:00
' pubkey ' : keys [ ' pubkey ' ] . data ,
2020-09-13 11:18:04 +00:00
' secret_login ' : self . secret_login
}
# ask operator and get response
2020-09-16 12:41:42 +00:00
resp_msg_d = self . ring_ring (
2020-09-13 11:18:04 +00:00
msg ,
route = ' login '
)
2020-09-16 13:46:54 +00:00
# print('got resp_msg_d:',resp_msg_d)
2020-09-16 10:53:55 +00:00
2020-09-13 11:18:04 +00:00
# get result
2020-09-16 12:41:42 +00:00
self . log ( ' Got resp_msg_d back from operator: ' , resp_msg_d )
2020-09-13 11:18:04 +00:00
2020-09-16 12:41:42 +00:00
# check msgs?
if ' res_check_msgs ' in resp_msg_d :
self . do_check_msgs ( resp_msg_d [ ' res_check_msgs ' ] )
resp_msg_d [ ' res_refresh ' ] = self . refresh ( check_msgs = False ) # already done in previous line
self . log ( ' --> ' , resp_msg_d )
return resp_msg_d
2020-09-13 11:18:04 +00:00
2020-09-13 18:06:41 +00:00
## MEETING PEOPLE
def find ( self , someone ) :
if type ( someone ) == str :
return Komrade ( name = someone )
if type ( someone ) == bytes :
return Komrade ( pubkey = someone )
self . log ( ' what is type of someoen here? ' , type ( someone ) )
return someone
2020-09-15 11:29:16 +00:00
# def meet(self,someone):
# # get person obj
# someone = self.find(someone)
# self.log('got someone =',someone,type(someone))
2020-09-13 18:06:41 +00:00
2020-09-14 08:48:23 +00:00
def contacts ( self ) :
# who do I know?
return sorted ( [ fn . split ( ' .png ' ) [ 0 ] for fn in os . listdir ( PATH_QRCODES ) ] )
2020-09-13 18:06:41 +00:00
2020-09-13 11:18:04 +00:00
2020-09-08 15:14:48 +00:00
2020-09-12 18:18:37 +00:00
def ring_ring ( self , msg , route = None , * * y ) :
if type ( msg ) == dict and not ROUTE_KEYNAME in msg :
msg [ ROUTE_KEYNAME ] = route
return super ( ) . ring_ring ( msg , caller = self , * * y )
2020-09-09 15:30:34 +00:00
2020-09-13 14:48:26 +00:00
2020-09-14 07:58:26 +00:00
def msg ( self , someone , something ) :
# find or boot
someone = Komrade ( someone )
self . log ( f ' found { someone } ' )
# can we?
if not someone . pubkey :
self . log ( f ''' Don ' t know the public key of { someone } ! ''' )
2020-09-13 19:42:04 +00:00
msg_obj = self . compose_msg_to (
2020-09-14 07:58:26 +00:00
something ,
someone
2020-09-13 19:42:04 +00:00
)
self . log ( ' composed msg: ' , msg_obj )
2020-09-14 07:58:26 +00:00
# encrypting
2020-09-13 19:42:04 +00:00
msg_obj . encrypt ( )
2020-09-14 07:58:26 +00:00
# attaching
direct_msg_data = msg_obj . msg
self . log ( ' going to send only this to op: ' , direct_msg_data )
# enclosing
msg_to_op = {
' deliver_from ' : self . pubkey . data_b64 ,
2020-09-15 08:27:02 +00:00
' deliver_from_name ' : self . name ,
2020-09-14 07:58:26 +00:00
' deliver_to ' : someone . pubkey . data_b64 ,
2020-09-15 08:27:02 +00:00
' deliver_to_name ' : someone . name ,
2020-09-14 07:58:26 +00:00
' deliver_msg ' : direct_msg_data ,
}
self . log ( ' going to send msg to op? ' , msg_to_op )
2020-09-09 15:30:34 +00:00
2020-09-14 07:58:26 +00:00
return self . ring_ring (
msg_to_op ,
route = ' deliver_msg '
)
2020-09-09 15:30:34 +00:00
2020-09-14 22:02:53 +00:00
def check_msgs ( self , inbox = None ) :
2020-09-14 22:00:13 +00:00
if not self . pubkey and self . privkey :
return { ' success ' : False , ' status ' : ' Need to be logged in ' }
# checking my own mail I presume?
if not inbox :
inbox = self . pubkey . data_b64
# send info op needs
msg = {
' secret_login ' : self . secret_login ,
' name ' : self . name ,
2020-09-15 06:39:11 +00:00
' pubkey ' : self . uri ,
2020-09-14 22:00:13 +00:00
' inbox ' : inbox
}
self . log ( ' sending msg to op: ' , msg )
2020-09-15 10:54:15 +00:00
# Ring operator
res = self . ring_ring (
msg ,
route = ' check_msgs '
)
2020-09-14 22:00:13 +00:00
self . log ( ' got back response: ' , res )
2020-09-16 12:41:42 +00:00
return self . do_check_msgs ( res )
def do_check_msgs ( self , res ) :
2020-09-15 06:39:11 +00:00
# decrypt?
2020-09-14 22:22:50 +00:00
if not res . get ( ' data_encr ' ) :
return { ' success ' : False , ' status ' : ' No data ' }
2020-09-15 06:39:11 +00:00
inbox_encr = res [ ' data_encr ' ]
2020-09-16 12:41:42 +00:00
inbox = SMessage (
self . privkey . data ,
self . op . pubkey . data
) . unwrap ( inbox_encr )
self . log ( ' inbox decrypted: ' , inbox )
2020-09-15 06:39:11 +00:00
# overwrite my local inbox with encrypted one from op?
2020-09-16 12:41:42 +00:00
return self . crypt_keys . set (
2020-09-15 06:39:11 +00:00
self . uri ,
2020-09-16 12:41:42 +00:00
inbox ,
2020-09-15 06:39:11 +00:00
prefix = ' /inbox/ ' ,
override = True
)
2020-09-15 07:56:09 +00:00
2020-09-16 12:41:42 +00:00
def refresh ( self , check_msgs = True ) :
2020-09-15 09:12:11 +00:00
# refresh inbox
2020-09-16 12:41:42 +00:00
if check_msgs :
self . check_msgs ( )
2020-09-15 09:12:11 +00:00
# status?
inbox_status = self . get_inbox_ids ( )
if not inbox_status [ ' success ' ] : return inbox_status
unread = inbox_status . get ( ' unread ' , [ ] )
inbox = inbox_status . get ( ' inbox ' , [ ] )
# download new messages
2020-09-15 13:42:39 +00:00
self . download_msgs ( post_ids = inbox )
2020-09-15 09:12:11 +00:00
2020-09-16 12:41:42 +00:00
res = {
2020-09-15 09:12:11 +00:00
' success ' : True ,
' status ' : ' Messages refreshed ' ,
' unread ' : unread ,
' inbox ' : inbox
}
2020-09-16 12:41:42 +00:00
self . log ( ' -> ' , res )
return res
2020-09-15 09:12:11 +00:00
2020-09-16 14:34:21 +00:00
def save_inbox ( self ,
post_ids ,
uri = None ,
encrypted = False ) :
self . log ( ' <- ' , post_ids )
newval = BSEP . join ( post_ids )
res = self . crypt_keys . set (
self . uri if not uri else uri ,
newval ,
prefix = ' /inbox/ ' ,
2020-09-15 14:19:22 +00:00
override = True
2020-09-15 10:59:19 +00:00
)
2020-09-16 14:34:21 +00:00
assert newval == self . crypt_keys . get (
self . uri ,
prefix = ' /inbox/ '
)
self . log ( ' -> ' , res )
return res
2020-09-15 10:59:19 +00:00
2020-09-15 14:05:33 +00:00
def delete_msg ( self , post_id ) :
return self . delete_msgs ( [ post_id ] )
2020-09-15 10:59:19 +00:00
def delete_msgs ( self , post_ids ) :
2020-09-16 14:34:21 +00:00
inbox_ids = self . get_inbox_ids ( ) . get ( ' inbox ' , [ ] )
#print(inbox_ids,'v1',len(inbox_ids))
2020-09-16 12:41:42 +00:00
deleted = [ ]
2020-09-15 10:59:19 +00:00
for post_id in post_ids :
2020-09-16 12:41:42 +00:00
#print('deleting post:',post_id)
2020-09-15 10:59:19 +00:00
self . crypt_keys . delete (
post_id ,
2020-09-15 14:15:59 +00:00
prefix = ' /post/ ' ,
2020-09-15 10:59:19 +00:00
)
2020-09-16 12:41:42 +00:00
deleted + = [ post_id ]
2020-09-16 14:34:21 +00:00
#print(post_id,inbox_ids,post_id in inbox_ids,'???')
# stop
if post_id in inbox_ids :
print ( ' removing from inbox... ' )
inbox_ids . remove ( post_id )
self . save_inbox ( inbox_ids )
#print(inbox_ids,'v2',len(inbox_ids))
res = {
2020-09-16 12:41:42 +00:00
' success ' : not bool ( set ( post_ids ) - set ( deleted ) ) ,
' status ' : f ' Deleted { len ( deleted ) } messages. ' ,
' deleted ' : deleted
}
2020-09-16 14:34:21 +00:00
self . log ( ' delete_msgs -> ' , res )
return res
2020-09-15 10:54:15 +00:00
2020-09-15 11:42:57 +00:00
def inbox ( self , topn = 100 , only_unread = False , delete_malformed = False ) :
2020-09-15 09:12:11 +00:00
# refreshing inbox
res = self . refresh ( )
2020-09-15 13:44:04 +00:00
# print('got from refresh',res)
2020-09-15 09:12:11 +00:00
if not res [ ' success ' ] : return res
2020-09-15 13:13:32 +00:00
2020-09-15 09:12:11 +00:00
boxname = ' inbox ' if not only_unread else ' unread '
post_ids = res [ boxname ]
msgs = [ ]
2020-09-15 10:59:19 +00:00
post_ids_malformed = [ ]
2020-09-15 09:12:11 +00:00
for post_id in post_ids :
2020-09-15 10:54:15 +00:00
malformed = False
2020-09-15 09:12:11 +00:00
try :
2020-09-15 11:45:19 +00:00
res = self . read_msg ( post_id )
2020-09-15 13:44:04 +00:00
# print('GOT FROM READ_MSG',res)
2020-09-15 09:12:11 +00:00
except ThemisError as e :
2020-09-15 11:42:57 +00:00
print ( f ' !! Could not decrypt post { post_id } ' )
2020-09-15 10:54:15 +00:00
malformed = True
2020-09-15 11:45:19 +00:00
2020-09-15 13:38:16 +00:00
#print(res,'ressss')
2020-09-15 13:13:32 +00:00
if not res . get ( ' success ' ) :
2020-09-15 13:45:07 +00:00
# return res
2020-09-15 13:45:38 +00:00
continue
2020-09-15 11:45:19 +00:00
msg = res . get ( ' msg ' )
2020-09-15 13:45:38 +00:00
if not msg : continue
2020-09-15 13:38:16 +00:00
#print(msg,'?!?!??!')
2020-09-15 11:49:34 +00:00
# stop
2020-09-15 10:29:13 +00:00
2020-09-15 11:46:04 +00:00
if not msg . from_name or not msg . from_pubkey :
2020-09-15 11:42:57 +00:00
print ( ' !! Invalid sender info! ' )
2020-09-15 10:54:15 +00:00
malformed = True
2020-09-15 14:02:42 +00:00
msg . post_id = post_id
2020-09-15 10:54:15 +00:00
if not malformed :
2020-09-15 13:46:36 +00:00
# print('good msg:',msg)
2020-09-15 10:54:15 +00:00
msgs . append ( msg )
2020-09-15 10:59:19 +00:00
else :
post_ids_malformed . append ( post_id )
2020-09-15 09:12:11 +00:00
if len ( msgs ) > = topn : break
# print('!!',post_id,msg.from_whom, msg.to_whom, msg.from_whom is self)
2020-09-15 10:17:06 +00:00
2020-09-15 10:59:19 +00:00
if delete_malformed :
self . delete_msgs ( post_ids_malformed )
2020-09-15 13:38:16 +00:00
#print(msgs,'msssgs')
2020-09-15 12:07:09 +00:00
return { ' success ' : True ,
' msgs ' : msgs }
2020-09-15 09:12:11 +00:00
# return all messages read?
def get_inbox_ids ( self ) :
2020-09-16 12:41:42 +00:00
inbox = self . crypt_keys . get (
2020-09-15 07:56:09 +00:00
self . uri ,
prefix = ' /inbox/ ' ,
)
2020-09-15 06:39:11 +00:00
# decrypt inbox?
2020-09-16 12:41:42 +00:00
if inbox :
2020-09-15 12:09:52 +00:00
inbox = inbox . split ( BSEP )
2020-09-16 12:41:42 +00:00
self . log ( ' inbox_l ' , inbox )
2020-09-15 12:09:52 +00:00
else :
inbox = [ ]
2020-09-14 22:22:50 +00:00
2020-09-15 06:39:11 +00:00
# find the unread ones
2020-09-14 22:29:47 +00:00
unread = [ ]
2020-09-15 06:10:22 +00:00
for post_id in inbox :
2020-09-14 22:29:47 +00:00
if not post_id : continue
2020-09-15 06:10:22 +00:00
if not self . crypt_keys . get ( post_id , prefix = ' /post/ ' ) :
unread . append ( post_id )
2020-09-14 22:22:50 +00:00
2020-09-15 06:39:11 +00:00
self . log ( f ' I { self } have { len ( unread ) } new messages ' )
2020-09-16 12:41:42 +00:00
res = {
2020-09-15 06:10:22 +00:00
' success ' : True ,
' status ' : ' Inbox retrieved. ' ,
' unread ' : unread ,
' inbox ' : inbox
}
2020-09-16 12:41:42 +00:00
self . log ( ' -> ' , res )
return res
2020-09-15 06:39:11 +00:00
def read_msg ( self , post_id ) :
# get post
2020-09-15 06:40:32 +00:00
post_encr = self . crypt_keys . get ( post_id , prefix = ' /post/ ' )
2020-09-15 13:46:36 +00:00
# print(post_id,'????')
2020-09-15 06:40:32 +00:00
if not post_encr :
2020-09-15 12:13:43 +00:00
self . download_msgs ( [ post_id ] )
post_encr = self . crypt_keys . get ( post_id , prefix = ' /post/ ' )
2020-09-15 13:46:36 +00:00
# print(post_id,'????')
2020-09-15 12:13:43 +00:00
2020-09-15 06:39:11 +00:00
return {
' success ' : False ,
' status ' : ' Post not found. '
}
2020-09-15 07:04:01 +00:00
self . log ( ' found encrypted post store: ' , post_encr )
2020-09-15 08:07:57 +00:00
# self.log('unpickling?',pickle.loads(post_encr))
2020-09-15 06:39:11 +00:00
2020-09-15 06:50:21 +00:00
2020-09-15 06:39:11 +00:00
# it should be twice decrypted
2020-09-15 08:27:02 +00:00
msg_op2me_obj = Message (
from_whom = self . op ,
2020-09-15 06:39:11 +00:00
to_whom = self ,
msg = post_encr
)
2020-09-15 08:27:02 +00:00
self . log ( ' assuming this is the message: ' , msg_op2me_obj )
2020-09-15 06:39:11 +00:00
# decrypt
2020-09-15 08:27:02 +00:00
msg_op2me_obj . decrypt ( )
2020-09-14 22:00:13 +00:00
2020-09-15 08:27:02 +00:00
# dict to/from/msg
msg_op2me = msg_op2me_obj . msg . msg_d
self . log ( ' msg_op2me is now ' , msg_op2me )
# this really to me?
assert msg_op2me . get ( ' to ' ) == self . uri
# now try to decrypt?
msg2me = Message (
to_whom = self ,
msg_d = {
' from ' : msg_op2me . get ( ' from ' ) ,
2020-09-15 08:51:52 +00:00
' from_name ' : msg_op2me . get ( ' from_name ' ) ,
2020-09-15 08:27:02 +00:00
' msg ' : msg_op2me . get ( ' msg ' )
}
)
# self.log('msg2me is now v1',msg2me)
msg2me . decrypt ( )
2020-09-15 08:51:52 +00:00
self . log ( ' msg2me is now v2 ' , dict_format ( msg2me . msg_d ) )
2020-09-15 08:27:02 +00:00
2020-09-15 11:45:19 +00:00
return {
' success ' : True ,
' msg ' : msg2me
}
2020-09-15 08:27:02 +00:00
2020-09-15 06:39:11 +00:00
def download_msgs ( self , post_ids = [ ] , inbox = None ) :
2020-09-15 06:10:22 +00:00
if not post_ids :
# get unerad
2020-09-15 09:12:11 +00:00
post_ids = self . get_inbox_ids ( ) . get ( ' unread ' , [ ] )
2020-09-15 06:10:22 +00:00
if not post_ids :
return { ' success ' : False , ' status ' : ' No messages requested ' }
# ask Op for them
2020-09-15 06:39:11 +00:00
res = self . ring_ring (
2020-09-15 06:10:22 +00:00
{
' secret_login ' : self . secret_login ,
' name ' : self . name ,
2020-09-15 06:39:11 +00:00
' pubkey ' : self . uri ,
2020-09-15 06:10:22 +00:00
' post_ids ' : post_ids ,
} ,
2020-09-15 06:39:11 +00:00
route = ' download_msgs '
2020-09-15 06:10:22 +00:00
)
2020-09-15 13:46:36 +00:00
# print('got back from op!',res)
2020-09-15 06:39:11 +00:00
if not ' data_encr ' or not res [ ' data_encr ' ] or type ( res [ ' data_encr ' ] ) != dict :
return { ' success ' : False , ' status ' : ' No valid data returned. ' }
# store -- encrypted!
2020-09-15 08:07:57 +00:00
posts_downloaded = [ ]
2020-09-15 06:39:11 +00:00
for post_id , post_encr in res [ ' data_encr ' ] . items ( ) :
2020-09-15 10:17:06 +00:00
# print('storing...',post_id)
2020-09-15 06:39:11 +00:00
self . crypt_keys . set (
post_id ,
post_encr ,
prefix = ' /post/ '
)
2020-09-15 08:07:57 +00:00
posts_downloaded . append ( post_id )
return {
' success ' : True ,
' status ' : ' Messages downloaded ' ,
' downloaded ' : posts_downloaded ,
}
2020-09-09 15:30:34 +00:00
2020-09-15 11:12:49 +00:00
2020-09-15 11:29:16 +00:00
### MEETING PEOLPE
2020-09-15 15:07:15 +00:00
def meet ( self , name = None , pubkey = None , returning = False ) :
2020-09-15 11:29:16 +00:00
if not name and not pubkey :
return { ' success ' : False , ' status ' : ' Meet whom? ' }
2020-09-15 11:12:49 +00:00
2020-09-16 14:34:21 +00:00
keystr = self . name + ' -> ' + name
if self . crypt_keys . get (
keystr ,
prefix = ' /met/ '
) :
return {
' success ' : False ,
' status ' : f ' You have already sent an introduction to @ { name } . It would be rude to send another. '
}
2020-09-15 11:31:25 +00:00
msg_to_op = {
' name ' : self . name ,
' secret_login ' : self . secret_login ,
' pubkey ' : self . uri ,
2020-09-15 11:12:49 +00:00
2020-09-15 11:31:25 +00:00
' meet_name ' : name ,
2020-09-15 15:07:15 +00:00
' meet_pubkey ' : pubkey ,
' returning ' : returning
2020-09-15 11:31:25 +00:00
}
2020-09-15 13:34:53 +00:00
# print('msg_to_op',msg_to_op)
2020-09-15 11:12:49 +00:00
2020-09-15 11:29:16 +00:00
res = self . ring_ring (
2020-09-15 11:31:57 +00:00
msg_to_op ,
2020-09-15 11:29:16 +00:00
route = ' introduce_komrades '
)
2020-09-16 10:42:54 +00:00
# print('res from op',res)
2020-09-15 11:12:49 +00:00
2020-09-16 14:34:21 +00:00
# record that I've already tried this
self . crypt_keys . set (
keystr ,
b ' y ' ,
prefix = ' /met/ '
)
2020-09-15 11:29:16 +00:00
return res
2020-09-15 11:12:49 +00:00
2020-09-10 14:00:55 +00:00
def test_register ( ) :
import random
num = random . choice ( list ( range ( 0 , 1000 ) ) )
2020-09-10 21:32:59 +00:00
botname = f ' marx { str ( num ) . zfill ( 3 ) } '
2020-09-13 07:15:44 +00:00
marxbot = Komrade ( botname )
# marxbot=Komrade()
2020-09-14 06:02:17 +00:00
marxbot . register ( passphrase = ' boo ' )
2020-09-09 15:30:34 +00:00
2020-09-13 19:42:04 +00:00
2020-09-13 20:24:31 +00:00
2020-09-13 19:42:04 +00:00
def test_msg ( ) :
2020-09-15 06:10:22 +00:00
b = Komrade ( ' bez ' )
2020-09-15 07:04:01 +00:00
b . inbox ( )
# b.read_msg(b'YWY3NDUxZjNjYjdhNDFmNmIyNDI2NzU3YTI4ZTA0OWM=')
2020-09-15 06:40:03 +00:00
#b.login()
2020-09-15 06:10:22 +00:00
2020-09-15 06:40:03 +00:00
#print(b.download_msgs())
2020-09-15 06:10:22 +00:00
# z = Komrade('zuckbot')
2020-09-13 20:24:31 +00:00
2020-09-15 06:10:22 +00:00
# b.msg(z,'you ssssssuck')
2020-09-13 20:24:31 +00:00
2020-09-13 19:42:04 +00:00
2020-09-14 07:58:26 +00:00
def test_loading ( ) :
2020-09-14 19:25:24 +00:00
z1 = Komrade ( ' elon ' )
2020-09-14 11:52:56 +00:00
# z1.register()
2020-09-14 08:25:55 +00:00
print ( z1 . keychain ( ) )
2020-09-14 11:52:56 +00:00
# exit()
2020-09-14 07:58:26 +00:00
z2 = Komrade ( b ' VUVDMgAAAC08BCMVA+0dMJXc66/W7hty669+3/3S61Q1yjmgJW8I0k3lqfDi ' )
print ( z2 )
print ( z2 . keychain ( ) )
2020-09-08 15:14:48 +00:00
2020-09-14 07:58:26 +00:00
pprint ( PHONEBOOK )
return
# z1.login()
2020-09-09 15:41:33 +00:00
2020-09-15 06:10:22 +00:00
def test_sign ( ) :
from pythemis import smessage
b = Komrade ( ' bez ' )
m = Komrade ( ' marx ' )
z = Komrade ( ' zuckbot ' )
msg = b ' this is cool. --bez '
signed_msg = smessage . ssign ( b . privkey . data , msg )
print ( signed_msg )
verified_msg = smessage . sverify ( b . pubkey . data , signed_msg )
print ( verified_msg )
2020-09-15 06:18:34 +00:00
def test_send ( ) :
z = Komrade ( ' zuckbot ' )
2020-09-09 17:39:30 +00:00
2020-09-14 07:58:26 +00:00
if __name__ == ' __main__ ' :
2020-09-15 06:10:22 +00:00
test_msg ( )