Mercurial > prosody-modules
annotate mod_http_pep_avatar/mod_http_pep_avatar.lua @ 3566:b50be75c8bef
mod_auth_ldap: Use module API for logging
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 29 Apr 2019 00:05:20 +0200 |
parents | 213679266dcb |
children | 00bdecb12779 |
rev | line source |
---|---|
3425
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
1 -- HTTP Access to PEP Avatar |
2294
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- By Kim Alvefur <zash@zash.se> |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
3241
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
4 local mod_pep = module:depends"pep"; |
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
5 |
3424
6ae875c98daf
mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents:
3423
diff
changeset
|
6 local um = require "core.usermanager"; |
2294
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local nodeprep = require "util.encodings".stringprep.nodeprep; |
3425
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
8 local base64_decode = require "util.encodings".base64.decode; |
3433
213679266dcb
mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents:
3425
diff
changeset
|
9 local urlencode = require "util.http".urlencode; |
2294
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
3241
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
11 module:depends("http") |
2294
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 module:provides("http", { |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 route = { |
3241
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
14 ["GET /*"] = function (event, user) |
3423 | 15 local request, response = event.request, event.response; |
16 local actor = request.ip; | |
3241
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
17 |
3433
213679266dcb
mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents:
3425
diff
changeset
|
18 local prepped = nodeprep(user); |
213679266dcb
mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents:
3425
diff
changeset
|
19 if not prepped then return 400; end |
213679266dcb
mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents:
3425
diff
changeset
|
20 if prepped ~= user then |
213679266dcb
mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents:
3425
diff
changeset
|
21 response.headers.location = module:http_url() .. "/" .. urlencode(prepped); |
213679266dcb
mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents:
3425
diff
changeset
|
22 return 302; |
213679266dcb
mod_http_pep_avatar: Redirect to prepped username
Kim Alvefur <zash@zash.se>
parents:
3425
diff
changeset
|
23 end |
3424
6ae875c98daf
mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents:
3423
diff
changeset
|
24 if not um.user_exists(user, module.host) then |
6ae875c98daf
mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents:
3423
diff
changeset
|
25 return 404; |
6ae875c98daf
mod_atom: Check whether user exists
Kim Alvefur <zash@zash.se>
parents:
3423
diff
changeset
|
26 end |
3241
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
27 |
3425
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
28 local pep_service = mod_pep.get_pep_service(user); |
3272
119e22ccd64a
mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents:
3241
diff
changeset
|
29 |
3425
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
30 local ok, avatar_hash, avatar_meta = pep_service:get_last_item("urn:xmpp:avatar:metadata", actor); |
3272
119e22ccd64a
mod_atom: Add some basic metadata to feed
Kim Alvefur <zash@zash.se>
parents:
3241
diff
changeset
|
31 |
3425
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
32 if not ok or not avatar_hash then |
3241
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
33 return 404; |
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
34 end |
3425
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
35 |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
36 if avatar_hash == request.headers.if_none_match then |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
37 return 304; |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
38 end |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
39 |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
40 local data_ok, avatar_data = pep_service:get_items("urn:xmpp:avatar:data", actor, avatar_hash); |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
41 if not data_ok or type(avatar_data) ~= "table" or not avatar_data[avatar_hash] then |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
42 return 404; |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
43 end |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
44 |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
45 response.headers.etag = avatar_hash; |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
46 |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
47 local info = avatar_meta.tags[1]:get_child("info"); |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
48 response.headers.content_type = info and info.attr.type or "application/octet-stream"; |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
49 |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
50 local data = avatar_data[avatar_hash]; |
461429e0db58
mod_http_pep_avatar: Provides PEP avatars via HTTP
Kim Alvefur <zash@zash.se>
parents:
3424
diff
changeset
|
51 return base64_decode(data.tags[1]:get_text()); |
3241
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
52 end; |
4b52cafd5811
mod_atom: Update to the new mod_pep
Kim Alvefur <zash@zash.se>
parents:
2294
diff
changeset
|
53 } |
2294
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 }); |