Annotate

mod_swedishchef/mod_swedishchef.lua @ 22:8d49732cf7dc

mod_swedishchef: Initial commit
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 02 Oct 2009 02:04:55 +0200
child 26:1fb5b8c20004
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
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
9 local chef = {
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
10 { th = "t" },
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
11
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
12 { ow = "o"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13 {["([^%w])o"] = "%1oo",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14 O = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
15
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16 {au = "oo",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
17 u = "oo", U = "Oo"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
18 {["([^o])o([^o])"] = "%1u%2"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
19 {ir = "ur",
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
20
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
21 an = "un", An = "Un", Au = "Oo"},
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 {e = "i", E = "I"},
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 { i = function () return select(math.random(2), "i", "ee"); end },
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 {a = "e", A = "E"},
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 {["e([^%w])"] = "e-a%1"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30 {f = "ff"},
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 {v = "f", V = "F"},
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33 {w = "v", W = "V"} };
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 function swedish(english)
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
36 local eng, url = english:match("(.*)(http://.*)$");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
37 if eng then english = eng; end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
39 for _,v in ipairs(chef) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
40 for k,v in pairs(v) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
41 english = english:gsub(k,v);
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
42 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
43 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
44 english = english:gsub("the", "zee");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
45 english = english:gsub("The", "Zee");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
46 english = english:gsub("tion", "shun");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
47 english = english:gsub("[.!?]$", "%1\nBork Bork Bork!");
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
48 return tostring(english..((url and url) or ""));
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
51 function check_message(data)
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52 local origin, stanza = data.origin, data.stanza;
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 local body, bodyindex;
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
55 for k,v in ipairs(stanza) do
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56 if v.name == "body" then
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 body, bodyindex = v, k;
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
60
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
61 if not body then return; end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
62 body = body:get_text();
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 body then
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
65 stanza[bodyindex][1] = swedish(body);
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
66 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
67 end
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
68
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
69 module:hook("message/bare", check_message);
8d49732cf7dc mod_swedishchef: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
70