annotate mod_invite/mod_invite.lua @ 2223:c3ad652cb71f

mod_storage_ejabberdsql_readonly: Initial commit
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 30 Jun 2016 05:27:02 +0100
parents 4b3037c7af62
children dc4e77824318
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";
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
9 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
10 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
11
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 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
13 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
14
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 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
16
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 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
18
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 local entities = {
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 ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;",
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 ["'"] = "&apos;", ["\""] = "&quot;", ["\n"] = "<br/>",
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 };
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
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 local function tohtml(plain)
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 (plain:gsub("[<>&'\"\n]", entities));
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 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
27
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 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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
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 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
40 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
41
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 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
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 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
45
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 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
47
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 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
49 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
50
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 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
52 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
53
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 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
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 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
57 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
58
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 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
60 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
61 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
62
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 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
64 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
65 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
66 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
67 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
68
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 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
70 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
71 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
72 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
73 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
74
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 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
76
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 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
78
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 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
80 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
81 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
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 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
84 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
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 -- 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
88 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
89
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 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
91 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
92 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
93
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 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
95 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
96 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
97
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 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
99
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 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
101
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 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
103 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
104 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
105
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 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
107
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 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
109 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
110 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
111
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 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
113
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 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
115
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 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
117
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 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
119
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 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
121 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
122 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
123
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 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
125 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
126 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
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 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
129 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
130 ["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
131 ["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
132 ["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
133 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
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 });
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
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 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
138 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
139
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 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
141
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 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
143 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
144 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
145
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 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
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 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
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 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
151
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
152 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
153 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
154
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
155 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
156
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
157 module:add_item("adhoc", adhoc_invite);