Merge 0.9->0.10
authorKim Alvefur <zash@zash.se>
Sun, 5 Oct 2014 13:37:21 +0000 (15:37 +0200)
committerKim Alvefur <zash@zash.se>
Sun, 5 Oct 2014 13:37:21 +0000 (15:37 +0200)
1  2 
net/dns.lua
plugins/mod_admin_adhoc.lua

diff --combined net/dns.lua
index 42bbdb013b80648f4dd0ec64128b4de9045bfcdf,dc2da1b608f3a29c33b1f22b0eeb175223bbd24e..2b03caf605af685408cd909d95592e7be6ee6d28
@@@ -14,7 -14,6 +14,7 @@@
  
  local socket = require "socket";
  local timer = require "util.timer";
 +local new_ip = require "util.ip".new_ip;
  
  local _, windows = pcall(require, "util.windows");
  local is_windows = (_ and windows) or os.getenv("WINDIR");
@@@ -135,17 -134,19 +135,19 @@@ en
  
  local function prune(rrs, time, soft)    -- - - - - - - - - - - - - - -  prune
        time = time or socket.gettime();
-       for i,rr in pairs(rrs) do
+       for i,rr in ipairs(rrs) do
                if rr.tod then
                        -- rr.tod = rr.tod - 50    -- accelerated decripitude
                        rr.ttl = math.floor(rr.tod - time);
                        if rr.ttl <= 0 then
+                               rrs[rr[rr.type:lower()]] = nil;
                                table.remove(rrs, i);
                                return prune(rrs, time, soft); -- Re-iterate
                        end
                elseif soft == 'soft' then    -- What is this?  I forget!
                        assert(rr.ttl == 0);
-                       rrs[i] = nil;
+                       rrs[rr[rr.type:lower()]] = nil;
+                       table.remove(rrs, i);
                end
        end
  end
@@@ -188,7 -189,7 +190,7 @@@ en
  local rrs_metatable = {};    -- - - - - - - - - - - - - - - - - -  rrs_metatable
  function rrs_metatable.__tostring(rrs)
        local t = {};
-       for i,rr in pairs(rrs) do
+       for i,rr in ipairs(rrs) do
                append(t, tostring(rr)..'\n');
        end
        return table.concat(t);
@@@ -598,12 -599,11 +600,12 @@@ function resolver:adddefaultnameservers
                if resolv_conf then
                        for line in resolv_conf:lines() do
                                line = line:gsub("#.*$", "")
 -                                      :match('^%s*nameserver%s+(.*)%s*$');
 +                                      :match('^%s*nameserver%s+([%x:%.]*)%s*$');
                                if line then
 -                                      line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
 -                                              self:addnameserver(address)
 -                                      end);
 +                                      local ip = new_ip(line);
 +                                      if ip then
 +                                              self:addnameserver(ip.addr);
 +                                      end
                                end
                        end
                end
@@@ -623,12 -623,7 +625,12 @@@ function resolver:getsocket(servernum
        if sock then return sock; end
  
        local err;
 -      sock, err = socket.udp();
 +      local peer = self.server[servernum];
 +      if peer:find(":") then
 +              sock, err = socket.udp6();
 +      else
 +              sock, err = socket.udp();
 +      end
        if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end
        if not sock then
                return nil, err;
        sock:settimeout(0);
        -- todo: attempt to use a random port, fallback to 0
        sock:setsockname('*', 0);
 -      sock:setpeername(self.server[servernum], 53);
 +      sock:setpeername(peer, 53);
        self.socket[servernum] = sock;
        self.socketset[sock] = servernum;
        return sock;
@@@ -681,7 -676,10 +683,10 @@@ function resolver:remember(rr, type
        self.cache = self.cache or setmetatable({}, cache_metatable);
        local rrs = get(self.cache, qclass, type, qname) or
                set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
-       append(rrs, rr);
+       if not rrs[rr[qtype:lower()]] then
+               rrs[rr[qtype:lower()]] = true;
+               append(rrs, rr);
+       end
  
        if type == 'MX' then self.unsorted[rrs] = true; end
  end
@@@ -759,7 -757,7 +764,7 @@@ function resolver:query(qname, qtype, q
                return nil, err;
        end
        conn:send (o.packet)
 -      
 +
        if timer and self.timeout then
                local num_servers = #self.server;
                local i = 1;
@@@ -855,7 -853,7 +860,7 @@@ function resolver:receive(rset)    -- 
                                        -- retire the query
                                        local queries = self.active[response.header.id];
                                        queries[response.question.raw] = nil;
 -                                      
 +
                                        if not next(queries) then self.active[response.header.id] = nil; end
                                        if not next(self.active) then self:closeall(); end
  
                                                set(self.wanted, q.class, q.type, q.name, nil);
                                        end
                                end
 -                              
 +
                        end
                end
        end
index 1910f9fa7e315766d4a5b472c1f7ef0c6e53817d,232fa5f777525980fe20f5b29a9f692e92e30162..405980c253e85e62702985ee39b149a7332a9849
@@@ -9,7 -9,6 +9,7 @@@ local _G = _G
  local prosody = _G.prosody;
  local hosts = prosody.hosts;
  local t_concat = table.concat;
 +local t_sort = table.sort;
  
  local module_host = module:get_host();
  
@@@ -119,7 -118,7 +119,7 @@@ local delete_user_layout = dataforms_ne
        instructions = "Fill out this form to delete a user.";
  
        { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
-       { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
+       { name = "accountjids", type = "jid-multi", required = true, label = "The Jabber ID(s) to delete" };
  };
  
  local delete_user_command_handler = adhoc_simple(delete_user_layout, function(fields, err)
@@@ -163,7 -162,7 +163,7 @@@ local end_user_session_layout = datafor
        instructions = "Fill out this form to end a user's session.";
  
        { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
-       { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
+       { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions", required = true };
  };
  
  local end_user_session_handler = adhoc_simple(end_user_session_layout, function(fields, err)
@@@ -346,7 -345,7 +346,7 @@@ local get_online_users_command_handler 
                count = count + 1;
                if fields.details then
                        for resource, session in pairs(user.sessions or {}) do
 -                              local status, priority = "unavailable", tostring(session.priority or "-");
 +                              local status, priority, ip = "unavailable", tostring(session.priority or "-"), session.ip or "<unknown>";
                                if session.presence then
                                        status = session.presence:child_with_name("show");
                                        if status then
                                                status = "available";
                                        end
                                end
 -                              users[#users+1] = " - "..resource..": "..status.."("..priority..")";
 +                              users[#users+1] = " - "..resource..": "..status.."("..priority.."), IP: ["..ip.."]";
                        end
                end
        end
        return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
  end);
  
 +-- Getting a list of S2S connections (this host)
 +local list_s2s_this_result = dataforms_new {
 +      title = "List of S2S connections on this host";
 +
 +      { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/s2s#list" };
 +      { name = "sessions", type = "text-multi", label = "Connections:" };
 +      { name = "num_in", type = "text-single", label = "#incomming connections:" };
 +      { name = "num_out", type = "text-single", label = "#outgoing connections:" };
 +};
 +
 +local function session_flags(session, line)
 +      line = line or {};
 +
 +      if session.id then
 +              line[#line+1] = "["..session.id.."]"
 +      else
 +              line[#line+1] = "["..session.type..(tostring(session):match("%x*$")).."]"
 +      end
 +
 +      local flags = {};
 +      if session.cert_identity_status == "valid" then
 +              flags[#flags+1] = "authenticated";
 +      end
 +      if session.secure then
 +              flags[#flags+1] = "encrypted";
 +      end
 +      if session.compressed then
 +              flags[#flags+1] = "compressed)";
 +      end
 +      if session.smacks then
 +              flags[#flags+1] = "sm";
 +      end
 +      if session.ip and session.ip:match(":") then
 +              flags[#flags+1] = "IPv6";
 +      end
 +      line[#line+1] = "("..t_concat(flags, ", ")..")";
 +
 +      return t_concat(line, " ");
 +end
 +
 +local function list_s2s_this_handler(self, data, state)
 +      local count_in, count_out = 0, 0;
 +      local s2s_list = {};
 +
 +      local s2s_sessions = module:shared"/*/s2s/sessions";
 +      for _, session in pairs(s2s_sessions) do
 +              local remotehost, localhost, direction;
 +              if session.direction == "outgoing" then
 +                      direction = "->";
 +                      count_out = count_out + 1;
 +                      remotehost, localhost = session.to_host or "?", session.from_host or "?";
 +              else
 +                      direction = "<-";
 +                      count_in = count_in + 1;
 +                      remotehost, localhost = session.from_host or "?", session.to_host or "?";
 +              end
 +              local sess_lines = { r = remotehost,
 +                      session_flags(session, { "", direction, remotehost or "?" })};
 +
 +              if remotehost:match(module_host) or localhost:match(module_host) then
 +                      s2s_list[#s2s_list+1] = sess_lines;
 +              end
 +      end
 +
 +      t_sort(s2s_list, function(a, b)
 +              return a.r < b.r;
 +      end);
 +
 +      for i, sess_lines in ipairs(s2s_list) do
 +              s2s_list[i] = sess_lines[1];
 +      end
 +
 +      return { status = "completed", result = { layout = list_s2s_this_result; values = {
 +              sessions = t_concat(s2s_list, "\n"),
 +              num_in = tostring(count_in),
 +              num_out = tostring(count_out)
 +      } } };
 +end
 +
  -- Getting a list of loaded modules
  local list_modules_result = dataforms_new {
        title = "List of loaded modules";
@@@ -569,7 -489,7 +569,7 @@@ local globally_reload_module_handler = 
        for _, host in pairs(hosts) do
                loaded_modules:append(array(keys(host.modules)));
        end
 -      loaded_modules = array(keys(set.new(loaded_modules):items())):sort();
 +      loaded_modules = array(set.new(loaded_modules):items()):sort();
        return { module = loaded_modules };
  end, function(fields, err)
        local is_global = false;
@@@ -711,7 -631,7 +711,7 @@@ local globally_unload_module_handler = 
        for _, host in pairs(hosts) do
                loaded_modules:append(array(keys(host.modules)));
        end
 -      loaded_modules = array(keys(set.new(loaded_modules):items())):sort();
 +      loaded_modules = array(set.new(loaded_modules):items()):sort();
        return { module = loaded_modules };
  end, function(fields, err)
        local is_global = false;
@@@ -807,7 -727,6 +807,7 @@@ local get_user_password_desc = adhoc_ne
  local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
  local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
  local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users-list", get_online_users_command_handler, "admin");
 +local list_s2s_this_desc = adhoc_new("List S2S connections", "http://prosody.im/protocol/s2s#list", list_s2s_this_handler, "admin");
  local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
  local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
  local globally_load_module_desc = adhoc_new("Globally load module", "http://prosody.im/protocol/modules#global-load", globally_load_module_handler, "global_admin");
@@@ -828,7 -747,6 +828,7 @@@ module:provides("adhoc", get_user_passw
  module:provides("adhoc", get_user_roster_desc);
  module:provides("adhoc", get_user_stats_desc);
  module:provides("adhoc", get_online_users_desc);
 +module:provides("adhoc", list_s2s_this_desc);
  module:provides("adhoc", list_modules_desc);
  module:provides("adhoc", load_module_desc);
  module:provides("adhoc", globally_load_module_desc);