XMPP/Jabber svn post commit hook

Description: xmpp_pc is a simple post-commit hook script which is able to inform a fixed set of xmpp/jabber users of a recent commit.
Raw download: xmpp_pc.py / post-commit
Jump to: xmpp_pc.py / post-commit

xmpp_pc.py (Raw)

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 #
  4 #    xmpp_pc.py - A very simple post commit hook the inform jabber users of new
  5 #                 revisions
  6 #
  7 #    WARNING: This script may possibly help attackers to DOS your server or even to run 
  8 #             commands with the rights of your svn server. (Is say 'possibly' because
  9 #             I can't think of a way how they could...but as I used popen with unfiltered
 10 #             parameters and many people are a lot smarter than me...anyway,
 11 #             just be aware that I am a beginner ;-)
 12 #
 13 #    KNOWN ISSUES:
 14 #             I didn't care for special characters at all and the xmpp module
 15 #             does not like them much either, the script is ok for my requirements
 16 #             though. Patches are welcome :-) Another known issue is my bad english,
 17 #             sorry for that^^
 18 #
 19 #    HOWTO USE:
 20 #             Make sure you have installed python and the xmpp module correctly.
 21 #             Adjust all settings in xmpp_pc.py for your jabber account. Then
 22 #             adjust the path to xmpp_pc.py in the post-commit script and place it
 23 #             in your svn repositories 'hooks' directory (be sure it's executable).
 24 #             That's it. Gl&Hf
 25 #
 26 #    Copyright (C) 2007 Stefan Hacker
 27 #
 28 #    This program is free software; you can redistribute it and/or modify
 29 #    it under the terms of the GNU General Public License as published by
 30 #    the Free Software Foundation; either version 2 of the License, or
 31 #    (at your option) any later version.
 32 #
 33 #    This program is distributed in the hope that it will be useful,
 34 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 35 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 36 #    GNU General Public License for more details.
 37 #
 38 #    You should have received a copy of the GNU General Public License along
 39 #    with this program; if not, write to the Free Software Foundation, Inc.,
 40 #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 41 
 42 import xmpp, sys, os
 43 
 44 def main():
 45     #JID as a string
 46     sjid      = "user@server.tld/resource"
 47     #Password
 48     password  = "secretpassword"
 49     #Additional settings
 50     
 51     #WARNING: Encryption options will fall back to disabled if the server
 52     #         does not support them. This may open some doors if the jabber
 53     #         server does not reside on the svn machine itself.
 54     
 55     ssl      = 0       #Enable/Disable ssl encrypted connection
 56     sasl     = 1       #Enable/Disable sasl auth
 57     #server  = (servername,port)
 58     server   = None    #Manual server/port settings
 59     proxy    = None    #Proxy settings if needed
 60     #Type of message to send (see xmpp module for other options)
 61     mtype     = "chat"
 62     #Full path of the svnlook binary
 63     svnlook  = "/usr/bin/svnlook"
 64     
 65     #default jids to send repository changes to
 66     subscribers = ["Someone@someserver.tld",
 67                    "otherone@otherserver.tld"]
 68     
 69     
 70     #Collect svn info
 71     path = sys.argv[1]
 72     rev = int(sys.argv[2])
 73     #Add specific subscribers
 74     for subscriber in sys.argv[3:]:
 75         if not subscriber in subscribers:
 76             subscribers.append(subscriber)
 77     
 78     #Get changed files/dirs via svnlook changed
 79     changed = os.popen("%s -r %d changed %s" % (svnlook, rev, path)).read()
 80     #Get author, data and comment via snvlook info
 81     info = os.popen("%s -r %d info %s" % (svnlook, rev, path)).read()
 82     author, date, num, comment = info.split('\n',3)
 83     
 84     #Connect to the server
 85     jid = xmpp.protocol.JID(sjid)
 86     connection = xmpp.Client(jid.getDomain(), debug=[])
 87     res = connection.connect(server, proxy, ssl) 
 88     if not res:
 89         print "Connection failed"
 90         exit(1)
 91     
 92     #Authenticate with server
 93     res = connection.auth(jid.getNode(), password, jid.getResource(), sasl)
 94     if not res:
 95         print "Auth failed"
 96         exit(2)
 97     
 98     #Create message
 99     message  = u"""Repository '%(path)s' now at revision %(rev)d :
100     Author: %(author)s
101     Date: %(date)s
102     Changes:
103     %(changed)s
104     Comment:
105     %(comment)s
106     """ % locals()
107 
108     #Send message to all subscribers
109     for subscriber in subscribers:
110         connection.send(xmpp.Message(subscriber, message, mtype))
111     
112 if __name__ == "__main__":
113     main()
114     '''
115     The end of the world...ehm...script ;-) Let the garbage collector and destructors
116     handle the rest.
117     '''

post-commit (Raw)

1 #!/usr/bin/env sh
2 /usr/bin/env python /PATH/TO/xmpp_pc.py $1 $2 subscriber@domain.tld othersub@gomain.tld

Created on Wed Aug 29 14:52:02 2007 using pygments.