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: |
paul@352 | 15 | peer = "paulb@localhost/receiver" |
paulb@291 | 16 | |
paulb@271 | 17 | s = libxml2dom.xmpp.Session(("localhost", 5222)) |
paul@352 | 18 | d = s.connect("localhost") |
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" |
paul@352 | 24 | auth.setCredentials("paulb@localhost", "paulb", "jabber") |
paul@352 | 25 | s.send(auth) |
paulb@271 | 26 | print "---- 2 ----" |
paul@352 | 27 | d = s.receive() |
paul@352 | 28 | print "---- 3 ----" |
paulb@271 | 29 | print d.toString() |
paulb@271 | 30 | |
paul@352 | 31 | if d.localName == "failure": |
paul@352 | 32 | if d.reason == "not-authorized": |
paul@352 | 33 | print "Not authorized: must register first!" |
paul@352 | 34 | s.disconnect() |
paul@352 | 35 | sys.exit(1) |
paul@352 | 36 | |
paul@352 | 37 | d = s.connect("localhost") |
paul@352 | 38 | print "---- 4 ----" |
paulb@271 | 39 | print d.toString() |
paulb@271 | 40 | |
paulb@271 | 41 | iq = s.createIq() |
paulb@271 | 42 | iq.makeBind() |
paulb@271 | 43 | if sender: |
paulb@271 | 44 | iq.bind.resource = "sender" |
paulb@271 | 45 | else: |
paulb@271 | 46 | iq.bind.resource = "receiver" |
paul@352 | 47 | s.send(iq) |
paul@352 | 48 | print "---- 5 ----" |
paul@352 | 49 | d = s.receive() |
paul@352 | 50 | print "---- 6 ----" |
paulb@271 | 51 | print d.toString() |
paulb@271 | 52 | |
paulb@271 | 53 | iq = s.createIq() |
paul@352 | 54 | iq.makeSession("localhost") |
paul@352 | 55 | s.send(iq) |
paul@352 | 56 | print "---- 7 ----" |
paul@352 | 57 | d = s.receive() |
paul@352 | 58 | print "---- 8 ----" |
paulb@271 | 59 | print d.toString() |
paulb@271 | 60 | |
paulb@271 | 61 | if sender: |
paulb@271 | 62 | message = s.createMessage() |
paul@352 | 63 | message.from_ = "paulb@localhost/sender" |
paulb@291 | 64 | message.to = peer |
paulb@271 | 65 | message.type = "chat" |
paulb@291 | 66 | message.body = message.createBody() |
paulb@271 | 67 | text = message.ownerDocument.createTextNode("Hello!") |
paulb@291 | 68 | message.body.appendChild(text) |
paulb@291 | 69 | print "Sending..." |
paulb@271 | 70 | print message.toString() |
paul@352 | 71 | s.send(message) |
paul@352 | 72 | d = s.receive() |
paulb@271 | 73 | |
paulb@291 | 74 | if receiver: |
paulb@291 | 75 | while 1: |
paulb@291 | 76 | print "Listening..." |
paulb@291 | 77 | doc = s.receive() |
paulb@291 | 78 | print doc.toString() |
paulb@291 | 79 | print |
paulb@291 | 80 | print "From:", doc.from_ |
paulb@291 | 81 | print "To:", doc.to |
paulb@291 | 82 | print "Type:", doc.type |
paulb@291 | 83 | print |
paulb@291 | 84 | if doc.localName == "message": |
paulb@291 | 85 | print "Message..." |
paulb@291 | 86 | if doc.type == "chat" and doc.body: |
paulb@291 | 87 | print doc.body.textContent |
paulb@291 | 88 | elif doc.event: |
paulb@291 | 89 | print "Composing?", doc.event.composing |
paulb@291 | 90 | print "Delivered?", doc.event.delivered |
paulb@291 | 91 | print "Displayed?", doc.event.displayed |
paulb@291 | 92 | print "Offline?", doc.event.offline |
paulb@291 | 93 | print "Id:", doc.event.id |
paulb@291 | 94 | elif doc.localName == "presence": |
paulb@291 | 95 | print "Presence..." |
paulb@291 | 96 | if doc.type == "subscribe": |
paulb@291 | 97 | presence = s.createPresence() |
paulb@291 | 98 | presence.type = "subscribed" |
paulb@291 | 99 | presence.from_ = doc.to |
paulb@291 | 100 | presence.to = doc.from_ |
paulb@291 | 101 | print "Sending..." |
paulb@291 | 102 | print presence.toString() |
paul@352 | 103 | s.send(presence) |
paul@352 | 104 | d = s.receive() |
paulb@291 | 105 | print |
paulb@291 | 106 | |
paulb@271 | 107 | # vim: tabstop=4 expandtab shiftwidth=4 |