Software /
code /
prosody
Comparison
plugins/muc/muc.lib.lua @ 2880:a3f6cc3417f2
MUC: Added support for letting clients manage discussion history.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 17 Mar 2010 03:16:43 +0500 |
parent | 2658:a4879b1e6cde |
child | 2926:fc96d439453b |
comparison
equal
deleted
inserted
replaced
2879:94299e640ed0 | 2880:a3f6cc3417f2 |
---|---|
120 stanza.attr.to = to; | 120 stanza.attr.to = to; |
121 if historic then -- add to history | 121 if historic then -- add to history |
122 local history = self._data['history']; | 122 local history = self._data['history']; |
123 if not history then history = {}; self._data['history'] = history; end | 123 if not history then history = {}; self._data['history'] = history; end |
124 stanza = st.clone(stanza); | 124 stanza = st.clone(stanza); |
125 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203 | 125 stanza.attr.to = ""; |
126 local stamp = datetime.datetime(); | |
127 local chars = #tostring(stanza); | |
128 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = stamp}):up(); -- XEP-0203 | |
126 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated) | 129 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated) |
127 t_insert(history, st.preserialize(stanza)); | 130 local entry = { stanza = stanza, stamp = stamp }; |
131 t_insert(history, entry); | |
128 while #history > history_length do t_remove(history, 1) end | 132 while #history > history_length do t_remove(history, 1) end |
129 end | 133 end |
130 end | 134 end |
131 function room_mt:broadcast_except_nick(stanza, nick) | 135 function room_mt:broadcast_except_nick(stanza, nick) |
132 for rnick, occupant in pairs(self._occupants) do | 136 for rnick, occupant in pairs(self._occupants) do |
149 :tag("item", {affiliation=o_data.affiliation or "none", role=o_data.role or "none"}):up(); | 153 :tag("item", {affiliation=o_data.affiliation or "none", role=o_data.role or "none"}):up(); |
150 self:_route_stanza(pres); | 154 self:_route_stanza(pres); |
151 end | 155 end |
152 end | 156 end |
153 end | 157 end |
154 function room_mt:send_history(to) | 158 function room_mt:send_history(to, stanza) |
155 local history = self._data['history']; -- send discussion history | 159 local history = self._data['history']; -- send discussion history |
156 if history then | 160 if history then |
157 for _, msg in ipairs(history) do | 161 local x_tag = stanza and stanza:get_child("x", "http://jabber.org/protocol/muc"); |
158 msg = st.deserialize(msg); | 162 local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc"); |
159 msg.attr.to=to; | 163 |
164 local maxchars = history_tag and tonumber(history_tag.attr.maxchars); | |
165 if maxchars then maxchars = math.floor(maxchars); end | |
166 | |
167 local maxstanzas = math.floor(history_tag and tonumber(history_tag.attr.maxstanzas) or #history); | |
168 if not history_tag then maxstanzas = 20; end | |
169 | |
170 local seconds = history_tag and tonumber(history_tag.attr.seconds); | |
171 if seconds then seconds = datetime.datetime(os.time() - math.floor(seconds)); end | |
172 | |
173 local since = history_tag and history_tag.attr.since; | |
174 if since and not since:match("^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%dZ$") then since = nil; end -- FIXME timezone support | |
175 if seconds and (not since or since < seconds) then since = seconds; end | |
176 | |
177 local n = 0; | |
178 local charcount = 0; | |
179 local stanzacount = 0; | |
180 | |
181 for i=#history,1,-1 do | |
182 local entry = history[i]; | |
183 if maxchars then | |
184 if not entry.chars then | |
185 entry.stanza.attr.to = ""; | |
186 entry.chars = #tostring(entry.stanza); | |
187 end | |
188 charcount = charcount + entry.chars + #to; | |
189 if charcount > maxchars then break; end | |
190 end | |
191 if since and since > entry.stamp then break; end | |
192 if n + 1 > maxstanzas then break; end | |
193 n = n + 1; | |
194 end | |
195 for i=#history-n+1,#history do | |
196 local msg = history[i].stanza; | |
197 msg.attr.to = to; | |
160 self:_route_stanza(msg); | 198 self:_route_stanza(msg); |
161 end | 199 end |
162 end | 200 end |
163 if self._data['subject'] then | 201 if self._data['subject'] then |
164 self:_route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject'])); | 202 self:_route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject'])); |
317 pr.attr.to = from; | 355 pr.attr.to = from; |
318 self:_route_stanza(pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | 356 self:_route_stanza(pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) |
319 :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up() | 357 :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up() |
320 :tag("status", {code='110'})); | 358 :tag("status", {code='110'})); |
321 end | 359 end |
322 self:send_history(from); | 360 self:send_history(from, stanza); |
323 else -- banned | 361 else -- banned |
324 local reply = st.error_reply(stanza, "auth", "forbidden"):up(); | 362 local reply = st.error_reply(stanza, "auth", "forbidden"):up(); |
325 reply.tags[1].attr.code = "403"; | 363 reply.tags[1].attr.code = "403"; |
326 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | 364 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
327 end | 365 end |