Comparison

plugins/mod_admin_telnet.lua @ 4971:bfc52b9137c8

mod_admin_telnet: Replace anonymous function with loop (saves a closure)
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Jul 2012 18:00:59 +0100
parent 4913:02dbed57a355
child 4977:7006ccbf22a9
comparison
equal deleted inserted replaced
4970:eeff01224865 4971:bfc52b9137c8
74 end 74 end
75 75
76 function console_listener.onincoming(conn, data) 76 function console_listener.onincoming(conn, data)
77 local session = sessions[conn]; 77 local session = sessions[conn];
78 78
79 -- Handle data 79 -- Handle data (loop allows us to break to add \0 after response)
80 (function(session, data) 80 repeat
81 local useglobalenv; 81 local useglobalenv;
82 82
83 if data:match("^>") then 83 if data:match("^>") then
84 data = data:gsub("^>", ""); 84 data = data:gsub("^>", "");
85 useglobalenv = true; 85 useglobalenv = true;
86 elseif data == "\004" then 86 elseif data == "\004" then
87 commands["bye"](session, data); 87 commands["bye"](session, data);
88 return; 88 break;
89 else 89 else
90 local command = data:lower(); 90 local command = data:lower();
91 command = data:match("^%w+") or data:match("%p"); 91 command = data:match("^%w+") or data:match("%p");
92 if commands[command] then 92 if commands[command] then
93 commands[command](session, data); 93 commands[command](session, data);
94 return; 94 break;
95 end 95 end
96 end 96 end
97 97
98 session.env._ = data; 98 session.env._ = data;
99 99
104 if not chunk then 104 if not chunk then
105 err = err:gsub("^%[string .-%]:%d+: ", ""); 105 err = err:gsub("^%[string .-%]:%d+: ", "");
106 err = err:gsub("^:%d+: ", ""); 106 err = err:gsub("^:%d+: ", "");
107 err = err:gsub("'<eof>'", "the end of the line"); 107 err = err:gsub("'<eof>'", "the end of the line");
108 session.print("Sorry, I couldn't understand that... "..err); 108 session.print("Sorry, I couldn't understand that... "..err);
109 return; 109 break;
110 end 110 end
111 end 111 end
112 112
113 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil); 113 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
114 114
115 local ranok, taskok, message = pcall(chunk); 115 local ranok, taskok, message = pcall(chunk);
116 116
117 if not (ranok or message or useglobalenv) and commands[data:lower()] then 117 if not (ranok or message or useglobalenv) and commands[data:lower()] then
118 commands[data:lower()](session, data); 118 commands[data:lower()](session, data);
119 return; 119 break;
120 end 120 end
121 121
122 if not ranok then 122 if not ranok then
123 session.print("Fatal error while running command, it did not complete"); 123 session.print("Fatal error while running command, it did not complete");
124 session.print("Error: "..taskok); 124 session.print("Error: "..taskok);
125 return; 125 break;
126 end 126 end
127 127
128 if not message then 128 if not message then
129 session.print("Result: "..tostring(taskok)); 129 session.print("Result: "..tostring(taskok));
130 return; 130 break;
131 elseif (not taskok) and message then 131 elseif (not taskok) and message then
132 session.print("Command completed with a problem"); 132 session.print("Command completed with a problem");
133 session.print("Message: "..tostring(message)); 133 session.print("Message: "..tostring(message));
134 return; 134 break;
135 end 135 end
136 136
137 session.print("OK: "..tostring(message)); 137 session.print("OK: "..tostring(message));
138 end)(session, data); 138 until true
139 139
140 session.send(string.char(0)); 140 session.send(string.char(0));
141 end 141 end
142 142
143 function console_listener.ondisconnect(conn, err) 143 function console_listener.ondisconnect(conn, err)