-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomponent.lua
More file actions
148 lines (124 loc) · 4.09 KB
/
component.lua
File metadata and controls
148 lines (124 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local verse = require "verse";
local stream = verse.stream_mt;
local jid_split = require "util.jid".split;
local lxp = require "lxp";
local st = require "util.stanza";
local sha1 = require "util.sha1".sha1;
-- Shortcuts to save having to load util.stanza
verse.message, verse.presence, verse.iq, verse.stanza, verse.reply, verse.error_reply =
st.message, st.presence, st.iq, st.stanza, st.reply, st.error_reply;
local new_xmpp_stream = require "util.xmppstream".new;
local xmlns_stream = "http://etherx.jabber.org/streams";
local xmlns_component = "jabber:component:accept";
local stream_callbacks = {
stream_ns = xmlns_stream,
stream_tag = "stream",
default_ns = xmlns_component };
function stream_callbacks.streamopened(stream, attr)
stream.stream_id = attr.id;
if not stream:event("opened", attr) then
stream.notopen = nil;
end
return true;
end
function stream_callbacks.streamclosed(stream)
return stream:event("closed");
end
function stream_callbacks.handlestanza(stream, stanza)
if stanza.attr.xmlns == xmlns_stream then
return stream:event("stream-"..stanza.name, stanza);
elseif stanza.attr.xmlns or stanza.name == "handshake" then
return stream:event("stream/"..(stanza.attr.xmlns or xmlns_component), stanza);
end
return stream:event("stanza", stanza);
end
function stream:reset()
if self.stream then
self.stream:reset();
else
self.stream = new_xmpp_stream(self, stream_callbacks);
end
self.notopen = true;
return true;
end
function stream:connect_component(jid, pass)
self.jid, self.password = jid, pass;
self.username, self.host, self.resource = jid_split(jid);
function self.data(conn, data)
local ok, err = self.stream:feed(data);
if ok then return; end
stream:debug("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "));
stream:close("xml-not-well-formed");
end
self:hook("incoming-raw", function (data) return self.data(self.conn, data); end);
self.curr_id = 0;
self.tracked_iqs = {};
self:hook("stanza", function (stanza)
local id, type = stanza.attr.id, stanza.attr.type;
if id and stanza.name == "iq" and (type == "result" or type == "error") and self.tracked_iqs[id] then
self.tracked_iqs[id](stanza);
self.tracked_iqs[id] = nil;
return true;
end
end);
self:hook("stanza", function (stanza)
if stanza.attr.xmlns == nil or stanza.attr.xmlns == "jabber:client" then
if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
if xmlns then
ret = self:event("iq/"..xmlns, stanza);
if not ret then
ret = self:event("iq", stanza);
end
end
if ret == nil then
self:send(verse.error_reply(stanza, "cancel", "service-unavailable"));
return true;
end
else
ret = self:event(stanza.name, stanza);
end
end
return ret;
end, -1);
self:hook("opened", function (attr)
print(self.jid, self.stream_id, attr.id);
local token = sha1(self.stream_id..pass, true);
self:send(st.stanza("handshake", { xmlns = xmlns_component }):text(token));
self:hook("stream/"..xmlns_component, function (stanza)
if stanza.name == "handshake" then
self:event("authentication-success");
end
end);
end);
local function stream_ready()
self:event("ready");
end
self:hook("authentication-success", stream_ready, -1);
-- Initialise connection
self:connect(self.connect_host or self.host, self.connect_port or 5347);
self:reopen();
end
function stream:reopen()
self:reset();
self:send(st.stanza("stream:stream", { to = self.host, ["xmlns:stream"]='http://etherx.jabber.org/streams',
xmlns = xmlns_component, version = "1.0" }):top_tag());
end
function stream:close(reason)
if not self.notopen then
self:send("</stream:stream>");
end
local on_disconnect = self.conn.disconnect();
self.conn:close();
on_disconnect(conn, reason);
end
function stream:send_iq(iq, callback)
local id = self:new_id();
self.tracked_iqs[id] = callback;
iq.attr.id = id;
self:send(iq);
end
function stream:new_id()
self.curr_id = self.curr_id + 1;
return tostring(self.curr_id);
end