1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#!/usr/bin/python
# Written by Stephane Graber <stgraber@stgraber.org>
# Daniel Bartlett <dan@f-box.org>
# Last modification : Mon Jan 28 22:33:23 CET 2008
# 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.
#
# 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
try:
import urllib, os, sys, re, getopt, select, xml.dom.minidom, gettext
from gettext import gettext as _
gettext.textdomain("pastebinit")
defaultPB = "http://pastebin.com" #Default pastebin
version = "0.10" #Version number to show in the usage
configfile = os.environ.get('HOME') + "/.pastebinit.xml"
# Custom urlopener to handle 401's
class pasteURLopener(urllib.FancyURLopener):
def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
return None
#Return the parameters depending of the pastebin used
def getParameters(website, content, user, jabberid, version, format, parentpid, permatag, title, username, password):
"Return the parameters array for the selected pastebin"
params={}
# pastebin.com 2010-02-24 (new owner + API)
if re.search("http://((([a-zA-Z0-9\-_\.]*)(pastebin\.com)))", website) and not website == "http://www.pastebin.com" or website == "http://pastebin.mozilla.org":
params['page'] = "/api_public.php"
params['paste_name'] = user
params['paste_code'] = content
params['paste_format'] = format #The format, for syntax hilighting
params['paste_expire_date'] = "N" #The expiration, N = no expiry
params['submit'] = "submit"
params['regexp'] = '^http://.*pastebin.com(.*)$'
# elif website == "http://pastebin.ca":
# params['name'] = user
# params['content'] = content
# params['type'] = "1" #The expiration, 1 = raw
# params['save'] = "0" #Do you want a cookie ?
# params['s'] = "Submit Post"
# params['regexp'] = '">http://pastebin.ca/(.*)</a></p><p>
elif re.search("http://((([a-zA-Z0-9\-_\.]*)(paste\.f\-box\.org|1t2\.us|paste\.stgraber\.org)))", website):
params['poster'] = user
params['jid'] = jabberid
params['code2'] = content
params['parent_pid'] = parentpid #For reply, "" means homepage (new thread)
params['format'] = format #The format, for syntax hilighting
params['paste'] = "Send"
params['remember'] = "0" #Do you want a cookie ?
params['expiry'] = "f" #The expiration, f = forever
params['permatag'] = permatag
params['title'] = title
params['username'] = username
params['password'] = password
params['version'] = version
elif website == "http://yourpaste.net":
params['syntax'] = format
params['name'] = user
params['desc'] = title
params['expire'] = "0" # Forever
params['code'] = content
params['private'] = "0" # It's not a private post
params['remember'] = "0" # Cookies? ;)
params['page'] = "/paste"
params['regexp'] = '">http://yourpaste.net(.*)/</a>'
elif website == "http://paste.ubuntu.com":
params['poster'] = user
params['syntax'] = format #The format, for syntax hilighting
params['content'] = content
else:
sys.exit(_("Unknown website, please post a bugreport to request this pastebin to be added (%s)") % website)
return params
#XML Handling methods
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
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
def Usage ():
print "pastebinit v" + version
print _("Required arguments:")
print _("\t-i <filename> or - to read from stdin")
print _("Optional arguments:")
print _("\t-b <pastebin url:default is '%s'>") % website
print _("\t-a <author:default is '%s'>") % user
print _("\t-f <format of paste:default is '%s'>") % format
print _("\t-r <parent posts ID:defaults to none>")
print _("Optional arguments supported only by 1t2.us and paste.stgraber.org:")
print _("\t-j <jabberid for notifications:default is '%s'>") % jabberid
print _("\t-m <permatag for all versions of a post:default is blank>")
print _("\t-t <title of paste:default is blank>")
print _("\t-u <username> -p <password>")
# Set defaults
website = defaultPB
user = os.environ.get('USER')
jabberid = ""
title = ""
permatag = ""
format = "text"
username = ""
password = ""
filename = ""
content = ""
parentpid = ""
#Example configuration file string
configexample = """\
<pastebinit>
<pastebin>http://1t2.us</pastebin>
<author>DanBUK</author>
<jabberid>dan@f-box.org</jabberid>
<format>text</format>
</pastebinit>
"""
#Open configuration file if it exists
try:
f = open(configfile)
configtext = f.read()
f.close()
gotconfigxml = 1
except KeyboardInterrupt:
sys.exit(_("KeyboardInterrupt caught."))
except:
gotconfigxml = 0
#Parse configuration file
if gotconfigxml == 1:
try:
configxml = xml.dom.minidom.parse(configfile)
website = getFirstNodeText(configxml, "pastebin")
user = getFirstNodeText(configxml, "author")
format = getFirstNodeText(configxml, "format")
jabberid = getFirstNodeText(configxml, "jabberid")
except KeyboardInterrupt:
sys.exit(_("KeyboardInterrupt caught."))
except:
print _("Error parsing configuration file!")
print _("Please ensure that your configuration file looks similar to the following:")
print configexample
sys.exit(1)
# Check number of arguments
if len(sys.argv) == 1 and filename == "":
print _("Error no arguments specified!\n")
Usage()
sys.exit(1)
# Get options
try:
optlist, list = getopt.getopt(sys.argv[1:], 'i:f:b:a:r:j:t:m:u:p:')
except KeyboardInterrupt:
sys.exit(_("KeyboardInterrupt caught."))
except getopt.GetoptError:
print _("Invalid arguments!\n")
Usage()
sys.exit(1)
# Iterate through options
for opt in optlist:
if opt[0] == "-i":
filename = opt[1]
elif opt[0] == "-f":
format = opt[1]
elif opt[0] == "-b":
website = opt[1]
elif opt[0] == "-a":
user = opt[1]
elif opt[0] == "-r":
parentpid = opt[1]
elif opt[0] == "-j":
jabberid = opt[1]
elif opt[0] == "-t":
title = opt[1]
elif opt[0] == "-m":
permatag = opt[1]
elif opt[0] == "-u":
username = opt[1]
elif opt[0] == "-p":
password = opt[1]
if filename == "" and len(list) >= 1:
filename = list[0]
#If - is specified as a filename read from stdin, otherwise load the specified file.
if (filename == "" or filename == "-") and content == "":
content = sys.stdin.read()
elif content == "":
try:
f = open(filename)
content = f.read()
f.close()
except KeyboardInterrupt:
sys.exit(_("KeyboardInterrupt caught."))
except:
sys.exit(_("Unable to read from: %s") % filename)
params = getParameters(website, content, user, jabberid, version, format, parentpid, permatag, title, username, password) #Get the parameters array
if not re.search(".*/", website):
website += "/"
reLink=None
if params.__contains__("page"):
website+=params['page']
tmp_page=params['page']
params.__delitem__("page")
if params.__contains__("regexp"):
reLink=params['regexp']
params.__delitem__("regexp")
params = urllib.urlencode(params) #Convert to a format usable with the HTML POST
url_opener = pasteURLopener()
page = url_opener.open(website, params) #Send the informations and be redirected to the final page
try:
if reLink: #Check if we have to apply a regexp
website = website.replace(tmp_page, "")
print website + re.split(reLink, page.read())[1] #Print the result of the Regexp
else:
print page.url #Get the final page and show the ur
except KeyboardInterrupt:
sys.exit(_("KeyboardInterrupt caught."))
except:
raise
sys.exit(_("Unable to read or parse the result page, it could be a server timeout or a change server side, try with another pastebin."))
except KeyboardInterrupt:
sys.exit(_("KeyboardInterrupt caught."))
|