summaryrefslogtreecommitdiff
path: root/xmpp.lua
blob: 84400bba9af2796567fff1016d71bcb1894ade33 (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
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
local xmpp_conf = {
	jid='wilson@ubq323.website',
	server='ubq323.website',
	auth='AHdpbHNvbgBncmVnb3J5PDM=',
	resource='cheese',
	muc='d@conference.ubq323.website',
}

local cqueues = require'cqueues'
local socket = require'cqueues.socket'
local xml = require'xml'
local X = xml.X
local xmlify = xml.xmlify
local pprint=require'pprint'
local discord=require'discord'


local function connect(conf)
	local sock = assert(socket.connect(conf.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(conf.jid, conf.server)

	-- state of the art xml parser
	local function check_and_send(test, text)
		local x = sock:read('-2048')
		assert(x:find(test))
		if text then sock:write(text) end
	end
	local function ietf_urn(v) return 'urn:ietf:params:xml:ns:xmpp-'..v end

	sock:write(start)
	check_and_send('starttls', xmlify(X.starttls{xmlns=ietf_urn"tls"}))
	check_and_send('proceed', nil)
	sock:starttls()
	sock:write(start)
	check_and_send('PLAIN',
		xmlify(X.auth{xmlns=ietf_urn"sasl", mechanism='PLAIN', conf.auth}))
	check_and_send('success',start)
	check_and_send('bind',
		xmlify(X.iq{type='set', id='aaaa',
			X.bind{xmlns=ietf_urn"bind", X.resource{conf.resource}}}))
	check_and_send('jid',X.presence{X.show{'chat'}})

	return sock
end


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


local function run_xmpp(cq)
	local sock = connect(xmpp_conf)

	-- sock:write(xmlify(
	-- 	X.presence{to=xmpp_conf.muc..'/wilson',
	-- 		X.x{xmlns='http://jabber.org/protocol/muc',
	-- 			X.history{maxstanzas='0'}}}))


	-- cq:wrap(function()
	-- 	local n = 1
	-- 	while true do
	-- 		sock:write(xmlify(
	-- 			X.message{to=xmpp_conf.muc, type='groupchat',
	-- 				id='wilson_episode_'..n,
	-- 				X.body{'i too am in episode '..n}}))
	-- 		n=n+1
	-- 		cqueues.sleep(60)
	-- 	end
	-- end)

	sock:write(xmlify(
		X.iq{type='get',to=xmpp_conf.muc, id='info1',
			X.query{xmlns='http://jabber.org/protocol/disco#info'}}))


	for x in xml.stanzae(function() return sock:read('-2048') end) do
		pprint(x)
		print(xmlify(x))
		local body = x'body' and x'body'[1]
		if x.label == 'message' then
			pprint('M', x.xarg.from, body)
			if body == 'hi wilson' then
				sock:write(xmlify(
					X.message{to=xmpp_conf.muc, type='groupchat',
						X.body{'hi '..x.xarg.from..'.'}}))
			end
			-- discord.grom(x.xarg.from, body or '*(there\'s no body here)*')
		end
	end
end

local cq = cqueues.new()
cq:wrap(run_xmpp, cq)

pprint(cq:loop())