Ejabberd hooks with twisted
Fabio Forno - - January 05, 2009Few days ago I’ve found on Launchpad an interesting project by Thomas Hervé, the twisted interface to OTP, which allows to connect to erlang nodes using Twisted Matrix. The idea is incredibily powerful because it allows to easily build mixed-distributed applications using both erlang and python/twisted where they are best. So I gave it a try with ejabberd which has hooks for adding callbacks to its internal components (i.e. it’s possible to intercept and modify packets). Unfortunately I’ve discovered that the present implementation of ejabberd hooks allows to connect only callbacks from the same node of ejabberd, i.e. as an extension module, but ejabberd folks have been incredibily quick in releasing a patch which allows distributed hooks, by adding the “CallNode” atom as parameter. The patch may be not stable yet, but it works perfectly with the last release of ejabberd (2.0.2), and here is an example of a twisted node which intercepts and logs all packets sent and received by any user (here is the full code, change the node names accordingly to your setup).
It works very simply. The hooks are written as methods of a regular python class with the “remote_” prefix:
class UserMonitor(object):
def remote_receive_packet(self, jid, frm, to, packet):
print ">>", to_domish(packet).toXml()
def remote_send_packet(self, frm, to, packet):
print "<<", to_domish(packet).toXml()
which is published by using a PersistentPortMapper:
nodeName = buildNodeName("uccaro@olindo.bluendo.priv")
epmd = PersistentPortMapperFactory(nodeName, cookie, "olindo.bluendo.priv")
epmd.publish(proxy = UserMonitor()) After this it’s possible to call the methods proxy:receive_packtes and proxy:send_packet from any erlang node.
Then we register with ejabberd using a second node which calls the register methods, using the ejabberd api:
@defer.inlineCallbacksdef register_hook(epmd):
# connect to the ejabberd node
inst = yield epmd.connectToNode(“ejabberd”)
# register hooks
r = yield inst.factory.callRemote(
inst,
“ejabberd_hooks”,
“add_dist”,
Atom(“user_receive_packet”), # hook name
“olindo.bluendo.priv”, # virtual host
Atom(“uccaro@olindo.bluendo.priv”), # hook node
Atom(“proxy”), # hook module
Atom(“receive_packet”), # hook method
10
)
And bingo, after this ejabberd happily starts calling our hooks!
As bonus in the code you find also a function which translates from the ejabberd xml representation to twisted domish nodes, which is far more usable.
Categories: Blogs Fabio Forno
Comments
No comments so far, you could be the first.Add comment
Erlang on Twitter
» tichise (Takuya Ichise): RT @AntiBayes: 【言語別業務時の服装】
・Clojure:全裸
・Scheme:全裸
・Gauche:全裸
・Prolog:全裸
・Scala:全裸
・Erlang:全裸
・C++:全裸
» mshiba64 (Masami Shibatani): ということで、ErlangのBit Syntaxに突入。language for distributed and concurrent programだからね。
» despenjahatdos (Jon champion): Eits jangan salah begini2 saya titisan dewa erlang RT @yolapitalokaa: Yg ngepost twit kyknya jg lg galau drtd ... http://t.co/QfCyVSIl
» erlangtriaji (erlang triaji ): Sini sun ahahaha RT @Encays: Udah udah, lo berduaan aja RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aj
» Encays (antarif cahyadi): Menjepit RT @erlangtriaji: Tegang! RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh
» erlangtriaji (erlang triaji ): Tegang! RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama banget
» Encays (antarif cahyadi): Udah udah, lo berduaan aja RT @revianh: Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama
» revianh (Revian Hermansyah): Kepooo! RT @erlangtriaji: Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama banget -_-
» erlangtriaji (erlang triaji ): Hadir RT @Encays: Udah, sama erlang aja RT @revianh: Nanggepnya lama banget -_-
» Encays (antarif cahyadi): Udah, sama erlang aja RT @revianh: Nanggepnya lama banget -_-
Statistics
Number of aggregated posts: 10454
Number of comments: 1392
Most recent article: January 31, 2012
Latest comments
» nobelboy on OpaDo Data Storage: Feel free to add some Qs here or contact me offline, and I will see what I can work into…
» darrensy on The Twisted Matrix: This has been a great idea you have shared. covers for kindle
» jony on Principle Software Engineer at LonoCloud (Full-time): That provides will become a internet marketer of little kinds of expert methods developers developing strategy using Erlang/OTP. There will…