mirror of https://github.com/r4sas/PBinCLI
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.
90 lines
3.8 KiB
90 lines
3.8 KiB
7 years ago
|
#!/usr/bin/env python
|
||
8 years ago
|
import os, sys, argparse
|
||
7 years ago
|
|
||
8 years ago
|
import pbincli.actions
|
||
7 years ago
|
from pbincli.api import PrivateBin
|
||
8 years ago
|
from pbincli.utils import PBinCLIException
|
||
|
|
||
7 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
|
||
8 years ago
|
|
||
8 years ago
|
def main():
|
||
|
parser = argparse.ArgumentParser()
|
||
|
subparsers = parser.add_subparsers(title="actions", help="List of commands")
|
||
8 years ago
|
|
||
8 years ago
|
# a send command
|
||
8 years ago
|
send_parser = subparsers.add_parser("send", description="Send data to PrivateBin instance", usage="""
|
||
|
%(prog)s --burn --discus --expire 1day --format plaintext \\
|
||
7 years ago
|
--text "My file" --password mypass image.txt"""
|
||
8 years ago
|
)
|
||
8 years ago
|
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 of sent paste")
|
||
8 years ago
|
send_parser.add_argument("-E", "--expire", default="1day", action="store",
|
||
|
choices=["5min", "10min", "1hour", "1day", "1week", "1month", "1year", "never"], help="expiration of paste (default: 1day)")
|
||
|
send_parser.add_argument("-F", "--format", default="plaintext", action="store",
|
||
7 years ago
|
choices=["plaintext", "syntaxhighlighting", "markdown"], help="format of text (default: plaintext)")
|
||
7 years ago
|
send_parser.add_argument("-t", "--text", help="comment in quotes. Ignored if used stdin")
|
||
8 years ago
|
send_parser.add_argument("-p", "--password", help="password for encrypting paste")
|
||
8 years ago
|
send_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
|
||
7 years ago
|
send_parser.add_argument("--dry", default=False, action="store_true", help="invoke dry run")
|
||
8 years ago
|
send_parser.add_argument("-f", "--file", help="example: image.jpg or full path to file")
|
||
7 years ago
|
|
||
7 years ago
|
send_parser.add_argument("stdin", help="input paste text from stdin", nargs="?", type=argparse.FileType("r"), default=sys.stdin)
|
||
8 years ago
|
send_parser.set_defaults(func=pbincli.actions.send)
|
||
8 years ago
|
|
||
8 years ago
|
get_parser = subparsers.add_parser("get", description="Get data from PrivateBin instance", usage="""
|
||
|
%(prog)s pasteid#password"""
|
||
|
)
|
||
|
get_parser.add_argument("pasteinfo", help="example: aabb#cccddd")
|
||
8 years ago
|
get_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
|
||
8 years ago
|
get_parser.add_argument("-p", "--password", help="password for decrypting paste")
|
||
8 years ago
|
get_parser.set_defaults(func=pbincli.actions.get)
|
||
|
|
||
8 years ago
|
delete_parser = subparsers.add_parser("delete", description="Delete paste from PrivateBin instance using token", usage="""
|
||
|
%(prog)s --paste aabb --token aabbcc"""
|
||
|
)
|
||
|
delete_parser.add_argument("-p", "--paste", required=True, help="paste id")
|
||
|
delete_parser.add_argument("-t", "--token", required=True, help="delete token")
|
||
|
delete_parser.add_argument("-d", "--debug", default=False, action="store_true", help="enable debug")
|
||
|
delete_parser.set_defaults(func=pbincli.actions.delete)
|
||
|
|
||
8 years ago
|
# parse arguments
|
||
|
args = parser.parse_args()
|
||
7 years ago
|
|
||
7 years ago
|
CONFIG = {"server": "https://paste.i2pd.xyz/",
|
||
|
"proxy": None}
|
||
|
|
||
|
for p in CONFIG_PATHS:
|
||
7 years ago
|
if os.path.exists(p):
|
||
7 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)
|
||
7 years ago
|
|
||
7 years ago
|
api_client = PrivateBin(CONFIG["server"], proxy=CONFIG["proxy"])
|
||
7 years ago
|
|
||
8 years ago
|
if hasattr(args, "func"):
|
||
|
try:
|
||
7 years ago
|
args.func(args, api_client)
|
||
8 years ago
|
except PBinCLIException as pe:
|
||
|
print("PBinCLI error: {}".format(pe))
|
||
|
sys.exit(1)
|
||
|
else:
|
||
|
parser.print_help()
|
||
|
|
||
8 years ago
|
|
||
8 years ago
|
if __name__ == "__main__":
|
||
|
main()
|