--
--   Copyright 2015 Ilkka Oksanen <iao@iki.fi>
--
--   Licensed under the Apache License, Version 2.0 (the "License");
--   you may not use this file except in compliance with the License.
--   You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing,
--   software distributed under the License is distributed on an "AS
--   IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
--   express or implied.  See the License for the specific language
--   governing permissions and limitations under the License.
--

local reporterUserId = ARGV[1]
local conversationId = ARGV[2]
local msgUserId = ARGV[3]
local msgBody = ARGV[4]
local key = 'conversationbuffer:' .. conversationId

-- Remove trailing whitespace, IRC servers may not conserve it. In
-- that case duplicates would pass the check.
msgBody = string.gsub(msgBody, "^(.-)%s*$", "%1")

local msgFingerprint = msgUserId .. ':' .. msgBody

local existingReporter = redis.call('HGET', key, msgFingerprint)

if (not existingReporter) or existingReporter == reporterUserId then
    redis.call('HSET', key, msgFingerprint, reporterUserId)
    redis.call('EXPIRE', key, 45)
    return false
else
    return true
end

--
--   Copyright 2015 Ilkka Oksanen <iao@iki.fi>
--
--   Licensed under the Apache License, Version 2.0 (the "License");
--   you may not use this file except in compliance with the License.
--   You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing,
--   software distributed under the License is distributed on an "AS
--   IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
--   express or implied.  See the License for the specific language
--   governing permissions and limitations under the License.
--

local reporterUserId = ARGV[1]
local conversationId = ARGV[2]
local msgUserId = ARGV[3]
local msgBody = ARGV[4]
local key = 'conversationbuffer:' .. conversationId

-- Remove trailing whitespace, IRC servers may not conserve it. In
-- that case duplicates would pass the check.
msgBody = string.gsub(msgBody, "^(.-)%s*$", "%1")

-- Undo the multiline to single line conversion that IRC backend does
msgBody = string.gsub(msgBody, "\n", " ")

local msgFingerprint = msgUserId .. ':' .. msgBody

local existingReporter = redis.call('HGET', key, msgFingerprint)

if (not existingReporter) or existingReporter == reporterUserId then
    redis.call('HSET', key, msgFingerprint, reporterUserId)
    redis.call('EXPIRE', key, 45)
    return false
else
    return true
end

Fix the duplicate detection of multi-line IRC messages
Fix the duplicate detection of multi-line IRC messages
