Download
Edit

Prosody mod_vjud

VanaryonVanaryon
Valérian Saliou
Added on 2012-03-18 21:35:47

 

Prosody mod_vjud - Show - Edit - Download

 

# From: http://q.zash.se/a22d974e.txt ; Author: Zash
 
local dm_load = require "util.datamanager".load;
local dm_store = require "util.datamanager".store;
 
local usermanager = require "core.usermanager";
local nodeprep = require "util.encodings".stringprep.nodeprep;
local dataforms_new = require "util.dataforms".new;
local jid_split = require "util.jid".split;
 
local st = require "util.stanza";
local template = require "util.template";
 
local get_reply = template[[
  <query xmlns='jabber:iq:search'>
    <instructions>
      Fill in one or more fields to search
      for any matching Jabber users.
    </instructions>
    <nick/>
  </query>
]].apply({});
local set_reply = template[[
  <query xmlns='jabber:iq:search'>
    <item jid='{jid}'>
      <nick>{nick}</nick>
    </item>
  </query>
]];
 
module:add_feature("jabber:iq:search");
 
local opted_in;
 
module:hook("iq/host/jabber:iq:search:query", function(event)
        local origin, stanza = event.origin, event.stanza;
 
        if stanza.attr.type == "get" then
                origin.send(st.reply(stanza):add_child(get_reply));
        else
                local query = stanza.tags[1];
                local nick = query:get_child("nick");
                nick = nick and nick:get_text();
                nick = nick and nodeprep(nick);
                if nick and usermanager.user_exists(nick, module.host) then
                        origin.send(st.reply(stanza):add_child(set_reply.apply({ nick = nick, jid = nick.."@"..module.host})));
                else
                        origin.send(st.reply(stanza):query("jabber:iq:search"));
                end
        end
        return true;
end);
 
local function opt_in_handler(self, data, state)
        local opted_in = opted_in or dm_load(nil, module.host, "user_index");
        local username, hostname = jid_split(data.from);
        local opt_in_layout = dataforms_new{
                title = "Search settings";
                instructions = "Do you want to appear in search results?";
                {
                        name = "searchable",
                        label = "Appear in search results?",
                        type = "boolean",
                        required = true,
                        default = opted_in[username],
                },
        };
 
        if state then -- the second return value
                if data.action == "cancel" then
                        return { status = "canceled" };
                end
 
                if not username or not hostname or hostname ~= module.host then
                        return { status = "error", error = { type = "cancel",
                                condition = "forbidden", message = "Invalid user or hostname." } };
                end
 
                local fields = opt_in_layout:data(data.form);
                opted_in[username] = fields.searchable or nil
                dm_store(nil, module.host, "opted_in", opted_in);
 
        else -- No state, send the form.
                return { status = "executing", actions  = { "complete" }, form = opt_in_layout }, true;
        end
end
 
local adhoc_new = module:require "adhoc".new;
local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler, "self" and nil);
module:add_item ("vjudsetup", adhoc_vjudsetup);