2 local connlisteners_register = require "net.connlisteners".register; |
2 local connlisteners_register = require "net.connlisteners".register; |
3 |
3 |
4 local console_listener = { default_port = 5582; default_mode = "*l"; }; |
4 local console_listener = { default_port = 5582; default_mode = "*l"; }; |
5 |
5 |
6 local commands = {}; |
6 local commands = {}; |
7 local default_env = {}; |
7 local def_env = {}; |
8 local default_env_mt = { __index = default_env }; |
8 local default_env_mt = { __index = def_env }; |
9 |
9 |
10 console = {}; |
10 console = {}; |
11 |
11 |
12 function console:new_session(conn) |
12 function console:new_session(conn) |
13 local w = conn.write; |
13 local w = conn.write; |
14 return { conn = conn; |
14 local session = { conn = conn; |
15 send = function (t) w(tostring(t)); end; |
15 send = function (t) w(tostring(t)); end; |
16 print = function (t) w("| "..tostring(t).."\n"); end; |
16 print = function (t) w("| "..tostring(t).."\n"); end; |
17 disconnect = function () conn.close(); end; |
17 disconnect = function () conn.close(); end; |
18 env = setmetatable({}, default_env_mt); |
|
19 }; |
18 }; |
|
19 session.env = setmetatable({}, default_env_mt); |
|
20 |
|
21 -- Load up environment with helper objects |
|
22 for name, t in pairs(def_env) do |
|
23 if type(t) == "table" then |
|
24 session.env[name] = setmetatable({ session = session }, { __index = t }); |
|
25 end |
|
26 end |
|
27 |
|
28 return session; |
20 end |
29 end |
21 |
30 |
22 local sessions = {}; |
31 local sessions = {}; |
23 |
32 |
24 function console_listener.listener(conn, data) |
33 function console_listener.listener(conn, data) |
109 end |
118 end |
110 session.print("Sorry, not sure what you want"); |
119 session.print("Sorry, not sure what you want"); |
111 end |
120 end |
112 |
121 |
113 -- Session environment -- |
122 -- Session environment -- |
114 -- Anything in default_env will be accessible within the session as a global variable |
123 -- Anything in def_env will be accessible within the session as a global variable |
115 |
124 |
116 default_env.server = {}; |
125 def_env.server = {}; |
117 function default_env.server.reload() |
126 function def_env.server:reload() |
118 dofile "main.lua" |
127 dofile "main.lua" |
119 return true, "Server reloaded"; |
128 return true, "Server reloaded"; |
120 end |
129 end |
121 |
130 |
122 default_env.module = {}; |
131 def_env.module = {}; |
123 function default_env.module.load(name) |
132 function def_env.module:load(name) |
124 local mm = require "modulemanager"; |
133 local mm = require "modulemanager"; |
125 local ok, err = mm.load(name); |
134 local ok, err = mm.load(name); |
126 if not ok then |
135 if not ok then |
127 return false, err or "Unknown error loading module"; |
136 return false, err or "Unknown error loading module"; |
128 end |
137 end |
129 return true, "Module loaded"; |
138 return true, "Module loaded"; |
130 end |
139 end |
131 |
140 |
132 default_env.config = {}; |
141 def_env.config = {}; |
133 function default_env.config.load(filename, format) |
142 function def_env.config:load(filename, format) |
134 local cfgm_load = require "core.configmanager".load; |
143 local config_load = require "core.configmanager".load; |
135 local ok, err = cfgm_load(filename, format); |
144 local ok, err = config_load(filename, format); |
136 if not ok then |
145 if not ok then |
137 return false, err or "Unknown error loading config"; |
146 return false, err or "Unknown error loading config"; |
138 end |
147 end |
139 return true, "Config loaded"; |
148 return true, "Config loaded"; |
140 end |
149 end |
|
150 |
|
151 function def_env.config:get(host, section, key) |
|
152 local config_get = require "core.configmanager".get |
|
153 return true, tostring(config_get(host, section, key)); |
|
154 end |
|
155 |
|
156 def_env.hosts = {}; |
|
157 function def_env.hosts:list() |
|
158 for host, host_session in pairs(hosts) do |
|
159 self.session.print(host); |
|
160 end |
|
161 return true, "Done"; |
|
162 end |
|
163 |
|
164 function def_env.hosts:add(name) |
|
165 end |