Software / code / prosody-modules
Annotate
mod_vjud/mod_vjud.lua @ 6301:fa45ae704b79
mod_cloud_notify: Update Readme
diff --git a/mod_cloud_notify/README.md b/mod_cloud_notify/README.md
--- a/mod_cloud_notify/README.md
+++ b/mod_cloud_notify/README.md
@@ -1,109 +1,106 @@
----
-labels:
-- 'Stage-Beta'
-summary: 'XEP-0357: Cloud push notifications'
----
+# Introduction
-Introduction
-============
+This module enables support for sending "push notifications" to clients
+that need it, typically those running on certain mobile devices.
-This module enables support for sending "push notifications" to clients that
-need it, typically those running on certain mobile devices.
+As well as this module, your client must support push notifications (the
+apps that need it generally do, of course) and the app developer's push
+gateway must be reachable from your Prosody server (this happens over a
+normal XMPP server-to-server 's2s' connection).
-As well as this module, your client must support push notifications (the apps
-that need it generally do, of course) and the app developer's push gateway
-must be reachable from your Prosody server (this happens over a normal XMPP
-server-to-server 's2s' connection).
-
-Details
-=======
+# Details
Some platforms, notably Apple's iOS and many versions of Android, impose
-limits that prevent applications from running or accessing the network in the
-background. This makes it difficult or impossible for an XMPP application to
-remain reliably connected to a server to receive messages.
-
-In order for messaging and other apps to receive notifications, the OS vendors
-run proprietary servers that their OS maintains a permanent connection to in
-the background. Then they provide APIs to application developers that allow
-sending notifications to specific devices via those servers.
+limits that prevent applications from running or accessing the network
+in the background. This makes it difficult or impossible for an XMPP
+application to remain reliably connected to a server to receive
+messages.
-When you connect to your server with an app that requires push notifications,
-it will use this module to set up a "push registration". When you receive
-a message but your device is not connected to the server, this module will
-generate a notification and send it to the push gateway operated by your
-application's developers). Their gateway will then connect to your device's
-OS vendor and ask them to forward the notification to your device. When your
-device receives the notification, it will display it or wake up the app so it
-can connect to XMPP and receive any pending messages.
+In order for messaging and other apps to receive notifications, the OS
+vendors run proprietary servers that their OS maintains a permanent
+connection to in the background. Then they provide APIs to application
+developers that allow sending notifications to specific devices via
+those servers.
-This protocol is described for developers in [XEP-0357: Push Notifications].
+When you connect to your server with an app that requires push
+notifications, it will use this module to set up a "push registration".
+When you receive a message but your device is not connected to the
+server, this module will generate a notification and send it to the push
+gateway operated by your application's developers). Their gateway will
+then connect to your device's OS vendor and ask them to forward the
+notification to your device. When your device receives the notification,
+it will display it or wake up the app so it can connect to XMPP and
+receive any pending messages.
-For this module to work reliably, you must have [mod_smacks], [mod_mam] and
-[mod_carbons] also enabled on your server.
+This protocol is described for developers in \[XEP-0357: Push
+Notifications\].
+
+For this module to work reliably, you must have \[mod_smacks\],
+\[mod_mam\] and \[mod_carbons\] also enabled on your server.
-Some clients, notably Siskin and Snikket iOS need some additional extensions
-that are not currently defined in a standard XEP. To support these clients,
-see [mod_cloud_notify_extensions].
+Some clients, notably Siskin and Snikket iOS need some additional
+extensions that are not currently defined in a standard XEP. To support
+these clients, see \[mod_cloud_notify_extensions\].
-Configuration
-=============
+# Configuration
- Option Default Description
- ------------------------------------ ----------------- -------------------------------------------------------------------------------------------------------------------
- `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
- `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
- `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
- `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
- `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
- `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
+ Option Default Description
+ -------------------------------------- ---------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
+ `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
+ `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
+ `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
+ `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
+ `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
-(\*) There are privacy implications for enabling these options.
+(\*) There are privacy implications for enabling these options.[^1]
-Internal design notes
-=====================
+# Internal design notes
-App servers are notified about offline messages, messages stored by [mod_mam]
-or messages waiting in the smacks queue.
-The business rules outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2].
+App servers are notified about offline messages, messages stored by
+\[mod_mam\] or messages waiting in the smacks queue. The business rules
+outlined
+[here](//mail.jabber.org/pipermail/standards/2016-February/030925.html)
+are all honored[^2].
-To cooperate with [mod_smacks] this module consumes some events:
-`smacks-ack-delayed`, `smacks-hibernation-start` and `smacks-hibernation-end`.
-These events allow this module to send out notifications for messages received
-while the session is hibernated by [mod_smacks] or even when smacks
-acknowledgements for messages are delayed by a certain amount of seconds
-configurable with the [mod_smacks] setting `smacks_max_ack_delay`.
+To cooperate with \[mod_smacks\] this module consumes some events:
+`smacks-ack-delayed`, `smacks-hibernation-start` and
+`smacks-hibernation-end`. These events allow this module to send out
+notifications for messages received while the session is hibernated by
+\[mod_smacks\] or even when smacks acknowledgements for messages are
+delayed by a certain amount of seconds configurable with the
+\[mod_smacks\] setting `smacks_max_ack_delay`.
-The `smacks_max_ack_delay` setting allows to send out notifications to clients
-which aren't already in smacks hibernation state (because the read timeout or
-connection close didn't already happen) but also aren't responding to acknowledgement
-request in a timely manner. This setting thus allows conversations to be smoother
-under such circumstances.
+The `smacks_max_ack_delay` setting allows to send out notifications to
+clients which aren't already in smacks hibernation state (because the
+read timeout or connection close didn't already happen) but also aren't
+responding to acknowledgement request in a timely manner. This setting
+thus allows conversations to be smoother under such circumstances.
-The new event `cloud-notify-ping` can be used by any module to send out a cloud
-notification to either all registered endpoints for the given user or only the endpoints
-given in the event data.
+The new event `cloud-notify-ping` can be used by any module to send out
+a cloud notification to either all registered endpoints for the given
+user or only the endpoints given in the event data.
-The config setting `push_notification_important_body` can be used to specify an alternative
-body text to send to the remote pubsub node if the stanza is encrypted or has a body.
-This way the real contents of the message aren't revealed to the push appserver but it
-can still see that the push is important.
-This is used by Chatsecure on iOS to send out high priority pushes in those cases for example.
+The config setting `push_notification_important_body` can be used to
+specify an alternative body text to send to the remote pubsub node if
+the stanza is encrypted or has a body. This way the real contents of the
+message aren't revealed to the push appserver but it can still see that
+the push is important. This is used by Chatsecure on iOS to send out
+high priority pushes in those cases for example.
-Compatibility
-=============
-
-**Note:** This module should be used with Lua 5.2 and higher. Using it with
-Lua 5.1 may cause push notifications to not be sent to some clients.
+# Compatibility
------- -----------------------------------------------------------------------------
- trunk Works
- 0.12 Works
- 0.11 Works
- 0.10 Works
- 0.9 Support dropped, use last supported version [675726ab06d3](//hg.prosody.im/prosody-modules/raw-file/675726ab06d3/mod_cloud_notify/mod_cloud_notify.lua)
------- -----------------------------------------------------------------------------
+**Note:** This module should be used with Lua 5.2 and higher. Using it
+with Lua 5.1 may cause push notifications to not be sent to some
+clients.
+ ------- -----------------------------------------------------------------
+ trunk Works as of 25-06-13
+ 13 Works
+ 0.12 Works
+ ------- -----------------------------------------------------------------
-[^1]: The service which is expected to forward notifications to something like Google Cloud Messaging or Apple Notification Service
-[^2]: [business_rules.markdown](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.markdown)
+[^1]: The service which is expected to forward notifications to
+ something like Google Cloud Messaging or Apple Notification Service
+
+[^2]: [business_rules.md](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.md)
| author | Menel <menel@snikket.de> |
|---|---|
| date | Fri, 13 Jun 2025 10:36:52 +0200 |
| parent | 4910:5dffb85e62c4 |
| rev | line source |
|---|---|
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local dm_load = require "util.datamanager".load; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local dm_store = require "util.datamanager".store; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local usermanager = require "core.usermanager"; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local dataforms_new = require "util.dataforms".new; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local jid_split = require "util.jid".prepped_split; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local vcard = module:require "vcard"; |
|
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
8 local rawget, rawset = rawget, rawset; |
|
880
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
9 local s_lower = string.lower; |
|
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
10 local s_find = string.find; |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local st = require "util.stanza"; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local template = require "util.template"; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
|
1296
b1a92a87309c
mod_vjud: Move instructions into a config option
Kim Alvefur <zash@zash.se>
parents:
882
diff
changeset
|
15 local instructions = module:get_option_string("vjud_instructions", "Fill in one or more fields to search for any matching Jabber users."); |
|
b1a92a87309c
mod_vjud: Move instructions into a config option
Kim Alvefur <zash@zash.se>
parents:
882
diff
changeset
|
16 |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local get_reply = template[[ |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 <query xmlns="jabber:iq:search"> |
|
1296
b1a92a87309c
mod_vjud: Move instructions into a config option
Kim Alvefur <zash@zash.se>
parents:
882
diff
changeset
|
19 <instructions>{instructions}</instructions> |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 <first/> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 <last/> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 <nick/> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 <email/> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 </query> |
|
1296
b1a92a87309c
mod_vjud: Move instructions into a config option
Kim Alvefur <zash@zash.se>
parents:
882
diff
changeset
|
25 ]].apply({ instructions = instructions }); |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local item_template = template[[ |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 <item xmlns="jabber:iq:search" jid="{jid}"> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 <first>{first}</first> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 <last>{last}</last> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 <nick>{nick}</nick> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 <email>{email}</email> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 </item> |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 ]]; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
35 local search_mode = module:get_option_string("vjud_mode", "opt-in"); |
|
882
4939788a47ea
mod_vjud: Disallow searching from remote hosts by default in search-all-users mode
Kim Alvefur <zash@zash.se>
parents:
881
diff
changeset
|
36 local allow_remote = module:get_option_boolean("allow_remote_searches", search_mode ~= "all"); |
|
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
37 local base_host = module:get_option_string("vjud_search_domain", |
|
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
38 module:get_host_type() == "component" |
|
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
39 and module.host:gsub("^[^.]+%.","") |
|
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
40 or module.host); |
|
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
41 |
|
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
42 module:depends"disco"; |
| 1319 | 43 if module:get_host_type() == "component" then |
|
1318
5d49dc72b732
mod_vjud: Add an <identity> if loaded as a component (thanks gryffus)
Kim Alvefur <zash@zash.se>
parents:
1296
diff
changeset
|
44 module:add_identity("directory", "user", module:get_option_string("name", "User search")); |
|
5d49dc72b732
mod_vjud: Add an <identity> if loaded as a component (thanks gryffus)
Kim Alvefur <zash@zash.se>
parents:
1296
diff
changeset
|
45 end |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 module:add_feature("jabber:iq:search"); |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 local vCard_mt = { |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 __index = function(t, k) |
|
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
50 if type(k) ~= "string" then return nil end |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 for i=1,#t do |
|
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
52 local t_i = rawget(t, i); |
|
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
53 if t_i and t_i.name == k then |
|
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
54 rawset(t, k, t_i); |
|
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
55 return t_i; |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 }; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 |
|
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
61 local function get_user_vcard(user, host) |
|
2179
a90c7d7e7413
mod_vjud: Fix missing comma (thanks Tobias)
Kim Alvefur <zash@zash.se>
parents:
2008
diff
changeset
|
62 local vCard, err = dm_load(user, host or base_host, "vcard"); |
|
2008
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
63 if not vCard then return nil, err; end |
|
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
64 vCard = st.deserialize(vCard); |
|
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
65 vCard, err = vcard.from_xep54(vCard); |
|
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
66 if not vCard then return nil, err; end |
|
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
67 return setmetatable(vCard, vCard_mt); |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 |
|
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
70 local at_host = "@"..base_host; |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
72 local users; -- The user iterator |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
73 |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 module:hook("iq/host/jabber:iq:search:query", function(event) |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 local origin, stanza = event.origin, event.stanza; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
|
882
4939788a47ea
mod_vjud: Disallow searching from remote hosts by default in search-all-users mode
Kim Alvefur <zash@zash.se>
parents:
881
diff
changeset
|
77 if not (allow_remote or origin.type == "c2s") then |
|
4939788a47ea
mod_vjud: Disallow searching from remote hosts by default in search-all-users mode
Kim Alvefur <zash@zash.se>
parents:
881
diff
changeset
|
78 origin.send(st.error_reply(stanza, "cancel", "not-allowed")) |
|
4939788a47ea
mod_vjud: Disallow searching from remote hosts by default in search-all-users mode
Kim Alvefur <zash@zash.se>
parents:
881
diff
changeset
|
79 return true; |
|
4939788a47ea
mod_vjud: Disallow searching from remote hosts by default in search-all-users mode
Kim Alvefur <zash@zash.se>
parents:
881
diff
changeset
|
80 end |
|
4939788a47ea
mod_vjud: Disallow searching from remote hosts by default in search-all-users mode
Kim Alvefur <zash@zash.se>
parents:
881
diff
changeset
|
81 |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 if stanza.attr.type == "get" then |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 origin.send(st.reply(stanza):add_child(get_reply)); |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 else -- type == "set" |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 local query = stanza.tags[1]; |
|
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
86 local first, last, nick, email = |
|
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
87 s_lower(query:get_child_text"first" or ""), |
|
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
88 s_lower(query:get_child_text"last" or ""), |
|
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
89 s_lower(query:get_child_text"nick" or ""), |
|
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
90 s_lower(query:get_child_text"email" or ""); |
|
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
91 |
|
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
92 first = #first >= 2 and first; |
|
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
93 last = #last >= 2 and last; |
|
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
94 nick = #nick >= 2 and nick; |
|
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
95 email = #email >= 2 and email; |
|
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
96 if not ( first or last or nick or email ) then |
|
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
97 origin.send(st.error_reply(stanza, "modify", "not-acceptable", "All fields were empty or too short")); |
|
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
98 return true; |
|
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
99 end |
|
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
100 |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 local reply = st.reply(stanza):query("jabber:iq:search"); |
|
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
102 |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 local username, hostname = jid_split(email); |
|
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
104 if hostname == base_host and username and usermanager.user_exists(username, hostname) then |
|
2008
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
105 local vCard, err = get_user_vcard(username); |
|
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
106 if not vCard then |
|
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
107 module:log("debug", "Couldn't get vCard for user %s: %s", username, err or "unknown error"); |
|
cf3bdcb633f0
mod_vjud: Handle vCard decoding errors by logging them (Thanks Roi)
Kim Alvefur <zash@zash.se>
parents:
1319
diff
changeset
|
108 else |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 reply:add_child(item_template.apply{ |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 jid = username..at_host; |
|
733
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
111 first = vCard.N and vCard.N[2] or nil; |
|
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
112 last = vCard.N and vCard.N[1] or nil; |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 nick = vCard.NICKNAME and vCard.NICKNAME[1] or username; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 email = vCard.EMAIL and vCard.EMAIL[1] or nil; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 }); |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 else |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
118 for username in users() do |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 local vCard = get_user_vcard(username); |
|
880
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
120 if vCard |
|
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
121 and ((first and vCard.N and s_find(s_lower(vCard.N[2]), first, nil, true)) |
|
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
122 or (last and vCard.N and s_find(s_lower(vCard.N[1]), last, nil, true)) |
|
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
123 or (nick and vCard.NICKNAME and s_find(s_lower(vCard.NICKNAME[1]), nick, nil, true)) |
|
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
124 or (email and vCard.EMAIL and s_find(s_lower(vCard.EMAIL[1]), email, nil, true))) then |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 reply:add_child(item_template.apply{ |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 jid = username..at_host; |
|
733
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
127 first = vCard.N and vCard.N[2] or nil; |
|
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
128 last = vCard.N and vCard.N[1] or nil; |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 nick = vCard.NICKNAME and vCard.NICKNAME[1] or username; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 email = vCard.EMAIL and vCard.EMAIL[1] or nil; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 }); |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 origin.send(reply); |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 return true; |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 end); |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
140 if search_mode == "all" then |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
141 function users() |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
142 return usermanager.users(base_host); |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
143 end |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
144 else -- if "opt-in", default |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
145 local opted_in; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
146 function module.load() |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
147 opted_in = dm_load(nil, module.host, "user_index") or {}; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
148 end |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
149 function module.unload() |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
150 dm_store(nil, module.host, "user_index", opted_in); |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
151 end |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
152 function users() |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
153 return pairs(opted_in); |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
154 end |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
155 local opt_in_layout = dataforms_new{ |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
156 title = "Search settings"; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
157 instructions = "Do you want to appear in search results?"; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
158 { |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
159 name = "searchable", |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
160 label = "Appear in search results?", |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
161 type = "boolean", |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
162 }, |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
163 }; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
164 local function opt_in_handler(self, data, state) |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
165 local username, hostname = jid_split(data.from); |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
166 if state then -- the second return value |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
167 if data.action == "cancel" then |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
168 return { status = "canceled" }; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
169 end |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
171 if not username or not hostname or hostname ~= base_host then |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
172 return { status = "error", error = { type = "cancel", |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
173 condition = "forbidden", message = "Invalid user or hostname." } }; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
174 end |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
176 local fields = opt_in_layout:data(data.form); |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
177 opted_in[username] = fields.searchable or nil |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
178 |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
179 return { status = "completed" } |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
180 else -- No state, send the form. |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
181 return { status = "executing", actions = { "complete" }, |
|
787
cec49ee88c23
mod_vjud: Correctly pass current state to form (Thanks Florob)
Kim Alvefur <zash@zash.se>
parents:
742
diff
changeset
|
182 form = { layout = opt_in_layout, values = { searchable = opted_in[username] } } }, true; |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
183 end |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 end |
|
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
186 local adhoc_new = module:require "adhoc".new; |
|
4910
5dffb85e62c4
mod_vjud: Add permission parameter "any" for compatibility with 0.12 (fixes #1720)
Matthew Wild <mwild1@gmail.com>
parents:
2179
diff
changeset
|
187 local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler, "any");--, "self");-- and nil); |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
188 module:depends"adhoc"; |
|
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
189 module:provides("adhoc", adhoc_vjudsetup); |
|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 |
|
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
191 end |