Software /
code /
prosody
Comparison
plugins/mod_console.lua @ 3009:06f7d8054065
mod_console: Make use of the new onconnect callback to initialise session and send banner
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 05 May 2010 17:21:50 +0100 |
parent | 2925:692b3c6c5bd2 |
child | 3025:f1e1b7629807 |
child | 3043:1fadbb2e3ca0 |
comparison
equal
deleted
inserted
replaced
3008:23915803adbf | 3009:06f7d8054065 |
---|---|
51 return session; | 51 return session; |
52 end | 52 end |
53 | 53 |
54 local sessions = {}; | 54 local sessions = {}; |
55 | 55 |
56 function console_listener.onconnect(conn) | |
57 -- Handle new connection | |
58 local session = console:new_session(conn); | |
59 sessions[conn] = session; | |
60 printbanner(session); | |
61 end | |
62 | |
56 function console_listener.onincoming(conn, data) | 63 function console_listener.onincoming(conn, data) |
57 local session = sessions[conn]; | 64 local session = sessions[conn]; |
58 | 65 |
59 if not session then | 66 -- Handle data |
60 -- Handle new connection | 67 (function(session, data) |
61 session = console:new_session(conn); | 68 local useglobalenv; |
62 sessions[conn] = session; | 69 |
63 printbanner(session); | 70 if data:match("^>") then |
64 end | 71 data = data:gsub("^>", ""); |
65 if data then | 72 useglobalenv = true; |
66 -- Handle data | 73 elseif data == "\004" then |
67 (function(session, data) | 74 commands["bye"](session, data); |
68 local useglobalenv; | 75 return; |
69 | 76 else |
70 if data:match("^>") then | 77 local command = data:lower(); |
71 data = data:gsub("^>", ""); | 78 command = data:match("^%w+") or data:match("%p"); |
72 useglobalenv = true; | 79 if commands[command] then |
73 elseif data == "\004" then | 80 commands[command](session, data); |
74 commands["bye"](session, data); | |
75 return; | 81 return; |
76 else | 82 end |
77 local command = data:lower(); | 83 end |
78 command = data:match("^%w+") or data:match("%p"); | 84 |
79 if commands[command] then | 85 session.env._ = data; |
80 commands[command](session, data); | 86 |
81 return; | 87 local chunk, err = loadstring("return "..data); |
82 end | 88 if not chunk then |
83 end | 89 chunk, err = loadstring(data); |
84 | |
85 session.env._ = data; | |
86 | |
87 local chunk, err = loadstring("return "..data); | |
88 if not chunk then | 90 if not chunk then |
89 chunk, err = loadstring(data); | 91 err = err:gsub("^%[string .-%]:%d+: ", ""); |
90 if not chunk then | 92 err = err:gsub("^:%d+: ", ""); |
91 err = err:gsub("^%[string .-%]:%d+: ", ""); | 93 err = err:gsub("'<eof>'", "the end of the line"); |
92 err = err:gsub("^:%d+: ", ""); | 94 session.print("Sorry, I couldn't understand that... "..err); |
93 err = err:gsub("'<eof>'", "the end of the line"); | |
94 session.print("Sorry, I couldn't understand that... "..err); | |
95 return; | |
96 end | |
97 end | |
98 | |
99 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil); | |
100 | |
101 local ranok, taskok, message = pcall(chunk); | |
102 | |
103 if not (ranok or message or useglobalenv) and commands[data:lower()] then | |
104 commands[data:lower()](session, data); | |
105 return; | 95 return; |
106 end | 96 end |
107 | 97 end |
108 if not ranok then | 98 |
109 session.print("Fatal error while running command, it did not complete"); | 99 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil); |
110 session.print("Error: "..taskok); | 100 |
111 return; | 101 local ranok, taskok, message = pcall(chunk); |
112 end | 102 |
113 | 103 if not (ranok or message or useglobalenv) and commands[data:lower()] then |
114 if not message then | 104 commands[data:lower()](session, data); |
115 session.print("Result: "..tostring(taskok)); | 105 return; |
116 return; | 106 end |
117 elseif (not taskok) and message then | 107 |
118 session.print("Command completed with a problem"); | 108 if not ranok then |
119 session.print("Message: "..tostring(message)); | 109 session.print("Fatal error while running command, it did not complete"); |
120 return; | 110 session.print("Error: "..taskok); |
121 end | 111 return; |
122 | 112 end |
123 session.print("OK: "..tostring(message)); | 113 |
124 end)(session, data); | 114 if not message then |
125 end | 115 session.print("Result: "..tostring(taskok)); |
116 return; | |
117 elseif (not taskok) and message then | |
118 session.print("Command completed with a problem"); | |
119 session.print("Message: "..tostring(message)); | |
120 return; | |
121 end | |
122 | |
123 session.print("OK: "..tostring(message)); | |
124 end)(session, data); | |
125 | |
126 session.send(string.char(0)); | 126 session.send(string.char(0)); |
127 end | 127 end |
128 | 128 |
129 function console_listener.ondisconnect(conn, err) | 129 function console_listener.ondisconnect(conn, err) |
130 local session = sessions[conn]; | 130 local session = sessions[conn]; |