Browse Source

Add URL support for paste deletion, rewrite info parsing code

Signed-off-by: R4SAS <r4sas@i2pmail.org>
gha
R4SAS 2 years ago
parent
commit
7b90b6a772
Signed by untrusted user: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 2
      pbincli/__init__.py
  2. 49
      pbincli/actions.py
  3. 55
      pbincli/cli.py
  4. 7
      pbincli/utils.py

2
pbincli/__init__.py

@ -2,6 +2,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__author__ = "R4SAS <r4sas@i2pmail.org>" __author__ = "R4SAS <r4sas@i2pmail.org>"
__version__ = "0.3.2" __version__ = "0.3.3"
__copyright__ = "Copyright (c) R4SAS" __copyright__ = "Copyright (c) R4SAS"
__license__ = "MIT" __license__ = "MIT"

49
pbincli/actions.py

@ -1,16 +1,20 @@
from pbincli.format import Paste
from pbincli.utils import PBinCLIError, validate_url_ending
import signal import signal
from urllib.parse import urlparse, parse_qsl
from pbincli.api import Shortener
from pbincli.format import Paste
from pbincli.utils import PBinCLIError, check_writable, json_encode, uri_validator, validate_url_ending
def signal_handler(sig, frame): def signal_handler(sig, frame):
print('Keyboard interrupt received, terminating…') print('Keyboard interrupt received, terminating…')
exit(0) exit(0)
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
def send(args, api_client, settings=None): def send(args, api_client, settings=None):
from pbincli.api import Shortener
if args.short: if args.short:
shortener = Shortener(settings) shortener = Shortener(settings)
@ -117,19 +121,17 @@ def send(args, api_client, settings=None):
def get(args, api_client, settings=None): def get(args, api_client, settings=None):
from pbincli.utils import check_writable, json_encode, uri_validator parseduri, isuri = uri_validator(args.pasteinfo)
try: if isuri and parseduri.query and parseduri.fragment:
if uri_validator(args.pasteinfo): api_client.server = args.pasteinfo.split("?")[0]
api_client.server, pasteinfo = args.pasteinfo.split("?") pasteid = parseduri.query
pasteid, passphrase = pasteinfo.split("#") passphrase = parseduri.fragment
else: elif parseduri.path and parseduri.path != "/" and parseduri.fragment:
pasteid, passphrase = args.pasteinfo.split("#") pasteid = parseduri.path
except ValueError: passphrase = parseduri.fragment
PBinCLIError("Provided info hasn't contain valid PasteID#Passphrase string") else:
PBinCLIError("Provided info hasn't contain valid URL or PasteID#Passphrase string")
if not (pasteid and passphrase):
PBinCLIError("Incorrect request")
if args.verbose: print("Used server: {}".format(api_client.getServer())) if args.verbose: print("Used server: {}".format(api_client.getServer()))
if args.debug: print("PasteID:\t{}\nPassphrase:\t{}".format(pasteid, passphrase)) if args.debug: print("PasteID:\t{}\nPassphrase:\t{}".format(pasteid, passphrase))
@ -194,10 +196,19 @@ def get(args, api_client, settings=None):
def delete(args, api_client, settings=None): def delete(args, api_client, settings=None):
from pbincli.utils import json_encode parseduri, isuri = uri_validator(args.pasteinfo)
pasteid = args.paste if isuri:
token = args.token api_client.server = args.pasteinfo.split("?")[0]
query = dict(parse_qsl(parseduri.query))
else:
query = dict(parse_qsl(args.pasteinfo))
if 'pasteid' in query and 'deletetoken' in query:
pasteid = query['pasteid']
token = query['deletetoken']
else:
PBinCLIError("Provided info hasn't contain required information")
if args.verbose: print("Used server: {}".format(api_client.getServer())) if args.verbose: print("Used server: {}".format(api_client.getServer()))
if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token)) if args.debug: print("PasteID:\t{}\nToken:\t\t{}".format(pasteid, token))

55
pbincli/cli.py

@ -36,20 +36,20 @@ def main():
# a send command # a send command
send_parser = subparsers.add_parser("send", description="Send data to PrivateBin instance") 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("-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("-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("-p", "--password", help="Password for encrypting paste")
send_parser.add_argument("-E", "--expire", default="1day", action="store", send_parser.add_argument("-E", "--expire", default="1day", action="store",
choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="paste lifetime (default: 1day)") 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("-B", "--burn", default=False, action="store_true", help="Set \"Burn after reading\" flag")
send_parser.add_argument("-D", "--discus", default=False, action="store_true", help="open discussion for sent paste") 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", send_parser.add_argument("-F", "--format", default="plaintext", action="store",
choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)") 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("-q", "--notext", default=False, action="store_true", help="Don't send text in paste")
send_parser.add_argument("-c", "--compression", default="zlib", action="store", 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") choices=["zlib", "none"], help="Set compression for paste (default: zlib). Note: works only on v2 paste format")
## URL shortener ## URL shortener
send_parser.add_argument("-S", "--short", default=False, action="store_true", help="use URL shortener") send_parser.add_argument("-S", "--short", default=False, action="store_true", help="Use URL shortener")
send_parser.add_argument("--short-api", default=argparse.SUPPRESS, action="store", send_parser.add_argument("--short-api", default=argparse.SUPPRESS, action="store",
choices=["tinyurl", "clckru", "isgd", "vgd", "cuttly", "yourls", "custom"], help="API used by shortener service") choices=["tinyurl", "clckru", "isgd", "vgd", "cuttly", "yourls", "custom"], help="API used by shortener service")
send_parser.add_argument("--short-url", default=argparse.SUPPRESS, help="URL of shortener service API") send_parser.add_argument("--short-url", default=argparse.SUPPRESS, help="URL of shortener service API")
@ -59,45 +59,44 @@ def main():
## Connection options ## Connection options
send_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)") send_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)")
send_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)") 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") send_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
send_parser.add_argument("--no-insecure-warning", default=False, action="store_true", send_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
help="suppress InsecureRequestWarning (only with --no-check-certificate)") help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
## ##
send_parser.add_argument("-L", "--mirrors", default=argparse.SUPPRESS, help="Comma-separated list of mirrors of service with scheme (default: None)") send_parser.add_argument("-L", "--mirrors", default=argparse.SUPPRESS, help="Comma-separated list of mirrors of service with scheme (default: None)")
send_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") 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") send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="Enable debug output")
send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run") 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) send_parser.add_argument("stdin", help="Input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin)
send_parser.set_defaults(func=pbincli.actions.send) send_parser.set_defaults(func=pbincli.actions.send)
# a get command # a get command
get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance") get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance")
get_parser.add_argument("pasteinfo", help="\"PasteID#Passphrase\" or full URL") get_parser.add_argument("pasteinfo", help="\"PasteID#Passphrase\" or full URL")
get_parser.add_argument("-p", "--password", help="password for decrypting paste") get_parser.add_argument("-p", "--password", help="Password for decrypting paste")
## Connection options ## Connection options
get_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/, ignored if URL used in pasteinfo)") get_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/, ignored if URL used in pasteinfo)")
get_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)") get_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
get_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="disable certificate validation") get_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
get_parser.add_argument("--no-insecure-warning", default=False, action="store_true", get_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
help="suppress InsecureRequestWarning (only with --no-check-certificate)") help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
## ##
get_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") 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") get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="Enable debug output")
get_parser.set_defaults(func=pbincli.actions.get) get_parser.set_defaults(func=pbincli.actions.get)
# a delete command # a delete command
delete_parser = subparsers.add_parser("delete", description="Delete paste from PrivateBin instance using token") delete_parser = subparsers.add_parser("delete", description="Delete paste from PrivateBin instance")
delete_parser.add_argument("-p", "--paste", required=True, help="paste id") delete_parser.add_argument("pasteinfo", help="Paste deletion URL or string in \"pasteid=PasteID&deletetoken=Token\" format")
delete_parser.add_argument("-t", "--token", required=True, help="paste deletion token")
## Connection options ## Connection options
delete_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)") delete_parser.add_argument("-s", "--server", default=argparse.SUPPRESS, help="Instance URL (default: https://paste.i2pd.xyz/)")
delete_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)") delete_parser.add_argument("-x", "--proxy", default=argparse.SUPPRESS, help="Proxy server address (default: None)")
delete_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="disable certificate validation") delete_parser.add_argument("--no-check-certificate", default=False, action="store_true", help="Disable certificate validation")
delete_parser.add_argument("--no-insecure-warning", default=False, action="store_true", delete_parser.add_argument("--no-insecure-warning", default=False, action="store_true",
help="suppress InsecureRequestWarning (only with --no-check-certificate)") help="Suppress InsecureRequestWarning (only with --no-check-certificate)")
## ##
delete_parser.add_argument("-v", "--verbose", default=False, action="store_true", help="enable verbose output") 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") delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="Enable debug output")
delete_parser.set_defaults(func=pbincli.actions.delete) delete_parser.set_defaults(func=pbincli.actions.delete)
# parse arguments # parse arguments

7
pbincli/utils.py

@ -36,9 +36,10 @@ def validate_url_ending(s):
return s return s
def uri_validator(x): def uri_validator(x):
from urllib.parse import urlparse from urllib.parse import urlsplit
try: try:
result = urlparse(x) result = urlsplit(x)
return all([result.scheme, result.netloc]) isuri = all([result.scheme, result.netloc])
return result, isuri
except ValueError: except ValueError:
return False return False
Loading…
Cancel
Save