1 #!/usr/bin/env python 2 3 """ 4 A test of XMPP support, providing a simple chat interface. 5 """ 6 7 import libxml2dom.xmpp 8 import os 9 import sys 10 11 try: 12 username, domain, password, mode = sys.argv[1:5] 13 except ValueError: 14 print sys.argv[0], "<username> <domain> <password> <mode> [ <endpoint> ]" 15 print 16 print "For example:" 17 print 18 print sys.argv[0], "libxml2dom localhost jabber listen # to listen" 19 print sys.argv[0], "libxml2dom localhost jabber contact 1234 # to contact a listener" 20 sys.exit(1) 21 22 receiving = mode == "listen" 23 register = mode == "register" 24 unregister = mode == "unregister" 25 26 # Get a peer to send messages to. 27 28 if not receiving and not register and not unregister: 29 if len(sys.argv) > 5: 30 peer = "%s@%s/%s" % (username, domain, sys.argv[5]) 31 else: 32 print "Need to specify a peer to send messages to." 33 sys.exit(1) 34 35 # Connect to the XMPP server. 36 37 s = libxml2dom.xmpp.Session(("localhost", 5222)) 38 d = s.connect("localhost") 39 40 print "---- Connection ----" 41 print d.toString(prettyprint=1) 42 43 # Register if requested. 44 45 if register: 46 print "---- Register ----" 47 iq = s.createIq() 48 iq.makeQuery() 49 print iq.toString(prettyprint=1) 50 s.send(iq) 51 52 print "---- Response ----" 53 d = s.receive() 54 print d.toString(prettyprint=1) 55 56 if not d.query.xpath("register:registered"): 57 instructions = d.query.xpath("register:instructions") 58 if instructions: 59 print instructions[0].textContent 60 61 print "---- Completing form ----" 62 iq = s.createIq() 63 iq.makeRegistration() 64 iq.registration["username"] = username 65 iq.registration["password"] = password 66 print iq.toString(prettyprint=1) 67 s.send(iq) 68 69 print "---- Response ----" 70 d = s.receive() 71 print d.toString(prettyprint=1) 72 73 s.disconnect() 74 sys.exit(1) 75 76 print "---- Authentication ----" 77 auth = s.createAuth() 78 auth.mechanism = "PLAIN" 79 auth.setCredentials(username, username, password) 80 print auth.toString(prettyprint=1) 81 s.send(auth) 82 83 print "---- Response ----" 84 d = s.receive() 85 print d.toString(prettyprint=1) 86 87 if d.localName == "failure": 88 if d.reason == "not-authorized": 89 print "Not authorized: perhaps register first!" 90 s.disconnect() 91 sys.exit(1) 92 93 # Reconnect. 94 95 d = s.connect("localhost") 96 97 print "---- Authenticated connection ----" 98 print d.toString(prettyprint=1) 99 100 print "---- Binding ----" 101 iq = s.createIq() 102 iq.makeBind() 103 iq.bind.resource = str(os.getpid()) 104 print iq.toString(prettyprint=1) 105 s.send(iq) 106 107 print "---- Response ----" 108 d = s.receive() 109 print d.toString(prettyprint=1) 110 111 print "---- Session ----" 112 iq = s.createIq() 113 iq.makeSession("localhost") 114 print iq.toString(prettyprint=1) 115 s.send(iq) 116 117 print "---- Response ----" 118 d = s.receive() 119 print d.toString(prettyprint=1) 120 121 # Unregister if requested. 122 123 if unregister: 124 print "---- Unregister ----" 125 iq = s.createIq() 126 iq.makeUnregistration() 127 print iq.toString(prettyprint=1) 128 s.send(iq) 129 130 print "---- Response ----" 131 d = s.receive() 132 print d.toString(prettyprint=1) 133 134 s.disconnect() 135 sys.exit(1) 136 137 # Otherwise, enter chat mode. 138 139 print "---- Chatting ----" 140 try: 141 while 1: 142 if not receiving: 143 message = s.createMessage() 144 message.from_ = "%s@%s/sender" % (username, domain) 145 message.to = peer 146 message.type = "chat" 147 message.body = message.createBody() 148 print "Enter message..." 149 message_text = raw_input() 150 text = message.ownerDocument.createTextNode(message_text) 151 message.body.appendChild(text) 152 print "Sending..." 153 print message.toString(prettyprint=1) 154 s.send(message) 155 receiving = 1 156 157 if receiving: 158 print "Listening as %s..." % os.getpid() 159 d = s.receive() 160 print d.toString(prettyprint=1) 161 print 162 print "From:", d.from_ 163 print "To:", d.to 164 print "Type:", d.type 165 print 166 if d.localName == "message": 167 print "Message..." 168 if d.type == "chat" and d.body: 169 print d.body.textContent 170 elif d.event: 171 print "Composing?", d.event.composing 172 print "Delivered?", d.event.delivered 173 print "Displayed?", d.event.displayed 174 print "Offline?", d.event.offline 175 print "Id:", d.event.id 176 elif d.localName == "presence": 177 print "Presence..." 178 if d.type == "subscribe": 179 presence = s.createPresence() 180 presence.type = "subscribed" 181 presence.from_ = d.to 182 presence.to = d.from_ 183 print "Sending..." 184 print presence.toString(prettyprint=1) 185 s.send(presence) 186 d = s.receive() 187 print d.toString(prettyprint=1) 188 print 189 receiving = 0 190 peer = d.from_ 191 192 finally: 193 s.disconnect() 194 195 # vim: tabstop=4 expandtab shiftwidth=4