Sending XHTML over Jabber
At times, sending something more than plain text is desirable. XEP-0071 XHTML-IM provides for that with Jabber. And sending such XHTML enriched messages with xmpppy turns out to be fairly straightforward. In fact, I’ve now set up my weblog so that I get notified whenever I’m online and a comment is made. Here’s how it works.
First, create a bare-bones text message, specifying the destination and some text. In my case, titles may contain numeric entities such as ’, but as this is relatively rare, and this is only the fall-back for clients that don’t support XHTML (and GAIM does), so I’m not overly worried about it.
message=xmpp.protocol.Message('rubys@rubix/Laptop', title)
Now I build a payload containing some markup, and add it to the message. UTF-8 and numeric entities works just fine. This data, however, absolutely must be well formed, so a try:/except: block may be in order for in most applications. Fortunately, mine is safe.
payload=xmpp.simplexml.XML2Node('<body xmlns="%s">%s</body>' %
(xml.dom.XML_NAMESPACE, '<a href="%s">%s</a>: %s' % (link, title, byline)))
message.addChild('html', {}, [payload], xmpp.NS_XHTML_IM)
Specify the sending Jabber ID.
jid=xmpp.protocol.JID('rubys@rubix/intertwingly')
cl=xmpp.Client(jid.getDomain(), debug=[])
Connect to the Jabber server. In most cases, you can simply call cl.connect() as the host and port can be determined from the JID. But if you have punched a hole in your firewall, you can specify the ip address and port number where your jabber server can be reached. Again, most people won’t need to do this as they can readily obtain public jabber IDs, but it is nice to know that it can be done. Note that the address and port are separated by a comma, not a colon as the documentation incorrectly states.
con=cl.connect((address,port)) if not con: return
Now, authorize yourself:
auth=cl.auth(jid.getNode(), 'password', resource=jid.getResource()) if not auth: return
Finally, send the message:
cl.send(message) time.sleep(1) cl.disconnect()