plugins/mod_console.lua

Sat, 03 Jan 2009 18:44:39 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Sat, 03 Jan 2009 18:44:39 +0500
changeset 669
9255abbb3068
parent 615
4ae3e81513f3
child 712
56410c0cd846
permissions
-rw-r--r--

mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response

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
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
20
382
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 connlisteners_register = require "net.connlisteners".register;
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 local console_listener = { default_port = 5582; default_mode = "*l"; };
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local commands = {};
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
27 local def_env = {};
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
28 local default_env_mt = { __index = def_env };
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 console = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 function console:new_session(conn)
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
33 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
34 local session = { conn = conn;
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 send = function (t) w(tostring(t)); end;
565
3a49d85cafbc Backed out changeset 099d8a102deb (committed too much)
Matthew Wild <mwild1@gmail.com>
parents: 563
diff changeset
36 print = function (t) w("| "..tostring(t).."\n"); end;
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 disconnect = function () conn.close(); end;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 };
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
39 session.env = setmetatable({}, default_env_mt);
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
40
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
41 -- Load up environment with helper objects
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
42 for name, t in pairs(def_env) do
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
43 if type(t) == "table" then
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
44 session.env[name] = setmetatable({ session = session }, { __index = t });
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
45 end
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
46 end
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
47
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
48 return session;
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local sessions = {};
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 function console_listener.listener(conn, data)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local session = sessions[conn];
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if not session then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 -- Handle new connection
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 session = console:new_session(conn);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 sessions[conn] = session;
440
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
60 printbanner(session);
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if data then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- Handle data
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
64 (function(session, data)
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
65 if data:match("[!.]$") then
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
66 local command = data:lower();
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
67 command = data:match("^%w+") or data:match("%p");
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
68 if commands[command] then
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
69 commands[command](session, data);
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
70 return;
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
71 end
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 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
73
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
74 session.env._ = data;
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
75
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
76 local chunk, err = loadstring("return "..data);
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if not chunk then
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
78 chunk, err = loadstring(data);
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
79 if not chunk then
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
80 err = err:gsub("^%[string .-%]:%d+: ", "");
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
81 err = err:gsub("^:%d+: ", "");
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
82 err = err:gsub("'<eof>'", "the end of the line");
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
83 session.print("Sorry, I couldn't understand that... "..err);
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
84 return;
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
85 end
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
86 end
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
87
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
88 setfenv(chunk, session.env);
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
89 local ranok, taskok, message = pcall(chunk);
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
90
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
91 if not ranok then
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
92 session.print("Fatal error while running command, it did not complete");
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
93 session.print("Error: "..taskok);
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 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
96
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
97 if not message then
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
98 session.print("Result: "..tostring(taskok));
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
99 return;
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
100 elseif (not taskok) and message then
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
101 session.print("Command completed with a problem");
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
102 session.print("Message: "..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
103 return;
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
104 end
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
105
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);
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
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));
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 function console_listener.disconnect(conn, err)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
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 connlisteners_register('console', console_listener);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 -- Console commands --
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 -- These are simple commands, not valid standalone in Lua
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 function commands.bye(session)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 session.print("See you! :)");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 session.disconnect();
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 commands["!"] = function (session, data)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 if data:match("^!!") then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 session.print("!> "..session.env._);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 return console_listener.listener(session.conn, session.env._);
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 local old, new = data:match("^!(.-[^\\])!(.-)!$");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 if old and new then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local ok, res = pcall(string.gsub, session.env._, old, new);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if not ok then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 session.print(res)
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 return;
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 session.print("!> "..res);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 return console_listener.listener(session.conn, res);
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 session.print("Sorry, not sure what you want");
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 -- Session environment --
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
145 -- Anything in def_env will be accessible within the session as a global variable
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
147 def_env.server = {};
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
148 function def_env.server:reload()
461
8e66201f566a Load prosody instead of main.lia in mod_console
Waqas Hussain <waqas20@gmail.com>
parents: 444
diff changeset
149 dofile "prosody"
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 return true, "Server reloaded";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
153 def_env.module = {};
444
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
154 function def_env.module:load(name, host, config)
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 local mm = require "modulemanager";
444
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
156 local ok, err = mm.load(host or self.env.host, name, config);
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 if not ok then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 return false, err or "Unknown error loading module";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 return true, "Module loaded";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
444
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
163 function def_env.module:unload(name, host)
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
164 local mm = require "modulemanager";
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
165 local ok, err = mm.unload(host or self.env.host, name);
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
166 if not ok then
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
167 return false, err or "Unknown error unloading module";
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
168 end
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
169 return true, "Module unloaded";
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
170 end
77485b9b840c Add module:unload() to mod_console, and allow module:load() to take config param
Matthew Wild <mwild1@gmail.com>
parents: 440
diff changeset
171
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
172 def_env.config = {};
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
173 function def_env.config:load(filename, format)
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
174 local config_load = require "core.configmanager".load;
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
175 local ok, err = config_load(filename, format);
382
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 if not ok then
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 return false, err or "Unknown error loading config";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 end
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 return true, "Config loaded";
9daf1e3d278e Add initial mod_console
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 end
411
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
181
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
182 function def_env.config:get(host, section, key)
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
183 local config_get = require "core.configmanager".get
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
184 return true, tostring(config_get(host, section, key));
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
185 end
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
186
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
187 def_env.hosts = {};
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
188 function def_env.hosts:list()
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
189 for host, host_session in pairs(hosts) do
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
190 self.session.print(host);
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
191 end
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
192 return true, "Done";
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
193 end
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
194
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
195 function def_env.hosts:add(name)
64982773cc15 Some mod_console changes
Matthew Wild <mwild1@gmail.com>
parents: 382
diff changeset
196 end
440
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
197
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
198 -------------
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
199
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
200 function printbanner(session)
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
201 session.print [[
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
202 ____ \ / _
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
203 | _ \ _ __ ___ ___ _-_ __| |_ _
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
204 | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
205 | __/| | | (_) \__ \ |_| | (_| | |_| |
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
206 |_| |_| \___/|___/\___/ \__,_|\__, |
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
207 A study in simplicity |___/
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
208
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
209 ]]
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
210 session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
211 session.print("You may find more help on using this console in our online documentation at ");
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
212 session.print("http://prosody.im/doc/console\n");
dee02bf4656a Some mod_console updates
Matthew Wild <mwild1@gmail.com>
parents: 411
diff changeset
213 end

mercurial