Annotate

plugins/mod_console.lua @ 756:2ca5fa47f317

mod_muc: Convert to unix line endings
author Matthew Wild <mwild1@gmail.com>
date Thu, 29 Jan 2009 20:46:34 +0000
parent 736:7cbae2d16fd6
child 758:b1885732e979
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 565
diff changeset
1 -- Prosody IM v0.2
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
19
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
20 module.host = "*";
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
21
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
22 local connlisteners_register = require "net.connlisteners".register;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
23
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
24 local console_listener = { default_port = 5582; default_mode = "*l"; };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
25
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
26 local commands = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
27 local def_env = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
28 local default_env_mt = { __index = def_env };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
29
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
30 console = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
31
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
32 function console:new_session(conn)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
33 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
34 local session = { conn = conn;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
35 send = function (t) w(tostring(t)); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
36 print = function (t) w("| "..tostring(t).."\n"); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
37 disconnect = function () conn.close(); end;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
38 };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
39 session.env = setmetatable({}, default_env_mt);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
40
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
41 -- Load up environment with helper objects
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
42 for name, t in pairs(def_env) do
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
43 if type(t) == "table" then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
44 session.env[name] = setmetatable({ session = session }, { __index = t });
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
45 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
46 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
47
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
48 return session;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
49 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
50
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
51 local sessions = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
52
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
53 function console_listener.listener(conn, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
54 local session = sessions[conn];
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
55
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
56 if not session then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
57 -- Handle new connection
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
58 session = console:new_session(conn);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
59 sessions[conn] = session;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
60 printbanner(session);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
61 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
62 if data then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
63 -- Handle data
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
64 (function(session, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
65 if data:match("[!.]$") then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
66 local command = data:lower();
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
67 command = data:match("^%w+") or data:match("%p");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
68 if commands[command] then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
69 commands[command](session, data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
70 return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
71 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
72 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
73
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
74 session.env._ = data;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
75
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
76 local chunk, err = loadstring("return "..data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
77 if not chunk then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
78 chunk, err = loadstring(data);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
79 if not chunk then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
80 err = err:gsub("^%[string .-%]:%d+: ", "");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
81 err = err:gsub("^:%d+: ", "");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
82 err = err:gsub("'<eof>'", "the end of the line");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
83 session.print("Sorry, I couldn't understand that... "..err);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
84 return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
85 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
86 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
87
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
88 setfenv(chunk, session.env);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
89 local ranok, taskok, message = pcall(chunk);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
90
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
91 if not ranok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
92 session.print("Fatal error while running command, it did not complete");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
93 session.print("Error: "..taskok);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
94 return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
95 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
96
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
97 if not message then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
98 session.print("Result: "..tostring(taskok));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
99 return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
100 elseif (not taskok) and message then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
101 session.print("Command completed with a problem");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
102 session.print("Message: "..tostring(message));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
103 return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
104 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
105
669
9255abbb3068 mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
106 session.print("OK: "..tostring(message));
9255abbb3068 mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
107 end)(session, data);
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
108 end
669
9255abbb3068 mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
109 session.send(string.char(0));
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
110 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
111
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
112 function console_listener.disconnect(conn, err)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
113
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
114 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
115
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
116 connlisteners_register('console', console_listener);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
117
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
118 -- Console commands --
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
119 -- These are simple commands, not valid standalone in Lua
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
120
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
121 function commands.bye(session)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
122 session.print("See you! :)");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
123 session.disconnect();
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
124 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
125
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
126 commands["!"] = function (session, data)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
127 if data:match("^!!") then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
128 session.print("!> "..session.env._);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
129 return console_listener.listener(session.conn, session.env._);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
130 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
131 local old, new = data:match("^!(.-[^\\])!(.-)!$");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
132 if old and new then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
133 local ok, res = pcall(string.gsub, session.env._, old, new);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
134 if not ok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
135 session.print(res)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
136 return;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
137 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
138 session.print("!> "..res);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
139 return console_listener.listener(session.conn, res);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
140 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
141 session.print("Sorry, not sure what you want");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
142 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
143
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
144 -- Session environment --
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
145 -- Anything in def_env will be accessible within the session as a global variable
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
146
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
147 def_env.server = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
148 function def_env.server:reload()
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
149 dofile "prosody"
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
150 return true, "Server reloaded";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
151 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
152
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
153 def_env.module = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
154 function def_env.module:load(name, host, config)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
155 local mm = require "modulemanager";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
156 local ok, err = mm.load(host or self.env.host, name, config);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
157 if not ok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
158 return false, err or "Unknown error loading module";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
159 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
160 return true, "Module loaded";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
161 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
162
712
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
163 function def_env.module:unload(name, host)
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
164 local mm = require "modulemanager";
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
165 local ok, err = mm.unload(host or self.env.host, name);
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
166 if not ok then
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
167 return false, err or "Unknown error unloading module";
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
168 end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
169 return true, "Module unloaded";
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
170 end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
171
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
172 function def_env.module:reload(name, host)
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
173 local mm = require "modulemanager";
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
174 local ok, err = mm.reload(host or self.env.host, name);
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
175 if not ok then
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
176 return false, err or "Unknown error reloading module";
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
177 end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
178 return true, "Module reloaded";
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
179 end
56410c0cd846 mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents: 669
diff changeset
180
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
181 def_env.config = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
182 function def_env.config:load(filename, format)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
183 local config_load = require "core.configmanager".load;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
184 local ok, err = config_load(filename, format);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
185 if not ok then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
186 return false, err or "Unknown error loading config";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
187 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
188 return true, "Config loaded";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
189 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
190
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
191 function def_env.config:get(host, section, key)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
192 local config_get = require "core.configmanager".get
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
193 return true, tostring(config_get(host, section, key));
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
194 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
195
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
196 def_env.hosts = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
197 function def_env.hosts:list()
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
198 for host, host_session in pairs(hosts) do
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
199 self.session.print(host);
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
200 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
201 return true, "Done";
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
202 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
203
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
204 function def_env.hosts:add(name)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
205 end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
206
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
207 -------------
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
208
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
209 function printbanner(session)
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
210 session.print [[
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
211 ____ \ / _
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
212 | _ \ _ __ ___ ___ _-_ __| |_ _
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
213 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
214 | __/| | | (_) \__ \ |_| | (_| | |_| |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
215 |_| |_| \___/|___/\___/ \__,_|\__, |
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
216 A study in simplicity |___/
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
217
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
218 ]]
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
219 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
220 session.print("You may find more help on using this console in our online documentation at ");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
221 session.print("http://prosody.im/doc/console\n");
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
222 end