Software / code / prosody
Annotate
plugins/mod_console.lua @ 1099:127e6ae089f8
net.connlisteners: Lower log level of multiple listeners warning (not interesting to end-users)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 03 May 2009 01:10:49 +0100 |
| parent | 1085:1ac11fb753ca |
| child | 1240:397b6e9c1568 |
| rev | line source |
|---|---|
| 896 | 1 -- Prosody IM v0.4 |
|
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
|
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
461
diff
changeset
|
4 -- |
| 758 | 5 -- This project is MIT/X11 licensed. Please see the |
| 6 -- COPYING file in the source package for more information. | |
|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
461
diff
changeset
|
7 -- |
|
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
461
diff
changeset
|
8 |
| 736 | 9 module.host = "*"; |
|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
461
diff
changeset
|
10 |
|
1065
3806173670f2
mod_*: Fix many unnecessary global accesses in modules (already committed to main repo)
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
11 local hosts = _G.hosts; |
| 736 | 12 local connlisteners_register = require "net.connlisteners".register; |
| 13 | |
| 14 local console_listener = { default_port = 5582; default_mode = "*l"; }; | |
| 15 | |
| 16 local commands = {}; | |
| 17 local def_env = {}; | |
| 18 local default_env_mt = { __index = def_env }; | |
| 19 | |
| 20 console = {}; | |
| 21 | |
| 22 function console:new_session(conn) | |
| 23 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end; | |
| 24 local session = { conn = conn; | |
| 25 send = function (t) w(tostring(t)); end; | |
| 26 print = function (t) w("| "..tostring(t).."\n"); end; | |
| 27 disconnect = function () conn.close(); end; | |
| 28 }; | |
| 29 session.env = setmetatable({}, default_env_mt); | |
| 30 | |
| 31 -- Load up environment with helper objects | |
| 32 for name, t in pairs(def_env) do | |
| 33 if type(t) == "table" then | |
| 34 session.env[name] = setmetatable({ session = session }, { __index = t }); | |
| 35 end | |
| 36 end | |
| 37 | |
| 38 return session; | |
| 39 end | |
| 40 | |
| 41 local sessions = {}; | |
| 42 | |
| 43 function console_listener.listener(conn, data) | |
| 44 local session = sessions[conn]; | |
| 45 | |
| 46 if not session then | |
| 47 -- Handle new connection | |
| 48 session = console:new_session(conn); | |
| 49 sessions[conn] = session; | |
| 50 printbanner(session); | |
| 51 end | |
| 52 if data then | |
| 53 -- Handle data | |
| 54 (function(session, data) | |
| 55 if data:match("[!.]$") then | |
| 56 local command = data:lower(); | |
| 57 command = data:match("^%w+") or data:match("%p"); | |
| 58 if commands[command] then | |
| 59 commands[command](session, data); | |
| 60 return; | |
| 61 end | |
| 62 end | |
| 63 | |
| 64 session.env._ = data; | |
| 65 | |
| 66 local chunk, err = loadstring("return "..data); | |
| 67 if not chunk then | |
| 68 chunk, err = loadstring(data); | |
| 69 if not chunk then | |
| 70 err = err:gsub("^%[string .-%]:%d+: ", ""); | |
| 71 err = err:gsub("^:%d+: ", ""); | |
| 72 err = err:gsub("'<eof>'", "the end of the line"); | |
| 73 session.print("Sorry, I couldn't understand that... "..err); | |
| 74 return; | |
| 75 end | |
| 76 end | |
| 77 | |
| 78 setfenv(chunk, session.env); | |
| 79 local ranok, taskok, message = pcall(chunk); | |
| 80 | |
| 81 if not ranok then | |
| 82 session.print("Fatal error while running command, it did not complete"); | |
| 83 session.print("Error: "..taskok); | |
| 84 return; | |
| 85 end | |
| 86 | |
| 87 if not message then | |
| 88 session.print("Result: "..tostring(taskok)); | |
| 89 return; | |
| 90 elseif (not taskok) and message then | |
| 91 session.print("Command completed with a problem"); | |
| 92 session.print("Message: "..tostring(message)); | |
| 93 return; | |
| 94 end | |
| 95 | |
|
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 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
|
97 end)(session, data); |
| 736 | 98 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
|
99 session.send(string.char(0)); |
| 736 | 100 end |
| 101 | |
| 102 function console_listener.disconnect(conn, err) | |
| 103 | |
| 104 end | |
| 105 | |
| 106 connlisteners_register('console', console_listener); | |
| 107 | |
| 108 -- Console commands -- | |
| 109 -- These are simple commands, not valid standalone in Lua | |
| 110 | |
| 111 function commands.bye(session) | |
| 112 session.print("See you! :)"); | |
| 113 session.disconnect(); | |
| 114 end | |
| 115 | |
| 116 commands["!"] = function (session, data) | |
| 117 if data:match("^!!") then | |
| 118 session.print("!> "..session.env._); | |
| 119 return console_listener.listener(session.conn, session.env._); | |
| 120 end | |
| 121 local old, new = data:match("^!(.-[^\\])!(.-)!$"); | |
| 122 if old and new then | |
| 123 local ok, res = pcall(string.gsub, session.env._, old, new); | |
| 124 if not ok then | |
| 125 session.print(res) | |
| 126 return; | |
| 127 end | |
| 128 session.print("!> "..res); | |
| 129 return console_listener.listener(session.conn, res); | |
| 130 end | |
| 131 session.print("Sorry, not sure what you want"); | |
| 132 end | |
| 133 | |
| 134 -- Session environment -- | |
| 135 -- Anything in def_env will be accessible within the session as a global variable | |
| 136 | |
| 137 def_env.server = {}; | |
| 138 function def_env.server:reload() | |
| 139 dofile "prosody" | |
| 140 return true, "Server reloaded"; | |
| 141 end | |
| 142 | |
| 143 def_env.module = {}; | |
| 144 function def_env.module:load(name, host, config) | |
| 145 local mm = require "modulemanager"; | |
| 146 local ok, err = mm.load(host or self.env.host, name, config); | |
| 147 if not ok then | |
| 148 return false, err or "Unknown error loading module"; | |
| 149 end | |
| 150 return true, "Module loaded"; | |
| 151 end | |
| 152 | |
|
712
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
153 function def_env.module:unload(name, host) |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
154 local mm = require "modulemanager"; |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
155 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
|
156 if not ok then |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
157 return false, err or "Unknown error unloading module"; |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
158 end |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
159 return true, "Module unloaded"; |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
160 end |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
161 |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
162 function def_env.module:reload(name, host) |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
163 local mm = require "modulemanager"; |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
164 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
|
165 if not ok then |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
166 return false, err or "Unknown error reloading module"; |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
167 end |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
168 return true, "Module reloaded"; |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
169 end |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
170 |
| 736 | 171 def_env.config = {}; |
| 172 function def_env.config:load(filename, format) | |
| 173 local config_load = require "core.configmanager".load; | |
| 174 local ok, err = config_load(filename, format); | |
| 175 if not ok then | |
| 176 return false, err or "Unknown error loading config"; | |
| 177 end | |
| 178 return true, "Config loaded"; | |
| 179 end | |
| 180 | |
| 181 function def_env.config:get(host, section, key) | |
| 182 local config_get = require "core.configmanager".get | |
| 183 return true, tostring(config_get(host, section, key)); | |
| 184 end | |
| 185 | |
| 186 def_env.hosts = {}; | |
| 187 function def_env.hosts:list() | |
| 188 for host, host_session in pairs(hosts) do | |
| 189 self.session.print(host); | |
| 190 end | |
| 191 return true, "Done"; | |
| 192 end | |
| 193 | |
| 194 function def_env.hosts:add(name) | |
| 195 end | |
| 196 | |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
197 def_env.s2s = {}; |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
198 function def_env.s2s:show() |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
199 local _print = self.session.print; |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
200 local print = self.session.print; |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
201 for host, host_session in pairs(hosts) do |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
202 print = function (...) _print(host); _print(...); print = _print; end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
203 for remotehost, session in pairs(host_session.s2sout) do |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
204 print(" "..host.." -> "..remotehost); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
205 if session.sendq then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
206 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
207 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
208 if session.type == "s2sout_unauthed" then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
209 if session.connecting then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
210 print(" Connection not yet established"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
211 if not session.srv_hosts then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
212 if not session.conn then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
213 print(" We do not yet have a DNS answer for this host's SRV records"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
214 else |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
215 print(" This host has no SRV records, using A record instead"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
216 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
217 elseif session.srv_choice then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
218 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
219 local srv_choice = session.srv_hosts[session.srv_choice]; |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
220 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269)); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
221 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
222 elseif session.notopen then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
223 print(" The <stream> has not yet been opened"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
224 elseif not session.dialback_key then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
225 print(" Dialback has not been initiated yet"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
226 elseif session.dialback_key then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
227 print(" Dialback has been requested, but no result received"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
228 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
229 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
230 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
231 |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
232 for session in pairs(incoming_s2s) do |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
233 if session.to_host == host then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
234 print(" "..host.." <- "..(session.from_host or "(unknown)")); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
235 if session.type == "s2sin_unauthed" then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
236 print(" Connection not yet authenticated"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
237 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
238 for name in pairs(session.hosts) do |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
239 if name ~= session.from_host then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
240 print(" also hosts "..tostring(name)); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
241 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
242 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
243 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
244 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
245 print = _print; |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
246 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
247 for session in pairs(incoming_s2s) do |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
248 if not session.to_host then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
249 print("Other incoming s2s connections"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
250 print(" (unknown) <- "..(session.from_host or "(unknown)")); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
251 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
252 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
253 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1065
diff
changeset
|
254 |
| 736 | 255 ------------- |
| 256 | |
| 257 function printbanner(session) | |
| 258 session.print [[ | |
| 259 ____ \ / _ | |
| 260 | _ \ _ __ ___ ___ _-_ __| |_ _ | |
| 261 | |_) | '__/ _ \/ __|/ _ \ / _` | | | | | |
| 262 | __/| | | (_) \__ \ |_| | (_| | |_| | | |
| 263 |_| |_| \___/|___/\___/ \__,_|\__, | | |
| 264 A study in simplicity |___/ | |
| 265 | |
| 266 ]] | |
| 267 session.print("Welcome to the Prosody administration console. For a list of commands, type: help"); | |
| 268 session.print("You may find more help on using this console in our online documentation at "); | |
| 269 session.print("http://prosody.im/doc/console\n"); | |
| 270 end |