1.1 --- a/tests/xmpp_test.py Thu Jul 09 20:48:31 2009 +0200
1.2 +++ b/tests/xmpp_test.py Sun Feb 27 18:32:58 2011 +0100
1.3 @@ -1,107 +1,195 @@
1.4 #!/usr/bin/env python
1.5
1.6 +"""
1.7 +A test of XMPP support, providing a simple chat interface.
1.8 +"""
1.9 +
1.10 import libxml2dom.xmpp
1.11 +import os
1.12 import sys
1.13
1.14 -sender = "sender" in sys.argv
1.15 -receiver = "receiver" in sys.argv
1.16 -if not (sender or receiver):
1.17 - print "Please specify sender or receiver."
1.18 +try:
1.19 + username, domain, password, mode = sys.argv[1:5]
1.20 +except ValueError:
1.21 + print sys.argv[0], "<username> <domain> <password> <mode> [ <endpoint> ]"
1.22 + print
1.23 + print "For example:"
1.24 + print
1.25 + print sys.argv[0], "libxml2dom localhost jabber listen # to listen"
1.26 + print sys.argv[0], "libxml2dom localhost jabber contact 1234 # to contact a listener"
1.27 sys.exit(1)
1.28
1.29 -if len(sys.argv) > 2:
1.30 - peer = sys.argv[2]
1.31 -elif sender:
1.32 - peer = "paulb@localhost/receiver"
1.33 +receiving = mode == "listen"
1.34 +register = mode == "register"
1.35 +unregister = mode == "unregister"
1.36 +
1.37 +# Get a peer to send messages to.
1.38 +
1.39 +if not receiving and not register and not unregister:
1.40 + if len(sys.argv) > 5:
1.41 + peer = "%s@%s/%s" % (username, domain, sys.argv[5])
1.42 + else:
1.43 + print "Need to specify a peer to send messages to."
1.44 + sys.exit(1)
1.45 +
1.46 +# Connect to the XMPP server.
1.47
1.48 s = libxml2dom.xmpp.Session(("localhost", 5222))
1.49 d = s.connect("localhost")
1.50 -print "---- 1 ----"
1.51 -print d.toString()
1.52 +
1.53 +print "---- Connection ----"
1.54 +print d.toString(prettyprint=1)
1.55 +
1.56 +# Register if requested.
1.57 +
1.58 +if register:
1.59 + print "---- Register ----"
1.60 + iq = s.createIq()
1.61 + iq.makeQuery()
1.62 + print iq.toString(prettyprint=1)
1.63 + s.send(iq)
1.64 +
1.65 + print "---- Response ----"
1.66 + d = s.receive()
1.67 + print d.toString(prettyprint=1)
1.68
1.69 -auth = s.createAuth()
1.70 -auth.mechanism = "PLAIN"
1.71 -auth.setCredentials("paulb@localhost", "paulb", "jabber")
1.72 -s.send(auth)
1.73 -print "---- 2 ----"
1.74 -d = s.receive()
1.75 -print "---- 3 ----"
1.76 -print d.toString()
1.77 + if not d.query.xpath("register:registered"):
1.78 + instructions = d.query.xpath("register:instructions")
1.79 + if instructions:
1.80 + print instructions[0].textContent
1.81
1.82 -if d.localName == "failure":
1.83 - if d.reason == "not-authorized":
1.84 - print "Not authorized: must register first!"
1.85 + print "---- Completing form ----"
1.86 + iq = s.createIq()
1.87 + iq.makeRegistration()
1.88 + iq.registration["username"] = username
1.89 + iq.registration["password"] = password
1.90 + print iq.toString(prettyprint=1)
1.91 + s.send(iq)
1.92 +
1.93 + print "---- Response ----"
1.94 + d = s.receive()
1.95 + print d.toString(prettyprint=1)
1.96 +
1.97 s.disconnect()
1.98 sys.exit(1)
1.99
1.100 +print "---- Authentication ----"
1.101 +auth = s.createAuth()
1.102 +auth.mechanism = "PLAIN"
1.103 +auth.setCredentials(username, username, password)
1.104 +print auth.toString(prettyprint=1)
1.105 +s.send(auth)
1.106 +
1.107 +print "---- Response ----"
1.108 +d = s.receive()
1.109 +print d.toString(prettyprint=1)
1.110 +
1.111 +if d.localName == "failure":
1.112 + if d.reason == "not-authorized":
1.113 + print "Not authorized: perhaps register first!"
1.114 + s.disconnect()
1.115 + sys.exit(1)
1.116 +
1.117 +# Reconnect.
1.118 +
1.119 d = s.connect("localhost")
1.120 -print "---- 4 ----"
1.121 -print d.toString()
1.122
1.123 +print "---- Authenticated connection ----"
1.124 +print d.toString(prettyprint=1)
1.125 +
1.126 +print "---- Binding ----"
1.127 iq = s.createIq()
1.128 iq.makeBind()
1.129 -if sender:
1.130 - iq.bind.resource = "sender"
1.131 -else:
1.132 - iq.bind.resource = "receiver"
1.133 +iq.bind.resource = str(os.getpid())
1.134 +print iq.toString(prettyprint=1)
1.135 s.send(iq)
1.136 -print "---- 5 ----"
1.137 +
1.138 +print "---- Response ----"
1.139 d = s.receive()
1.140 -print "---- 6 ----"
1.141 -print d.toString()
1.142 +print d.toString(prettyprint=1)
1.143
1.144 +print "---- Session ----"
1.145 iq = s.createIq()
1.146 iq.makeSession("localhost")
1.147 +print iq.toString(prettyprint=1)
1.148 s.send(iq)
1.149 -print "---- 7 ----"
1.150 +
1.151 +print "---- Response ----"
1.152 d = s.receive()
1.153 -print "---- 8 ----"
1.154 -print d.toString()
1.155 +print d.toString(prettyprint=1)
1.156 +
1.157 +# Unregister if requested.
1.158
1.159 -if sender:
1.160 - message = s.createMessage()
1.161 - message.from_ = "paulb@localhost/sender"
1.162 - message.to = peer
1.163 - message.type = "chat"
1.164 - message.body = message.createBody()
1.165 - text = message.ownerDocument.createTextNode("Hello!")
1.166 - message.body.appendChild(text)
1.167 - print "Sending..."
1.168 - print message.toString()
1.169 - s.send(message)
1.170 +if unregister:
1.171 + print "---- Unregister ----"
1.172 + iq = s.createIq()
1.173 + iq.makeUnregistration()
1.174 + print iq.toString(prettyprint=1)
1.175 + s.send(iq)
1.176 +
1.177 + print "---- Response ----"
1.178 d = s.receive()
1.179 + print d.toString(prettyprint=1)
1.180
1.181 -if receiver:
1.182 + s.disconnect()
1.183 + sys.exit(1)
1.184 +
1.185 +# Otherwise, enter chat mode.
1.186 +
1.187 +print "---- Chatting ----"
1.188 +try:
1.189 while 1:
1.190 - print "Listening..."
1.191 - doc = s.receive()
1.192 - print doc.toString()
1.193 - print
1.194 - print "From:", doc.from_
1.195 - print "To:", doc.to
1.196 - print "Type:", doc.type
1.197 - print
1.198 - if doc.localName == "message":
1.199 - print "Message..."
1.200 - if doc.type == "chat" and doc.body:
1.201 - print doc.body.textContent
1.202 - elif doc.event:
1.203 - print "Composing?", doc.event.composing
1.204 - print "Delivered?", doc.event.delivered
1.205 - print "Displayed?", doc.event.displayed
1.206 - print "Offline?", doc.event.offline
1.207 - print "Id:", doc.event.id
1.208 - elif doc.localName == "presence":
1.209 - print "Presence..."
1.210 - if doc.type == "subscribe":
1.211 - presence = s.createPresence()
1.212 - presence.type = "subscribed"
1.213 - presence.from_ = doc.to
1.214 - presence.to = doc.from_
1.215 - print "Sending..."
1.216 - print presence.toString()
1.217 - s.send(presence)
1.218 - d = s.receive()
1.219 - print
1.220 + if not receiving:
1.221 + message = s.createMessage()
1.222 + message.from_ = "%s@%s/sender" % (username, domain)
1.223 + message.to = peer
1.224 + message.type = "chat"
1.225 + message.body = message.createBody()
1.226 + print "Enter message..."
1.227 + message_text = raw_input()
1.228 + text = message.ownerDocument.createTextNode(message_text)
1.229 + message.body.appendChild(text)
1.230 + print "Sending..."
1.231 + print message.toString(prettyprint=1)
1.232 + s.send(message)
1.233 + receiving = 1
1.234 +
1.235 + if receiving:
1.236 + print "Listening as %s..." % os.getpid()
1.237 + d = s.receive()
1.238 + print d.toString(prettyprint=1)
1.239 + print
1.240 + print "From:", d.from_
1.241 + print "To:", d.to
1.242 + print "Type:", d.type
1.243 + print
1.244 + if d.localName == "message":
1.245 + print "Message..."
1.246 + if d.type == "chat" and d.body:
1.247 + print d.body.textContent
1.248 + elif d.event:
1.249 + print "Composing?", d.event.composing
1.250 + print "Delivered?", d.event.delivered
1.251 + print "Displayed?", d.event.displayed
1.252 + print "Offline?", d.event.offline
1.253 + print "Id:", d.event.id
1.254 + elif d.localName == "presence":
1.255 + print "Presence..."
1.256 + if d.type == "subscribe":
1.257 + presence = s.createPresence()
1.258 + presence.type = "subscribed"
1.259 + presence.from_ = d.to
1.260 + presence.to = d.from_
1.261 + print "Sending..."
1.262 + print presence.toString(prettyprint=1)
1.263 + s.send(presence)
1.264 + d = s.receive()
1.265 + print d.toString(prettyprint=1)
1.266 + print
1.267 + receiving = 0
1.268 + peer = d.from_
1.269 +
1.270 +finally:
1.271 + s.disconnect()
1.272
1.273 # vim: tabstop=4 expandtab shiftwidth=4