#!/usr/bin/python
import urllib, os, sys, re
defaultPB="http://archlinux.pastebin.com" #Default pastebin
version="0.7.1" #Version number to show in the usage
#Return the parameters depending of the pastebin used
def getParameters(website,content,user,version):
"Return the parameters array for the selected pastebin"
params={}
if website == "http://archlinux.pastebin.com":
params['poster']=user
params['code2']=content
params['parent_pid']="" #For reply, "" means homepage (new thread)
params['format']="text" #The format, for syntax hilighting
params['paste']="Send"
params['remember']="0" #Do you want a cookie ?
params['expiry']="f" #The expiration, f = forever
params['regexp']="None"
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://.*\.pastebin\.com", website):
params['poster']=user
params['code2']=content
params['parent_pid']="" #For reply, "" means homepage (new thread)
params['format']="text" #The format, for syntax hilighting
params['paste']="Send"
params['remember']="0" #Do you want a cookie ?
params['expiry']="f" #The expiration, f = forever
params['regexp']="None"
elif website == "http://pastebin.com":
params['poster']=user
params['code2']=content
params['parent_pid']="" #For reply, "" means homepage (new thread)
params['format']="text" #The format, for syntax hilighting
params['paste']="Send"
params['remember']="0" #Do you want a cookie ?
params['expiry']="f" #The expiration, f = forever
params['regexp']="None"
else:
sys.exit("Unknown website, please post a request for this pastebin to be added ("+website+")")
return params
#Check if a filename is passed as an argument, otherwise show the usages
try:
filename=sys.argv[1]
except:
sys.exit("usage: "+sys.argv[0]+" [filename|-] [URL]\n\nDefault pastebin: "+defaultPB+"\nVersion: "+version)
#If - is specified as a filename read from stdin, otherwise load the specified file.
if filename == "-":
content=sys.stdin.read()
else:
try:
f=open(filename)
content=f.read()
f.close()
except:
sys.exit("Unable to read from: "+filename)
#Check if an extra pastebin is pasted as an argument otherwise use the default Pastebin
try:
website=sys.argv[2]
except:
website=defaultPB
user=os.environ.get('USER') #Get the current username from the environment
params=getParameters(website,content,user,version) #Get the parameters array
reLink=params['regexp'] #Extract the regexp
params['regexp']=None #Remove the regexp from what will be sent
params=urllib.urlencode(params) #Convert to a format usable with the HTML POST
page=urllib.urlopen(website+'/',params) #Send the informations and be redirected to the final page
try:
if reLink != "None": #Check if we have to apply a regexp
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 url
except:
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.")