Software / code / prosody
Comparison
plugins/mod_admin_telnet.lua @ 5186:ad898e50b8f3
mod_admin_telnet: Refactor so that command processing is performed in a separate function (usable from other modules)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 22 Nov 2012 18:32:27 +0000 |
| parent | 5168:46fc0eff10b4 |
| child | 5227:97f395938028 |
comparison
equal
deleted
inserted
replaced
| 5185:ca30b21946ef | 5186:ad898e50b8f3 |
|---|---|
| 70 end | 70 end |
| 71 | 71 |
| 72 return session; | 72 return session; |
| 73 end | 73 end |
| 74 | 74 |
| 75 function console:process_line(session, line) | |
| 76 local useglobalenv; | |
| 77 | |
| 78 if line:match("^>") then | |
| 79 line = line:gsub("^>", ""); | |
| 80 useglobalenv = true; | |
| 81 elseif line == "\004" then | |
| 82 commands["bye"](session, line); | |
| 83 return; | |
| 84 else | |
| 85 local command = line:match("^%w+") or line:match("%p"); | |
| 86 if commands[command] then | |
| 87 commands[command](session, line); | |
| 88 return; | |
| 89 end | |
| 90 end | |
| 91 | |
| 92 session.env._ = line; | |
| 93 | |
| 94 local chunkname = "=console"; | |
| 95 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil | |
| 96 local chunk, err = envload("return "..line, chunkname, env); | |
| 97 if not chunk then | |
| 98 chunk, err = envload(line, chunkname, env); | |
| 99 if not chunk then | |
| 100 err = err:gsub("^%[string .-%]:%d+: ", ""); | |
| 101 err = err:gsub("^:%d+: ", ""); | |
| 102 err = err:gsub("'<eof>'", "the end of the line"); | |
| 103 session.print("Sorry, I couldn't understand that... "..err); | |
| 104 return; | |
| 105 end | |
| 106 end | |
| 107 | |
| 108 local ranok, taskok, message = pcall(chunk); | |
| 109 | |
| 110 if not (ranok or message or useglobalenv) and commands[line:lower()] then | |
| 111 commands[line:lower()](session, line); | |
| 112 return; | |
| 113 end | |
| 114 | |
| 115 if not ranok then | |
| 116 session.print("Fatal error while running command, it did not complete"); | |
| 117 session.print("Error: "..taskok); | |
| 118 return; | |
| 119 end | |
| 120 | |
| 121 if not message then | |
| 122 session.print("Result: "..tostring(taskok)); | |
| 123 return; | |
| 124 elseif (not taskok) and message then | |
| 125 session.print("Command completed with a problem"); | |
| 126 session.print("Message: "..tostring(message)); | |
| 127 return; | |
| 128 end | |
| 129 | |
| 130 session.print("OK: "..tostring(message)); | |
| 131 end | |
| 132 | |
| 75 local sessions = {}; | 133 local sessions = {}; |
| 76 | 134 |
| 77 function console_listener.onconnect(conn) | 135 function console_listener.onconnect(conn) |
| 78 -- Handle new connection | 136 -- Handle new connection |
| 79 local session = console:new_session(conn); | 137 local session = console:new_session(conn); |
| 89 if partial then | 147 if partial then |
| 90 data = partial..data; | 148 data = partial..data; |
| 91 end | 149 end |
| 92 | 150 |
| 93 for line in data:gmatch("[^\n]*[\n\004]") do | 151 for line in data:gmatch("[^\n]*[\n\004]") do |
| 94 -- Handle data (loop allows us to break to add \0 after response) | 152 console:process_line(session, line); |
| 95 repeat | |
| 96 local useglobalenv; | |
| 97 | |
| 98 if line:match("^>") then | |
| 99 line = line:gsub("^>", ""); | |
| 100 useglobalenv = true; | |
| 101 elseif line == "\004" then | |
| 102 commands["bye"](session, line); | |
| 103 break; | |
| 104 else | |
| 105 local command = line:match("^%w+") or line:match("%p"); | |
| 106 if commands[command] then | |
| 107 commands[command](session, line); | |
| 108 break; | |
| 109 end | |
| 110 end | |
| 111 | |
| 112 session.env._ = line; | |
| 113 | |
| 114 local chunkname = "=console"; | |
| 115 local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil | |
| 116 local chunk, err = envload("return "..line, chunkname, env); | |
| 117 if not chunk then | |
| 118 chunk, err = envload(line, chunkname, env); | |
| 119 if not chunk then | |
| 120 err = err:gsub("^%[string .-%]:%d+: ", ""); | |
| 121 err = err:gsub("^:%d+: ", ""); | |
| 122 err = err:gsub("'<eof>'", "the end of the line"); | |
| 123 session.print("Sorry, I couldn't understand that... "..err); | |
| 124 break; | |
| 125 end | |
| 126 end | |
| 127 | |
| 128 local ranok, taskok, message = pcall(chunk); | |
| 129 | |
| 130 if not (ranok or message or useglobalenv) and commands[line:lower()] then | |
| 131 commands[line:lower()](session, line); | |
| 132 break; | |
| 133 end | |
| 134 | |
| 135 if not ranok then | |
| 136 session.print("Fatal error while running command, it did not complete"); | |
| 137 session.print("Error: "..taskok); | |
| 138 break; | |
| 139 end | |
| 140 | |
| 141 if not message then | |
| 142 session.print("Result: "..tostring(taskok)); | |
| 143 break; | |
| 144 elseif (not taskok) and message then | |
| 145 session.print("Command completed with a problem"); | |
| 146 session.print("Message: "..tostring(message)); | |
| 147 break; | |
| 148 end | |
| 149 | |
| 150 session.print("OK: "..tostring(message)); | |
| 151 until true | |
| 152 | |
| 153 session.send(string.char(0)); | 153 session.send(string.char(0)); |
| 154 end | 154 end |
| 155 session.partial_data = data:match("[^\n]+$"); | 155 session.partial_data = data:match("[^\n]+$"); |
| 156 end | 156 end |
| 157 | 157 |