annotate tiny-nix.lua @ 0:0d4d8b432980 default tip master

Initial commit.
author Kooda <kooda@upyum.com>
date Fri, 27 Aug 2010 17:00:32 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1 require("libs.utils");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2 local verse = require("verse");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3 require("verse.client");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
4
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
5 local config = config_load();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
6 room_map = {
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
7 [config.muc1.jid] = config.muc2.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
8 [config.muc2.jid] = config.muc1.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
9 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
10 local bot = verse.new();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
11 local occupants = {
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
12 [config.muc1.jid] = {};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
13 [config.muc2.jid] = {};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
14 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
15
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
16 function new_clone(table, room, nick, presence)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
17 table[room][nick] = verse.new();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
18 table[room][nick]:connect_client(config.jid.."/"..nick, config.password);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
19 table[room][nick]:hook("ready", function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
20 table[room][nick]:send(presence);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
21 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
22 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
23
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
24 function stanza_split(stanza)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
25 return jid.bare(stanza.attr.from),
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
26 room_map[jid.bare(stanza.attr.from)],
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
27 select(3, jid.split(stanza.attr.from));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
28 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
29
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
30 bot:hook("ready", function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
31 room_join(bot, config.muc1, config.nickname);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
32 room_join(bot, config.muc2, config.nickname);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
33 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
34
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
35 bot:hook("stanza", function(stanza)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
36 local room_from, room_to, nick = stanza_split(stanza);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
37 if not room_from or not room_to or not nick or nick == config.nickname then return; end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
38 if occupants[room_from][nick] then return; end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
39 if stanza.name == "message" and stanza.attr.type ~= "groupchat" then return; end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
40
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
41 local reply = stanza; reply.attr.from = nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
42 reply.attr.to = room_to;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
43 if stanza.name == "presence" then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
44 reply.attr.to = room_to.."/"..nick;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
45 if not occupants[room_to][nick] then new_clone(occupants, room_to, nick, reply); end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
46 if stanza.attr.type == "unavailable" then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
47 occupants[room_to][nick]:send(reply);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
48 occupants[room_to][nick]:close();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
49 occupants[room_to][nick] = nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
50 occupants[room_from][nick] = nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
51 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
52 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
53 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
54 if occupants[room_to][nick] then occupants[room_to][nick]:send(reply); end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
55 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
56
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
57 bot:connect_client(config.jid, config.password);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
58 verse.loop();