annotate mod_swedishchef/mod_swedishchef.lua @ 363:72a5778579c5

mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 12 Apr 2011 22:40:26 +0000
parents 1fb5b8c20004
children 93ef813dfd06
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
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
3 --
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 local st = require "util.stanza";
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
8
26
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
9 local trigger_string = config.get(module.host, "core", "swedishchef_trigger");
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
10 trigger_string = (trigger_string and trigger_string .. " ") or "";
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
11
22
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
12 local chef = {
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13 { th = "t" },
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
15 { ow = "o"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16 {["([^%w])o"] = "%1oo",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
17 O = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
18
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
19 {au = "oo",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
20 u = "oo", U = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
21 {["([^o])o([^o])"] = "%1u%2"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
22 {ir = "ur",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
23
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24 an = "un", An = "Un", Au = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
25
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
26 {e = "i", E = "I"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
27
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
28 { i = function () return select(math.random(2), "i", "ee"); end },
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30 {a = "e", A = "E"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
31
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
32 {["e([^%w])"] = "e-a%1"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33 {f = "ff"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
34
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
35 {v = "f", V = "F"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
36 {w = "v", W = "V"} };
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
37
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38 function swedish(english)
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
39 local eng, url = english:match("(.*)(http://.*)$");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
40 if eng then english = eng; end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
41
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
42 for _,v in ipairs(chef) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
43 for k,v in pairs(v) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
44 english = english:gsub(k,v);
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 end
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("The", "Zee");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 english = english:gsub("tion", "shun");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50 english = english:gsub("[.!?]$", "%1\nBork Bork Bork!");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
51 return tostring(english..((url and url) or ""));
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
53
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
54 function check_message(data)
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
55 local origin, stanza = data.origin, data.stanza;
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 local body, bodyindex;
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58 for k,v in ipairs(stanza) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 if v.name == "body" then
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
60 body, bodyindex = v, k;
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
61 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
62 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
63
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64 if not body then return; end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
65 body = body:get_text();
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
66
26
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
67 if body and (body:find(trigger_string, 1, true) == 1) then
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
68 module:log("debug", body:find(trigger_string, 1, true));
1fb5b8c20004 mod_swedishchef: Added configurable trigger
Florian Zeitz <florob@babelmonkeys.de>
parents: 22
diff changeset
69 stanza[bodyindex][1] = swedish(body:gsub("^" .. trigger_string, "", 1));
22
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 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
72
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
73 module:hook("message/bare", check_message);
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
74