Mercurial > nix
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0d4d8b432980 |
---|---|
1 require("libs.utils"); | |
2 local verse = require("verse"); | |
3 require("verse.client"); | |
4 | |
5 local config = config_load(); | |
6 room_map = { | |
7 [config.muc1.jid] = config.muc2.jid; | |
8 [config.muc2.jid] = config.muc1.jid; | |
9 }; | |
10 local bot = verse.new(); | |
11 local occupants = { | |
12 [config.muc1.jid] = {}; | |
13 [config.muc2.jid] = {}; | |
14 }; | |
15 | |
16 function new_clone(table, room, nick, presence) | |
17 table[room][nick] = verse.new(); | |
18 table[room][nick]:connect_client(config.jid.."/"..nick, config.password); | |
19 table[room][nick]:hook("ready", function() | |
20 table[room][nick]:send(presence); | |
21 end); | |
22 end; | |
23 | |
24 function stanza_split(stanza) | |
25 return jid.bare(stanza.attr.from), | |
26 room_map[jid.bare(stanza.attr.from)], | |
27 select(3, jid.split(stanza.attr.from)); | |
28 end | |
29 | |
30 bot:hook("ready", function() | |
31 room_join(bot, config.muc1, config.nickname); | |
32 room_join(bot, config.muc2, config.nickname); | |
33 end); | |
34 | |
35 bot:hook("stanza", function(stanza) | |
36 local room_from, room_to, nick = stanza_split(stanza); | |
37 if not room_from or not room_to or not nick or nick == config.nickname then return; end; | |
38 if occupants[room_from][nick] then return; end; | |
39 if stanza.name == "message" and stanza.attr.type ~= "groupchat" then return; end; | |
40 | |
41 local reply = stanza; reply.attr.from = nil; | |
42 reply.attr.to = room_to; | |
43 if stanza.name == "presence" then | |
44 reply.attr.to = room_to.."/"..nick; | |
45 if not occupants[room_to][nick] then new_clone(occupants, room_to, nick, reply); end; | |
46 if stanza.attr.type == "unavailable" then | |
47 occupants[room_to][nick]:send(reply); | |
48 occupants[room_to][nick]:close(); | |
49 occupants[room_to][nick] = nil; | |
50 occupants[room_from][nick] = nil; | |
51 return; | |
52 end | |
53 end | |
54 if occupants[room_to][nick] then occupants[room_to][nick]:send(reply); end; | |
55 end); | |
56 | |
57 bot:connect_client(config.jid, config.password); | |
58 verse.loop(); |