annotate mod_swedishchef/mod_swedishchef.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 d93a73282a93
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
1 -- Copyright (C) 2009 Florian Zeitz
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
2 -- Copyright (C) 2009 Matthew Wild
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
3 --
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
6 --
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
7
985
93ef813dfd06 mod_swedishchef: Use newer config API
Kim Alvefur <zash@zash.se>
parents: 26
diff changeset
8 local trigger_string = module:get_option_string("swedishchef_trigger");
26
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
9 trigger_string = (trigger_string and trigger_string .. " ") or "";
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
10
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
11 local chef = {
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
12 { th = "t" },
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14 { ow = "o"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
15 {["([^%w])o"] = "%1oo",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16 O = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
17
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
18 {au = "oo",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
19 u = "oo", U = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
20 {["([^o])o([^o])"] = "%1u%2"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
21 {ir = "ur",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
23 an = "un", An = "Un", Au = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
25 {e = "i", E = "I"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
26
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
27 { i = function () return select(math.random(2), "i", "ee"); end },
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
28
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29 {a = "e", A = "E"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
31 {["e([^%w])"] = "e-a%1"},
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
32 {f = "ff"},
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
34 {v = "f", V = "F"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
35 {w = "v", W = "V"} };
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
36
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
37 function swedish(english)
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38 local eng, url = english:match("(.*)(http://.*)$");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
39 if eng then english = eng; end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
40
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
41 for _,v in ipairs(chef) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
42 for k,v in pairs(v) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
43 english = english:gsub(k,v);
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
44 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
45 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
46 english = english:gsub("the", "zee");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
47 english = english:gsub("The", "Zee");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
48 english = english:gsub("tion", "shun");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 english = english:gsub("[.!?]$", "%1\nBork Bork Bork!");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50 return tostring(english..((url and url) or ""));
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
51 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
53 function check_message(data)
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
54 local origin, stanza = data.origin, data.stanza;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
55
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56 local body, bodyindex;
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 for k,v in ipairs(stanza) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58 if v.name == "body" then
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 body, bodyindex = v, k;
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
60 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
61 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
62
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
63 if not body then return; end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64 body = body:get_text();
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 985
diff changeset
65
26
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
66 if body and (body:find(trigger_string, 1, true) == 1) then
2780
d93a73282a93 mod_swedishchef: Fix logging and traceback uncovered by util.format (thanks Kafkei)
Kim Alvefur <zash@zash.se>
parents: 2779
diff changeset
67 module:log("debug", "Found trigger: %s", body:match(trigger_string, 1, true));
26
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
68 stanza[bodyindex][1] = swedish(body:gsub("^" .. trigger_string, "", 1));
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
69 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
70 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
71
2778
2615facc461b mod_swedishchef: Add configuration toggle
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
72 if module:get_option_boolean("swedish_chef_enabled", true) then
2615facc461b mod_swedishchef: Add configuration toggle
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
73 module:hook("message/bare", check_message);
2615facc461b mod_swedishchef: Add configuration toggle
Matthew Wild <mwild1@gmail.com>
parents: 1343
diff changeset
74 end
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
75
2779
c53cc1ae4788 mod_swedishchef: Add event so other modules can use this filter
Matthew Wild <mwild1@gmail.com>
parents: 2778
diff changeset
76 module:hook("swedish_chef/message_filter", check_message);