mod_muc_inject_mentions/mod_muc_inject_mentions.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 24 Sep 2022 09:26:26 +0100
changeset 5063 5f1120c284c5
parent 4316 33a41503b9e3
permissions -rw-r--r--
mod_cloud_notify_extensions: Add note about dependency Noting here because people might not click through to see it on the mod_cloud_notify_encrypted page.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
     1
module:depends("muc");
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
     2
4144
fea4a4831e10 mod_muc_inject_mentions: (W211) unused variable node, (W211) unused variable host
Seve Ferrer <seve@delape.net>
parents: 4143
diff changeset
     3
local jid_resource = require "util.jid".resource;
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
     4
local st = require "util.stanza";
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
     5
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
     6
local prefixes = module:get_option_set("muc_inject_mentions_prefixes", {})
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
     7
local suffixes = module:get_option_set("muc_inject_mentions_suffixes", {})
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
     8
local enabled_rooms = module:get_option("muc_inject_mentions_enabled_rooms", nil)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
     9
local disabled_rooms = module:get_option("muc_inject_mentions_disabled_rooms", nil)
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    10
local mention_delimiters = module:get_option_set("muc_inject_mentions_mention_delimiters",  {" ", "", "\n", "\t"})
4166
f7bc0e4ab4a2 mod_muc_inject_mentions: Should not append mentions by default
Seve Ferrer <seve@delape.net>
parents: 4165
diff changeset
    11
local append_mentions = module:get_option("muc_inject_mentions_append_mentions", false)
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    12
local strip_out_prefixes = module:get_option("muc_inject_mentions_strip_out_prefixes", false)
4168
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    13
local reserved_nicks = module:get_option("muc_inject_mentions_reserved_nicks", false)
4257
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
    14
local use_bare_jid = module:get_option("muc_inject_mentions_use_bare_jid", true)
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
    15
local prefix_mandatory = module:get_option("muc_inject_mentions_prefix_mandatory", false)
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    16
local reserved_nicknames = {}
4159
308b92b07da6 mod_muc_inject_mentions: Refactor code using in_list utility function to improve readability
Seve Ferrer <seve@delape.net>
parents: 4149
diff changeset
    17
4163
94e3e7753220 mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
    18
local reference_xmlns = "urn:xmpp:reference:0"
4159
308b92b07da6 mod_muc_inject_mentions: Refactor code using in_list utility function to improve readability
Seve Ferrer <seve@delape.net>
parents: 4149
diff changeset
    19
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    20
local function update_reserved_nicknames(event)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    21
    local room, data, jid = event.room.jid, event.data, event.jid
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    22
    load_room_reserved_nicknames(event.room)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    23
    local nickname = (data or {})["reserved_nickname"]
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    24
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    25
    if nickname then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    26
        reserved_nicknames[room][nickname] = jid
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    27
    else
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    28
        local nickname_to_remove
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    29
        for _nickname, _jid in pairs(reserved_nicknames[room]) do
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    30
            if _jid == jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    31
                nickname_to_remove = _nickname
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    32
                break
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    33
            end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    34
        end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    35
        if nickname_to_remove then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    36
            reserved_nicknames[room][nickname_to_remove] = nil
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    37
        end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    38
    end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    39
end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    40
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    41
function load_room_reserved_nicknames(room)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    42
    if not reserved_nicknames[room.jid] then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    43
        reserved_nicknames[room.jid] = {}
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    44
        for jid, data in pairs(room._affiliations_data or {}) do
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    45
            local reserved_nickname = data["reserved_nickname"]
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    46
            if reserved_nicknames then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    47
                reserved_nicknames[room.jid][reserved_nickname] = jid
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    48
            end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    49
        end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    50
    end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    51
end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    52
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    53
local function get_jid(room, nickname)
4257
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
    54
    local bare_jid = reserved_nicknames[room.jid][nickname]
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
    55
    if bare_jid and use_bare_jid then
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
    56
        return bare_jid
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    57
    end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    58
4257
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
    59
    if bare_jid and not use_bare_jid then
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    60
        return room.jid .. "/" .. nickname
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    61
    end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    62
end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
    63
4168
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    64
local function get_participants(room)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    65
    if not reserved_nicks then
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    66
        local occupants = room._occupants
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    67
        local key, occupant = next(occupants)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    68
        return function ()
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    69
            while occupant do -- luacheck: ignore
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    70
                local nick = jid_resource(occupant.nick);
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    71
                local bare_jid = occupant.bare_jid
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    72
                key, occupant = next(occupants, key)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    73
                return bare_jid, nick
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    74
            end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    75
        end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    76
    else
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    77
        local generator = room:each_affiliation()
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    78
        local jid, _, affiliation_data = generator(nil, nil)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    79
        return function ()
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    80
           while jid do
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    81
                local bare_jid, nick = jid, (affiliation_data or {})["reserved_nickname"]
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    82
                jid, _, affiliation_data = generator(nil, bare_jid)
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    83
                if nick then
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    84
                    return bare_jid, nick
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    85
                end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    86
           end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    87
        end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    88
    end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    89
end
a82b0745383b mod_muc_inject_mentions: Add new configuration setting to choose between registered nicknames or online participants
Seve Ferrer <seve@delape.net>
parents: 4167
diff changeset
    90
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    91
local function add_mention(mentions, bare_jid, first, last, prefix_indices, has_prefix)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    92
    if strip_out_prefixes then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    93
        if has_prefix then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    94
            table.insert(prefix_indices, first-1)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    95
        end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    96
        first = first - #prefix_indices
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    97
        last = last - #prefix_indices
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    98
    end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
    99
    mentions[first] = {bare_jid=bare_jid, first=first, last=last}
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   100
end
4165
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   101
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   102
local function get_client_mentions(stanza)
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   103
    local has_mentions = false
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   104
    local client_mentions = {}
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   105
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   106
    for element in stanza:childtags("reference", reference_xmlns) do
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   107
        if element.attr.type == "mention" then
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   108
            local key = tonumber(element.attr.begin) + 1 -- count starts at 0
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   109
            client_mentions[key] = {bare_jid=element.attr.uri, first=element.attr.begin, last=element.attr["end"]}
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   110
            has_mentions = true
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   111
        end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   112
    end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   113
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   114
    return has_mentions, client_mentions
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   115
end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   116
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   117
local function is_room_eligible(jid)
4316
33a41503b9e3 Improve UX by providing defaults users expect
Seve Ferrer <seve@delape.net>
parents: 4315
diff changeset
   118
    if not enabled_rooms and not disabled_rooms then return true; end
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   119
4316
33a41503b9e3 Improve UX by providing defaults users expect
Seve Ferrer <seve@delape.net>
parents: 4315
diff changeset
   120
    if enabled_rooms then
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   121
        for _, _jid in ipairs(enabled_rooms) do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   122
            if _jid == jid then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   123
                return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   124
            end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   125
        end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   126
        return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   127
    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   128
4316
33a41503b9e3 Improve UX by providing defaults users expect
Seve Ferrer <seve@delape.net>
parents: 4315
diff changeset
   129
    if disabled_rooms then
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   130
        for _, _jid in ipairs(disabled_rooms) do
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   131
            if _jid == jid then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   132
                return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   133
            end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   134
        end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   135
        return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   136
    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   137
end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   138
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   139
local function has_nick_prefix(body, first)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   140
    -- There are no configured prefixes
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   141
    if not prefixes or #prefixes < 1 then return false end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   142
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   143
    -- Prefix must have a space before it,
4146
6906562af2ee mod_muc_inject_mentions: Allow preffixes to be used after a new line
Seve Ferrer <seve@delape.net>
parents: 4145
diff changeset
   144
    -- be the first character of the body
6906562af2ee mod_muc_inject_mentions: Allow preffixes to be used after a new line
Seve Ferrer <seve@delape.net>
parents: 4145
diff changeset
   145
    -- or be the first character after a new line
4163
94e3e7753220 mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
   146
    if not mention_delimiters:contains(body:sub(first - 2, first - 2)) then
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   147
        return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   148
    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   149
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   150
    local prefix = body:sub(first - 1, first - 1)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   151
    for _, _prefix in ipairs(prefixes) do
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   152
        if prefix == _prefix then
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   153
            return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   154
        end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   155
    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   156
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   157
    return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   158
end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   159
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   160
local function has_nick_suffix(body, last)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   161
    -- There are no configured suffixes
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   162
    if not suffixes or #suffixes < 1 then return false end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   163
4147
b2080f76e0aa mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents: 4146
diff changeset
   164
    -- Suffix must have a space after it,
b2080f76e0aa mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents: 4146
diff changeset
   165
    -- be the last character of the body
b2080f76e0aa mod_muc_inject_mentions: Allow suffixes to be used before a new line
Seve Ferrer <seve@delape.net>
parents: 4146
diff changeset
   166
    -- or be the last character before a new line
4163
94e3e7753220 mod_muc_inject_mentions: Improve mentions lookup by using a set instead of a list
Seve Ferrer <seve@delape.net>
parents: 4159
diff changeset
   167
    if not mention_delimiters:contains(body:sub(last + 2, last + 2)) then
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   168
        return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   169
    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   170
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   171
    local suffix = body:sub(last+1, last+1)
4143
c6bb64a12f92 mod_muc_inject_mentions: (W213) unused loop variable i
Seve Ferrer <seve@delape.net>
parents: 4142
diff changeset
   172
    for _, _suffix in ipairs(suffixes) do
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   173
        if suffix == _suffix then
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   174
            return true
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   175
        end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   176
    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   177
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   178
    return false
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   179
end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   180
4165
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   181
local function search_mentions(room, body, client_mentions)
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   182
    load_room_reserved_nicknames(room)
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   183
    local mentions, prefix_indices = {}, {}
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   184
    local current_word = ""
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   185
    local current_word_start
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   186
    for i = 1, #body+1 do
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   187
        local char = body:sub(i,i)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   188
        -- Mention delimiter found, current_word is completed now
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   189
        if mention_delimiters:contains(char) and current_word_start then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   190
            -- Check for nickname without prefix
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   191
            local jid = get_jid(room, current_word)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   192
            if jid then
4257
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
   193
                if not prefix_mandatory then
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
   194
                    add_mention(mentions, jid, current_word_start, i - 1, prefix_indices, false)
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
   195
                end
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   196
            else
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   197
                -- Check for nickname with affixes
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   198
                local prefix = prefixes:contains(current_word:sub(1,1))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   199
                local suffix = suffixes:contains(current_word:sub(-1))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   200
                if prefix and suffix then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   201
                    jid = get_jid(room, current_word:sub(2, -2))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   202
                    if jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   203
                        add_mention(mentions, jid, current_word_start + 1, i - 2, prefix_indices, true)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   204
                    end
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   205
                elseif prefix then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   206
                    jid = get_jid(room, current_word:sub(2))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   207
                    if jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   208
                        add_mention(mentions, jid, current_word_start + 1, i - 1, prefix_indices, true)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   209
                    end
4257
32b4901a9d8d mod_muc_inject_mentions: Add new setting to trigger mentions only if a prefix is found
Seve Ferrer <seve@delape.net>
parents: 4247
diff changeset
   210
                elseif suffix and not prefix_mandatory then
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   211
                    jid = get_jid(room, current_word:sub(1, -2))
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   212
                    if jid then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   213
                        add_mention(mentions, jid, current_word_start, i - 2, prefix_indices, false)
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   214
                    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   215
                end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   216
            end
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   217
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   218
            current_word = ""
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   219
            current_word_start = nil
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   220
        elseif not mention_delimiters:contains(char) then
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   221
            current_word_start = current_word_start or i
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   222
            current_word = current_word .. char
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   223
        end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   224
    end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   225
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   226
    return mentions, prefix_indices
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   227
end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   228
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   229
local function muc_inject_mentions(event)
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   230
    local room, stanza = event.room, event.stanza;
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   231
    local body = stanza:get_child_text("body")
4165
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   232
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   233
    if not body or #body < 1 then return; end
4165
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   234
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   235
    -- Inject mentions only if the room is configured for them
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   236
    if not is_room_eligible(room.jid) then return; end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   237
4165
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   238
    -- Only act on messages that do not include mentions
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   239
    -- unless configuration states otherwise.
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   240
    local has_mentions, client_mentions = get_client_mentions(stanza)
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   241
    if has_mentions and not append_mentions then return; end
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   242
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   243
    local mentions, prefix_indices = search_mentions(room, body, client_mentions)
4165
032e1c79d039 mod_muc_inject_mentions: Add new configuration setting to look for mentions even if the client sent some already
Seve Ferrer <seve@delape.net>
parents: 4163
diff changeset
   244
    for _, mention in pairs(mentions) do
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   245
        -- https://xmpp.org/extensions/xep-0372.html#usecase_mention
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   246
        stanza:tag(
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   247
            "reference", {
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   248
                xmlns=reference_xmlns,
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   249
                begin=tostring(mention.first - 1), -- count starts at 0
4315
a6c253bc63a5 End value is index+1 as per the newest revision https://xmpp.org/extensions/xep-0372.html#revision-history-v0.4.0
Seve Ferrer <seve@delape.net>
parents: 4257
diff changeset
   250
                ["end"]=tostring(mention.last),
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   251
                type="mention",
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   252
                uri="xmpp:" .. mention.bare_jid,
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   253
            }
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   254
        ):up()
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   255
    end
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   256
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   257
    if strip_out_prefixes then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   258
        local body_without_prefixes = ""
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   259
        local from = 0
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   260
        if #prefix_indices > 0 then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   261
            for _, prefix_index in ipairs(prefix_indices) do
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   262
                body_without_prefixes = body_without_prefixes .. body:sub(from, prefix_index-1)
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   263
                from = prefix_index + 1
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   264
            end
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   265
            body_without_prefixes = body_without_prefixes .. body:sub(from, #body)
4167
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   266
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   267
            -- Replace original body containing prefixes
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   268
            stanza:maptags(
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   269
                function(tag)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   270
                    if tag.name ~= "body" then
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   271
                        return tag
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   272
                    end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   273
                    return st.stanza("body"):text(body_without_prefixes)
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   274
                end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   275
            )
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   276
        end
320f6d374b5d mod_muc_inject_mentions: Add new configuration setting to strip out prefixes from mentions
Seve Ferrer <seve@delape.net>
parents: 4166
diff changeset
   277
    end
4142
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   278
end
e8c1b35bc25b mod_muc_inject_mentions: Publish module to repository
Seve Ferrer <seve@delape.net>
parents:
diff changeset
   279
4247
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   280
module:hook("muc-occupant-groupchat", muc_inject_mentions)
aed7038ab2ab mod_muc_inject_mentions: Make module scalable by iterating through the body instead of participants list as the main loop
Seve Ferrer <seve@delape.net>
parents: 4168
diff changeset
   281
module:hook("muc-set-affiliation", update_reserved_nicknames)