Annotate

plugins/bind.lua @ 54:1a2a3d598254

verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
author Matthew Wild <mwild1@gmail.com>
date Thu, 06 May 2010 10:27:11 +0100
parent 43:a33036b7e5ab
child 78:f4188eff53a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local st = require "util.stanza";
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local xmlns_bind = "urn:ietf:params:xml:ns:xmpp-bind";
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 function verse.plugins.bind(stream)
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local function handle_features(features)
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 if stream.bound then return; end
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 stream:debug("Binding resource...");
40
afd037420977 plugins.bind: Requested JID^Wresource should be in <jid/>^W<resource/> tag (thanks Maranda :) )
Matthew Wild <mwild1@gmail.com>
parents: 39
diff changeset
8 stream:send_iq(st.iq({ type = "set" }):tag("bind", {xmlns=xmlns_bind}):tag("resource"):text(stream.resource),
9
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 function (reply)
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 if reply.attr.type == "result" then
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local result_jid = reply
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 :get_child("bind", xmlns_bind)
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 :get_child("jid")
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 :get_text();
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 stream.username, stream.host, stream.resource = jid.split(result_jid);
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 stream.jid, stream.bound = result_jid, true;
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 stream:event("binding-success", full_jid);
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 elseif reply.attr.type == "error" then
43
a33036b7e5ab verse.plugins.bind: Fix incorrect variable name causing traceback on unsuccessful bind
Matthew Wild <mwild1@gmail.com>
parents: 40
diff changeset
19 local err = reply:child_with_name("error");
a33036b7e5ab verse.plugins.bind: Fix incorrect variable name causing traceback on unsuccessful bind
Matthew Wild <mwild1@gmail.com>
parents: 40
diff changeset
20 local type, condition, text = reply:get_error();
9
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 stream:event("binding-failure", { error = condition, text = text, type = type });
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end);
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 stream:hook("stream-features", handle_features, 200);
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return true;
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
ca225a2d67b4 plugins.bind: Add plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28