mod_admin_messageconsole.lua
changeset 0 dc4eb8fe8b94
child 1 7265595dbc3b
equal deleted inserted replaced
-1:000000000000 0:dc4eb8fe8b94
       
     1 -- Prosody IM
       
     2 --
       
     3 -- This module depends on Prosody's admin_telnet module
       
     4 --
       
     5 -- Copyright (C) 2008-2010 Matthew Wild
       
     6 -- Copyright (C) 2008-2010 Waqas Hussain
       
     7 -- Copyright (C) 2012 Mikael Berthe
       
     8 --
       
     9 -- This project is MIT/X11 licensed. Please see the
       
    10 -- COPYING file in the source package for more information.
       
    11 --
       
    12 
       
    13 local st = require "util.stanza"; -- Import Prosody's stanza API into 'st'
       
    14 local um_is_admin = require "core.usermanager".is_admin;
       
    15 
       
    16 local admin_telnet = module:depends("admin_telnet");
       
    17 local telnet_def_env = module:shared("/*/admin_telnet/env");
       
    18 local telnet_commands = module:shared("/*/admin_telnet/commands")
       
    19 local default_env_mt = { __index = telnet_def_env };
       
    20 
       
    21 local host = module.host;
       
    22 
       
    23 -- Create our own session.  print() will store the results in a text
       
    24 -- string.  send(), quit(), disconnect() are no-op.
       
    25 local function new_session ()
       
    26 	local session = {
       
    27 			send	    = function ()  end;
       
    28 			quit	    = function ()  end;
       
    29 			disconnect  = function ()  end;
       
    30 			};
       
    31 
       
    32 	session.print = function (...)
       
    33 		local t = {};
       
    34 		for i=1,select("#", ...) do
       
    35 			t[i] = tostring(select(i, ...));
       
    36 		end
       
    37 		local text = "| "..table.concat(t, "\t");
       
    38 		if session.fulltext then
       
    39 		    session.fulltext = session.fulltext .. "\n" .. text;
       
    40 		else
       
    41 		    session.fulltext = text;
       
    42 		end
       
    43 	end;
       
    44 
       
    45 	session.env = setmetatable({}, default_env_mt);
       
    46 
       
    47 	-- Load up environment with helper objects
       
    48 	for name, t in pairs(telnet_def_env) do
       
    49 		if type(t) == "table" then
       
    50 			session.env[name] = setmetatable({ session = session },
       
    51 							 { __index = t });
       
    52 		end
       
    53 	end
       
    54 
       
    55 	return session;
       
    56 end
       
    57 
       
    58 local function on_message(event)
       
    59 	-- Check the type of the incoming stanza to avoid loops:
       
    60 	if event.stanza.attr.type == "error" then
       
    61 		return; -- We do not want to reply to these, so leave.
       
    62 	end
       
    63 
       
    64 	local userjid = event.stanza.attr.from;
       
    65 	local bodytag = event.stanza:get_child("body");
       
    66 	local body = bodytag and bodytag:get_text() or "";
       
    67 	if not body or body == "" then
       
    68 		-- We do not reply to empty messages (chatstates, etc.)
       
    69 		return true;
       
    70 	end
       
    71 
       
    72 	-- Check the requester is an admin user
       
    73 	if not um_is_admin(userjid, module.host) then
       
    74 		module:log("info", "Ignored request from non-admin: %s",
       
    75 			   userjid);
       
    76 		return;
       
    77 	end
       
    78 
       
    79 	-- Create a session in order to use an admin_telnet-like environment
       
    80 	local session = new_session();
       
    81 
       
    82 	-- Process the message using admin_telnet's onincoming function
       
    83 	admin_telnet.console_incoming_message(session, body.."\n");
       
    84 
       
    85 	-- Strip trailing blank line
       
    86 	session.fulltext = tostring(session.fulltext):gsub("\n\|%s*$", "")
       
    87 
       
    88 	-- Send the reply stanza
       
    89 	local reply_stanza = st.message({ from = host, to = userjid,
       
    90 					type = "chat" });
       
    91 	reply_stanza = reply_stanza:body(session.fulltext);
       
    92 	module:send(reply_stanza);
       
    93 
       
    94 	return true;
       
    95 end
       
    96 
       
    97 local function on_presence(event)
       
    98 
       
    99 	local send_presence = false;
       
   100 
       
   101 	local userjid = event.stanza.attr.from;
       
   102 
       
   103 	-- Check the requester is an admin user
       
   104 	if not um_is_admin(userjid, module.host) then
       
   105 		module:log("info", "Ignored presence from non-admin: %s",
       
   106 			   userjid);
       
   107 		return;
       
   108 	end
       
   109 
       
   110 	if (event.stanza.attr.type == "subscribe") then
       
   111 		module:log("info", "Subscription request from %s", userjid);
       
   112 		send_presence = true;
       
   113 		-- Send a subscription ack
       
   114 		local presence_stanza = st.presence({ from = host,
       
   115 					to = userjid, type = "subscribed",
       
   116 					id = event.stanza.attr.id });
       
   117 		module:send(presence_stanza);
       
   118 	elseif (event.stanza.attr.type == "probe") then
       
   119 		send_presence = true;
       
   120 	elseif (event.stanza.attr.type == "unsubscribe") then
       
   121 		-- For information only...
       
   122 		module:log("info", "Unsubscription request from %s", userjid);
       
   123 	end
       
   124 
       
   125 	if (send_presence == true) then
       
   126 		-- Send a presence stanza
       
   127 		module:send(st.presence({ from = host, to = userjid }));
       
   128 	end
       
   129 	return true;
       
   130 end
       
   131 
       
   132 module:hook("message/bare", on_message);
       
   133 module:hook("presence/bare", on_presence);
       
   134 
       
   135 -- vim:set noet sts=8 sw=8: