1 -- HTTP Access to PEP Avatar
2 -- By Kim Alvefur <zash@zash.se>
4 local mod_pep
= module
:depends
"pep";
6 local um
= require
"core.usermanager";
7 local nodeprep
= require
"util.encodings".stringprep
.nodeprep
;
8 local base64_decode
= require
"util.encodings".base64
.decode
;
9 local urlencode
= require
"util.http".urlencode
;
11 module
:depends("http")
12 module
:provides("http", {
14 ["GET /*"] = function (event
, user
)
16 return [[<h1>Hello from mod_http_pep_avatar</h1><p>This module provides access to public avatars of local users.</p>]];
19 local request
, response
= event
.request
, event
.response
;
20 local actor
= request
.ip
;
22 local prepped
= nodeprep(user
);
23 if not prepped
then return 400; end
24 if prepped
~= user
then
25 response
.headers
.location
= module
:http_url() .. "/" .. urlencode(prepped
);
28 if not um
.user_exists(user
, module
.host
) then
32 local pep_service
= mod_pep
.get_pep_service(user
);
34 local ok
, avatar_hash
, avatar_meta
= pep_service
:get_last_item("urn:xmpp:avatar:metadata", actor
);
36 if not ok
or not avatar_hash
then
40 if avatar_hash
== request
.headers
.if_none_match
then
44 local data_ok
, avatar_data
= pep_service
:get_items("urn:xmpp:avatar:data", actor
, avatar_hash
);
45 if not data_ok
or type(avatar_data
) ~= "table" or not avatar_data
[avatar_hash
] then
49 response
.headers
.etag
= avatar_hash
;
51 local info
= avatar_meta
.tags
[1]:get_child("info");
52 response
.headers
.content_type
= info
and info
.attr
.type or "application/octet-stream";
54 local data
= avatar_data
[avatar_hash
];
55 return base64_decode(data
.tags
[1]:get_text());