From 5bf4e308cc4a090d1f99c26637d92473f57d928d Mon Sep 17 00:00:00 2001 From: David Goulet Date: Mon, 7 Apr 2014 21:50:10 -0400 Subject: [PATCH] Initial import of the prosody otr module Signed-off-by: David Goulet --- mod_otr.lua | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 mod_otr.lua diff --git a/mod_otr.lua b/mod_otr.lua new file mode 100644 index 0000000..481b073 --- /dev/null +++ b/mod_otr.lua @@ -0,0 +1,79 @@ +-- +-- Copyright (C) 2014 - David Goulet +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License, version 2 only, +-- as published by the Free Software Foundation. +-- +-- This program is distributed in the hope that it will be useful, but WITHOUT +-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +-- more details. +-- +-- You should have received a copy of the GNU General Public License along +-- with this program; if not, write to the Free Software Foundation, Inc., +-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +-- + +local st = require "util.stanza"; + +-- Module option. +-- +-- This tells the module policy which for now there is two choices. +-- mandatory: OTR will be enforced +-- optional: Warn user to suggest OTR. (default) +local policy = module:get_option_string("otr_policy", "optional"); + +local mandatory; +local mandatory_msg = "For security reasons, OTR encryption is required for conversations on this server"; +local optional; +local optional_msg = "For security reasons, OTR encryption is STRONGLY recommended for conversations on this server"; + +local messaged = {}; + +-- +-- Check body message for the presence of the OTR tag. +local function check_message_otr(event) + local body = event.stanza:get_child_text("body"); + local session = event.origin; + local is_otr; + + -- Is this body is an OTR message? + if body and body:sub(1,4) ~= "?OTR" then + is_otr = 0; + else + is_otr = 1; + end + + -- Force OTR if policy is mandatory. + if mandatory and is_otr == 0 then + event.origin.send(st.message{ type = "chat", from = module.host, to = event.stanza.attr.from }:tag("body"):text(mandatory_msg)); + return true; + end + + -- Warn if NO otr is detected and if we've NOT warned before the user. + if optional and messaged[session.full_jid] == nil and is_otr == 0 then + event.origin.send(st.message{ type = "chat", from = module.host, to = event.stanza.attr.from }:tag("body"):text(optional_msg)); + messaged[session.full_jid] = 1 + end +end + +-- Module load entry point. +function module.load() + -- Validate policy option. + if policy == "mandatory" then + mandatory = 1; + elseif policy == "optional" then + optional = 1; + else + -- Invalid policy, stop loading module. + module:log("error", "Unknown policy %s", policy); + return; + end + + module:log("info", "OTR policy set to %s", policy); +end + +module:hook("message/bare", check_message_otr, 300); +module:hook("message/full", check_message_otr, 300); +module:hook("message/host", check_message_otr, 300);