annotate mod_muc_dicebot/mod_muc_dicebot.lua @ 4579:b305814bd930

mod_muc_dicebot: A thing to roll dice Do you see what happens, Jitsi? Do you see what happens when you make it hard for me to use a proper bot? This is what happens, Jitsi. This is what happens when you meet a stranger in the alps! Ahem. In all seriousness, this is more of a quick hack than anything else. It will look for `.r` in MUC messages and if it finds it, it'll interpret it as an instruction to roll a few dice. Injects the results in the body of the message. Eats the message alive if it is malformed.
author Jonas Schäfer <jonas@wielicki.name>
date Sat, 29 May 2021 15:17:05 +0200
parents
children 785389a2d2b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4579
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
1 local muc = module:depends("muc");
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
2 local rand = require"util.random";
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
3
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
4 local s_match = string.match;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
5 local s_gmatch = string.gmatch;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
6 local t_insert = table.insert;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
7 local t_concat = table.concat;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
8
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
9 local rooms = module:get_option_set("muc_dicebot_rooms", nil);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
10 local xmlns_nick = "http://jabber.org/protocol/nick";
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
11
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
12 local function is_room_affected(roomjid)
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
13 return not rooms or rooms:contains(roomjid)
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
14 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
15
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
16 local function roll(sides)
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
17 if sides > 256 then
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
18 return nil, "too many sides"
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
19 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
20 local factor = math.floor(256 / sides);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
21 local cutoff = sides * factor;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
22 module:log("error", "%d -> %d %d %d", sides, max, factor, cutoff);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
23 for i=1,10 do
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
24 local randomness = string.byte(rand.bytes(1), 1);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
25 module:log("error", "%d", randomness);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
26 if randomness < cutoff then
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
27 return (randomness % sides) + 1
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
28 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
29 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
30 return nil, "failed to find valid number"
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
31 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
32
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
33 local function muc_broadcast_message(event)
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
34 if not is_room_affected(event.room.jid) then
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
35 return
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
36 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
37
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
38 local stanza = event.stanza;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
39 local body = stanza:get_child("body");
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
40 local text = body:get_text();
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
41 module:log("error", "%q %q %q", stanza, body, text);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
42 local dice = s_match(text, "^[%.!]r%s(.+)$");
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
43 if not dice or dice == "" then
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
44 return
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
45 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
46
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
47 local results = {};
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
48 local count = 0;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
49 local sum = 0;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
50 for ndice, sep, sides in s_gmatch(dice, "(%d*)([wd]?)(%d+)") do
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
51 if not sep or sep == "" then
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
52 sides = ndice .. sides
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
53 ndice = "1"
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
54 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
55 local ndice = tonumber(ndice);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
56 count = count + ndice;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
57 if count > 100 then
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
58 return true
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
59 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
60 local sides = tonumber(sides);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
61 for i=1,ndice do
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
62 local value = roll(sides);
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
63 t_insert(results, tostring(value));
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
64 sum = sum + value;
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
65 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
66 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
67 body:text("\n⇒ "..t_concat(results, " ").." (sum: "..sum..")");
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
68 end
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
69
b305814bd930 mod_muc_dicebot: A thing to roll dice
Jonas Schäfer <jonas@wielicki.name>
parents:
diff changeset
70 module:hook("muc-broadcast-message", muc_broadcast_message);