PrivateBin CLI on python 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
5.7 KiB

#!/usr/bin/env python
import os, sys, argparse
7 years ago
import pbincli.actions
from pbincli.api import PrivateBin, Shortener
from pbincli.utils import PBinCLIException, validate_url
7 years ago
6 years ago
CONFIG_PATHS = [os.path.join(".", "pbincli.conf", ),
os.path.join(os.getenv("HOME") or "~", ".config", "pbincli", "pbincli.conf") ]
def read_config(filename):
"""Read config variables from a file"""
settings = {}
with open(filename) as f:
for l in f.readlines():
key, value = l.strip().split("=")
settings[key.strip()] = value.strip()
return settings
7 years ago
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title="actions", help="List of commands")
7 years ago
7 years ago
# a send command
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")
send_parser.add_argument("-E", "--expire", default="1day", action="store",
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")
send_parser.add_argument("-F", "--format", default="plaintext", action="store",
choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)")
send_parser.add_argument("-q", "--notext", default=False, action="store_true", help="don't send text in paste")
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")
# URL shortener
send_parser.add_argument("-s", "--shorten", default=False, action="store_true", help="use URL shortener")
send_parser.add_argument("--shorten-api", default="yourls", action="store",
choices=["clckru", "yourls"], help="API used by shortener service (default: YOURLS)")
send_parser.add_argument("--shorten-url", help="URL of shortener service API")
send_parser.add_argument("--shorten-user", help="Shortener username")
send_parser.add_argument("--shorten-pass", help="Shortener password")
send_parser.add_argument("--shorten-token", help="Shortener token")
# Certificates check bypass
send_parser.add_argument("--no-check-certificate", default=True, action="store_false", help="disable certificate validation")
send_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)")
#
send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run")
send_parser.add_argument("stdin", help="input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin)
7 years ago
send_parser.set_defaults(func=pbincli.actions.send)
7 years ago
# a get command
get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance")
7 years ago
get_parser.add_argument("pasteinfo", help="example: aabb#cccddd")
get_parser.add_argument("-p", "--password", help="password for decrypting paste")
get_parser.add_argument("--no-check-certificate", default=True, action="store_false", help="disable certificate validation")
get_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)")
get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
7 years ago
get_parser.set_defaults(func=pbincli.actions.get)
# a delete command
delete_parser = subparsers.add_parser("delete", description="Delete paste from PrivateBin instance using token")
delete_parser.add_argument("-p", "--paste", required=True, help="paste id")
delete_parser.add_argument("-t", "--token", required=True, help="paste deletion token")
delete_parser.add_argument("--no-check-certificate", default=True, action="store_false", help="disable certificate validation")
delete_parser.add_argument("--no-insecure-warning", default=False, action="store_true", help="suppress InsecureRequestWarning (only with --no-check-certificate)")
delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
delete_parser.set_defaults(func=pbincli.actions.delete)
7 years ago
# parse arguments
args = parser.parse_args()
CONFIG = {
"server": "https://paste.i2pd.xyz/",
"proxy": None
}
6 years ago
for p in CONFIG_PATHS:
if os.path.exists(p):
6 years ago
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)
SETTINGS = {
"server" : validate_url(CONFIG["server"]),
"proxy": CONFIG["proxy"],
"nocheckcert": args.no_check_certificate,
"noinsecurewarn": args.no_insecure_warning
}
api_client = PrivateBin(SETTINGS)
7 years ago
if hasattr(args, "func"):
try:
args.func(args, api_client)
7 years ago
except PBinCLIException as pe:
raise PBinCLIException("error: {}".format(pe))
7 years ago
else:
parser.print_help()
7 years ago
if __name__ == "__main__":
main()