Comparison

mod_alias/mod_alias.lua @ 1953:0c3ba5ff7a3b

mod_alias: New alias module
author moparisthebest <admin@moparisthebest.com>
date Sat, 21 Nov 2015 00:10:08 -0500
child 2887:65082d91950e
comparison
equal deleted inserted replaced
1952:9d0c33ebbcc5 1953:0c3ba5ff7a3b
1 -- Copyright (C) 2015 Travis Burtrum
2 -- This file is MIT/X11 licensed.
3
4 -- set like so in prosody config, works on full or bare jids, or hosts:
5 --aliases = {
6 -- ["old@example.net"] = "new@example.net";
7 -- ["you@example.com"] = "you@example.net";
8 -- ["conference.example.com"] = "conference.example.net";
9 --}
10
11 local aliases = module:get_option("aliases", {});
12 local alias_response = module:get_option("alias_response", "User $alias can be contacted at $target");
13
14 local st = require "util.stanza";
15
16 function handle_alias(event)
17
18 if event.stanza.attr.type ~= "error" then
19 local alias = event.stanza.attr.to;
20 local target = aliases[alias];
21 if target then
22 local replacements = {
23 alias = alias,
24 target = target
25 };
26 local error_message = alias_response:gsub("%$([%w_]+)", function (v)
27 return replacements[v] or nil;
28 end);
29 local message = st.message{ type = "chat", from = alias, to = event.stanza.attr.from }:tag("body"):text(error_message);
30 module:send(message);
31 return event.origin.send(st.error_reply(event.stanza, "cancel", "gone", error_message));
32 end
33 end
34
35 end
36
37 module:hook("message/bare", handle_alias, 300);
38 module:hook("message/full", handle_alias, 300);
39 module:hook("message/host", handle_alias, 300);
40
41 module:hook("presence/bare", handle_alias, 300);
42 module:hook("presence/full", handle_alias, 300);
43 module:hook("presence/host", handle_alias, 300);