summaryrefslogtreecommitdiff
path: root/xmpp.lua
blob: aebbee94be4fa37acdeb95d68280d563a868d3e7 (plain)
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
local jid = 'wilson@ubq323.website'
local server = 'ubq323.website'
local auth = 'AHdpbHNvbgBncmVnb3J5PDM='
local resource = 'cheese'


local cqueues = require'cqueues'
local socket = require'cqueues.socket'


local function connect()
	local sock = assert(socket.connect(server, 5222))
	sock:setmode('bn','bn')

	local start = ([[
<?xml version='1.0'?><stream:stream from='%s' to='%s' version='1.0' xml:lang='en' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>]]):format(jid, server)

	-- state of the art xml parser
	local function check_and_send(test, text)
		local x = sock:read('-2048')
		print(x)
		if text then sock:write(text) end
	end

	sock:write(start)
	check_and_send('starttls', [[<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>]])
	check_and_send('proceed', nil)
	sock:starttls()
	sock:write(start)
	check_and_send('PLAIN',
		([[<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>]]):format(auth))
	check_and_send('success',start)
	check_and_send('bind', ([[<iq type='set' id='aaa'>
		<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>%s</resource></bind></iq>
	]]):format(resource))
	check_and_send('jid','<presence/>')

	return sock
end


local mucs = {
	'ja@conference.ubq323.website',
	'a@conference.ubq323.website',
}

local xml = require'xml'
local pprint=require'pprint'

local function run_xmpp()
	local sock = connect()

	for _, muc in ipairs(mucs) do
		sock:write(([[
			<presence to='%s'>
				<x xmlns='http://jabber.org/protocol/muc'>
					<history maxstanzas='0'/>
				</x>
			</presence>]]):format(muc .. '/wilson'))
	end

	for x in xml.stanzae(function() return sock:read('-2048') end) do
		-- pprint(x)
		local body = x'body' and x'body'[1]
		if x.label == 'message' then pprint('M', x.xarg.from, body) end
	end
end

local cq = cqueues.new()
cq:wrap(run_xmpp)
pprint(cq:loop())