Software /
code /
prosody
Comparison
plugins/mod_admin_telnet.lua @ 9735:2d8ca54ecbc6
mod_admin_telnet: Enable async processing using util.async
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 27 Dec 2018 02:53:34 +0100 |
parent | 9734:cc01bb7a3305 |
child | 9736:a5ae3f4e1a40 |
comparison
equal
deleted
inserted
replaced
9734:cc01bb7a3305 | 9735:2d8ca54ecbc6 |
---|---|
29 local set, array = require "util.set", require "util.array"; | 29 local set, array = require "util.set", require "util.array"; |
30 local cert_verify_identity = require "util.x509".verify_identity; | 30 local cert_verify_identity = require "util.x509".verify_identity; |
31 local envload = require "util.envload".envload; | 31 local envload = require "util.envload".envload; |
32 local envloadfile = require "util.envload".envloadfile; | 32 local envloadfile = require "util.envload".envloadfile; |
33 local has_pposix, pposix = pcall(require, "util.pposix"); | 33 local has_pposix, pposix = pcall(require, "util.pposix"); |
34 local async = require "util.async"; | |
34 | 35 |
35 local commands = module:shared("commands") | 36 local commands = module:shared("commands") |
36 local def_env = module:shared("env"); | 37 local def_env = module:shared("env"); |
37 local default_env_mt = { __index = def_env }; | 38 local default_env_mt = { __index = def_env }; |
38 | 39 |
45 end; | 46 end; |
46 return env; | 47 return env; |
47 end | 48 end |
48 | 49 |
49 console = {}; | 50 console = {}; |
51 | |
52 local runner_callbacks = {}; | |
53 | |
54 function runner_callbacks:ready() | |
55 self.data.conn:resume(); | |
56 end | |
57 | |
58 function runner_callbacks:waiting() | |
59 self.data.conn:pause(); | |
60 end | |
61 | |
62 function runner_callbacks:error(err) | |
63 module:log("error", "Traceback[telnet]: %s", err); | |
64 end | |
65 | |
50 | 66 |
51 function console:new_session(conn) | 67 function console:new_session(conn) |
52 local w = function(s) conn:write(s:gsub("\n", "\r\n")); end; | 68 local w = function(s) conn:write(s:gsub("\n", "\r\n")); end; |
53 local session = { conn = conn; | 69 local session = { conn = conn; |
54 send = function (t) w(tostring(t)); end; | 70 send = function (t) w(tostring(t)); end; |
61 end; | 77 end; |
62 disconnect = function () conn:close(); end; | 78 disconnect = function () conn:close(); end; |
63 }; | 79 }; |
64 session.env = setmetatable({}, default_env_mt); | 80 session.env = setmetatable({}, default_env_mt); |
65 | 81 |
82 session.thread = async.runner(function (line) | |
83 console:process_line(session, line); | |
84 session.send(string.char(0)); | |
85 end, runner_callbacks, session); | |
86 | |
66 -- Load up environment with helper objects | 87 -- Load up environment with helper objects |
67 for name, t in pairs(def_env) do | 88 for name, t in pairs(def_env) do |
68 if type(t) == "table" then | 89 if type(t) == "table" then |
69 session.env[name] = setmetatable({ session = session }, { __index = t }); | 90 session.env[name] = setmetatable({ session = session }, { __index = t }); |
70 end | 91 end |
149 data = partial..data; | 170 data = partial..data; |
150 end | 171 end |
151 | 172 |
152 for line in data:gmatch("[^\n]*[\n\004]") do | 173 for line in data:gmatch("[^\n]*[\n\004]") do |
153 if session.closed then return end | 174 if session.closed then return end |
154 console:process_line(session, line); | 175 session.thread:run(line); |
155 session.send(string.char(0)); | |
156 end | 176 end |
157 session.partial_data = data:match("[^\n]+$"); | 177 session.partial_data = data:match("[^\n]+$"); |
158 end | 178 end |
159 | 179 |
160 function console_listener.onreadtimeout(conn) | 180 function console_listener.onreadtimeout(conn) |