1 -- mod_muc_notifications
3 -- Copyright (C) 2019 Marcos de Vera Piquero <marcos.devera@quobis.com>
5 -- This file is MIT/X11 licensed.
7 -- A module to notify non-present members of messages in a group chat
10 local id
= require
"util.id"
11 local st
= require
"util.stanza"
13 local use_invite
= module
:get_option_boolean("muc_notification_invite", false)
15 -- Given a stanza, compute if it qualifies as important (notifiable)
16 -- return true for message stanzas with non-empty body
17 -- Should probably use something similar to muc-message-is-historic event
18 local function is_important(stanza
)
19 local body
= stanza
:find("body#")
23 local function handle_muc_message(event
)
24 -- event.room and event.stanza are available
25 local room
= event
.room
26 local stanza
= event
.stanza
27 for jid
, aff
in pairs(room
._affiliations
) do
28 if aff
~= "outcast" then
29 local is_occupant
= false
30 for _
, occupant
in pairs(room
._occupants
) do
31 if occupant
.bare_jid
== jid
then
36 if not is_occupant
and is_important(stanza
) then
37 -- send notification to jid
44 xmlns
= "http://quobis.com/xmpp/muc#push",
47 local reason
= "You have messages in group chat "..room
:get_name()
48 local notification
= st
.message(attrs
)
50 :tag("notification", not_attrs
):up()
51 :tag("no-store", {xmlns
= "urn:xmpp:hints"})
52 local invite
= st
.message(attrs
):tag("x", {xmlns
= "http://jabber.org/protocol/muc#user"})
53 :tag("invite", {from
= stanza
.attr
.from
})
54 :tag("reason"):text(reason
):up():up():up()
55 :tag("notification", not_attrs
):up()
56 :tag("no-store", {xmlns
= "urn:xmpp:hints"})
57 module
:log("debug", "notifying with %s", tostring(use_invite
and invite
or notification
))
58 module
:send(use_invite
and invite
or notification
)
59 module
:log("debug", "sent notification of MUC message %s", use_invite
and invite
or notification
)
65 module
:hook("muc-broadcast-message", handle_muc_message
)
67 module
:log("debug", "Module loaded")