comparison mod_muc_notifications/mod_muc_notifications.lua @ 3498:69219097aa85

muc_notifications: notify non-present members of new messages This module, in the event of a new message in a Group Chat, will generate a notification for each of those members not present at that time in the Group Chat
author marc0s <marcos.devera@quobis.com>
date Fri, 29 Mar 2019 17:03:05 +0100
parents
children ef752c5115f7
comparison
equal deleted inserted replaced
3497:bc67519803f5 3498:69219097aa85
1 -- mod_muc_notifications
2 --
3 -- Copyright (C) 2019 Marcos de Vera Piquero <marcos.devera@quobis.com>
4 --
5 -- This file is MIT/X11 licensed.
6 --
7 -- A module to notify non-present members of messages in a group chat
8 --
9
10 local id = require"util.id"
11 local st = require"util.stanza"
12
13 local use_invite = module:get_option_boolean("muc_notification_invite", false)
14
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#")
20 return body and #body
21 end
22
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
32 is_occupant = true
33 break
34 end
35 end
36 if not is_occupant and is_important(stanza) then
37 -- send notification to jid
38 local attrs = {
39 to = jid,
40 id = id.short(),
41 from = room.jid,
42 }
43 local not_attrs = {
44 xmlns = "http://quobis.com/xmpp/muc#push",
45 jid = room.jid,
46 }
47 local reason = "You have messages in group chat "..room:get_name()
48 local notification = st.message(attrs)
49 :body(reason):up()
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)
60 end
61 end
62 end
63 end
64
65 module:hook("muc-broadcast-message", handle_muc_message)
66
67 module:log("debug", "Module loaded")