Software /
code /
prosody
Annotate
plugins/mod_console.lua @ 651:99f365b758f1
Added tag 0.20, -m, Fix incorrect version number as tag for changeset 90da4c9b34b5
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 27 Dec 2008 15:58:35 +0000 |
parent | 615:4ae3e81513f3 |
child | 669:9255abbb3068 |
rev | line source |
---|---|
615 | 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 | 21 |
22 local connlisteners_register = require "net.connlisteners".register; | |
23 | |
24 local console_listener = { default_port = 5582; default_mode = "*l"; }; | |
25 | |
26 local commands = {}; | |
411 | 27 local def_env = {}; |
28 local default_env_mt = { __index = def_env }; | |
382 | 29 |
30 console = {}; | |
31 | |
32 function console:new_session(conn) | |
33 local w = conn.write; | |
411 | 34 local session = { conn = conn; |
382 | 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 | 37 disconnect = function () conn.close(); end; |
38 }; | |
411 | 39 session.env = setmetatable({}, default_env_mt); |
40 | |
41 -- Load up environment with helper objects | |
42 for name, t in pairs(def_env) do | |
43 if type(t) == "table" then | |
44 session.env[name] = setmetatable({ session = session }, { __index = t }); | |
45 end | |
46 end | |
47 | |
48 return session; | |
382 | 49 end |
50 | |
51 local sessions = {}; | |
52 | |
53 function console_listener.listener(conn, data) | |
54 local session = sessions[conn]; | |
55 | |
56 if not session then | |
57 -- Handle new connection | |
58 session = console:new_session(conn); | |
59 sessions[conn] = session; | |
440 | 60 printbanner(session); |
382 | 61 end |
62 if data then | |
63 -- Handle data | |
64 | |
65 if data:match("[!.]$") then | |
66 local command = data:lower(); | |
67 command = data:match("^%w+") or data:match("%p"); | |
68 if commands[command] then | |
69 commands[command](session, data); | |
70 return; | |
71 end | |
72 end | |
73 | |
74 session.env._ = data; | |
75 | |
76 local chunk, err = loadstring("return "..data); | |
77 if not chunk then | |
78 chunk, err = loadstring(data); | |
79 if not chunk then | |
80 err = err:gsub("^%[string .-%]:%d+: ", ""); | |
81 err = err:gsub("^:%d+: ", ""); | |
82 err = err:gsub("'<eof>'", "the end of the line"); | |
83 session.print("Sorry, I couldn't understand that... "..err); | |
84 return; | |
85 end | |
86 end | |
87 | |
88 setfenv(chunk, session.env); | |
89 local ranok, taskok, message = pcall(chunk); | |
90 | |
91 if not ranok then | |
92 session.print("Fatal error while running command, it did not complete"); | |
93 session.print("Error: "..taskok); | |
94 return; | |
95 end | |
96 | |
97 if not message then | |
98 session.print("Result: "..tostring(taskok)); | |
99 return; | |
100 elseif (not taskok) and message then | |
101 session.print("Command completed with a problem"); | |
102 session.print("Message: "..tostring(message)); | |
103 return; | |
104 end | |
105 | |
106 session.print("OK: "..tostring(message)); | |
107 end | |
108 end | |
109 | |
110 function console_listener.disconnect(conn, err) | |
111 | |
112 end | |
113 | |
114 connlisteners_register('console', console_listener); | |
115 | |
116 -- Console commands -- | |
117 -- These are simple commands, not valid standalone in Lua | |
118 | |
119 function commands.bye(session) | |
120 session.print("See you! :)"); | |
121 session.disconnect(); | |
122 end | |
123 | |
124 commands["!"] = function (session, data) | |
125 if data:match("^!!") then | |
126 session.print("!> "..session.env._); | |
127 return console_listener.listener(session.conn, session.env._); | |
128 end | |
129 local old, new = data:match("^!(.-[^\\])!(.-)!$"); | |
130 if old and new then | |
131 local ok, res = pcall(string.gsub, session.env._, old, new); | |
132 if not ok then | |
133 session.print(res) | |
134 return; | |
135 end | |
136 session.print("!> "..res); | |
137 return console_listener.listener(session.conn, res); | |
138 end | |
139 session.print("Sorry, not sure what you want"); | |
140 end | |
141 | |
142 -- Session environment -- | |
411 | 143 -- Anything in def_env will be accessible within the session as a global variable |
382 | 144 |
411 | 145 def_env.server = {}; |
146 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
|
147 dofile "prosody" |
382 | 148 return true, "Server reloaded"; |
149 end | |
150 | |
411 | 151 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
|
152 function def_env.module:load(name, host, config) |
382 | 153 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
|
154 local ok, err = mm.load(host or self.env.host, name, config); |
382 | 155 if not ok then |
156 return false, err or "Unknown error loading module"; | |
157 end | |
158 return true, "Module loaded"; | |
159 end | |
160 | |
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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
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 |
411 | 170 def_env.config = {}; |
171 function def_env.config:load(filename, format) | |
172 local config_load = require "core.configmanager".load; | |
173 local ok, err = config_load(filename, format); | |
382 | 174 if not ok then |
175 return false, err or "Unknown error loading config"; | |
176 end | |
177 return true, "Config loaded"; | |
178 end | |
411 | 179 |
180 function def_env.config:get(host, section, key) | |
181 local config_get = require "core.configmanager".get | |
182 return true, tostring(config_get(host, section, key)); | |
183 end | |
184 | |
185 def_env.hosts = {}; | |
186 function def_env.hosts:list() | |
187 for host, host_session in pairs(hosts) do | |
188 self.session.print(host); | |
189 end | |
190 return true, "Done"; | |
191 end | |
192 | |
193 function def_env.hosts:add(name) | |
194 end | |
440 | 195 |
196 ------------- | |
197 | |
198 function printbanner(session) | |
199 session.print [[ | |
200 ____ \ / _ | |
201 | _ \ _ __ ___ ___ _-_ __| |_ _ | |
202 | |_) | '__/ _ \/ __|/ _ \ / _` | | | | | |
203 | __/| | | (_) \__ \ |_| | (_| | |_| | | |
204 |_| |_| \___/|___/\___/ \__,_|\__, | | |
205 A study in simplicity |___/ | |
206 | |
207 ]] | |
208 session.print("Welcome to the Prosody administration console. For a list of commands, type: help"); | |
209 session.print("You may find more help on using this console in our online documentation at "); | |
210 session.print("http://prosody.im/doc/console\n"); | |
211 end |