Annotate

component.lua @ 106:7d293830f4a4

squishy: Add proxy65, jingle, jingle_ft, jingle_s5b, disco plugins
author Matthew Wild <mwild1@gmail.com>
date Sat, 21 Aug 2010 15:37:04 +0100
parent 84:d85d2443478e
child 150:728cc7f2f0c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local verse = require "verse";
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local stream = verse.stream_mt;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local jid_split = require "util.jid".split;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local lxp = require "lxp";
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local st = require "util.stanza";
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local sha1 = require "util.sha1".sha1;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 -- Shortcuts to save having to load util.stanza
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply =
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 st.message, st.presence, st.iq, st.stanza, st.reply, st.error_reply;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local init_xmlhandlers = require "core.xmlhandlers";
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local xmlns_stream = "http://etherx.jabber.org/streams";
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local xmlns_component = "jabber:component:accept";
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local stream_callbacks = {
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 stream_ns = xmlns_stream,
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 stream_tag = "stream",
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 default_ns = "jabber:client" };
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function stream_callbacks.streamopened(stream, attr)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 stream.stream_id = attr.id;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 if not stream:event("opened", attr) then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 stream.notopen = nil;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return true;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 function stream_callbacks.streamclosed(stream)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 return stream:event("closed");
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 function stream_callbacks.handlestanza(stream, stanza)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if stanza.attr.xmlns == xmlns_stream then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 return stream:event("stream-"..stanza.name, stanza);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 elseif stanza.attr.xmlns then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 return stream:event("stream/"..stanza.attr.xmlns, stanza);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return stream:event("stanza", stanza);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 function stream:reset()
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 -- Reset stream
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local parser = lxp.new(init_xmlhandlers(self, stream_callbacks), "\1");
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 self.parser = parser;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 self.notopen = true;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return true;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 function stream:connect_component(jid, pass)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 self.jid, self.password = jid, pass;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 self.username, self.host, self.resource = jid_split(jid);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 function self.data(conn, data)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local ok, err = self.parser:parse(data);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if ok then return; end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "));
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 stream:close("xml-not-well-formed");
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 self:hook("incoming-raw", function (data) return self.data(self.conn, data); end);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 self.curr_id = 0;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 self.tracked_iqs = {};
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 self:hook("stanza", function (stanza)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local id, type = stanza.attr.id, stanza.attr.type;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if id and stanza.name == "iq" and (type == "result" or type == "error") and self.tracked_iqs[id] then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 self.tracked_iqs[id](stanza);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 self.tracked_iqs[id] = nil;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 return true;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 self:hook("stanza", function (stanza)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 if stanza.attr.xmlns == nil or stanza.attr.xmlns == "jabber:client" then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 if xmlns then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 ret = self:event("iq/"..xmlns, stanza);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 if not ret then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 ret = self:event("iq", stanza);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if ret == nil then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 self:send(verse.error_reply(stanza, "cancel", "service-unavailable"));
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 return true;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 else
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 ret = self:event(stanza.name, stanza);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return ret;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end, -1);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 self:hook("opened", function (attr)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 print(self.jid, self.stream_id, attr.id);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local token = sha1(self.stream_id..pass, true);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 self:send(st.stanza("handshake", { xmlns = xmlns_component }):text(token));
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 self:hook("stream/"..xmlns_component, function (stanza)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 if stanza.name == "handshake" then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 self:event("authentication-success");
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 local function stream_ready()
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 self:event("ready");
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 self:hook("authentication-success", stream_ready, -1);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 -- Initialise connection
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 self:connect(self.connect_host or self.host, self.connect_port or 5347);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 self:reopen();
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 function stream:reopen()
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 self:reset();
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams',
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 xmlns = xmlns_component, version = "1.0" }):top_tag());
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 function stream:close(reason)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 if not self.notopen then
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 self:send("</stream:stream>");
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local on_disconnect = self.conn.disconnect();
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 self.conn:close();
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 on_disconnect(conn, reason);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 function stream:send_iq(iq, callback)
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 local id = self:new_id();
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 self.tracked_iqs[id] = callback;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 iq.attr.id = id;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 self:send(iq);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 function stream:new_id()
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 self.curr_id = self.curr_id + 1;
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 return tostring(self.curr_id);
d85d2443478e verse.component: XEP-0114 support \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end