mod_swedishchef/mod_swedishchef.lua
author Kim Alvefur <zash@zash.se>
Sun, 03 Mar 2024 11:23:40 +0100
changeset 5857 97c9b76867ca
parent 2784 d93a73282a93
permissions -rw-r--r--
mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel) Otherwise the global event handlers accumulate, one added each time logging is reoladed, and each invocation of the signal or event triggers one dump of each created ringbuffer.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2784
d93a73282a93 mod_swedishchef: Fix logging and traceback uncovered by util.format (thanks Kafkei)
Kim Alvefur <zash@zash.se>
parents: 2783
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
2782
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
2783
c53cc1ae4788 mod_swedishchef: Add event so other modules can use this filter
Matthew Wild <mwild1@gmail.com>
parents: 2782
diff changeset
    76
module:hook("swedish_chef/message_filter", check_message);