| | 124 | //// |
| | 125 | // create xmpp client |
| | 126 | // register callbacks |
| | 127 | // connect to xmpp server |
| | 128 | xmpp = new XMPPClient(); |
| | 129 | xmpp.onPresence = function(ntype, from, to) { |
| | 130 | if (! ntype) { |
| | 131 | userList.onUserAvailable(from, from); |
| | 132 | } |
| | 133 | else if (ntype == "unavailable") { |
| | 134 | userList.onUserUnavailable(from); |
| | 135 | } |
| | 136 | else if (ntype == "subscribe") { |
| | 137 | xmpp.sendSubscribed(from, to); |
| | 138 | } |
| | 139 | else if (ntype == "subscribed") { |
| | 140 | alert(from + " added to your buddy list!"); |
| | 141 | } |
| | 142 | else if (ntype == "unsubscribed") { |
| | 143 | userList.onUserUnavailable(from); |
| | 144 | } |
| | 145 | } |
| | 146 | xmpp.onMessage = onMessage; |
| | 147 | xmpp.onSocketConnect = function() { |
| | 148 | domain = prompt("Domain","localhost"); |
| | 149 | if (domain) { |
| | 150 | xmpp.connectServer(domain, connectSuccess, connectFailure); |
| | 151 | } |
| | 152 | } |
| | 153 | //// |
| | 154 | // 'localhost' IS A PLACEHOLDER |
| | 155 | xmpp.connect('localhost', 5222); |
| | 156 | // (CHANGE TO ADDRESS OF PHYSICAL MACHINE) |
| | 157 | //// |
| | 158 | // success / failure callbacks |
| | 159 | function registerSuccess() { |
| | 160 | alert("Welcome!"); |
| | 161 | } |
| | 162 | function registerFailure() { |
| | 163 | if (confirm("That user name is taken. Try again?")) { |
| | 164 | prompt_register(); |
| | 165 | } |
| | 166 | } |
| | 167 | function loginSuccess() { |
| | 168 | alert("Welcome!"); |
| | 169 | } |
| | 170 | function loginFailure() { |
| | 171 | if (confirm("Login failed. Register a new user account?")) { |
| | 172 | prompt_register(); |
| | 173 | } |
| | 174 | else { |
| | 175 | prompt_login(); |
| | 176 | } |
| | 177 | } |
| | 178 | function connectSuccess() { |
| | 179 | prompt_login(); |
| | 180 | } |
| | 181 | function connectFailure() { |
| | 182 | alert("Unknown domain"); |
| | 183 | } |
| | 184 | //// |
| | 185 | // helpers |
| | 186 | function prompt_login() { |
| | 187 | var u = prompt("User name","frank"); |
| | 188 | if (u) { |
| | 189 | var p = prompt("Password","pass"); |
| | 190 | if (p) { |
| | 191 | xmpp.login(u, p, loginSuccess, loginFailure); |
| | 192 | } |
| | 193 | } |
| | 194 | } |
| | 195 | function prompt_register() { |
| | 196 | var u = prompt("New user name","ariel"); |
| | 197 | if (u) { |
| | 198 | var p = prompt("New password","pass"); |
| | 199 | if (p) { |
| | 200 | xmpp.register(u, p, registerSuccess, registerFailure); |
| | 201 | } |
| | 202 | } |
| | 203 | } |
| | 204 | //// |
| | 205 | |