2018-04-21 05:04:03 +00:00
'''
2019-06-20 00:59:05 +00:00
Onionr - Private P2P Communication
2018-05-15 06:43:29 +00:00
This plugin acts as a plugin manager , and allows the user to install other plugins distributed over Onionr .
'''
'''
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < https : / / www . gnu . org / licenses / > .
2018-04-21 05:04:03 +00:00
'''
# useful libraries
import logger , config
2018-05-14 04:11:31 +00:00
import os , sys , json , time , random , shutil , base64 , getpass , datetime , re
2018-05-16 03:08:42 +00:00
from onionrblockapi import Block
2019-06-25 23:07:35 +00:00
from onionrutils import importnewblocks , stringvalidators
2018-04-21 05:04:03 +00:00
2018-04-23 03:42:37 +00:00
plugin_name = ' pluginmanager '
2018-04-21 05:04:03 +00:00
2018-05-14 04:11:31 +00:00
keys_data = { ' keys ' : { } , ' plugins ' : [ ] , ' repositories ' : { } }
2018-04-23 03:42:37 +00:00
# key functions
def writeKeys ( ) :
'''
Serializes and writes the keystore in memory to file
'''
2018-06-01 04:25:28 +00:00
with open ( keys_file , ' w ' ) as file :
file . write ( json . dumps ( keys_data , indent = 4 , sort_keys = True ) )
file . close ( )
2018-04-23 03:42:37 +00:00
def readKeys ( ) :
'''
Loads the keystore into memory
'''
global keys_data
2018-06-01 04:25:28 +00:00
with open ( keys_file ) as file :
keys_data = json . loads ( file . read ( ) )
2018-04-23 03:42:37 +00:00
return keys_data
def getKey ( plugin ) :
'''
Returns the public key for a given plugin
'''
2018-05-14 04:11:31 +00:00
global keys_data
2018-04-23 03:42:37 +00:00
readKeys ( )
return ( keys_data [ ' keys ' ] [ plugin ] if plugin in keys_data [ ' keys ' ] else None )
def saveKey ( plugin , key ) :
'''
Saves the public key for a plugin to keystore
'''
2018-05-14 04:11:31 +00:00
global keys_data
readKeys ( )
2018-04-23 03:42:37 +00:00
keys_data [ ' keys ' ] [ plugin ] = key
writeKeys ( )
2018-05-14 04:11:31 +00:00
def getPlugins ( ) :
'''
Returns a list of plugins installed by the plugin manager
'''
global keys_data
readKeys ( )
return keys_data [ ' plugins ' ]
def addPlugin ( plugin ) :
'''
Saves the plugin name , to remember that it was installed by the pluginmanager
'''
global keys_data
readKeys ( )
if not plugin in keys_data [ ' plugins ' ] :
keys_data [ ' plugins ' ] . append ( plugin )
writeKeys ( )
def removePlugin ( plugin ) :
'''
Removes the plugin name from the pluginmanager ' s records
'''
global keys_data
readKeys ( )
if plugin in keys_data [ ' plugins ' ] :
keys_data [ ' plugins ' ] . remove ( plugin )
writeKeys ( )
def getRepositories ( ) :
'''
Returns a list of plugins installed by the plugin manager
'''
global keys_data
readKeys ( )
return keys_data [ ' repositories ' ]
2018-06-01 04:25:28 +00:00
def addRepository ( blockhash , data ) :
2018-05-14 04:11:31 +00:00
'''
Saves the plugin name , to remember that it was installed by the pluginmanager
'''
global keys_data
readKeys ( )
2018-06-01 04:25:28 +00:00
keys_data [ ' repositories ' ] [ blockhash ] = data
2018-05-14 04:11:31 +00:00
writeKeys ( )
2018-06-01 04:25:28 +00:00
def removeRepository ( blockhash ) :
2018-05-14 04:11:31 +00:00
'''
Removes the plugin name from the pluginmanager ' s records
'''
global keys_data
readKeys ( )
2018-06-01 04:25:28 +00:00
if blockhash in keys_data [ ' repositories ' ] :
del keys_data [ ' repositories ' ] [ blockhash ]
2018-05-14 04:11:31 +00:00
writeKeys ( )
2018-06-01 04:25:28 +00:00
def createRepository ( plugins ) :
contents = { ' plugins ' : plugins , ' author ' : getpass . getuser ( ) , ' compiled-by ' : plugin_name }
block = Block ( core = pluginapi . get_core ( ) )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
block . setType ( ' repository ' )
block . setContent ( json . dumps ( contents ) )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
return block . save ( True )
2018-04-23 03:42:37 +00:00
def check ( ) :
'''
Checks to make sure the keystore file still exists
'''
global keys_file
keys_file = pluginapi . plugins . get_data_folder ( plugin_name ) + ' keystore.json '
if not os . path . isfile ( keys_file ) :
writeKeys ( )
2018-05-13 03:45:32 +00:00
# plugin management
2018-05-14 04:11:31 +00:00
def sanitize ( name ) :
2018-12-09 17:29:39 +00:00
return re . sub ( ' [^0-9a-zA-Z_]+ ' , ' ' , str ( name ) . lower ( ) ) [ : 255 ]
2018-05-14 04:11:31 +00:00
def blockToPlugin ( block ) :
try :
2018-06-01 04:25:28 +00:00
block = Block ( block , core = pluginapi . get_core ( ) )
2018-05-16 03:08:42 +00:00
blockContent = json . loads ( block . getContent ( ) )
2018-05-14 04:11:31 +00:00
name = sanitize ( blockContent [ ' name ' ] )
author = blockContent [ ' author ' ]
date = blockContent [ ' date ' ]
version = None
if ' version ' in blockContent [ ' info ' ] :
version = blockContent [ ' info ' ] [ ' version ' ]
content = base64 . b64decode ( blockContent [ ' content ' ] . encode ( ) )
source = pluginapi . plugins . get_data_folder ( plugin_name ) + ' plugin.zip '
destination = pluginapi . plugins . get_folder ( name )
with open ( source , ' wb ' ) as f :
f . write ( content )
if os . path . exists ( destination ) and not os . path . isfile ( destination ) :
shutil . rmtree ( destination )
shutil . unpack_archive ( source , destination )
pluginapi . plugins . enable ( name )
2019-06-20 00:59:05 +00:00
logger . info ( ' Installation of %s complete. ' % name , terminal = True )
2018-05-14 04:11:31 +00:00
return True
except Exception as e :
2019-06-20 00:59:05 +00:00
logger . error ( ' Failed to install plugin. ' , error = e , timestamp = False , terminal = True )
2018-05-14 04:11:31 +00:00
return False
2018-05-13 03:45:32 +00:00
def pluginToBlock ( plugin , import_block = True ) :
try :
2018-05-14 04:11:31 +00:00
plugin = sanitize ( plugin )
2018-05-13 03:45:32 +00:00
directory = pluginapi . get_pluginapi ( ) . get_folder ( plugin )
data_directory = pluginapi . get_pluginapi ( ) . get_data_folder ( plugin )
zipfile = pluginapi . get_pluginapi ( ) . get_data_folder ( plugin_name ) + ' plugin.zip '
if os . path . exists ( directory ) and not os . path . isfile ( directory ) :
if os . path . exists ( data_directory ) and not os . path . isfile ( data_directory ) :
shutil . rmtree ( data_directory )
if os . path . exists ( zipfile ) and os . path . isfile ( zipfile ) :
os . remove ( zipfile )
if os . path . exists ( directory + ' __pycache__ ' ) and not os . path . isfile ( directory + ' __pycache__ ' ) :
shutil . rmtree ( directory + ' __pycache__ ' )
shutil . make_archive ( zipfile [ : - 4 ] , ' zip ' , directory )
2018-06-01 04:25:28 +00:00
data = ' '
with open ( zipfile , ' rb ' ) as file :
data = base64 . b64encode ( file . read ( ) )
2018-05-13 03:45:32 +00:00
2018-05-14 04:11:31 +00:00
author = getpass . getuser ( )
2018-05-14 04:19:33 +00:00
description = ' Default plugin description '
2018-05-14 04:11:31 +00:00
info = { " name " : plugin }
try :
if os . path . exists ( directory + ' info.json ' ) :
2018-06-01 04:25:28 +00:00
info = ' '
with open ( directory + ' info.json ' ) . read ( ) as file :
info = json . loads ( file . read ( ) )
2018-11-11 03:25:40 +00:00
2018-05-14 04:11:31 +00:00
if ' author ' in info :
author = info [ ' author ' ]
if ' description ' in info :
2018-05-14 04:19:33 +00:00
description = info [ ' description ' ]
2018-05-14 04:11:31 +00:00
except :
pass
2019-07-06 19:54:52 +00:00
metadata = {
' author ' : author ,
' date ' : str ( datetime . datetime . now ( ) . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ) ,
' name ' : plugin ,
' info ' : info ,
' compiled-by ' : plugin_name ,
' content ' : data . decode ( ' utf-8 ' ) ,
' description ' : description
}
2018-05-13 03:45:32 +00:00
2018-06-01 04:25:28 +00:00
block = Block ( core = pluginapi . get_core ( ) )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
block . setType ( ' plugin ' )
block . setContent ( json . dumps ( metadata ) )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
hash = block . save ( True )
# hash = pluginapi.get_core().insertBlock(, header = 'plugin', sign = True)
2018-05-13 03:45:32 +00:00
if import_block :
2019-06-23 17:41:07 +00:00
importnewblocks . import_new_blocks ( pluginapi . get_core ( ) )
2018-05-13 03:45:32 +00:00
return hash
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Plugin %s does not exist. ' % plugin , terminal = True )
2018-05-13 03:45:32 +00:00
except Exception as e :
2019-06-20 00:59:05 +00:00
logger . error ( ' Failed to convert plugin to block. ' , error = e , timestamp = False , terminal = True )
2018-05-13 03:45:32 +00:00
return False
2018-05-14 04:11:31 +00:00
def installBlock ( block ) :
try :
2018-06-01 04:25:28 +00:00
block = Block ( block , core = pluginapi . get_core ( ) )
2019-07-06 19:54:52 +00:00
print ( block . getContent ( ) )
2018-05-16 03:08:42 +00:00
blockContent = json . loads ( block . getContent ( ) )
2018-05-14 04:11:31 +00:00
name = sanitize ( blockContent [ ' name ' ] )
author = blockContent [ ' author ' ]
date = blockContent [ ' date ' ]
version = None
if ' version ' in blockContent [ ' info ' ] :
version = blockContent [ ' info ' ] [ ' version ' ]
install = False
2019-06-20 00:59:05 +00:00
logger . info ( ( ' Will install %s ' + ( ' v ' + version if not version is None else ' ' ) + ' ( %s ), by %s ' ) % ( name , date , author ) , terminal = True )
2018-05-14 04:11:31 +00:00
# TODO: Convert to single line if statement
if os . path . exists ( pluginapi . plugins . get_folder ( name ) ) :
install = logger . confirm ( message = ' Continue with installation (will overwrite existing plugin) %s ? ' )
else :
install = logger . confirm ( message = ' Continue with installation %s ? ' )
if install :
2018-05-16 03:08:42 +00:00
blockToPlugin ( block . getHash ( ) )
2018-05-14 04:11:31 +00:00
addPlugin ( name )
else :
2019-06-20 00:59:05 +00:00
logger . info ( ' Installation cancelled. ' , terminal = True )
2018-05-14 04:11:31 +00:00
return False
return True
except Exception as e :
2019-06-20 00:59:05 +00:00
logger . error ( ' Failed to install plugin. ' , error = e , timestamp = False , terminal = True )
2018-05-14 04:11:31 +00:00
return False
def uninstallPlugin ( plugin ) :
try :
plugin = sanitize ( plugin )
pluginFolder = pluginapi . plugins . get_folder ( plugin )
exists = ( os . path . exists ( pluginFolder ) and not os . path . isfile ( pluginFolder ) )
installedByPluginManager = plugin in getPlugins ( )
remove = False
if not exists :
2019-06-20 00:59:05 +00:00
logger . warn ( ' Plugin %s does not exist. ' % plugin , timestamp = False , terminal = True )
2018-05-14 04:11:31 +00:00
return False
default = ' y '
if not installedByPluginManager :
2019-06-20 00:59:05 +00:00
logger . warn ( ' The plugin %s was not installed by %s . ' % ( plugin , plugin_name ) , timestamp = False , terminal = True )
2018-05-14 04:11:31 +00:00
default = ' n '
remove = logger . confirm ( message = ' All plugin data will be lost. Are you sure you want to proceed %s ? ' , default = default )
if remove :
if installedByPluginManager :
removePlugin ( plugin )
pluginapi . plugins . disable ( plugin )
shutil . rmtree ( pluginFolder )
2019-06-20 00:59:05 +00:00
logger . info ( ' Uninstallation of %s complete. ' % plugin , terminal = True )
2018-05-14 04:11:31 +00:00
return True
else :
logger . info ( ' Uninstallation cancelled. ' )
except Exception as e :
2019-06-20 00:59:05 +00:00
logger . error ( ' Failed to uninstall plugin. ' , error = e , terminal = True )
2018-05-14 04:11:31 +00:00
return False
2018-04-23 03:42:37 +00:00
# command handlers
def help ( ) :
2019-06-20 00:59:05 +00:00
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' <plugin> [public key/block hash] ' , terminal = True )
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' <plugin> [public key/block hash] ' , terminal = True )
2018-04-23 03:42:37 +00:00
def commandInstallPlugin ( ) :
if len ( sys . argv ) > = 3 :
check ( )
pluginname = sys . argv [ 2 ]
pkobh = None # public key or block hash
2018-05-14 04:11:31 +00:00
version = None
if ' : ' in pluginname :
details = pluginname
pluginname = sanitize ( details [ 0 ] )
version = details [ 1 ]
sanitize ( pluginname )
2018-04-23 03:42:37 +00:00
if len ( sys . argv ) > = 4 :
# public key or block hash specified
pkobh = sys . argv [ 3 ]
else :
# none specified, check if in config file
pkobh = getKey ( pluginname )
if pkobh is None :
2018-05-14 04:11:31 +00:00
# still nothing found, try searching repositories
2019-06-20 00:59:05 +00:00
logger . info ( ' Searching for public key in repositories... ' , terminal = True )
2018-05-14 04:11:31 +00:00
try :
repos = getRepositories ( )
distributors = list ( )
for repo , records in repos . items ( ) :
if pluginname in records :
2019-06-20 00:59:05 +00:00
logger . debug ( ' Found %s in repository %s for plugin %s . ' % ( records [ pluginname ] , repo , pluginname ) , terminal = True )
2018-05-14 04:11:31 +00:00
distributors . append ( records [ pluginname ] )
if len ( distributors ) != 0 :
distributor = None
if len ( distributors ) == 1 :
2019-06-20 00:59:05 +00:00
logger . info ( ' Found distributor: %s ' % distributors [ 0 ] , terminal = True )
2018-05-14 04:11:31 +00:00
distributor = distributors [ 0 ]
else :
distributors_message = ' '
index = 1
for dist in distributors :
distributors_message + = ' ' + logger . colors . bold + str ( index ) + ' ) ' + logger . colors . reset + str ( dist ) + ' \n '
index + = 1
2019-06-20 00:59:05 +00:00
logger . info ( ( logger . colors . bold + ' Found distributors ( %s ): ' + logger . colors . reset + ' \n ' + distributors_message ) % len ( distributors ) , terminal = True )
2018-05-14 04:11:31 +00:00
valid = False
while not valid :
2019-06-20 00:59:05 +00:00
choice = logger . readline ( ' Select the number of the key to use, from 1 to %s , or press Ctrl+C to cancel: ' % ( index - 1 ) , terminal = True )
2018-05-14 04:11:31 +00:00
try :
2018-06-01 04:25:28 +00:00
choice = int ( choice )
if choice < = index and choice > = 1 :
2018-05-14 04:11:31 +00:00
distributor = distributors [ int ( choice ) ]
valid = True
except KeyboardInterrupt :
2019-06-20 00:59:05 +00:00
logger . info ( ' Installation cancelled. ' , terminal = True )
2018-05-14 04:11:31 +00:00
return True
except :
pass
if not distributor is None :
pkobh = distributor
except Exception as e :
2019-06-20 00:59:05 +00:00
logger . warn ( ' Failed to lookup plugin in repositories. ' , timestamp = False , terminal = True )
2018-06-01 04:25:28 +00:00
return True
2018-05-14 04:11:31 +00:00
if pkobh is None :
2019-06-20 00:59:05 +00:00
logger . error ( ' No key for this plugin found in keystore or repositories, please specify. ' , timestamp = False , terminal = True )
2018-11-11 03:25:40 +00:00
2018-04-23 03:42:37 +00:00
return True
2019-06-25 23:07:35 +00:00
valid_hash = stringvalidators . validate_hash ( pkobh )
2018-04-23 03:42:37 +00:00
real_block = False
2019-06-25 08:21:36 +00:00
valid_key = stringvalidators . validate_pub_key ( pkobh )
2018-04-23 03:42:37 +00:00
real_key = False
if valid_hash :
2018-05-16 03:08:42 +00:00
real_block = Block . exists ( pkobh )
2018-04-23 03:42:37 +00:00
elif valid_key :
2019-06-25 08:21:36 +00:00
real_key = pkobh in pluginapi . get_core ( ) . listPeers ( )
2018-04-23 03:42:37 +00:00
blockhash = None
if valid_hash and not real_block :
2019-06-20 00:59:05 +00:00
logger . error ( ' Block hash not found. Perhaps it has not been synced yet? ' , timestamp = False , terminal = True )
logger . debug ( ' Is valid hash, but does not belong to a known block. ' , terminal = True )
2018-05-14 04:11:31 +00:00
2018-04-23 03:42:37 +00:00
return True
elif valid_hash and real_block :
blockhash = str ( pkobh )
2019-06-20 00:59:05 +00:00
logger . debug ( ' Using block %s ... ' % blockhash , terminal = True )
2018-05-11 05:18:39 +00:00
2018-05-13 03:45:32 +00:00
installBlock ( blockhash )
2018-04-23 03:42:37 +00:00
elif valid_key and not real_key :
2019-06-20 00:59:05 +00:00
logger . error ( ' Public key not found. Try adding the node by address manually, if possible. ' , timestamp = False , terminal = True )
logger . debug ( ' Is valid key, but the key is not a known one. ' , terminal = True )
2018-04-23 03:42:37 +00:00
elif valid_key and real_key :
publickey = str ( pkobh )
2019-06-20 00:59:05 +00:00
logger . debug ( ' Using public key %s ... ' % publickey , terminal = True )
2018-04-23 03:42:37 +00:00
saveKey ( pluginname , pkobh )
2018-05-11 05:18:39 +00:00
2018-05-16 03:08:42 +00:00
signedBlocks = Block . getBlocks ( type = ' plugin ' , signed = True , signer = publickey )
2018-05-14 04:11:31 +00:00
mostRecentTimestamp = None
mostRecentVersionBlock = None
2018-05-16 03:08:42 +00:00
for block in signedBlocks :
2018-05-14 04:11:31 +00:00
try :
2018-05-16 03:08:42 +00:00
blockContent = json . loads ( block . getContent ( ) )
2018-05-14 04:11:31 +00:00
if not ( ( ' author ' in blockContent ) and ( ' info ' in blockContent ) and ( ' date ' in blockContent ) and ( ' name ' in blockContent ) ) :
2018-05-16 03:08:42 +00:00
raise ValueError ( ' Missing required parameter `date` in block %s . ' % block . getHash ( ) )
2018-05-14 04:11:31 +00:00
blockDatetime = datetime . datetime . strptime ( blockContent [ ' date ' ] , ' % Y- % m- %d % H: % M: % S ' )
if blockContent [ ' name ' ] == pluginname :
if ( ' version ' in blockContent [ ' info ' ] ) and ( blockContent [ ' info ' ] [ ' version ' ] == version ) and ( not version is None ) :
mostRecentTimestamp = blockDatetime
2018-05-16 03:08:42 +00:00
mostRecentVersionBlock = block . getHash ( )
2018-05-14 04:11:31 +00:00
break
elif mostRecentTimestamp is None :
mostRecentTimestamp = blockDatetime
2018-05-16 03:08:42 +00:00
mostRecentVersionBlock = block . getHash ( )
2018-05-14 04:11:31 +00:00
elif blockDatetime > mostRecentTimestamp :
mostRecentTimestamp = blockDatetime
2018-05-16 03:08:42 +00:00
mostRecentVersionBlock = block . getHash ( )
2018-05-14 04:11:31 +00:00
except Exception as e :
pass
2019-06-20 00:59:05 +00:00
logger . warn ( ' Only continue the installation if you are absolutely certain that you trust the plugin distributor. Public key of plugin distributor: %s ' % publickey , timestamp = False , terminal = True )
logger . debug ( ' Most recent block matching parameters is %s ' % mostRecentVersionBlock , terminal = True )
2018-05-14 04:11:31 +00:00
installBlock ( mostRecentVersionBlock )
2018-04-23 03:42:37 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Unknown data " %s " ; must be public key or block hash. ' % str ( pkobh ) , timestamp = False , terminal = True )
2018-04-23 03:42:37 +00:00
return
else :
2019-06-20 00:59:05 +00:00
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' <plugin> [public key/block hash] ' , terminal = True )
2018-04-23 03:42:37 +00:00
return True
2018-04-21 05:04:03 +00:00
2018-04-23 03:42:37 +00:00
def commandUninstallPlugin ( ) :
2018-05-14 04:11:31 +00:00
if len ( sys . argv ) > = 3 :
uninstallPlugin ( sys . argv [ 2 ] )
else :
2019-06-20 00:59:05 +00:00
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' <plugin> ' , terminal = True )
2018-05-14 04:11:31 +00:00
return True
2018-04-21 05:04:03 +00:00
2018-04-23 03:42:37 +00:00
def commandSearchPlugin ( ) :
2019-06-20 00:59:05 +00:00
logger . info ( ' This feature has not been created yet. Please check back later. ' , terminal = True )
2018-05-14 04:11:31 +00:00
return True
def commandAddRepository ( ) :
2018-05-15 06:16:55 +00:00
if len ( sys . argv ) > = 3 :
check ( )
blockhash = sys . argv [ 2 ]
2019-06-25 23:07:35 +00:00
if stringvalidators . validate_hash ( blockhash ) :
2018-05-16 03:08:42 +00:00
if Block . exists ( blockhash ) :
2018-05-15 06:16:55 +00:00
try :
2018-06-01 04:25:28 +00:00
blockContent = json . loads ( Block ( blockhash , core = pluginapi . get_core ( ) ) . getContent ( ) )
2018-05-15 06:16:55 +00:00
pluginslist = dict ( )
2018-06-01 04:25:28 +00:00
for pluginname , distributor in blockContent [ ' plugins ' ] :
2019-06-25 08:21:36 +00:00
if stringvalidators . validate_pub_key ( distributor ) :
2018-05-15 06:16:55 +00:00
pluginslist [ pluginname ] = distributor
2019-06-20 00:59:05 +00:00
logger . debug ( ' Found %s records in repository. ' % len ( pluginslist ) , terminal = True )
2018-05-15 06:16:55 +00:00
if len ( pluginslist ) != 0 :
addRepository ( blockhash , pluginslist )
2019-06-20 00:59:05 +00:00
logger . info ( ' Successfully added repository. ' , terminal = True )
2018-05-15 06:16:55 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Repository contains no records, not importing. ' , timestamp = False , terminal = True )
2018-05-15 06:16:55 +00:00
except Exception as e :
2019-06-20 00:59:05 +00:00
logger . error ( ' Failed to parse block. ' , error = e , terminal = True )
2018-05-15 06:16:55 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Block hash not found. Perhaps it has not been synced yet? ' , timestamp = False , terminal = True )
2018-05-15 06:16:55 +00:00
logger . debug ( ' Is valid hash, but does not belong to a known block. ' )
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Unknown data " %s " ; must be block hash. ' % str ( pkobh ) , timestamp = False , terminal = True )
2018-05-15 06:16:55 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' [block hash] ' , terminal = True )
2018-05-15 06:16:55 +00:00
2018-05-14 04:11:31 +00:00
return True
def commandRemoveRepository ( ) :
2018-05-15 06:16:55 +00:00
if len ( sys . argv ) > = 3 :
check ( )
blockhash = sys . argv [ 2 ]
2019-06-25 23:07:35 +00:00
if stringvalidators . validate_hash ( blockhash ) :
2018-05-15 06:16:55 +00:00
if blockhash in getRepositories ( ) :
try :
removeRepository ( blockhash )
2019-06-20 00:59:05 +00:00
logger . info ( ' Successfully removed repository. ' , terminal = True )
2018-05-15 06:16:55 +00:00
except Exception as e :
2019-06-20 00:59:05 +00:00
logger . error ( ' Failed to parse block. ' , error = e , terminal = True )
2018-05-15 06:16:55 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Repository has not been imported, nothing to remove. ' , timestamp = False , terminal = True )
2018-05-15 06:16:55 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Unknown data " %s " ; must be block hash. ' % str ( pkobh ) , terminal = True )
2018-05-15 06:16:55 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' [block hash] ' , terminal = True )
2018-05-15 06:16:55 +00:00
2018-05-14 04:11:31 +00:00
return True
def commandPublishPlugin ( ) :
if len ( sys . argv ) > = 3 :
check ( )
pluginname = sanitize ( sys . argv [ 2 ] )
pluginfolder = pluginapi . plugins . get_folder ( pluginname )
if os . path . exists ( pluginfolder ) and not os . path . isfile ( pluginfolder ) :
block = pluginToBlock ( pluginname )
2019-06-20 00:59:05 +00:00
logger . info ( ' Plugin saved in block %s . ' % block , terminal = True )
2018-05-14 04:11:31 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Plugin %s does not exist. ' % pluginname , timestamp = False , terminal = True )
2018-05-14 04:11:31 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' <plugin> ' , terminal = True )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
def commandCreateRepository ( ) :
if len ( sys . argv ) > = 3 :
check ( )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
plugins = list ( )
script = sys . argv [ 0 ]
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
del sys . argv [ : 2 ]
success = True
for pluginname in sys . argv :
distributor = None
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
if ' : ' in pluginname :
split = pluginname . split ( ' : ' )
pluginname = split [ 0 ]
distributor = split [ 1 ]
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
pluginname = sanitize ( pluginname )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
if distributor is None :
distributor = getKey ( pluginname )
if distributor is None :
2019-06-20 00:59:05 +00:00
logger . error ( ' No distributor key was found for the plugin %s . ' % pluginname , timestamp = False , terminal = True )
2018-06-01 04:25:28 +00:00
success = False
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
plugins . append ( [ pluginname , distributor ] )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
if not success :
2019-06-20 00:59:05 +00:00
logger . error ( ' Please correct the above errors, then recreate the repository. ' , terminal = True )
2018-06-01 04:25:28 +00:00
return True
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
blockhash = createRepository ( plugins )
if not blockhash is None :
2019-06-20 00:59:05 +00:00
logger . info ( ' Successfully created repository. Execute the following command to add the repository: \n ' + logger . colors . underline + ' %s --add-repository %s ' % ( script , blockhash ) , terminal = True )
2018-06-01 04:25:28 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . error ( ' Failed to create repository, an unknown error occurred. ' , terminal = True )
2018-06-01 04:25:28 +00:00
else :
2019-06-20 00:59:05 +00:00
logger . info ( sys . argv [ 0 ] + ' ' + sys . argv [ 1 ] + ' [plugins...] ' , terminal = True )
2018-11-11 03:25:40 +00:00
2018-06-01 04:25:28 +00:00
return True
2018-11-11 03:25:40 +00:00
2018-04-21 05:04:03 +00:00
# event listeners
def on_init ( api , data = None ) :
global pluginapi
pluginapi = api
2018-04-23 03:42:37 +00:00
check ( )
2018-04-21 05:04:03 +00:00
# register some commands
2018-04-23 03:42:37 +00:00
api . commands . register ( [ ' install-plugin ' , ' installplugin ' , ' plugin-install ' , ' install ' , ' plugininstall ' ] , commandInstallPlugin )
api . commands . register ( [ ' remove-plugin ' , ' removeplugin ' , ' plugin-remove ' , ' uninstall-plugin ' , ' uninstallplugin ' , ' plugin-uninstall ' , ' uninstall ' , ' remove ' , ' pluginremove ' ] , commandUninstallPlugin )
api . commands . register ( [ ' search ' , ' filter-plugins ' , ' search-plugins ' , ' searchplugins ' , ' search-plugin ' , ' searchplugin ' , ' findplugin ' , ' find-plugin ' , ' filterplugin ' , ' plugin-search ' , ' pluginsearch ' ] , commandSearchPlugin )
2018-05-14 04:11:31 +00:00
api . commands . register ( [ ' add-repo ' , ' add-repository ' , ' addrepo ' , ' addrepository ' , ' repository-add ' , ' repo-add ' , ' repoadd ' , ' addrepository ' , ' add-plugin-repository ' , ' add-plugin-repo ' , ' add-pluginrepo ' , ' add-pluginrepository ' , ' addpluginrepo ' , ' addpluginrepository ' ] , commandAddRepository )
api . commands . register ( [ ' remove-repo ' , ' remove-repository ' , ' removerepo ' , ' removerepository ' , ' repository-remove ' , ' repo-remove ' , ' reporemove ' , ' removerepository ' , ' remove-plugin-repository ' , ' remove-plugin-repo ' , ' remove-pluginrepo ' , ' remove-pluginrepository ' , ' removepluginrepo ' , ' removepluginrepository ' , ' rm-repo ' , ' rm-repository ' , ' rmrepo ' , ' rmrepository ' , ' repository-rm ' , ' repo-rm ' , ' reporm ' , ' rmrepository ' , ' rm-plugin-repository ' , ' rm-plugin-repo ' , ' rm-pluginrepo ' , ' rm-pluginrepository ' , ' rmpluginrepo ' , ' rmpluginrepository ' ] , commandRemoveRepository )
api . commands . register ( [ ' publish-plugin ' , ' plugin-publish ' , ' publishplugin ' , ' pluginpublish ' , ' publish ' ] , commandPublishPlugin )
2018-06-01 04:25:28 +00:00
api . commands . register ( [ ' create-repository ' , ' create-repo ' , ' createrepo ' , ' createrepository ' , ' repocreate ' ] , commandCreateRepository )
2018-04-21 05:04:03 +00:00
# add help menus once the features are actually implemented
return