Annotate

plugins/mod_console.lua @ 382:9daf1e3d278e

Add initial mod_console
author Matthew Wild <mwild1@gmail.com>
date Sun, 23 Nov 2008 04:54:44 +0000
child 411:64982773cc15
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local connlisteners_register = require "net.connlisteners".register;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local console_listener = { default_port = 5582; default_mode = "*l"; };
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local commands = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local default_env = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local default_env_mt = { __index = default_env };
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 console = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function console:new_session(conn)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local w = conn.write;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 return { conn = conn;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 send = function (t) w(tostring(t)); end;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 print = function (t) w("| "..tostring(t).."\n"); end;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 disconnect = function () conn.close(); end;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 env = setmetatable({}, default_env_mt);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 };
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local sessions = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 function console_listener.listener(conn, data)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local session = sessions[conn];
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if not session then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 -- Handle new connection
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 session = console:new_session(conn);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 sessions[conn] = session;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 session.print("Welcome to the lxmppd admin console!");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if data then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 -- Handle data
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if data:match("[!.]$") then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local command = data:lower();
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 command = data:match("^%w+") or data:match("%p");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if commands[command] then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 commands[command](session, data);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 session.env._ = data;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local chunk, err = loadstring("return "..data);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 if not chunk then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 chunk, err = loadstring(data);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if not chunk then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 err = err:gsub("^%[string .-%]:%d+: ", "");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 err = err:gsub("^:%d+: ", "");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 err = err:gsub("'<eof>'", "the end of the line");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 session.print("Sorry, I couldn't understand that... "..err);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 setfenv(chunk, session.env);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local ranok, taskok, message = pcall(chunk);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if not ranok then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 session.print("Fatal error while running command, it did not complete");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 session.print("Error: "..taskok);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 if not message then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 session.print("Result: "..tostring(taskok));
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 elseif (not taskok) and message then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 session.print("Command completed with a problem");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 session.print("Message: "..tostring(message));
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 session.print("OK: "..tostring(message));
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 function console_listener.disconnect(conn, err)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 connlisteners_register('console', console_listener);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 -- Console commands --
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 -- These are simple commands, not valid standalone in Lua
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 function commands.bye(session)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 session.print("See you! :)");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 session.disconnect();
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 commands["!"] = function (session, data)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if data:match("^!!") then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 session.print("!> "..session.env._);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return console_listener.listener(session.conn, session.env._);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local old, new = data:match("^!(.-[^\\])!(.-)!$");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 if old and new then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local ok, res = pcall(string.gsub, session.env._, old, new);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 if not ok then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 session.print(res)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 session.print("!> "..res);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 return console_listener.listener(session.conn, res);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 session.print("Sorry, not sure what you want");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 -- Session environment --
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 -- Anything in default_env will be accessible within the session as a global variable
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 default_env.server = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 function default_env.server.reload()
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 dofile "main.lua"
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 return true, "Server reloaded";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 default_env.module = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 function default_env.module.load(name)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local mm = require "modulemanager";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 local ok, err = mm.load(name);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 if not ok then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return false, err or "Unknown error loading module";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 return true, "Module loaded";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 default_env.config = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 function default_env.config.load(filename, format)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 local cfgm_load = require "core.configmanager".load;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local ok, err = cfgm_load(filename, format);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 if not ok then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 return false, err or "Unknown error loading config";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 return true, "Config loaded";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end