Thu, 20 May 2010 14:32:21 +0100
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
0 | 1 | local events = require "events"; |
2 | local st = require "util.stanza"; | |
3 | ||
4 | local room_mt = {}; | |
5 | room_mt.__index = room_mt; | |
6 | ||
7 | local xmlns_delay = "urn:xmpp:delay"; | |
8 | ||
9 | function riddim.plugins.groupchat(bot) | |
10 | bot.rooms = {}; | |
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
11 | |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
12 | bot:hook("started", function () |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
13 | for k, v in pairs(bot.config.autojoin or {}) do |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
14 | if type(k) == "number" then |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
15 | bot:join_room(v); |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
16 | elseif type(k) == "string" then |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
17 | if type(v) == "string" then |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
18 | bot:join_room(k, v); |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
19 | end |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
20 | end |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
21 | end |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
22 | end); |
0 | 23 | |
24 | bot.stream:hook("stanza", function (stanza) | |
25 | local room_jid = jid.bare(stanza.attr.from); | |
26 | local room = bot.rooms[room_jid] | |
27 | if room then | |
28 | local nick = select(3, jid.split(stanza.attr.from)); | |
29 | local body = stanza:get_child("body"); | |
30 | local delay = stanza:get_child("delay", xmlns_delay); | |
31 | local event = { | |
32 | room_jid = room_jid; | |
33 | room = room; | |
34 | sender = room.occupants[nick]; | |
35 | nick = nick; | |
36 | body = (body and body:get_text()) or nil; | |
37 | stanza = stanza; | |
38 | delay = (delay and delay.attr.stamp); | |
39 | }; | |
40 | if stanza.name == "message" then | |
41 | local replied; | |
42 | local r = st.reply(stanza); | |
43 | if stanza.attr.type == "groupchat" then | |
44 | r.attr.type = stanza.attr.type; | |
45 | r.attr.to = jid.bare(stanza.attr.to); | |
46 | end | |
47 | function event:reply(reply) | |
48 | if not reply then reply = "Nothing to say to you"; end | |
49 | if replied then return false; end | |
50 | replied = true; | |
51 | if r.attr.type == "groupchat" then | |
52 | reply = event.sender.nick..": "..reply; | |
53 | end | |
54 | room:send(r:tag("body"):text(reply)); | |
55 | end | |
56 | end | |
57 | local ret; | |
58 | if stanza.name ~= "message" or nick ~= room.nick then | |
59 | ret = room:event(stanza.name, event); | |
60 | end | |
61 | return ret or (stanza.name == "message") or nil; | |
62 | end | |
63 | end, 500); | |
64 | ||
65 | function bot:join_room(jid, nick) | |
66 | nick = nick or bot.config.nick or ("bot"..math.random(10000,99999)); | |
67 | local room = setmetatable({ | |
68 | bot = bot, jid = jid, nick = nick, | |
69 | occupants = {}, | |
70 | events = events.new() }, room_mt); | |
71 | self.rooms[jid] = room; | |
72 | local occupants = room.occupants; | |
73 | room:hook("presence", function (presence) | |
74 | local nick = presence.nick; | |
9
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
75 | if not occupants[nick] and presence.stanza.attr.type ~= "unavailable" then |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
76 | occupants[nick] = { |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
77 | nick = nick; |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
78 | jid = presence.stanza.attr.from; |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
79 | presence = presence.stanza; |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
80 | }; |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
81 | if nick == room.nick then |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
82 | room.bot:event("groupchat/joined", room); |
0 | 83 | else |
9
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
84 | room:event("occupant-joined", occupants[nick]); |
0 | 85 | end |
9
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
86 | elseif occupants[nick] and presence.stanza.attr.type == "unavailable" then |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
87 | occupants[nick].presence = presence.stanza; |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
88 | room:event("occupant-left", occupants[nick]); |
8c3bec93087b
groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents:
0
diff
changeset
|
89 | occupants[nick] = nil; |
0 | 90 | end |
91 | end); | |
92 | self:send(st.presence({to = jid.."/"..nick})); | |
93 | self:event("groupchat/joining", room); | |
94 | return room; | |
95 | end | |
96 | end | |
97 | ||
98 | function room_mt:send(stanza) | |
99 | if stanza.name == "message" and not stanza.attr.type then | |
100 | stanza.attr.type = "groupchat"; | |
101 | end | |
102 | if stanza.attr.type == "groupchat" then | |
103 | stanza.attr.to = self.jid; | |
104 | end | |
105 | self.bot:send(stanza); | |
106 | end | |
107 | ||
108 | function room_mt:send_message(text) | |
109 | self:send(st.message():tag("body"):text(text)); | |
110 | end | |
111 | ||
112 | function room_mt:leave(message) | |
113 | self.bot:event("groupchat/leaving", room); | |
114 | self:send(st.presence({type="unavailable"})); | |
115 | self.bot:event("groupchat/left", room); | |
116 | end | |
117 | ||
118 | function room_mt:event(name, arg) | |
119 | self.bot.stream:debug("Firing room event: %s", name); | |
120 | return self.events.fire_event(name, arg); | |
121 | end | |
122 | ||
123 | function room_mt:hook(name, callback, priority) | |
124 | return self.events.add_handler(name, callback, priority); | |
125 | end |