27 if room then |
27 if room then |
28 local nick = select(3, jid.split(stanza.attr.from)); |
28 local nick = select(3, jid.split(stanza.attr.from)); |
29 local body = stanza:get_child("body"); |
29 local body = stanza:get_child("body"); |
30 local delay = stanza:get_child("delay", xmlns_delay); |
30 local delay = stanza:get_child("delay", xmlns_delay); |
31 local event = { |
31 local event = { |
32 room_jid = room_jid; |
32 room_jid = room_jid; |
33 room = room; |
33 room = room; |
34 sender = room.occupants[nick]; |
34 sender = room.occupants[nick]; |
35 nick = nick; |
35 nick = nick; |
36 body = (body and body:get_text()) or nil; |
36 body = (body and body:get_text()) or nil; |
37 stanza = stanza; |
37 stanza = stanza; |
38 delay = (delay and delay.attr.stamp); |
38 delay = (delay and delay.attr.stamp); |
39 }; |
39 }; |
40 if stanza.name == "message" then |
40 if stanza.name == "message" then |
41 local replied; |
41 local replied; |
42 local r = st.reply(stanza); |
42 local r = st.reply(stanza); |
43 if stanza.attr.type == "groupchat" then |
43 if stanza.attr.type == "groupchat" then |
44 r.attr.type = stanza.attr.type; |
44 r.attr.type = stanza.attr.type; |
65 function bot:join_room(jid, nick) |
65 function bot:join_room(jid, nick) |
66 nick = nick or bot.config.nick or ("bot"..math.random(10000,99999)); |
66 nick = nick or bot.config.nick or ("bot"..math.random(10000,99999)); |
67 local room = setmetatable({ |
67 local room = setmetatable({ |
68 bot = bot, jid = jid, nick = nick, |
68 bot = bot, jid = jid, nick = nick, |
69 occupants = {}, |
69 occupants = {}, |
70 events = events.new() }, room_mt); |
70 events = events.new() |
|
71 }, room_mt); |
71 self.rooms[jid] = room; |
72 self.rooms[jid] = room; |
72 local occupants = room.occupants; |
73 local occupants = room.occupants; |
73 room:hook("presence", function (presence) |
74 room:hook("presence", function (presence) |
74 local nick = presence.nick; |
75 local nick = presence.nick; |
75 if not occupants[nick] and presence.stanza.attr.type ~= "unavailable" then |
76 if not occupants[nick] and presence.stanza.attr.type ~= "unavailable" then |
76 occupants[nick] = { |
77 occupants[nick] = { |
77 nick = nick; |
78 nick = nick; |
78 jid = presence.stanza.attr.from; |
79 jid = presence.stanza.attr.from; |
79 presence = presence.stanza; |
80 presence = presence.stanza; |
80 }; |
81 }; |
81 if nick == room.nick then |
82 if nick == room.nick then |
82 room.bot:event("groupchat/joined", room); |
83 room.bot:event("groupchat/joined", room); |
83 else |
84 else |
84 room:event("occupant-joined", occupants[nick]); |
85 room:event("occupant-joined", occupants[nick]); |
85 end |
86 end |