Annotate

plugins/muc/affiliation_notify.lib.lua @ 6990:f476e2497568

MUC: Fire per-field events for the config form, including those with value == nil (no <value/> element in form)
author Matthew Wild <mwild1@gmail.com>
date Fri, 11 Dec 2015 15:27:01 +0000
parent 6394:fe034fa564ee
child 6991:84e01dbb739e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6394
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
1 -- Prosody IM
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
2 -- Copyright (C) 2014 Daurnimator
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
3 --
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
6 --
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
7
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
8 --[[
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
9 Out of courtesy, a MUC service MAY send an out-of-room <message/>
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
10 if a user's affiliation changes while the user is not in the room;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
11 the message SHOULD be sent from the room to the user's bare JID,
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
12 MAY contain a <body/> element describing the affiliation change,
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
13 and MUST contain a status code of 101.
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
14 ]]
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
15
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
16
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
17 local st = require "util.stanza";
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
18
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
19 local function get_affiliation_notify(room)
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
20 return room._data.affiliation_notify;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
21 end
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
22
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
23 local function set_affiliation_notify(room, affiliation_notify)
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
24 affiliation_notify = affiliation_notify and true or nil;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
25 if room._data.affiliation_notify == affiliation_notify then return false; end
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
26 room._data.affiliation_notify = affiliation_notify;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
27 if room.save then room:save(true); end
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
28 return true;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
29 end
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
30
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
31 module:hook("muc-config-form", function(event)
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
32 table.insert(event.form, {
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
33 name = "muc#roomconfig_affiliationnotify";
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
34 type = "boolean";
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
35 label = "Notify users when their affiliation changes when they are not in the room?";
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
36 value = get_affiliation_notify(event.room);
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
37 });
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
38 end);
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
39
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
40 module:hook("muc-config-submitted", function(event)
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
41 local new = event.fields["muc#roomconfig_affiliationnotify"];
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
42 if new ~= nil and set_affiliation_notify(event.room, new) then
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
43 event.status_codes["104"] = true;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
44 end
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
45 end);
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
46
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
47 module:hook("muc-set-affiliation", function(event)
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
48 local room = event.room;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
49 if not event.in_room and get_affiliation_notify(room) then
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
50 local body = string.format("Your affiliation in room %s is now %s.", room.jid, event.affiliation);
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
51 local stanza = st.message({
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
52 type = "headline";
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
53 from = room.jid;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
54 to = event.jid;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
55 }, body)
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
56 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
57 :tag("status", {code="101"}):up()
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
58 :up();
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
59 room:route_stanza(stanza);
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
60 end
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
61 end);
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
62
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
63 return {
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
64 get = get_affiliation_notify;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
65 set = set_affiliation_notify;
fe034fa564ee plugins/muc: Add affiliation_notify config option to send out status code 101
daurnimator <quae@daurnimator.com>
parents:
diff changeset
66 };