Mercurial > prosody-modules
annotate mod_atom/mod_atom.lua @ 2618:c6652d055ba3
mod_firewall: Add some more comments
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 10 Mar 2017 10:36:17 +0000 |
parents | 4915b8223b07 |
children | 4b52cafd5811 |
rev | line source |
---|---|
2294
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- HTTP Access to PEP -> microblog |
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 |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 module:depends"http"; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 module:depends"pep"; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local nodeprep = require "util.encodings".stringprep.nodeprep; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local st = require "util.stanza"; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local host, hosts = module.host, hosts; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local function handle_request(event, path) |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local response = event.response; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local user = nodeprep(path); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 if not user then return 400 end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local jid = user .. "@" .. host; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local pep_data = hosts[host].modules.pep.module.save(); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 if not pep_data.data[jid] or |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 not pep_data.data[jid]["urn:xmpp:microblog:0"] then |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 return 404; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local microblogdata = pep_data.data[jid]["urn:xmpp:microblog:0"][2]:get_child("entry", "http://www.w3.org/2005/Atom"); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 if not microblogdata then return 404; end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local feed = st.stanza("feed", { xmlns="http://www.w3.org/2005/Atom" } ); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local source = microblogdata:get_child("source"); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 if source then |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 for i = 1,#source do |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 feed:add_child(source[i]):up(); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 for i = 1,#microblogdata do |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 if microblogdata[i].name == "source" then |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 table.remove(microblogdata, i); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 break |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 feed:add_child(microblogdata); |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 response.headers.content_type = "application/atom+xml"; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 return "<?xml version='1.0' encoding='utf-8'?>" .. tostring(feed) .. "\n"; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 module:provides("http", { |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 route = { |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 ["GET /*"] = handle_request; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 }; |
4915b8223b07
mod_atom: Expose Microbloging PEP data over HTTP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 }); |