I-Nex/pastebinit

466 lines
16 KiB
Plaintext
Raw Normal View History

2014-06-11 20:49:27 +08:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Stéphane Graber <stgraber@ubuntu.com>
# Written by Stéphane Graber <stgraber@stgraber.org>
# Daniel Bartlett <dan@f-box.org>
2014-06-11 20:49:27 +08:00
# Last modification : Mon Jan 6 18:46:46 EST 2014
2006-11-14 02:56:49 +08:00
# 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 2 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.
#
2006-11-14 02:56:49 +08:00
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2014-06-11 20:49:27 +08:00
from __future__ import print_function
import sys
if sys.version[0] == "2":
2016-09-28 01:12:18 +08:00
from ConfigParser import NoOptionError
from ConfigParser import SafeConfigParser as ConfigParser
2014-06-11 20:49:27 +08:00
from urllib import urlencode
from urllib import FancyURLopener
else:
2016-09-28 01:12:18 +08:00
from configparser import ConfigParser, NoOptionError
2014-06-11 20:49:27 +08:00
from urllib.parse import urlencode
from urllib.request import FancyURLopener
# Set the default pastebin
2016-09-28 01:12:18 +08:00
defaultPB = "pastebin.com"
2014-06-11 20:49:27 +08:00
# Now try to override it with a distributor pastebin
try:
2016-09-28 01:12:18 +08:00
import platform
release = platform.linux_distribution()[0].lower()
if release == 'debian':
2016-09-28 01:12:18 +08:00
defaultPB = "paste.debian.net"
elif release == 'fedora':
2016-09-28 01:12:18 +08:00
defaultPB = "fpaste.org"
elif release == 'ubuntu':
2016-09-28 01:12:18 +08:00
defaultPB = "paste.ubuntu.com"
except ImportError:
pass
try:
2014-06-11 20:49:27 +08:00
import getopt
import gettext
import os
import re
import socket
import xml.dom.minidom
2011-01-20 20:51:33 +08:00
try:
2014-06-11 20:49:27 +08:00
import json
2011-01-20 20:51:33 +08:00
except ImportError:
2014-06-11 20:49:27 +08:00
json = None
2007-06-11 07:28:43 +08:00
2014-06-11 20:49:27 +08:00
_ = gettext.gettext
gettext.textdomain("pastebinit")
2014-06-11 20:49:27 +08:00
# Timeout after 5s
socket.setdefaulttimeout(15)
# Version number to show in the usage
2016-09-28 01:12:18 +08:00
version = "1.5"
configfile = os.path.expanduser("~/.pastebinit.xml")
# Custom urlopener to handle 401's
2014-06-11 20:49:27 +08:00
class pasteURLopener(FancyURLopener):
version = "Pastebinit v%s" % version
def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
return None
def preloadPastebins():
# Check several places for config files:
# - global config in /etc/pastebin.d
# - for source checkout, config in the checkout
# - user's overrides in ~/.pastebin.d
# Files found later override files found earlier.
pastebind = {}
2016-09-28 01:12:18 +08:00
for confdir in ['/usr/share/pastebin.d', '/etc/pastebin.d',
2014-06-11 20:49:27 +08:00
'/usr/local/etc/pastebin.d',
os.path.expanduser('~/.pastebin.d'),
2014-06-11 20:49:27 +08:00
os.path.join(
os.path.dirname(
os.path.realpath(__file__)), 'pastebin.d')]:
try:
confdirlist = os.listdir(confdir)
except OSError:
continue
for fileitem in confdirlist:
2014-06-11 20:49:27 +08:00
if fileitem.startswith('.') or not fileitem.endswith('.conf'):
continue
filename = os.path.join(confdir, fileitem)
2016-09-28 01:12:18 +08:00
instance = ConfigParser()
try:
2014-06-11 20:49:27 +08:00
instance.read(filename)
except UnicodeError:
continue
2014-06-11 20:49:27 +08:00
if not instance.has_section('pastebin'):
print(_('%s: no section [pastebin]') % filename,
file=sys.stderr)
continue
2014-06-11 20:49:27 +08:00
if not instance.has_option('pastebin', 'basename'):
print(_("%s: no 'basename' in [pastebin]") % filename,
file=sys.stderr)
continue
2014-06-11 20:49:27 +08:00
pastebind[instance.get('pastebin', 'basename')] = instance
return pastebind
2016-09-28 01:12:18 +08:00
# Return the parameters depending of the pastebin used
2014-06-11 20:49:27 +08:00
def getParameters(website, pastebind, content, user, jabberid, version,
2016-09-28 01:12:18 +08:00
format, permatag, title, username,
password, private):
"Return the parameters array for the selected pastebin"
2009-12-30 21:47:26 +08:00
params = {}
2014-06-11 20:49:27 +08:00
for paste_name, paste_config in pastebind.items():
basename = paste_config.get('pastebin', 'basename')
2016-09-28 01:12:18 +08:00
try:
https = paste_config.get('pastebin', 'https')
except:
https = False
2014-06-11 20:49:27 +08:00
if basename == website or paste_name == website:
2016-09-28 01:12:18 +08:00
if https:
website = "https://%s" % basename
else:
website = "http://%s" % basename
2014-06-11 20:49:27 +08:00
if re.search(paste_config.get('pastebin', 'regexp'), website):
if paste_config.has_option('pastebin', 'sizelimit'):
params['sizelimit'] = paste_config.get('pastebin',
'sizelimit')
for param in paste_config.options('format'):
paramname = paste_config.get('format', param)
2009-12-27 02:00:11 +08:00
if param == 'user':
params[paramname] = user
elif param == 'content':
params[paramname] = content
elif param == 'title':
params[paramname] = title
elif param == 'version':
params[paramname] = version
elif param == 'format':
2016-09-28 01:12:18 +08:00
try:
params[paramname] = paste_config.get('defaults',
'format')
except NoOptionError:
params[paramname] = format
2009-12-27 02:00:11 +08:00
elif param == 'permatag':
2009-12-30 21:43:22 +08:00
params[paramname] = permatag
2016-09-28 01:12:18 +08:00
elif param == 'private':
params[paramname] = private
2009-12-27 02:00:11 +08:00
elif param == 'username':
params[paramname] = username
elif param == 'password':
params[paramname] = password
elif param == 'jabberid':
params[paramname] = jabberid
else:
2014-06-11 20:49:27 +08:00
params[paramname] = paste_config.get('defaults', param)
if params:
2014-06-11 20:49:27 +08:00
return website, params
else:
2014-06-11 20:49:27 +08:00
print(_("Unknown website, please post a bugreport to request "
"this pastebin to be added (%s)") % website,
file=sys.stderr)
sys.exit(1)
2016-09-28 01:12:18 +08:00
# XML Handling methods
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
2014-06-11 20:49:27 +08:00
return rc
def getNodes(nodes, title):
return nodes.getElementsByTagName(title)
def getFirstNode(nodes, title):
return getNodes(nodes, title)[0]
def getFirstNodeText(nodes, title):
return getText(getFirstNode(nodes, title).childNodes)
# Display usage instructions
2014-06-11 20:49:27 +08:00
def Usage(fd=sys.stdout):
print("pastebinit v" + version, file=fd)
print(_("Reads on stdin for input or takes a list of filenames "
"as parameters"), file=fd)
2016-09-28 01:12:18 +08:00
print(_("\t-E also print content to standard output"), file=fd)
2014-06-11 20:49:27 +08:00
print(_("Optional arguments (not supported by all pastebins):"),
file=fd)
print(_("\t-a <author:default is '%s'>") % user, file=fd)
print(_("\t-b <pastebin url:default is '%s'>") % website, file=fd)
2016-09-28 01:12:18 +08:00
print(_("\t-f <format of paste:default is '%s' (or from pastebin config)>") % format, file=fd) # noqa
2014-06-11 20:49:27 +08:00
print(_("\t-h This help screen"), file=fd)
print(_("\t-i <input file>"), file=fd)
print(_("\t-l List all supported pastebins"), file=fd)
print(_("\t-j <jabberid for notifications:default is '%s'>") %
jabberid, file=fd)
print(_("\t-m <permatag for all versions of a post:default is blank>"),
file=fd)
print(_("\t-t <title of paste:default is blank>"), file=fd)
2016-09-28 01:12:18 +08:00
print(_("\t-P Private. Makes your paste hidden if possible"), file=fd)
2014-06-11 20:49:27 +08:00
print(_("\t-u <username> -p <password>"), file=fd)
print(_("\t-v Print the version number"), file=fd)
2016-09-28 01:12:18 +08:00
print(_("\t--verbose Verbose output to stderr"), file=fd)
# Set defaults
website = defaultPB
2016-09-28 01:12:18 +08:00
user = os.environ.get('USER', os.environ.get('LOGNAME'))
jabberid = ""
title = ""
permatag = ""
format = "text"
username = ""
password = ""
2014-06-11 20:49:27 +08:00
filenames = []
content = ""
2016-09-28 01:12:18 +08:00
private = 0
verbose = False
echo = False
2016-09-28 01:12:18 +08:00
# Example configuration file string
configexample = """\
<pastebinit>
2016-09-28 01:12:18 +08:00
<pastebin>paste.debian.net</pastebin>
2014-06-11 20:49:27 +08:00
<author>A pastebinit user</author>
2008-12-20 03:30:29 +08:00
<jabberid>nobody@nowhere.org</jabberid>
<format>text</format>
</pastebinit>
"""
2016-09-28 01:12:18 +08:00
# Open configuration file if it exists
try:
f = open(configfile)
configtext = f.read()
f.close()
gotconfigxml = True
except KeyboardInterrupt:
2014-06-11 20:49:27 +08:00
print(_("KeyboardInterrupt caught."), file=sys.stderr)
sys.exit(1)
except:
gotconfigxml = False
2016-09-28 01:12:18 +08:00
# Parse configuration file
if gotconfigxml:
try:
configxml = xml.dom.minidom.parseString(configtext)
2014-06-11 20:49:27 +08:00
for variable, key in (('pastebin', 'website'), ('author', 'user'),
('format', 'format'),
('jabberid', 'jabberid')):
try:
value = getFirstNodeText(configxml, variable)
2014-06-11 20:49:27 +08:00
vars()[key] = value
except:
pass
except KeyboardInterrupt:
2014-06-11 20:49:27 +08:00
print(_("KeyboardInterrupt caught."), file=sys.stderr)
sys.exit(1)
except:
2014-06-11 20:49:27 +08:00
print(_("Error parsing configuration file!"), file=sys.stderr)
print(_("Please ensure that your configuration file looks "
"similar to the following:"), file=sys.stderr)
print(configexample, file=sys.stderr)
sys.exit(1)
# Get options
try:
2014-06-11 20:49:27 +08:00
optlist, arglist = getopt.getopt(sys.argv[1:],
2016-09-28 01:12:18 +08:00
'EPhvli:f:b:a:j:t:m:u:p:',
('verbose',))
except KeyboardInterrupt:
2014-06-11 20:49:27 +08:00
print(_("KeyboardInterrupt caught."), file=sys.stderr)
sys.exit(1)
2016-09-28 01:12:18 +08:00
except getopt.GetoptError as e:
print(_("Invalid arguments: %s!" % e)+"\n", file=sys.stderr)
2014-06-11 20:49:27 +08:00
Usage(sys.stderr)
sys.exit(1)
2014-06-11 20:49:27 +08:00
# Get the config
pastebind = preloadPastebins()
# Iterate through options
for opt in optlist:
if opt[0] == "-h":
Usage()
2014-06-11 20:49:27 +08:00
sys.exit(0)
if opt[0] == "-i":
2014-06-11 20:49:27 +08:00
filenames.append(opt[1])
2016-09-28 01:12:18 +08:00
elif opt[0] == "-E":
echo = True
elif opt[0] == "-f":
format = opt[1]
elif opt[0] == "-b":
website = opt[1]
elif opt[0] == "-a":
user = opt[1]
elif opt[0] == "-j":
jabberid = opt[1]
elif opt[0] == "-l":
2014-06-11 20:49:27 +08:00
print(_("Supported pastebins:"))
for pastebin in sorted(pastebind):
2014-06-11 20:49:27 +08:00
print("- %s" % pastebin)
sys.exit(0)
elif opt[0] == "-t":
title = opt[1]
elif opt[0] == "-m":
permatag = opt[1]
2016-09-28 01:12:18 +08:00
elif opt[0] == "-P":
private = 1
elif opt[0] == "-u":
username = opt[1]
elif opt[0] == "-p":
password = opt[1]
2014-06-11 20:49:27 +08:00
elif opt[0] == "-v":
print("pastebinit v" + version)
sys.exit(0)
2016-09-28 01:12:18 +08:00
elif opt[0] == "--verbose":
verbose = True
2014-06-11 20:49:27 +08:00
filenames += arglist
2014-06-11 20:49:27 +08:00
if not filenames:
filenames.append("-")
contents = []
for filename in filenames:
# If - is specified as a filename read from stdin
# otherwise load the specified files.
if filename == "-":
content = sys.stdin.read()
2011-01-20 20:51:33 +08:00
else:
2014-06-11 20:49:27 +08:00
try:
with open(filename, "rb") as fd:
content = fd.read()
except KeyboardInterrupt:
print(_("KeyboardInterrupt caught."), file=sys.stderr)
sys.exit(1)
except:
print(_("Unable to read from: %s") % filename, file=sys.stderr)
sys.exit(1)
if not content:
print(_("You are trying to send an empty document, exiting."),
file=sys.stderr)
sys.exit(1)
2011-01-20 20:51:33 +08:00
2014-06-11 20:49:27 +08:00
contents.append(content)
2016-09-28 01:12:18 +08:00
if echo:
print(content)
2014-06-11 20:49:27 +08:00
for content in contents:
# Get the parameter array
website, params = getParameters(website, pastebind, content, user,
2016-09-28 01:12:18 +08:00
jabberid, version, format,
permatag, title, username, password,
private)
2014-06-11 20:49:27 +08:00
if not website.endswith("/"):
website += "/"
if "sizelimit" in params:
if len(content) > int(params['sizelimit']):
print(_("The content you are trying to send exceeds "
"the pastebin's size limit."), file=sys.stderr)
sys.exit(1)
else:
2014-06-11 20:49:27 +08:00
del params['sizelimit']
if "page" in params:
2016-09-28 01:12:18 +08:00
# Use page, without leading slash: website has a trailing one.
fetch_url = website + params['page'].lstrip("/")
del params['page']
else:
fetch_url = website
2014-06-11 20:49:27 +08:00
if "regexp" in params:
reLink = params['regexp']
del params["regexp"]
2016-09-28 01:12:18 +08:00
else:
reLink = False
# Get target_url for replacement, only used with reLink.
2014-06-11 20:49:27 +08:00
if "target_url" in params:
2016-09-28 01:12:18 +08:00
relink_target_url = params["target_url"]
2014-06-11 20:49:27 +08:00
del params["target_url"]
2016-09-28 01:12:18 +08:00
if not reLink:
print("Warning: using target_url without regexp.",
file=sys.stderr)
elif reLink:
relink_target_url = website
2014-06-11 20:49:27 +08:00
if 'post_format' in params:
post_format = params['post_format']
del params['post_format']
else:
post_format = 'standard'
2014-06-11 20:49:27 +08:00
url_opener = pasteURLopener()
if post_format == 'json':
if json:
params = json.dumps(params)
url_opener.addheader('Content-type', 'text/json')
2010-04-12 00:06:40 +08:00
else:
2014-06-11 20:49:27 +08:00
print(_("Could not find any json library."), file=sys.stderr)
sys.exit(1)
else:
2014-06-11 20:49:27 +08:00
# Convert to a format usable with the HTML POST
params = urlencode(params)
# Send the informations and be redirected to the final page
2016-09-28 01:12:18 +08:00
if verbose:
print("POSTing to: %s\nParams: %s" % (
fetch_url, str(params)), file=sys.stderr)
2014-06-11 20:49:27 +08:00
try:
2016-09-28 01:12:18 +08:00
page = url_opener.open(fetch_url, params)
2014-06-11 20:49:27 +08:00
except Exception as e:
print(_("Failed to contact the server: %s") % e, file=sys.stderr)
sys.exit(1)
try:
# Check if we have to apply a regexp
2016-09-28 01:12:18 +08:00
page_url = ""
2014-06-11 20:49:27 +08:00
if reLink:
if reLink == '(.*)':
2016-09-28 01:12:18 +08:00
page_url = page.read().decode('utf-8').strip()
2014-06-11 20:49:27 +08:00
else:
# Print the result of the regexp
2016-09-28 01:12:18 +08:00
page_url = relink_target_url + re.split(
reLink,
page.read().decode('utf-8'))[1]
2014-06-11 20:49:27 +08:00
else:
# Get the final page and show the url
2016-09-28 01:12:18 +08:00
if echo:
print("-" * len(page.url))
page_url = page.url
print(page_url.strip())
2014-06-11 20:49:27 +08:00
except KeyboardInterrupt:
print(_("KeyboardInterrupt caught."), file=sys.stderr)
sys.exit(1)
except:
print(_("Unable to read or parse the result page, it could be a "
"server timeout or a change server side, "
"try with another pastebin."), file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt:
2014-06-11 20:49:27 +08:00
print(_("KeyboardInterrupt caught."), file=sys.stderr)
sys.exit(1)