This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.lib.lua
112 lines (90 loc) · 2.87 KB
/
utils.lib.lua
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
local it = require('util.iterators')
local jsplit = require('util.jid').split
local mod_muc = module:depends('muc')
-- read allowed bot tokens from config
local ALLOWED_ZULIP_BOTS = module:get_option_array('allowed_zulip_bots', {})
-- Public functions
--- return iterator which iterates over each value in rooms _occupant table
-- @param table room
local function each_occupant(room)
local function co_iter()
for room_jid, occ_tbl in pairs(room._occupants) do
local _, _, node = jsplit(room_jid)
if node ~= 'focus' then
coroutine.yield(occ_tbl)
end
end
end
return coroutine.wrap(function() co_iter() end)
end
--- returns iterator which iterates over each MUC room
-- compat with prosody MUC code < 0.11
local each_room do
local rooms = rawget(mod_muc, 'rooms')
each_room = rawget(mod_muc, 'each_room') or function() return it.values(rooms) end
end
--- returns the first session of occupants session table
-- @param table occupant
local function get_session(occupant)
local _, session_stanza = next(occupant.sessions)
return session_stanza
end
--- returns nick which is shown in jitsi frontend
-- @param table occupant
local function get_jitsi_nick_from_occupant(occupant)
local stanza = get_session(occupant)
return stanza:get_child_text('nick', 'http://jabber.org/protocol/nick')
end
--- returns the room object for the given room name
-- @param string room_name
local function get_room_from_room_name(room_name)
for room in each_room() do
local name = tostring(room:get_name()) or (room.jid and room.jid:match('^(.-)@.*$'))
if room:get_name() == room_name then
return room
end
end
return nil
end
--- validate received bot token
-- @param string token
local function has_valid_bot_token(token)
for _, btoken in ipairs(ALLOWED_ZULIP_BOTS) do
if token == btoken then return true end
end
return false
end
--- parsed the received message
-- @param string msg
-- @param string trigger
local mention_patt = '^@%*%*%w+%*%*%s+([%w%_]+)%s?([%w%_%-]*).*$'
local priv_patt = '^([%w%_]+)%s?([%w%_%-]*).*$'
local function parse_message(msg, trigger)
module:log('debug', 'Received message: %s', tostring(msg))
if not msg or #msg <= 0 then
module:log('debug', 'Received message is invalid')
return nil
end
local cmd, arg
local function process(c, a)
cmd = c:lower()
arg = a
end
if trigger == 'mention' then
msg:gsub(mention_patt, process)
else
msg:gsub(priv_patt, process)
end
module:log('debug', 'Parsed command: %q', tostring(cmd))
module:log('debug', 'Parsed args: %q', tostring(arg))
return cmd, arg
end
return {
each_occupant = each_occupant,
each_room = each_room,
get_session = get_session,
has_valid_bot_token = has_valid_bot_token,
get_jitsi_nick_from_occupant = get_jitsi_nick_from_occupant,
get_room_from_room_name = get_room_from_room_name,
parse_message = parse_message
}