Comparison

mod_muc_search/mod_muc_search.lua @ 4136:d2a9aa27169d

mod_muc_search: Implementation of the search.jabber.network XMPP API https://search.jabber.network/docs/api#xmpp
author Kim Alvefur <zash@zash.se>
date Fri, 17 Aug 2018 09:49:27 +0200
child 4137:5f4bcaad18ee
comparison
equal deleted inserted replaced
4135:ed0c7044b00f 4136:d2a9aa27169d
1 local dataforms = require "util.dataforms";
2 local st = require "util.stanza";
3
4 local mod_muc = module:depends("muc");
5 assert(mod_muc.live_rooms, "Missing required MUC API. Prosody >= hg:f5c43e829d93 required")
6
7 local search_form = dataforms.new {
8 {
9 type = "hidden";
10 value = "https://xmlns.zombofant.net/muclumbus/search/1.0#params";
11 name = "FORM_TYPE";
12 };
13 {
14 type = "text-single";
15 label = "Search for";
16 name = "q";
17 };
18 {
19 type = "boolean";
20 value = true;
21 label = "Search in name";
22 name = "sinname";
23 };
24 {
25 type = "boolean";
26 value = true;
27 label = "Search in description";
28 name = "sindescription";
29 };
30 {
31 type = "boolean";
32 value = true;
33 label = "Search in address";
34 name = "sinaddr";
35 };
36 {
37 type = "text-single";
38 value = "1";
39 label = "Minimum number of users";
40 name = "min_users";
41 };
42 {
43 options = {
44 {
45 label = "Number of online users";
46 value = "nusers";
47 };
48 {
49 label = "Address";
50 value = "address";
51 };
52 };
53 type = "list-single";
54 value = "nusers";
55 label = "Sort results by";
56 name = "key";
57 };
58 };
59
60 module:hook("iq-get/host/https://xmlns.zombofant.net/muclumbus/search/1.0:search", function (event)
61 local origin, stanza = event.origin, event.stanza;
62 origin.send(st.reply(stanza)
63 :tag("search", { xmlns = "https://xmlns.zombofant.net/muclumbus/search/1.0" })
64 :add_child(search_form:form()));
65 return true;
66 end);
67
68 module:hook("iq-set/host/https://xmlns.zombofant.net/muclumbus/search/1.0:search", function (event)
69 local origin, stanza = event.origin, event.stanza;
70 local search = stanza.tags[1];
71 local submitted = search:get_child("x", "jabber:x:data");
72 if not submitted then
73 origin.send(st.error_reply("modify", "bad-request", "Missing dataform"));
74 return;
75 end
76 local query = search_form:data(submitted);
77 module:log("debug", "Got query: %q", query);
78
79 local result = st.reply(stanza)
80 :tag("result", { xmlns = "https://xmlns.zombofant.net/muclumbus/search/1.0" });
81
82 for room in mod_muc.live_rooms() do
83
84 if room:get_public() and not room:get_members_only() then
85 module:log("debug", "Looking at room %s %q", room.jid, room._data);
86 if (query.sinname and room:get_name():find(query.q, 1, true))
87 or (query.sindescription and (room:get_description() or ""):find(query.q, 1, true))
88 or (query.sinaddr and room.jid:find(query.q, 1, true)) then
89 result:tag("item", { address = room.jid })
90 :text_tag("name", room:get_name())
91 :text_tag("description", room:get_description())
92 :text_tag("language", room:get_language())
93 :tag("is-open"):up()
94 :up();
95 end
96 end
97 end
98 origin.send(result);
99 return true;
100 end);