Software /
code /
prosody
Comparison
plugins/mod_console.lua @ 669:9255abbb3068
mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 03 Jan 2009 18:44:39 +0500 |
parent | 615:4ae3e81513f3 |
child | 712:56410c0cd846 |
comparison
equal
deleted
inserted
replaced
668:50072761e02d | 669:9255abbb3068 |
---|---|
28 local default_env_mt = { __index = def_env }; | 28 local default_env_mt = { __index = def_env }; |
29 | 29 |
30 console = {}; | 30 console = {}; |
31 | 31 |
32 function console:new_session(conn) | 32 function console:new_session(conn) |
33 local w = conn.write; | 33 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end; |
34 local session = { conn = conn; | 34 local session = { conn = conn; |
35 send = function (t) w(tostring(t)); end; | 35 send = function (t) w(tostring(t)); end; |
36 print = function (t) w("| "..tostring(t).."\n"); end; | 36 print = function (t) w("| "..tostring(t).."\n"); end; |
37 disconnect = function () conn.close(); end; | 37 disconnect = function () conn.close(); end; |
38 }; | 38 }; |
59 sessions[conn] = session; | 59 sessions[conn] = session; |
60 printbanner(session); | 60 printbanner(session); |
61 end | 61 end |
62 if data then | 62 if data then |
63 -- Handle data | 63 -- Handle data |
64 | 64 (function(session, data) |
65 if data:match("[!.]$") then | 65 if data:match("[!.]$") then |
66 local command = data:lower(); | 66 local command = data:lower(); |
67 command = data:match("^%w+") or data:match("%p"); | 67 command = data:match("^%w+") or data:match("%p"); |
68 if commands[command] then | 68 if commands[command] then |
69 commands[command](session, data); | 69 commands[command](session, data); |
70 return; | |
71 end | |
72 end | |
73 | |
74 session.env._ = data; | |
75 | |
76 local chunk, err = loadstring("return "..data); | |
77 if not chunk then | |
78 chunk, err = loadstring(data); | |
79 if not chunk then | |
80 err = err:gsub("^%[string .-%]:%d+: ", ""); | |
81 err = err:gsub("^:%d+: ", ""); | |
82 err = err:gsub("'<eof>'", "the end of the line"); | |
83 session.print("Sorry, I couldn't understand that... "..err); | |
84 return; | |
85 end | |
86 end | |
87 | |
88 setfenv(chunk, session.env); | |
89 local ranok, taskok, message = pcall(chunk); | |
90 | |
91 if not ranok then | |
92 session.print("Fatal error while running command, it did not complete"); | |
93 session.print("Error: "..taskok); | |
70 return; | 94 return; |
71 end | 95 end |
72 end | 96 |
73 | 97 if not message then |
74 session.env._ = data; | 98 session.print("Result: "..tostring(taskok)); |
75 | |
76 local chunk, err = loadstring("return "..data); | |
77 if not chunk then | |
78 chunk, err = loadstring(data); | |
79 if not chunk then | |
80 err = err:gsub("^%[string .-%]:%d+: ", ""); | |
81 err = err:gsub("^:%d+: ", ""); | |
82 err = err:gsub("'<eof>'", "the end of the line"); | |
83 session.print("Sorry, I couldn't understand that... "..err); | |
84 return; | 99 return; |
85 end | 100 elseif (not taskok) and message then |
86 end | 101 session.print("Command completed with a problem"); |
87 | 102 session.print("Message: "..tostring(message)); |
88 setfenv(chunk, session.env); | 103 return; |
89 local ranok, taskok, message = pcall(chunk); | 104 end |
90 | 105 |
91 if not ranok then | 106 session.print("OK: "..tostring(message)); |
92 session.print("Fatal error while running command, it did not complete"); | 107 end)(session, data); |
93 session.print("Error: "..taskok); | 108 end |
94 return; | 109 session.send(string.char(0)); |
95 end | |
96 | |
97 if not message then | |
98 session.print("Result: "..tostring(taskok)); | |
99 return; | |
100 elseif (not taskok) and message then | |
101 session.print("Command completed with a problem"); | |
102 session.print("Message: "..tostring(message)); | |
103 return; | |
104 end | |
105 | |
106 session.print("OK: "..tostring(message)); | |
107 end | |
108 end | 110 end |
109 | 111 |
110 function console_listener.disconnect(conn, err) | 112 function console_listener.disconnect(conn, err) |
111 | 113 |
112 end | 114 end |