annotate mod_invite/mod_invite.lua @ 2239:df696c8d5282

mod_invite: Add explicit dependency on mod_adhoc to make sure it is loaded
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jul 2016 23:57:20 +0200
parents dc4e77824318
children 967ba25edf58
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
1 local adhoc_new = module:require "adhoc".new;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
2 local uuid_new = require "util.uuid".generate;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
3 local jid_split = require "util.jid".split;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
4 local jid_join = require "util.jid".join;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
5 local http_formdecode = require "net.http".formdecode;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
6 local http_urlencode = require "net.http".urlencode;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
7 local usermanager = require "core.usermanager";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
8 local rostermanager = require "core.rostermanager";
2238
dc4e77824318 mod_invite: Use XML/HTML entity escaping from util.stanza
Kim Alvefur <zash@zash.se>
parents: 2058
diff changeset
9 local tohtml = require "util.stanza".xml_escape
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
10 local nodeprep = require "util.encodings".stringprep.nodeprep;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
11 local tostring = tostring;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
12
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
13 local invite_storage = module:open_store();
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
14 local inviter_storage = module:open_store("inviter");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
15
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
16 local serve = module:depends"http_files".serve;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
17
2239
df696c8d5282 mod_invite: Add explicit dependency on mod_adhoc to make sure it is loaded
Kim Alvefur <zash@zash.se>
parents: 2238
diff changeset
18 module:depends"adhoc";
2058
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
19 module:depends"http";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
20
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
21 local function apply_template(template, args)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
22 return
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
23 template:gsub("{{([^}]*)}}", function (k)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
24 if args[k] then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
25 return tohtml(args[k])
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
26 else
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
27 return k
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
28 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
29 end)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
30 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
31
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
32 function generate_page(event, display_options)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
33 local request, response = event.request, event.response;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
34
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
35 local tokens = invite_storage:get() or {};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
36
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
37 local token = request.path:match("^/invite/([^/]*)$");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
38
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
39 response.headers.content_type = "text/html; charset=utf-8";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
40
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
41 if not token or not tokens[token] then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
42 local template = assert(module:load_resource("invite/invite_result.html")):read("*a");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
43
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
44 return apply_template(template, { classes = "alert-danger", message = "This invite has expired." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
45 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
46
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
47 local template = assert(module:load_resource("invite/invite.html")):read("*a");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
48
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
49 return apply_template(template, { user = jid_join(tokens[token], module.host), server = module.host, token = token });
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
50 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
51
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
52 function subscribe(user1, user2)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
53 local user1_jid = jid_join(user1, module.host);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
54 local user2_jid = jid_join(user2, module.host);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
55
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
56 rostermanager.set_contact_pending_out(user2, module.host, user1_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
57 rostermanager.set_contact_pending_in(user1, module.host, user2_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
58 rostermanager.subscribed(user1, module.host, user2_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
59 rostermanager.process_inbound_subscription_approval(user2, module.host, user1_jid);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
60 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
61
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
62 function handle_form(event, display_options)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
63 local request, response = event.request, event.response;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
64 local form_data = http_formdecode(request.body);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
65 local user, password, token = form_data["user"], form_data["password"], form_data["token"];
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
66 local tokens = invite_storage:get() or {};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
67
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
68 local template = assert(module:load_resource("invite/invite_result.html")):read("*a");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
69
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
70 response.headers.content_type = "text/html; charset=utf-8";
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
71
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
72 if not user or #user == 0 or not password or #password == 0 or not token then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
73 return apply_template(template, { classes = "alert-warning", message = "Please fill in all fields." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
74 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
75
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
76 if not tokens[token] then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
77 return apply_template(template, { classes = "alert-danger", message = "This invite has expired." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
78 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
79
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
80 -- Shamelessly copied from mod_register_web.
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
81 local prepped_username = nodeprep(user);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
82
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
83 if not prepped_username or #prepped_username == 0 then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
84 return apply_template(template, { classes = "alert-warning", message = "This username contains invalid characters." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
85 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
86
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
87 if usermanager.user_exists(prepped_username, module.host) then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
88 return apply_template(template, { classes = "alert-warning", message = "This username is already in use." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
89 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
90
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
91 local registering = { username = prepped_username , host = module.host, allowed = true }
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
92
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
93 module:fire_event("user-registering", registering);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
94
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
95 if not registering.allowed then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
96 return apply_template(template, { classes = "alert-danger", message = "Registration is not allowed." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
97 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
98
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
99 local ok, err = usermanager.create_user(prepped_username, password, module.host);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
100
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
101 if ok then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
102 subscribe(prepped_username, tokens[token]);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
103 subscribe(tokens[token], prepped_username);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
104
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
105 inviter_storage:set(prepped_username, { inviter = tokens[token] });
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
106
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
107 rostermanager.roster_push(tokens[token], module.host, jid_join(prepped_username, module.host));
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
108
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
109 tokens[token] = nil;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
110
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
111 invite_storage:set(nil, tokens);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
112
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
113 return apply_template(template, { classes = "alert-success", message = "Your account has been created! You can now log in using an XMPP client." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
114 else
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
115 module:log("debug", "Registration failed: " .. tostring(err));
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
116
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
117 return apply_template(template, { classes = "alert-danger", message = "An unknown error has occurred." })
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
118 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
119 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
120
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
121 module:provides("http", {
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
122 route = {
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
123 ["GET /a_file.txt"] = serve(module:get_directory().."/my_file.txt");
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
124 ["GET /bootstrap.min.css"] = serve(module:get_directory());
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
125 ["GET /*"] = generate_page;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
126 POST = handle_form;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
127 };
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
128 });
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
129
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
130 function invite_command_handler(self, data, state)
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
131 local uuid = uuid_new();
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
132
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
133 local user, host = jid_split(data.from);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
134
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
135 if host ~= module.host then
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
136 return { status = "completed", error = { message = "You are not allowed to invite users to this server." }};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
137 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
138
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
139 local tokens = invite_storage:get() or {};
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
140
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
141 tokens[uuid] = user;
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
142
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
143 invite_storage:set(nil, tokens);
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
144
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
145 return { info = module:http_url() .. "/" .. uuid, status = "completed" };
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
146 end
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
147
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
148 local adhoc_invite = adhoc_new("Invite user", "invite", invite_command_handler, "user")
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
149
4b3037c7af62 mod_invite: Allows existing users to generate URLs that can be used to invite new users. Mutual presence subscriptions are automatically created when the creation succeeds.
Thijs Alkemade <me@thijsalkema.de>
parents:
diff changeset
150 module:add_item("adhoc", adhoc_invite);