The cvsmsg Script

Let’s now have a look at the script, called cvsmsg. It has to send a notification message, which it receives on STDIN, to a JID, which it receives as an argument passed to the script, as shown in Example 8-3.

Example 8-3. The cvsmsg Python script

import jabber
import sys

Server   = 'gnu.pipetree.com'
Username = 'cvsmsg'
Password = 'secret'
Resource = 'cvsmsg'

cvsuser  = sys.argv[1]
message  = ''

for line in sys.stdin.readlines(): message = message + line

con = jabber.Client(host=Server)

try:
    con.connect()
except IOError, e:
    print "Couldn't connect: %s" % e 
    sys.exit(0)

con.auth(Username,Password,Resource)
con.send(jabber.Message(cvsuser, message, subject="CVS Watch Alarm"))
con.disconnect()

It’s not that long but worth breaking down to examine piece by piece.

We’re going to use the Jabberpy Python library for Jabber, so the first thing we do in the script is import it. We also import the sys module for reading from STDIN:

import jabber
import sys

As the usage of the script will be fairly static, we can get away here with hardcoding a few parameters:

Server   = 'gnu.pipetree.com'
Username = 'cvsmsg'
Password = 'secret'
Resource = 'cvsmsg'

Specified here are the connection and authentication details for the cvsmsg script itself. If it’s to send a message via Jabber, it must itself connect to Jabber. The Server variable specifies which Jabber server to connect to, and the Username, Password, and Resource variables contain the rest of the information for the script’s ...

Get Programming Jabber now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.