paulb@271 | 1 | #!/usr/bin/env python |
paulb@271 | 2 | |
paulb@271 | 3 | import libxml2dom.xmpp |
paulb@271 | 4 | import sys |
paulb@271 | 5 | |
paulb@271 | 6 | sender = "sender" in sys.argv |
paulb@271 | 7 | receiver = "receiver" in sys.argv |
paulb@271 | 8 | if not (sender or receiver): |
paulb@271 | 9 | print "Please specify sender or receiver." |
paulb@271 | 10 | sys.exit(1) |
paulb@271 | 11 | |
paulb@291 | 12 | if len(sys.argv) > 2: |
paulb@291 | 13 | peer = sys.argv[2] |
paulb@291 | 14 | elif sender: |
paulb@291 | 15 | peer = "paulb@jeremy/receiver" |
paulb@291 | 16 | |
paulb@271 | 17 | s = libxml2dom.xmpp.Session(("localhost", 5222)) |
paulb@271 | 18 | d = s.connect("jeremy") |
paulb@271 | 19 | print "---- 1 ----" |
paulb@271 | 20 | print d.toString() |
paulb@271 | 21 | |
paulb@271 | 22 | auth = s.createAuth() |
paulb@271 | 23 | auth.mechanism = "PLAIN" |
paulb@271 | 24 | auth.setCredentials("paulb@jeremy", "paulb", "jabber") |
paulb@271 | 25 | d = s.send(auth) |
paulb@271 | 26 | print "---- 2 ----" |
paulb@271 | 27 | print d.toString() |
paulb@271 | 28 | |
paulb@271 | 29 | d = s.connect("jeremy") |
paulb@271 | 30 | print "---- 3 ----" |
paulb@271 | 31 | print d.toString() |
paulb@271 | 32 | |
paulb@271 | 33 | iq = s.createIq() |
paulb@271 | 34 | iq.makeBind() |
paulb@271 | 35 | if sender: |
paulb@271 | 36 | iq.bind.resource = "sender" |
paulb@271 | 37 | else: |
paulb@271 | 38 | iq.bind.resource = "receiver" |
paulb@271 | 39 | d = s.send(iq) |
paulb@271 | 40 | print "---- 4 ----" |
paulb@271 | 41 | print d.toString() |
paulb@271 | 42 | |
paulb@271 | 43 | iq = s.createIq() |
paulb@271 | 44 | iq.makeSession("jeremy") |
paulb@271 | 45 | d = s.send(iq) |
paulb@271 | 46 | print "---- 5 ----" |
paulb@271 | 47 | print d.toString() |
paulb@271 | 48 | |
paulb@271 | 49 | if sender: |
paulb@271 | 50 | message = s.createMessage() |
paulb@271 | 51 | message.from_ = "paulb@jeremy/sender" |
paulb@291 | 52 | message.to = peer |
paulb@271 | 53 | message.type = "chat" |
paulb@291 | 54 | message.body = message.createBody() |
paulb@271 | 55 | text = message.ownerDocument.createTextNode("Hello!") |
paulb@291 | 56 | message.body.appendChild(text) |
paulb@291 | 57 | print "Sending..." |
paulb@271 | 58 | print message.toString() |
paulb@274 | 59 | d = s.send(message) |
paulb@271 | 60 | |
paulb@291 | 61 | if receiver: |
paulb@291 | 62 | while 1: |
paulb@291 | 63 | print "Listening..." |
paulb@291 | 64 | doc = s.receive() |
paulb@291 | 65 | print doc.toString() |
paulb@291 | 66 | print |
paulb@291 | 67 | print "From:", doc.from_ |
paulb@291 | 68 | print "To:", doc.to |
paulb@291 | 69 | print "Type:", doc.type |
paulb@291 | 70 | print |
paulb@291 | 71 | if doc.localName == "message": |
paulb@291 | 72 | print "Message..." |
paulb@291 | 73 | if doc.type == "chat" and doc.body: |
paulb@291 | 74 | print doc.body.textContent |
paulb@291 | 75 | elif doc.event: |
paulb@291 | 76 | print "Composing?", doc.event.composing |
paulb@291 | 77 | print "Delivered?", doc.event.delivered |
paulb@291 | 78 | print "Displayed?", doc.event.displayed |
paulb@291 | 79 | print "Offline?", doc.event.offline |
paulb@291 | 80 | print "Id:", doc.event.id |
paulb@291 | 81 | elif doc.localName == "presence": |
paulb@291 | 82 | print "Presence..." |
paulb@291 | 83 | if doc.type == "subscribe": |
paulb@291 | 84 | presence = s.createPresence() |
paulb@291 | 85 | presence.type = "subscribed" |
paulb@291 | 86 | presence.from_ = doc.to |
paulb@291 | 87 | presence.to = doc.from_ |
paulb@291 | 88 | print "Sending..." |
paulb@291 | 89 | print presence.toString() |
paulb@291 | 90 | d = s.send(presence) |
paulb@291 | 91 | print |
paulb@291 | 92 | |
paulb@271 | 93 | # vim: tabstop=4 expandtab shiftwidth=4 |