2018-02-15 23:02:52 +00:00
#!/usr/bin/env python
2017-03-02 11:23:05 +00:00
import os , sys , argparse
2018-05-23 08:14:35 +00:00
2017-02-18 18:00:40 +00:00
import pbincli . actions
2019-09-18 10:22:58 +00:00
from pbincli . api import PrivateBin
2022-01-24 05:06:07 +00:00
from pbincli . utils import PBinCLIException , PBinCLIError , validate_url_ending
2017-02-18 18:00:40 +00:00
2022-01-09 22:24:06 +00:00
CONFIG_PATHS = [
os . path . join ( " . " , " pbincli.conf " , ) ,
2022-01-11 11:15:05 +00:00
os . path . join ( os . getenv ( " HOME " ) or " ~ " , " .config " , " pbincli " , " pbincli.conf " )
2022-01-09 22:24:06 +00:00
]
2018-05-24 08:15:14 +00:00
2022-01-11 11:15:05 +00:00
if sys . platform == " win32 " :
CONFIG_PATHS . append ( os . path . join ( os . getenv ( " APPDATA " ) , " pbincli " , " pbincli.conf " ) )
elif sys . platform == " darwin " :
CONFIG_PATHS . append ( os . path . join ( os . getenv ( " HOME " ) or " ~ " , " Library " , " Application Support " , " pbincli " , " pbincli.conf " ) )
2018-05-24 08:15:14 +00:00
def read_config ( filename ) :
""" Read config variables from a file """
settings = { }
with open ( filename ) as f :
for l in f . readlines ( ) :
2020-06-22 11:22:21 +00:00
if len ( l . strip ( ) ) == 0 :
continue
try :
2022-01-24 04:13:26 +00:00
key , value = l . strip ( ) . split ( " = " , 1 )
2020-06-22 11:22:21 +00:00
settings [ key . strip ( ) ] = value . strip ( )
2020-06-22 11:33:34 +00:00
except ValueError :
2020-06-22 11:22:21 +00:00
PBinCLIError ( " Unable to parse config file, please check it for errors. " )
2018-05-24 08:15:14 +00:00
return settings
2017-03-02 10:09:52 +00:00
2017-02-18 18:00:40 +00:00
def main ( ) :
2022-01-24 16:07:40 +00:00
parser = argparse . ArgumentParser ( description = ' Full-featured PrivateBin command-line client ' )
2017-02-18 18:00:40 +00:00
subparsers = parser . add_subparsers ( title = " actions " , help = " List of commands " )
2017-02-18 18:27:24 +00:00
2017-02-18 18:00:40 +00:00
# a send command
2019-06-07 11:11:53 +00:00
send_parser = subparsers . add_parser ( " send " , description = " Send data to PrivateBin instance " )
send_parser . add_argument ( " -t " , " --text " , help = " text in quotes. Ignored if used stdin. If not used, forcefully used stdin " )
send_parser . add_argument ( " -f " , " --file " , help = " example: image.jpg or full path to file " )
send_parser . add_argument ( " -p " , " --password " , help = " password for encrypting paste " )
2017-02-23 11:13:03 +00:00
send_parser . add_argument ( " -E " , " --expire " , default = " 1day " , action = " store " ,
2019-06-07 11:11:53 +00:00
choices = [ " 5min " , " 10min " , " 1hour " , " 1day " , " 1week " , " 1month " , " 1year " , " never " ] , help = " paste lifetime (default: 1day) " )
send_parser . add_argument ( " -B " , " --burn " , default = False , action = " store_true " , help = " burn sent paste after reading " )
send_parser . add_argument ( " -D " , " --discus " , default = False , action = " store_true " , help = " open discussion for sent paste " )
2017-02-23 11:13:03 +00:00
send_parser . add_argument ( " -F " , " --format " , default = " plaintext " , action = " store " ,
2018-05-22 16:26:49 +00:00
choices = [ " plaintext " , " syntaxhighlighting " , " markdown " ] , help = " format of text (default: plaintext) " )
2019-06-01 14:26:17 +00:00
send_parser . add_argument ( " -q " , " --notext " , default = False , action = " store_true " , help = " don ' t send text in paste " )
2019-06-07 09:12:00 +00:00
send_parser . add_argument ( " -c " , " --compression " , default = " zlib " , action = " store " ,
choices = [ " zlib " , " none " ] , help = " set compression for paste (default: zlib). Note: works only on v2 paste format " )
2020-04-11 17:59:19 +00:00
## URL shortener
2019-09-17 10:54:58 +00:00
send_parser . add_argument ( " -S " , " --short " , default = False , action = " store_true " , help = " use URL shortener " )
2020-01-07 00:58:19 +00:00
send_parser . add_argument ( " --short-api " , default = argparse . SUPPRESS , action = " store " ,
2022-01-09 23:00:25 +00:00
choices = [ " tinyurl " , " clckru " , " isgd " , " vgd " , " cuttly " , " yourls " , " custom " ] , help = " API used by shortener service " )
2019-09-17 10:54:58 +00:00
send_parser . add_argument ( " --short-url " , default = argparse . SUPPRESS , help = " URL of shortener service API " )
send_parser . add_argument ( " --short-user " , default = argparse . SUPPRESS , help = " Shortener username " )
send_parser . add_argument ( " --short-pass " , default = argparse . SUPPRESS , help = " Shortener password " )
send_parser . add_argument ( " --short-token " , default = argparse . SUPPRESS , help = " Shortener token " )
2020-04-11 17:59:19 +00:00
## Connection options
2022-01-24 05:06:07 +00:00
send_parser . add_argument ( " -s " , " --server " , default = argparse . SUPPRESS , help = " Instance URL (default: https://paste.i2pd.xyz/) " )
2019-09-17 10:54:58 +00:00
send_parser . add_argument ( " -x " , " --proxy " , default = argparse . SUPPRESS , help = " Proxy server address (default: None) " )
send_parser . add_argument ( " --no-check-certificate " , default = False , action = " store_true " , help = " disable certificate validation " )
2020-01-07 00:58:19 +00:00
send_parser . add_argument ( " --no-insecure-warning " , default = False , action = " store_true " ,
help = " suppress InsecureRequestWarning (only with --no-check-certificate) " )
2020-04-11 17:59:19 +00:00
##
2022-01-09 22:24:06 +00:00
send_parser . add_argument ( " -L " , " --mirrors " , default = argparse . SUPPRESS , help = " Comma-separated list of mirrors of service with scheme (default: None) " )
2021-06-04 20:13:26 +00:00
send_parser . add_argument ( " -v " , " --verbose " , default = False , action = " store_true " , help = " enable verbose output " )
send_parser . add_argument ( " -d " , " --debug " , default = False , action = " store_true " , help = " enable debug output " )
2018-02-12 14:28:18 +00:00
send_parser . add_argument ( " --dry " , default = False , action = " store_true " , help = " invoke dry run " )
2018-02-16 08:56:13 +00:00
send_parser . add_argument ( " stdin " , help = " input paste text from stdin " , nargs = " ? " , type = argparse . FileType ( " r " ) , default = sys . stdin )
2017-02-18 18:00:40 +00:00
send_parser . set_defaults ( func = pbincli . actions . send )
2017-02-18 18:27:24 +00:00
2019-06-07 11:11:53 +00:00
# a get command
get_parser = subparsers . add_parser ( " get " , description = " Get data from PrivateBin instance " )
2022-01-24 05:06:07 +00:00
get_parser . add_argument ( " pasteinfo " , help = " \" PasteID#Passphrase \" or full URL " )
2017-02-20 22:27:32 +00:00
get_parser . add_argument ( " -p " , " --password " , help = " password for decrypting paste " )
2020-04-11 17:59:19 +00:00
## Connection options
2022-01-24 05:06:07 +00:00
get_parser . add_argument ( " -s " , " --server " , default = argparse . SUPPRESS , help = " Instance URL (default: https://paste.i2pd.xyz/, ignored if URL used in pasteinfo) " )
2020-01-06 18:22:11 +00:00
get_parser . add_argument ( " -x " , " --proxy " , default = argparse . SUPPRESS , help = " Proxy server address (default: None) " )
2019-09-17 10:54:58 +00:00
get_parser . add_argument ( " --no-check-certificate " , default = False , action = " store_true " , help = " disable certificate validation " )
2020-01-07 00:58:19 +00:00
get_parser . add_argument ( " --no-insecure-warning " , default = False , action = " store_true " ,
help = " suppress InsecureRequestWarning (only with --no-check-certificate) " )
2020-04-11 17:59:19 +00:00
##
2021-06-04 20:13:26 +00:00
get_parser . add_argument ( " -v " , " --verbose " , default = False , action = " store_true " , help = " enable verbose output " )
get_parser . add_argument ( " -d " , " --debug " , default = False , action = " store_true " , help = " enable debug output " )
2017-02-20 12:13:42 +00:00
get_parser . set_defaults ( func = pbincli . actions . get )
2019-06-07 11:11:53 +00:00
# a delete command
delete_parser = subparsers . add_parser ( " delete " , description = " Delete paste from PrivateBin instance using token " )
2017-02-21 13:33:03 +00:00
delete_parser . add_argument ( " -p " , " --paste " , required = True , help = " paste id " )
2019-06-07 11:11:53 +00:00
delete_parser . add_argument ( " -t " , " --token " , required = True , help = " paste deletion token " )
2020-04-11 17:59:19 +00:00
## Connection options
2022-01-24 05:06:07 +00:00
delete_parser . add_argument ( " -s " , " --server " , default = argparse . SUPPRESS , help = " Instance URL (default: https://paste.i2pd.xyz/) " )
2020-01-06 18:22:11 +00:00
delete_parser . add_argument ( " -x " , " --proxy " , default = argparse . SUPPRESS , help = " Proxy server address (default: None) " )
2019-09-17 10:54:58 +00:00
delete_parser . add_argument ( " --no-check-certificate " , default = False , action = " store_true " , help = " disable certificate validation " )
2020-01-07 00:58:19 +00:00
delete_parser . add_argument ( " --no-insecure-warning " , default = False , action = " store_true " ,
help = " suppress InsecureRequestWarning (only with --no-check-certificate) " )
2020-04-11 17:59:19 +00:00
##
2021-06-04 20:13:26 +00:00
delete_parser . add_argument ( " -v " , " --verbose " , default = False , action = " store_true " , help = " enable verbose output " )
delete_parser . add_argument ( " -d " , " --debug " , default = False , action = " store_true " , help = " enable debug output " )
2017-02-21 13:33:03 +00:00
delete_parser . set_defaults ( func = pbincli . actions . delete )
2017-02-18 18:00:40 +00:00
# parse arguments
args = parser . parse_args ( )
2018-05-23 08:14:35 +00:00
2019-07-16 20:32:37 +00:00
CONFIG = {
2019-09-17 10:54:58 +00:00
' server ' : ' https://paste.i2pd.xyz/ ' ,
2022-02-02 13:09:25 +00:00
' mirrors ' : None ,
2019-09-17 10:54:58 +00:00
' proxy ' : None ,
2022-02-02 13:09:25 +00:00
' expire ' : None ,
' burn ' : False ,
' discus ' : False ,
' format ' : None ,
2022-01-24 16:06:29 +00:00
' short ' : False ,
2019-09-17 10:54:58 +00:00
' short_api ' : None ,
' short_url ' : None ,
' short_user ' : None ,
' short_pass ' : None ,
' short_token ' : None ,
' no_check_certificate ' : False ,
2022-02-02 13:09:25 +00:00
' no_insecure_warning ' : False ,
' compression ' : None
2019-07-16 20:32:37 +00:00
}
2018-05-24 08:15:14 +00:00
2019-09-18 10:22:58 +00:00
# Configuration preference order:
# 1. Command line switches
# 2. Environment variables
# 3. Configuration file
# 4. Default values below
2019-09-17 10:54:58 +00:00
2018-05-24 08:15:14 +00:00
for p in CONFIG_PATHS :
2018-05-31 17:09:48 +00:00
if os . path . exists ( p ) :
2018-05-24 08:15:14 +00:00
CONFIG . update ( read_config ( p ) )
break
for key in CONFIG . keys ( ) :
var = " PRIVATEBIN_ {} " . format ( key . upper ( ) )
if var in os . environ : CONFIG [ key ] = os . getenv ( var )
2019-09-17 10:54:58 +00:00
# values from command line switches are preferred
args_var = vars ( args )
if key in args_var :
CONFIG [ key ] = args_var [ key ]
2018-05-23 08:14:35 +00:00
2019-09-17 10:54:58 +00:00
# Re-validate PrivateBin instance URL
2022-01-24 05:06:07 +00:00
CONFIG [ ' server ' ] = validate_url_ending ( CONFIG [ ' server ' ] )
2019-07-16 20:32:37 +00:00
2019-09-17 10:54:58 +00:00
api_client = PrivateBin ( CONFIG )
2018-05-23 08:14:35 +00:00
2017-02-18 18:00:40 +00:00
if hasattr ( args , " func " ) :
try :
2019-09-17 10:54:58 +00:00
args . func ( args , api_client , settings = CONFIG )
2017-02-18 18:00:40 +00:00
except PBinCLIException as pe :
2019-09-17 08:55:59 +00:00
raise PBinCLIException ( " error: {} " . format ( pe ) )
2017-02-18 18:00:40 +00:00
else :
parser . print_help ( )
2017-03-02 10:09:52 +00:00
2017-02-18 18:00:40 +00:00
if __name__ == " __main__ " :
main ( )