Annotate

mod_http_muc_log/mod_http_muc_log.lua @ 1576:91b91052e0e8

mod_http_muc_log: Send a HTML mime type with responses
author Kim Alvefur <zash@zash.se>
date Thu, 20 Nov 2014 19:50:43 +0100
parent 1575:464ed6bc5a73
child 1577:0a6974f2cb55
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require "util.stanza";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local datetime = require"util.datetime";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local jid_split = require"util.jid".split;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local nodeprep = require"util.encodings".stringprep.nodeprep;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local uuid = require"util.uuid".generate;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local it = require"util.iterators";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local gettime = require"socket".gettime;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
1571
eed7db9f3157 mod_mam_muc, mod_http_muc_log: Change store name from 'archive2' to 'muc_log' to distinguish it from personal MAM archives. Old data will require migration.
Kim Alvefur <zash@zash.se>
parents: 1564
diff changeset
9 local archive = module:open_store("muc_log", "archive");
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 -- Support both old and new MUC code
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local mod_muc = module:depends"muc";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local rooms = rawget(mod_muc, "rooms");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
1554
8059b7cdaf17 mod_http_muc_log: Make MUC local code identical to mod_mam_muc
Kim Alvefur <zash@zash.se>
parents: 1553
diff changeset
15 local new_muc = not rooms;
8059b7cdaf17 mod_http_muc_log: Make MUC local code identical to mod_mam_muc
Kim Alvefur <zash@zash.se>
parents: 1553
diff changeset
16 if new_muc then
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 rooms = module:shared"muc/rooms";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 function (jid)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return rooms[jid];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local function get_room(name)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local jid = name .. '@' .. module.host;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 return get_room_from_jid(jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 module:depends"http";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local function template(data)
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
32 --[[ DOC
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 Like util.template, but deals with plain text
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 Returns a closure that is called with a table of values
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 {name} is substituted for values["name"] and is XML escaped
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 {name!} is substituted without XML escaping
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 {name?} is optional and is replaced with an empty string if no value exists
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 ]]
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 return function(values)
1560
6c6c1fab4ee5 mod_http_muc_log: Remove unnessesary character from pattern
Kim Alvefur <zash@zash.se>
parents: 1559
diff changeset
40 return (data:gsub("{([^}]-)(%p?)}", function (name, opt)
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local value = values[name];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if value then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 if opt ~= "!" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return st.xml_escape(value);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 return value;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 elseif opt == "?" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return "";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end));
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
54 -- TODO Move templates into files
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
55 local base = template(template[[
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 <!DOCTYPE html>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
57 <html>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
58 <head>
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 <meta charset="utf-8">
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
60 <meta name="viewport" content="width=device-width, initial-scale=1">
1559
96f11a1c8b37 mod_http_muc_log: Include the canonical URL in a meta tag
Kim Alvefur <zash@zash.se>
parents: 1558
diff changeset
61 <link rel="canonical" href="{canonical}">
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 <title>{title}</title>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 <style>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
64 body{background-color:#eeeeec;margin:1ex 0;padding-bottom:3em;font-family:Arial,Helvetica,sans-serif;}
1557
876af57a3983 mod_http_muc_log: Fix header margin
Kim Alvefur <zash@zash.se>
parents: 1556
diff changeset
65 header,footer{margin:1ex 1em;}
876af57a3983 mod_http_muc_log: Fix header margin
Kim Alvefur <zash@zash.se>
parents: 1556
diff changeset
66 footer{font-size:smaller;color:#babdb6;}
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
67 .content{background-color:white;padding:1em;list-style-position:inside;}
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
68 nav{font-size:large;margin:1ex 1ex;clear:both;line-height:1.5em;}
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
69 nav a{padding: 1ex;text-decoration:none;}
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
70 nav a.up{font-size:smaller;}
1561
2eaf4833969a mod_http_muc_log: Render arrows in prev/next links using CSS
Kim Alvefur <zash@zash.se>
parents: 1560
diff changeset
71 nav a.prev{float:left;}
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
72 nav a.next{float:right;}
1561
2eaf4833969a mod_http_muc_log: Render arrows in prev/next links using CSS
Kim Alvefur <zash@zash.se>
parents: 1560
diff changeset
73 nav a.next::after{content:" →";}
2eaf4833969a mod_http_muc_log: Render arrows in prev/next links using CSS
Kim Alvefur <zash@zash.se>
parents: 1560
diff changeset
74 nav a.prev::before{content:"← ";}
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
75 nav a:empty::after,nav a:empty::before{content:""}
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
76 @media screen and (min-width: 460px) {
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
77 nav{font-size:x-large;margin:1ex 1em;}
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
78 }
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
79 a:link,a:visited{color:#2e3436;text-decoration:none;}
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
80 a:link:hover,a:visited:hover{color:#3465a4;}
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
81 ul,ol{padding:0;}
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
82 li{list-style:none;}
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
83 hr{visibility:hidden;clear:both;}
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
84 br{clear:both;}
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
85 li time{float:right;font-size:small;opacity:0.2;}
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
86 li:hover time{opacity:1;}
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
87 .room-list .description{font-size:smaller;}
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
88 q.body::before,q.body::after{content:"";}
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
89 .presence .verb{font-style:normal;color:#30c030;}
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
90 .presence.unavailable .verb{color:#c03030;}
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 </style>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
92 </head>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
93 <body>
1557
876af57a3983 mod_http_muc_log: Fix header margin
Kim Alvefur <zash@zash.se>
parents: 1556
diff changeset
94 <header>
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 <h1>{title}</h1>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
96 {header!}
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
97 </header>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
98 <hr>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
99 <div class="content">
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 {body!}
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
101 </div>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
102 <hr>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
103 <footer>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
104 {footer!}
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
105 <br>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
106 <div class="powered-by">Prosody {prosody_version?}</div>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
107 </footer>
1558
4e5d307c96d2 mod_http_muc_log: Fix closing tag
Kim Alvefur <zash@zash.se>
parents: 1557
diff changeset
108 </body>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
109 </html>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
110 ]] { prosody_version = prosody.version });
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 local dates_template = template(base{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 title = "Logs for room {room}";
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
114 header = [[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
115 <nav>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
116 <a href=".." class="up">Back to room list</a>
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
117 </nav>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
118 ]];
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
119 body = [[
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
120 <nav>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
121 <ul class="dates">
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
122 {lines!}</ul>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
123 </nav>
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
124 ]];
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
125 footer = "";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 })
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 local date_line_template = template[[
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 <li><a href="{date}">{date}</a></li>
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 ]];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 local page_template = template(base{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 title = "Logs for room {room} on {date}";
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
134 header = [[
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
135 <nav>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
136 <a class="up" href=".">Back to date list</a>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
137 <br>
1561
2eaf4833969a mod_http_muc_log: Render arrows in prev/next links using CSS
Kim Alvefur <zash@zash.se>
parents: 1560
diff changeset
138 <a class="prev" href="{prev}">{prev}</a>
2eaf4833969a mod_http_muc_log: Render arrows in prev/next links using CSS
Kim Alvefur <zash@zash.se>
parents: 1560
diff changeset
139 <a class="next" href="{next}">{next}</a>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
140 </nav>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
141 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 body = [[
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
143 <ol class="chat-logs">
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
144 {logs!}</ol>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
145 ]];
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
146 footer = [[
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
147 <nav>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
148 <div>
1561
2eaf4833969a mod_http_muc_log: Render arrows in prev/next links using CSS
Kim Alvefur <zash@zash.se>
parents: 1560
diff changeset
149 <a class="prev" href="{prev}">{prev}</a>
2eaf4833969a mod_http_muc_log: Render arrows in prev/next links using CSS
Kim Alvefur <zash@zash.se>
parents: 1560
diff changeset
150 <a class="next" href="{next}">{next}</a>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
151 </div>
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
152 </nav>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
153 <script>
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
154 /*
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
155 * Local timestamps
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
156 */
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
157 (function () {
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
158 var timeTags = document.getElementsByTagName("time");
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
159 var i = 0, tag, date;
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
160 while(timeTags[i]) {
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
161 tag = timeTags[i++];
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
162 if(date = tag.getAttribute("datetime")) {
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
163 date = new Date(date);
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
164 tag.textContent = date.toLocaleTimeString();
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
165 tag.setAttribute("title", date.toString());
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
166 }
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
167 }
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
168 })();
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
169 </script>
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
170 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
173 local line_template = template[[
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
174 <li class="{st_name} {st_type?}" id="{key}">
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
175 <span class="time">
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
176 <a href="#{key}"><time datetime="{datetime}">{time}</time></a>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
177 </span>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
178 <b class="nick">{nick}</b>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
179 <em class="verb">{verb?}</em>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
180 <q class="body">{body?}</q>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
181 </li>
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
182 ]];
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 local room_list_template = template(base{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 title = "Rooms on {host}";
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
186 header = "";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 body = [[
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
188 <nav>
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
189 <dl class="room-list">
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
190 {rooms!}
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
191 </dl>
1563
49ba05d75119 mod_http_muc_log: Style fixes and mobile optimizations
Kim Alvefur <zash@zash.se>
parents: 1562
diff changeset
192 </nav>
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
193 ]];
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
194 footer = "";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 local room_item_template = template[[
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
198 <dt class="name"><a href="{room}/">{name}</a></dt>
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
199 <dd class="description">{description?}</dd>
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 ]];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 local function public_room(room)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 if type(room) == "string" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 room = get_room(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 end
1575
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
206 return (room
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
207 and not (room.get_hidden or room.is_hidden)(room)
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
208 and not (room.get_members_only or room.is_members_only)(room)
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
209 and room._data.logging ~= false);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 -- FIXME Invent some more efficient API for this
1552
81b3599c02fb mod_http_muc_log: Have URL of date list page end with a slash
Kim Alvefur <zash@zash.se>
parents: 1550
diff changeset
213 local function dates_page(event, path)
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 local request, response = event.request, event.response;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215
1552
81b3599c02fb mod_http_muc_log: Have URL of date list page end with a slash
Kim Alvefur <zash@zash.se>
parents: 1550
diff changeset
216 local room = nodeprep(path:match("^(.*)/$"));
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 if not room or not public_room(room) then return end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 local dates, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 module:log("debug", "Find all dates with messages");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 local next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 repeat
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 local iter = archive:find(room, {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 ["start"] = next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 limit = 1;
1553
1398d2bbcd42 mod_http_muc_log: List only dates with messages
Kim Alvefur <zash@zash.se>
parents: 1552
diff changeset
226 with = "message<groupchat";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 })
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 if not iter then break end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 next_day = nil;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 for key, message, when in iter do
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 next_day = datetime.date(when);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 dates[i], i = date_line_template{
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 date = next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 }, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
235 next_day = datetime.parse(next_day .. "T23:59:59Z") + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236 break;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 until not next_day;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
240 response.headers.content_type = "text/html";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 return dates_template{
1559
96f11a1c8b37 mod_http_muc_log: Include the canonical URL in a meta tag
Kim Alvefur <zash@zash.se>
parents: 1558
diff changeset
242 host = module.host;
96f11a1c8b37 mod_http_muc_log: Include the canonical URL in a meta tag
Kim Alvefur <zash@zash.se>
parents: 1558
diff changeset
243 canonical = module:http_url() .. "/" .. path;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 room = room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 lines = table.concat(dates);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 local function logs_page(event, path)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 local request, response = event.request, event.response;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 local room, date = path:match("^(.-)/(%d%d%d%d%-%d%d%-%d%d)$");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 room = nodeprep(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 if not room then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 return dates_page(event, path);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 if not public_room(room) then return end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 local logs, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 local iter, err = archive:find(room, {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 ["start"] = datetime.parse(date.."T00:00:00Z");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 ["end"] = datetime.parse(date.."T23:59:59Z");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 -- with = "message<groupchat";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 if not iter then return 500; end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
267 local verb, subject, body;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
268 for key, item, when in iter do
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
269 body = item:get_child_text("body");
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
270 subject = item:get_child_text("subject");
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
271 verb = nil;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
272 if subject then
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
273 verb = "set the topic to";
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
274 elseif body and body:sub(1,4) == "/me " then
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
275 verb, body = body:sub(5), nil;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
276 elseif item.name == "presence" then
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
277 verb = item.attr.type == "unavailable" and "has left" or "has joined";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278 end
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
279 logs[i], i = line_template {
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
280 key = key;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
281 datetime = datetime.datetime(when);
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
282 time = datetime.time(when);
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
283 verb = verb;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
284 body = subject or body;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
285 nick = select(3, jid_split(item.attr.from));
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
286 st_name = item.name;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
287 st_type = item.attr.type;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
288 }, i + 1;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
291 local next_when = "";
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
292 local prev_when = "";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 module:log("debug", "Find next date with messages");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 for key, message, when in archive:find(room, {
1556
0b80a02c2e3d mod_http_muc_log: Search for next date with messages from 00:00:00, not 00:00:01
Kim Alvefur <zash@zash.se>
parents: 1555
diff changeset
296 ["start"] = datetime.parse(date.."T00:00:00Z") + 86400;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 }) do
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
298 next_when = datetime.date(when);
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
299 module:log("debug", "Next message: %s", datetime.datetime(when));
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
300 break;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
301 end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
303 module:log("debug", "Find prev date with messages");
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
304 for key, message, when in archive:find(room, {
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
305 ["end"] = datetime.parse(date.."T00:00:00Z") - 1;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
306 reverse = true;
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
307 }) do
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
308 prev_when = datetime.date(when);
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
309 module:log("debug", "Previous message: %s", datetime.datetime(when));
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
310 break;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
313 response.headers.content_type = "text/html";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
314 return page_template{
1559
96f11a1c8b37 mod_http_muc_log: Include the canonical URL in a meta tag
Kim Alvefur <zash@zash.se>
parents: 1558
diff changeset
315 canonical = module:http_url() .. "/" .. path;
96f11a1c8b37 mod_http_muc_log: Include the canonical URL in a meta tag
Kim Alvefur <zash@zash.se>
parents: 1558
diff changeset
316 host = module.host;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
317 room = room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 date = date;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
319 logs = table.concat(logs);
1562
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
320 next = next_when;
bc9cfd1c5391 mod_http_muc_log: Hide prev/next when on the edge of the archive
Kim Alvefur <zash@zash.se>
parents: 1561
diff changeset
321 prev = prev_when;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
322 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
323 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
324
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
325 local function list_rooms(event)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
326 local room_list, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
327 for room in each_room() do
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
328 if public_room(room) then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329 room_list[i], i = room_item_template {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 room = jid_split(room.jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 name = room:get_name();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 description = room:get_description();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 }, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 end
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
336
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
337 event.response.headers.content_type = "text/html";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 return room_list_template {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 host = module.host;
1559
96f11a1c8b37 mod_http_muc_log: Include the canonical URL in a meta tag
Kim Alvefur <zash@zash.se>
parents: 1558
diff changeset
340 canonical = module:http_url() .. "/";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 rooms = table.concat(room_list);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 local cache = setmetatable({}, {__mode = 'v'});
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 local function with_cache(f)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 return function (event, path)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349 local request, response = event.request, event.response;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 local ckey = path or "";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 local cached = cache[ckey];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 if cached then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 local etag = cached.etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 local if_none_match = request.headers.if_none_match;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 if etag == if_none_match then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 module:log("debug", "Client cache hit");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 return 304;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 module:log("debug", "Server cache hit");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 response.headers.etag = etag;
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
362 response.headers.content_type = "text/html";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 return cached[1];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
366 local start = gettime();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
367 local render = f(event, path);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
368 module:log("debug", "Rendering took %dms", math.floor( (gettime() - start) * 1000 + 0.5));
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 if type(render) == "string" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 local etag = uuid();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 cached = { render, etag = etag, date = datetime.date() };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
373 response.headers.etag = etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 cache[ckey] = cached;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
377 response.headers.content_type = "text/html";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378 return render;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
379 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
380 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
381
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
382 -- How is cache invalidation a hard problem? ;)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 module:hook("muc-broadcast-message", function (event)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 local room = event.room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 local room_name = jid_split(room.jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 local today = datetime.date();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 cache[ room_name .. "/" .. today ] = nil;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 if cache[room_name] and cache[room_name].date ~= today then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 cache[room_name] = nil;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 end);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 module:log("info", module:http_url());
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 module:provides("http", {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 route = {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396 ["GET /"] = list_rooms;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 ["GET /*"] = with_cache(logs_page);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 };
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 });
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400