Annotate

mod_muc_badge/mod_muc_badge.lua @ 3172:8d2fe3b93c15

mod_muc_badge: Simplify SVG
author Kim Alvefur <zash@zash.se>
date Wed, 04 Jul 2018 23:04:04 +0200
parent 3141:774845606d73
child 3173:cd23446eaff1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- MIT License
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- Copyright (c) 2018 Kim Alvefur
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- Permission is hereby granted, free of charge, to any person obtaining a copy
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- of this software and associated documentation files (the "Software"), to deal
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- in the Software without restriction, including without limitation the rights
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- copies of the Software, and to permit persons to whom the Software is
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 -- furnished to do so, subject to the following conditions:
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 -- The above copyright notice and this permission notice shall be included in
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 -- all copies or substantial portions of the Software.
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 --
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 module:depends"http";
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local jid_prep = require "util.jid".prep;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 -- Support both old and new MUC code
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local mod_muc = module:depends"muc";
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local rooms = rawget(mod_muc, "rooms");
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 function (jid)
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 return rooms[jid];
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- I believe the origins of this template to be in the public domain as per
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 -- https://github.com/badges/shields/blob/master/LICENSE.md
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 local template = module:get_option_string("badge_template", [[
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
38 <?xml version="1.0"?>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
39 <svg xmlns="http://www.w3.org/2000/svg" width="144" height="20">
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
40 <linearGradient id="b" x2="0" y2="100%">
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
41 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
42 <stop offset="1" stop-opacity=".1"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
43 </linearGradient>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
44 <clipPath id="a">
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
45 <rect width="144" height="20" rx="3" fill="#fff"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
46 </clipPath>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
47 <g clip-path="url(#a)">
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
48 <path fill="#555" d="M0 0h69v20H0z"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
49 <path fill="#fe7d37" d="M69 0h75v20H69z"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
50 <path fill="url(#b)" d="M0 0h144v20H0z"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
51 </g>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
52 <g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
53 <text fill="#010101" y="14" x="0" textLength="59">{label}</text>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
54 <text y="13" x="0" textLength="59">{label}</text>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
55 <text fill="#010101" y="14" x="59" textLength="65">{number}</text>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
56 <text y="13" x="59" textLength="65">{number}</text>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
57 </g>
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 </svg>
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 ]]);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 template = assert(require "util.template"(template));
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local label = module:get_option_string("badge_label", "Chatroom");
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 local number = module:get_option_string("badge_count", "%d online");
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 module:provides("http", {
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 route = {
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 ["GET /*"] = function (event, path)
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 local jid = jid_prep(path);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 if not jid then return end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local room = get_room_from_jid(jid);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 if not room then return end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if (room.get_hidden or room.is_hidden)(room) then return end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 local count = 0;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 for _ in pairs(room._occupants) do
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 count = count + 1;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 local response = event.response;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 response.headers.content_type = "image/svg+xml";
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 local svg = [[<?xml version="1.0"?>]] ..
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 tostring(template.apply({
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 label = label;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 number = string.format(number, count);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 }));
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 return svg;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 }
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 });