annotate mod_muc_badge/mod_muc_badge.lua @ 5229:c24a622a7b85

mod_http_oauth2: Fix appending of query parts in error redirects Looks like this meant to check whether the redirect_uri has a ?query part, but forgot to inspect the field for this in the returned table.
author Kim Alvefur <zash@zash.se>
date Fri, 10 Mar 2023 11:54:30 +0100
parents 8a4b17e2e984
children
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;
4982
8a4b17e2e984 mod_muc_badge: If room has no title use localpart of room's jid (fixes #1767)
Citronalco <geierb@geierb.de>
parents: 3286
diff changeset
26 local jid_split = require "util.jid".split;
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 -- 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
29 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
30 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
31 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
32 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
33 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
34 end
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 -- 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
37 -- 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
38 local template = module:get_option_string("badge_template", [[
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
39 <?xml version="1.0"?>
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
40 <svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="20">
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
41 <linearGradient id="b" x2="0" y2="100%">
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
42 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
43 <stop offset="1" stop-opacity=".1"/>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
44 </linearGradient>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
45 <clipPath id="a">
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
46 <rect width="{width}" height="20" rx="3" fill="#fff"/>
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
47 </clipPath>
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
48 <g clip-path="url(#a)">
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
49 <path fill="#555" d="M0 0h{labelwidth}v20H0z"/>
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
50 <path fill="#fe7d37" d="M{labelwidth} 0h{countwidth}v20H{labelwidth}z"/>
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
51 <path fill="url(#b)" d="M0 0h{width}v20H0z"/>
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
52 </g>
3284
29a4d8b63fb1 mod_muc_badge: Preserve spaces in SVG, fixes displaying nicer borders.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3173
diff changeset
53 <g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11" xml:space="preserve">
3286
4d1f6d47c889 mod_muc_badge: Reduce the opacity of the text shadows.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3285
diff changeset
54 <text fill="#010101" fill-opacity=".3" y="14" x="0" textLength="{labelwidth}">{label}</text>
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
55 <text y="13" x="0" textLength="{labelwidth}">{label}</text>
3286
4d1f6d47c889 mod_muc_badge: Reduce the opacity of the text shadows.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3285
diff changeset
56 <text fill="#010101" fill-opacity=".3" y="14" x="{labelwidth}" textLength="{countwidth}">{number}</text>
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
57 <text y="13" x="{labelwidth}" textLength="{countwidth}">{number}</text>
3172
8d2fe3b93c15 mod_muc_badge: Simplify SVG
Kim Alvefur <zash@zash.se>
parents: 3141
diff changeset
58 </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
59 </svg>
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 ]]);
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 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
62
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");
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
64 local charwidth = 7;
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 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
67 route = {
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 ["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
69 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
70 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
71
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 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
73 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
74 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
75
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 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
77 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
78 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
79 end
4982
8a4b17e2e984 mod_muc_badge: If room has no title use localpart of room's jid (fixes #1767)
Citronalco <geierb@geierb.de>
parents: 3286
diff changeset
80 local badge_label = (" %s "):format(room:get_name() or jid_split(jid));
3285
38892f9f3fe9 mod_muc_badge: Add spaces around each part of the text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3284
diff changeset
81 local badge_count = (" %s "):format(string.format(number, count));
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 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
84 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
85 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
86 tostring(template.apply({
3173
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
87 label = badge_label;
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
88 number = badge_count;
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
89 width = ("%d"):format( (#badge_label + #badge_count) * charwidth );
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
90 labelwidth = ("%d"):format( #badge_label * charwidth );
cd23446eaff1 mod_muc_badge: Use room name instead of fixed string (beware the silly width calculations)
Kim Alvefur <zash@zash.se>
parents: 3172
diff changeset
91 countwidth = ("%d"):format( #badge_count * charwidth );
3141
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 }));
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 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
94 end;
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 }
774845606d73 mod_muc_badge: Generates a SVG badge with the number of occupants in rooms
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 });