Skip to content

Commit

Permalink
Adding ~/.pastebinit.xml, permatag & jabber notification support
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Bartlett committed Feb 26, 2007
1 parent 7d52330 commit 00a8cca
Showing 1 changed file with 95 additions and 17 deletions.
112 changes: 95 additions & 17 deletions pastebinit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/python
# Written by Stephane Graber <stgraber@stgraber.org>
# Daniel Bartlett <dan@f-box.org>
# Last modification : Sun Feb 25 13:23:00 CET 2007
# Last modification : Mon Feb 26 18:53:00 CET 2007

# 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
Expand All @@ -17,44 +17,61 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

import urllib, os, sys, re, getopt, select
import urllib, os, sys, re, getopt, select, xml.dom.minidom

defaultPB = "http://paste.stgraber.org" #Default pastebin
version = "0.7" #Version number to show in the usage
version = "0.71" #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, version, format, title, username, password):
def getParameters(website, content, user, jabberid, version, format, parentpid, permatag, title, username, password):
"Return the parameters array for the selected pastebin"
params={}
# pastebin.com v0.50
if re.search("http://((([a-zA-Z0-9\-_\.]*)(pastebin\.com))|(paste.stgraber.org))", website):
params['poster'] = user
params['code2'] = content
params['version'] = version
params['parent_pid'] = "" #For reply, "" means homepage (new thread)
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['regexp'] = "None"
# pastebin.com v0.60
elif re.search("http://([a-zA-Z0-9\-_\.]*)1t2\.us", website):
# elif re.search("http://((([a-zA-Z0-9\-_\.]*)(1t2\.us)))", website):
# params['poster'] = user
# 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['title'] = title
# params['username'] = username
# params['password'] = password
# params['regexp'] = "None"
# pastebin.com v0.61
elif re.search("http://((([a-zA-Z0-9\-_\.]*)(paste\.f\-box\.org|1t2\.us)))", website):
params['poster'] = user
params['jid'] = jabberid
params['code2'] = content
params['parent_pid'] = "" #For reply, "" means homepage (new thread)
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['regexp'] = "None"
params['version'] = version
elif website == "http://pastebin.ca":
params['name'] = user
params['content'] = content
Expand All @@ -63,36 +80,90 @@ def getParameters(website, content, user, version, format, title, username, pass
params['s'] = "Submit Post"
params['regexp'] = '">http://pastebin.ca/(.*)</a></p><p>'
else:
sys.exit("Unknown website, please post a bugreport to request this pastebin to be added (" + website + ")")
sys.exit("Unknown website, please post a bugreport to request this pastebin to be added ("+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 pipe the text)"
print "Optional arguments:"
print "\t-b <pastebin url:default " + defaultPB + ">"
print "\t-a <author:defaults to system username>"
print "\t-f <format of paste:default text only>"
print "\t-b <pastebin url:default is '" + website + "'>"
print "\t-a <author:default is '" + user + "'>"
print "\t-f <format of paste:default is '" + format + "'>"
print "\t-r <parent posts ID:defaults to none>"
print "Optional arguments supported only by 1t2.us:"
print "\t-j <jabberid for notifications:default is '" + 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>"
sys.exit(0)


# Set defaults
user = os.environ.get('USER')
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:
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:
print "Error parsing configuration file!"
print "Please ensure that your configuration file looks similar to the following:"
print configexample
sys.exit(1)

#Check if some datas were passed by pipe, if yes directly assign content
l_r = select.select([sys.stdin], [], [], 0)
l_r = select.select([sys.stdin],[],[],0)
try :
content=l_r[0][0].read()
filename="-"
Expand All @@ -101,13 +172,14 @@ except:

# 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:t:u:p:')
optlist, list = getopt.getopt(sys.argv[1:], 'i:f:b:a:r:j:t:m:u:p:')
except getopt.GetoptError:
print "Invalid arguments!\n"
Usage()
Expand All @@ -123,8 +195,14 @@ for opt in optlist:
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":
Expand All @@ -145,7 +223,7 @@ elif content == "":
except:
sys.exit("Unable to read from: " + filename)

params = getParameters(website, content, user, version, format, title, username, password) #Get the parameters array
params = getParameters(website, content, user, jabberid, version, format, parentpid, permatag, title, username, password) #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
Expand Down

0 comments on commit 00a8cca

Please sign in to comment.