Software / code / prosody
Annotate
plugins/mod_console.lua @ 1496:4fa337035f46
mod_console: server:version() and server:uptime() commands
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 08 Jul 2009 04:32:02 +0100 |
| parent | 1491:694a0a00e1a5 |
| child | 1502:0f895c06e03f |
| 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 |
|
1342
947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents:
1341
diff
changeset
|
11 local _G = _G; |
|
947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents:
1341
diff
changeset
|
12 |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
13 local prosody = _G.prosody; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
14 local hosts = prosody.hosts; |
| 736 | 15 local connlisteners_register = require "net.connlisteners".register; |
| 16 | |
| 17 local console_listener = { default_port = 5582; default_mode = "*l"; }; | |
| 18 | |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
19 require "util.iterators"; |
|
1491
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
20 local jid_bare = require "util.jid".bare; |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
21 local set, array = require "util.set", require "util.array"; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
22 |
| 736 | 23 local commands = {}; |
| 24 local def_env = {}; | |
| 25 local default_env_mt = { __index = def_env }; | |
| 26 | |
|
1342
947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents:
1341
diff
changeset
|
27 local function redirect_output(_G, session) |
|
947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents:
1341
diff
changeset
|
28 return setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end, __newindex = function (t, k, v) rawset(_G, k, v); end }); |
|
947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents:
1341
diff
changeset
|
29 end |
|
947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents:
1341
diff
changeset
|
30 |
| 736 | 31 console = {}; |
| 32 | |
| 33 function console:new_session(conn) | |
| 34 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end; | |
| 35 local session = { conn = conn; | |
| 36 send = function (t) w(tostring(t)); end; | |
| 37 print = function (t) w("| "..tostring(t).."\n"); end; | |
| 38 disconnect = function () conn.close(); end; | |
| 39 }; | |
| 40 session.env = setmetatable({}, default_env_mt); | |
| 41 | |
| 42 -- Load up environment with helper objects | |
| 43 for name, t in pairs(def_env) do | |
| 44 if type(t) == "table" then | |
| 45 session.env[name] = setmetatable({ session = session }, { __index = t }); | |
| 46 end | |
| 47 end | |
| 48 | |
| 49 return session; | |
| 50 end | |
| 51 | |
| 52 local sessions = {}; | |
| 53 | |
| 54 function console_listener.listener(conn, data) | |
| 55 local session = sessions[conn]; | |
| 56 | |
| 57 if not session then | |
| 58 -- Handle new connection | |
| 59 session = console:new_session(conn); | |
| 60 sessions[conn] = session; | |
| 61 printbanner(session); | |
| 62 end | |
| 63 if data then | |
| 64 -- Handle data | |
| 65 (function(session, data) | |
|
1317
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
66 local useglobalenv; |
|
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
67 |
| 736 | 68 if data:match("[!.]$") then |
| 69 local command = data:lower(); | |
| 70 command = data:match("^%w+") or data:match("%p"); | |
| 71 if commands[command] then | |
| 72 commands[command](session, data); | |
| 73 return; | |
| 74 end | |
| 75 end | |
|
1317
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
76 |
|
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
77 if data:match("^>") then |
|
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
78 data = data:gsub("^>", ""); |
|
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
79 useglobalenv = true; |
|
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
80 end |
| 736 | 81 |
| 82 session.env._ = data; | |
| 83 | |
| 84 local chunk, err = loadstring("return "..data); | |
| 85 if not chunk then | |
| 86 chunk, err = loadstring(data); | |
| 87 if not chunk then | |
| 88 err = err:gsub("^%[string .-%]:%d+: ", ""); | |
| 89 err = err:gsub("^:%d+: ", ""); | |
| 90 err = err:gsub("'<eof>'", "the end of the line"); | |
| 91 session.print("Sorry, I couldn't understand that... "..err); | |
| 92 return; | |
| 93 end | |
| 94 end | |
| 95 | |
|
1342
947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents:
1341
diff
changeset
|
96 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil); |
|
1317
f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents:
1316
diff
changeset
|
97 |
| 736 | 98 local ranok, taskok, message = pcall(chunk); |
| 99 | |
| 100 if not ranok then | |
| 101 session.print("Fatal error while running command, it did not complete"); | |
| 102 session.print("Error: "..taskok); | |
| 103 return; | |
| 104 end | |
| 105 | |
| 106 if not message then | |
| 107 session.print("Result: "..tostring(taskok)); | |
| 108 return; | |
| 109 elseif (not taskok) and message then | |
| 110 session.print("Command completed with a problem"); | |
| 111 session.print("Message: "..tostring(message)); | |
| 112 return; | |
| 113 end | |
| 114 | |
|
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
|
115 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
|
116 end)(session, data); |
| 736 | 117 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
|
118 session.send(string.char(0)); |
| 736 | 119 end |
| 120 | |
| 121 function console_listener.disconnect(conn, err) | |
| 122 | |
| 123 end | |
| 124 | |
| 125 connlisteners_register('console', console_listener); | |
| 126 | |
| 127 -- Console commands -- | |
| 128 -- These are simple commands, not valid standalone in Lua | |
| 129 | |
| 130 function commands.bye(session) | |
| 131 session.print("See you! :)"); | |
| 132 session.disconnect(); | |
| 133 end | |
| 134 | |
| 135 commands["!"] = function (session, data) | |
| 136 if data:match("^!!") then | |
| 137 session.print("!> "..session.env._); | |
| 138 return console_listener.listener(session.conn, session.env._); | |
| 139 end | |
| 140 local old, new = data:match("^!(.-[^\\])!(.-)!$"); | |
| 141 if old and new then | |
| 142 local ok, res = pcall(string.gsub, session.env._, old, new); | |
| 143 if not ok then | |
| 144 session.print(res) | |
| 145 return; | |
| 146 end | |
| 147 session.print("!> "..res); | |
| 148 return console_listener.listener(session.conn, res); | |
| 149 end | |
| 150 session.print("Sorry, not sure what you want"); | |
| 151 end | |
| 152 | |
| 153 -- Session environment -- | |
| 154 -- Anything in def_env will be accessible within the session as a global variable | |
| 155 | |
| 156 def_env.server = {}; | |
| 157 function def_env.server:reload() | |
|
1316
28ae044f1aaf
mod_console: Some "improvements" to the useless server:reload() command :)
Matthew Wild <mwild1@gmail.com>
parents:
1315
diff
changeset
|
158 prosody.unlock_globals(); |
| 736 | 159 dofile "prosody" |
|
1316
28ae044f1aaf
mod_console: Some "improvements" to the useless server:reload() command :)
Matthew Wild <mwild1@gmail.com>
parents:
1315
diff
changeset
|
160 prosody = _G.prosody; |
| 736 | 161 return true, "Server reloaded"; |
| 162 end | |
| 163 | |
|
1496
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
164 function def_env.server:version() |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
165 return true, tostring(prosody.version or "unknown"); |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
166 end |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
167 |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
168 function def_env.server:uptime() |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
169 local t = os.time()-prosody.start_time; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
170 local seconds = t%60; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
171 t = (t - seconds)/60; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
172 local minutes = t%60; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
173 t = (t - minutes)/60; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
174 local hours = t%24; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
175 t = (t - hours)/24; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
176 local days = t; |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
177 return true, string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)", |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
178 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "", |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
179 minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time)); |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
180 end |
|
4fa337035f46
mod_console: server:version() and server:uptime() commands
Matthew Wild <mwild1@gmail.com>
parents:
1491
diff
changeset
|
181 |
| 736 | 182 def_env.module = {}; |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
183 |
|
1433
e7bd00e70973
mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents:
1342
diff
changeset
|
184 local function get_hosts_set(hosts, module) |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
185 if type(hosts) == "table" then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
186 if hosts[1] then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
187 return set.new(hosts); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
188 elseif hosts._items then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
189 return hosts; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
190 end |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
191 elseif type(hosts) == "string" then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
192 return set.new { hosts }; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
193 elseif hosts == nil then |
|
1433
e7bd00e70973
mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents:
1342
diff
changeset
|
194 local mm = require "modulemanager"; |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
195 return set.new(array.collect(keys(prosody.hosts))) |
|
1433
e7bd00e70973
mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents:
1342
diff
changeset
|
196 / function (host) return prosody.hosts[host].type == "local" or module and mm.is_loaded(host, module); end; |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
197 end |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
198 end |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
199 |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
200 function def_env.module:load(name, hosts, config) |
| 736 | 201 local mm = require "modulemanager"; |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
202 |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
203 hosts = get_hosts_set(hosts); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
204 |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
205 -- Load the module for each host |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
206 local ok, err, count = true, nil, 0; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
207 for host in hosts do |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
208 if (not mm.is_loaded(host, name)) then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
209 ok, err = mm.load(host, name, config); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
210 if not ok then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
211 ok = false; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
212 self.session.print(err or "Unknown error loading module"); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
213 else |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
214 count = count + 1; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
215 self.session.print("Loaded for "..host); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
216 end |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
217 end |
| 736 | 218 end |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
219 |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
220 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); |
| 736 | 221 end |
| 222 | |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
223 function def_env.module:unload(name, hosts) |
|
712
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
224 local mm = require "modulemanager"; |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
225 |
|
1433
e7bd00e70973
mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents:
1342
diff
changeset
|
226 hosts = get_hosts_set(hosts, name); |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
227 |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
228 -- Unload the module for each host |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
229 local ok, err, count = true, nil, 0; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
230 for host in hosts do |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
231 if mm.is_loaded(host, name) then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
232 ok, err = mm.unload(host, name); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
233 if not ok then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
234 ok = false; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
235 self.session.print(err or "Unknown error unloading module"); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
236 else |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
237 count = count + 1; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
238 self.session.print("Unloaded from "..host); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
239 end |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
240 end |
|
712
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
241 end |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
242 return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); |
|
712
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
243 end |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
244 |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
245 function def_env.module:reload(name, hosts) |
|
712
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
246 local mm = require "modulemanager"; |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
247 |
|
1433
e7bd00e70973
mod_console: Reload/unload a module on a component host if it is loaded there
Matthew Wild <mwild1@gmail.com>
parents:
1342
diff
changeset
|
248 hosts = get_hosts_set(hosts, name); |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
249 |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
250 -- Reload the module for each host |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
251 local ok, err, count = true, nil, 0; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
252 for host in hosts do |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
253 if mm.is_loaded(host, name) then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
254 ok, err = mm.reload(host, name); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
255 if not ok then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
256 ok = false; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
257 self.session.print(err or "Unknown error reloading module"); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
258 else |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
259 count = count + 1; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
260 if ok == nil then |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
261 ok = true; |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
262 end |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
263 self.session.print("Reloaded on "..host); |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
264 end |
|
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
265 end |
|
712
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
266 end |
|
1315
bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
267 return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); |
|
712
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
268 end |
|
56410c0cd846
mod_console: Added module:reload
Waqas Hussain <waqas20@gmail.com>
parents:
669
diff
changeset
|
269 |
| 736 | 270 def_env.config = {}; |
| 271 function def_env.config:load(filename, format) | |
| 272 local config_load = require "core.configmanager".load; | |
| 273 local ok, err = config_load(filename, format); | |
| 274 if not ok then | |
| 275 return false, err or "Unknown error loading config"; | |
| 276 end | |
| 277 return true, "Config loaded"; | |
| 278 end | |
| 279 | |
| 280 function def_env.config:get(host, section, key) | |
| 281 local config_get = require "core.configmanager".get | |
| 282 return true, tostring(config_get(host, section, key)); | |
| 283 end | |
| 284 | |
| 285 def_env.hosts = {}; | |
| 286 function def_env.hosts:list() | |
| 287 for host, host_session in pairs(hosts) do | |
| 288 self.session.print(host); | |
| 289 end | |
| 290 return true, "Done"; | |
| 291 end | |
| 292 | |
| 293 function def_env.hosts:add(name) | |
| 294 end | |
| 295 | |
|
1241
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
296 def_env.c2s = {}; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
297 |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
298 local function show_c2s(callback) |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
299 for hostname, host in pairs(hosts) do |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
300 for username, user in pairs(host.sessions or {}) do |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
301 for resource, session in pairs(user.sessions or {}) do |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
302 local jid = username.."@"..hostname.."/"..resource; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
303 callback(jid, session); |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
304 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
305 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
306 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
307 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
308 |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
309 function def_env.c2s:show(match_jid) |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
310 local print, count = self.session.print, 0; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
311 show_c2s(function (jid) |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
312 if (not match_jid) or jid:match(match_jid) then |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
313 count = count + 1; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
314 print(jid); |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
315 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
316 end); |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
317 return true, "Total: "..count.." clients"; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
318 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
319 |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
320 function def_env.c2s:show_insecure(match_jid) |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
321 local print, count = self.session.print, 0; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
322 show_c2s(function (jid, session) |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
323 if ((not match_jid) or jid:match(match_jid)) and not session.secure then |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
324 count = count + 1; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
325 print(jid); |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
326 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
327 end); |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
328 return true, "Total: "..count.." insecure client connections"; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
329 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
330 |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
331 function def_env.c2s:show_secure(match_jid) |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
332 local print, count = self.session.print, 0; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
333 show_c2s(function (jid, session) |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
334 if ((not match_jid) or jid:match(match_jid)) and session.secure then |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
335 count = count + 1; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
336 print(jid); |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
337 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
338 end); |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
339 return true, "Total: "..count.." secure client connections"; |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
340 end |
|
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
341 |
|
1491
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
342 function def_env.c2s:close(match_jid) |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
343 local print, count = self.session.print, 0; |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
344 show_c2s(function (jid, session) |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
345 if jid == match_jid or jid_bare(jid) == match_jid then |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
346 count = count + 1; |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
347 session:close(); |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
348 end |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
349 end); |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
350 return true, "Total: "..count.." sessions closed"; |
|
694a0a00e1a5
mod_console: Add c2s:close() command
Matthew Wild <mwild1@gmail.com>
parents:
1483
diff
changeset
|
351 end |
|
1241
9c53fb182044
mod_console: c2s:show(), c2s:show_secure(), c2s:show_insecure()
Matthew Wild <mwild1@gmail.com>
parents:
1240
diff
changeset
|
352 |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
353 def_env.s2s = {}; |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
354 function def_env.s2s:show(match_jid) |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
355 local _print = self.session.print; |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
356 local print = self.session.print; |
|
1322
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
357 |
|
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
358 local count_in, count_out = 0,0; |
|
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
359 |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
360 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:
1042
diff
changeset
|
361 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:
1042
diff
changeset
|
362 for remotehost, session in pairs(host_session.s2sout) do |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
363 if (not match_jid) or remotehost:match(match_jid) or host:match(match_jid) then |
|
1322
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
364 count_out = count_out + 1; |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
365 print(" "..host.." -> "..remotehost); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
366 if session.sendq then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
367 print(" There are "..#session.sendq.." queued outgoing stanzas for this connection"); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
368 end |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
369 if session.type == "s2sout_unauthed" then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
370 if session.connecting then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
371 print(" Connection not yet established"); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
372 if not session.srv_hosts then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
373 if not session.conn then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
374 print(" We do not yet have a DNS answer for this host's SRV records"); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
375 else |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
376 print(" This host has no SRV records, using A record instead"); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
377 end |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
378 elseif session.srv_choice then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
379 print(" We are on SRV record "..session.srv_choice.." of "..#session.srv_hosts); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
380 local srv_choice = session.srv_hosts[session.srv_choice]; |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
381 print(" Using "..(srv_choice.target or ".")..":"..(srv_choice.port or 5269)); |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
382 end |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
383 elseif session.notopen then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
384 print(" The <stream> has not yet been opened"); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
385 elseif not session.dialback_key then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
386 print(" Dialback has not been initiated yet"); |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
387 elseif session.dialback_key then |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
388 print(" Dialback has been requested, but no result received"); |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
389 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
390 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
391 end |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
392 end |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
393 |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
394 for session in pairs(incoming_s2s) do |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
395 if session.to_host == host and ((not match_jid) or host:match(match_jid) |
|
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
396 or (session.from_host and session.from_host:match(match_jid))) then |
|
1322
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
397 count_in = count_in + 1; |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
398 print(" "..host.." <- "..(session.from_host or "(unknown)")); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
399 if session.type == "s2sin_unauthed" then |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
400 print(" Connection not yet authenticated"); |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
401 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
402 for name in pairs(session.hosts) do |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
403 if name ~= session.from_host then |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
404 print(" also hosts "..tostring(name)); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
405 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
406 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
407 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
408 end |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
409 |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
410 print = _print; |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
411 end |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
412 |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
413 for session in pairs(incoming_s2s) do |
|
1240
397b6e9c1568
mod_console: Allow restricting results to matching JIDs in s2s:show()
Matthew Wild <mwild1@gmail.com>
parents:
1085
diff
changeset
|
414 if not session.to_host and ((not match_jid) or session.from_host and session.from_host:match(match_jid)) then |
|
1322
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
415 count_in = count_in + 1; |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
416 print("Other incoming s2s connections"); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
417 print(" (unknown) <- "..(session.from_host or "(unknown)")); |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
418 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
419 end |
|
1322
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
420 |
|
33d103b0283f
mod_console: Show total incoming/outgoing s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1317
diff
changeset
|
421 return true, "Total: "..count_out.." outgoing, "..count_in.." incoming connections"; |
|
1085
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
422 end |
|
1ac11fb753ca
mod_console: Add s2s:show() command to list s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1042
diff
changeset
|
423 |
|
1340
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
424 function def_env.s2s:close(from, to) |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
425 local print, count = self.session.print, 0; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
426 |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
427 if not (from and to) then |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
428 return false, "Syntax: s2s:close('from', 'to') - Closes all s2s sessions from 'from' to 'to'"; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
429 elseif from == to then |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
430 return false, "Both from and to are the same... you can't do that :)"; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
431 end |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
432 |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
433 if hosts[from] and not hosts[to] then |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
434 -- Is an outgoing connection |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
435 local session = hosts[from].s2sout[to]; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
436 if not session then |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
437 print("No outgoing connection from "..from.." to "..to) |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
438 else |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
439 s2smanager.destroy_session(session); |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
440 count = count + 1; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
441 print("Closed outgoing session from "..from.." to "..to); |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
442 end |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
443 elseif hosts[to] and not hosts[from] then |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
444 -- Is an incoming connection |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
445 for session in pairs(incoming_s2s) do |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
446 if session.to_host == to and session.from_host == from then |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
447 s2smanager.destroy_session(session); |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
448 count = count + 1; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
449 end |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
450 end |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
451 |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
452 if count == 0 then |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
453 print("No incoming connections from "..from.." to "..to); |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
454 else |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
455 print("Closed "..count.." incoming session"..((count == 1 and "") or "s").." from "..from.." to "..to); |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
456 end |
|
1341
53decd1ee351
mod_console: Fix syntax error
Matthew Wild <mwild1@gmail.com>
parents:
1340
diff
changeset
|
457 elseif hosts[to] and hosts[from] then |
|
1340
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
458 return false, "Both of the hostnames you specified are local, there are no s2s sessions to close"; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
459 else |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
460 return false, "Neither of the hostnames you specified are being used on this server"; |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
461 end |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
462 |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
463 return true, "Closed "..count.." s2s session"..((count == 1 and "") or "s"); |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
464 end |
|
f707d0957155
mod_console: Add s2s:close() to close s2s sessions between two hosts
Matthew Wild <mwild1@gmail.com>
parents:
1322
diff
changeset
|
465 |
| 736 | 466 ------------- |
| 467 | |
| 468 function printbanner(session) | |
|
1483
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
469 local option = config.get("*", "core", "console_banner"); |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
470 if option == nil or option == "full" or option == "graphic" then |
| 736 | 471 session.print [[ |
| 472 ____ \ / _ | |
| 473 | _ \ _ __ ___ ___ _-_ __| |_ _ | |
| 474 | |_) | '__/ _ \/ __|/ _ \ / _` | | | | | |
| 475 | __/| | | (_) \__ \ |_| | (_| | |_| | | |
| 476 |_| |_| \___/|___/\___/ \__,_|\__, | | |
| 477 A study in simplicity |___/ | |
| 478 | |
| 479 ]] | |
|
1483
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
480 end |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
481 if option == nil or option == "short" or option == "full" then |
| 736 | 482 session.print("Welcome to the Prosody administration console. For a list of commands, type: help"); |
| 483 session.print("You may find more help on using this console in our online documentation at "); | |
| 484 session.print("http://prosody.im/doc/console\n"); | |
| 485 end | |
|
1483
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
486 if option and option ~= "short" and option ~= "full" and option ~= "graphic" then |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
487 if type(option) == "string" then |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
488 session.print(option) |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
489 elseif type(option) == "function" then |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
490 setfenv(option, redirect_output(_G, session)); |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
491 pcall(option, session); |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
492 end |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
493 end |
|
efd19cdda6ca
mod_console: Allow customisation/suppression of the banner
Matthew Wild <mwild1@gmail.com>
parents:
1433
diff
changeset
|
494 end |