Meerkat XML-RPC over SMTP

by Rael Dornfest


Experiments in Meerkat XML-RPC over SMTP using Python and a little hackery; a followup to yesterday's thoughts on the subject.



rpcmail.py
The script to which an incoming email message containing an XML-RPC request is routed by procmail. Basically a silly proxy between SMTP and Meerkat's XML-RPC server; yes, I could have merged the two, but the latter is written in PHP and it is Sunday after all.







#!/usr/bin/python

import sys, rfc822, xmlrpclib, smtplib

# Grab the message headers and body
header = rfc822.Message(sys.stdin)
body = sys.stdin.read()

# Decode the XML request, extracting parameters and method
(p, method) = xmlrpclib.loads(body)

# Ick! If you don't know, don't worry about it :-\
params = {}
for pp in p: params.update(pp)

# Instantiate a new XML-RPC Server object
server = xmlrpclib.Server(
"http://www.oreillynet.com/meerkat/xml-rpc/server.php"
)

# A simple dispatch map of supported methods
methods = {
'meerkat.getItems': server.meerkat.getItems,
'system.listMethods': server.system.listMethods,
'meerkat.getCategories': server.meerkat.getCategories,
'meerkat.getChannels': server.meerkat.getChannels,
'meerkat.getChannelsByCategory': server.meerkat.getChannelsByCategory,
'meerkat.getItems': server.meerkat.getItems
}

# Reply to the sender with the results of the Meerkat
# XML-RPC method call
smtplib.SMTP('127.0.0.1').sendmail(
header['from'],
'meerkat@oreilly.com',
'Subject: re:' + header['subject'] + "\n" +
methods[method](params).read()
)



xmlrpclib.py
Some minor alterations to the Python xmlrpclib module so as to retrieve a raw XML response from Meerkat's XML-RPC server.







$ diff xmlrpclib.py ../Modules/xmlrpclib.py
585,586c585
< #return self.parse_response(h.getfile())
< return h.getfile()
---
> return self.parse_response(h.getfile())
634,635c633,634
< #if len(response) == 1:
< # return response[0]
---
> if len(response) == 1:
> return response[0]



The Request
An XML-RPC request-laden email message sent to meerkat@oreilly.com.







From rael@oreilly.com Sun Mar 25 21:46:17 2001
Date: Sun, 25 Mar 2001 21:43:56 -0800
From: Rael Dornfest
To: meerkat...
Subject: rpcmail: getItems

<?xml version="1.0"?>
<methodCall>
<methodName>meerkat.getItems</methodName>
<params>
<param>
<value><struct>
<member><name>category</name>
<value><int>18</int></value>
</member>
<member><name>time_period</name>
<value><string>7DAY</string></value>
</member>
<member><name>ids</name>
<value><i4>0</i4></value>
</member>
<member><name>descriptions</name>
<value><i4>1</i4></value>
</member>
<member><name>categories</name>
<value><i4>0</i4></value>
</member>
<member><name>channels</name>
<value><i4>1</i4></value>
</member>
<member><name>dates</name>
<value><i4>0</i4></value>
</member>
<member><name>num_items</name>
<value><i4>5</i4></value>
</member>
</struct></value>
</param>
</params>
</methodCall>



The Response
An XML-RPC response-laden reply to rael@oreilly.com.







From meerkat... Sun Mar 25 22:13:14 2001
Date: Sun, 25 Mar 2001 22:09:10 -0800
To: rael@oreilly.com
Subject: re:rpcmail: getItems

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><array>
<data>

...

<value><struct>
<member><name>title</name>
<value><string>PikiePikie</string></value>
</member>
<member><name>link</name>
<value><string>
http://www.vex.net/parnassus/apyllo.py?i=83041017
</string></value>
</member>
<member><name>description</name>
<value><string>Another Python-driven WikiWiki implementation.
[Win32, Unix]</string></value>
</member>
<member><name>channel</name>
<value><string>Vaults of Parnassus</string></value>
</member>
</struct></value>

...

</data>
</array></value>
</param>
</params>
</methodResponse>