comparison mod_mam/mod_mam.lua @ 1149:d055c44a7f61

mod_mam: Clean up and explain the code that determines who's archive we put the message in
author Kim Alvefur <zash@zash.se>
date Sun, 11 Aug 2013 23:19:35 +0200
parents 0d6ab5e4bc30
children 296820f18ba6
comparison
equal deleted inserted replaced
1148:723367b5de8c 1149:d055c44a7f61
174 174
175 -- Handle messages 175 -- Handle messages
176 local function message_handler(event, c2s) 176 local function message_handler(event, c2s)
177 local origin, stanza = event.origin, event.stanza; 177 local origin, stanza = event.origin, event.stanza;
178 local orig_type = stanza.attr.type or "normal"; 178 local orig_type = stanza.attr.type or "normal";
179 local orig_to = stanza.attr.to;
180 local orig_from = stanza.attr.from; 179 local orig_from = stanza.attr.from;
181 180 local orig_to = stanza.attr.to or orig_from;
182 if not orig_from and c2s then 181 -- Stanza without 'to' are treated as if it was to their own bare jid
183 orig_from = origin.full_jid;
184 end
185 orig_to = orig_to or orig_from; -- Weird corner cases
186 182
187 -- Don't store messages of these types 183 -- Don't store messages of these types
188 if orig_type == "error" 184 if orig_type == "error"
189 or orig_type == "headline" 185 or orig_type == "headline"
190 or orig_type == "groupchat" 186 or orig_type == "groupchat"
191 or not stanza:get_child("body") then 187 or not stanza:get_child("body") then
192 return; 188 return;
193 end 189 end
194 190
195 local store_user = jid_split(c2s and orig_from or orig_to); 191 -- Whos storage do we put it in?
196 local target_jid = c2s and orig_to or orig_from; 192 local store_user = c2s and origin.username or jid_split(orig_to);
197 local target_bare = jid_bare(target_jid); 193 -- And who are they chatting with?
198 194 local with = jid_bare(c2s and orig_to or orig_from);
199 if shall_store(store_user, target_bare) then 195
196 -- Check with the users preferences
197 if shall_store(store_user, with) then
200 module:log("debug", "Archiving stanza: %s", stanza:top_tag()); 198 module:log("debug", "Archiving stanza: %s", stanza:top_tag());
201 199
202 -- And stash it 200 -- And stash it
203 local ok, id = archive:append(store_user, time_now(), target_bare, stanza); 201 local ok, id = archive:append(store_user, time_now(), with, stanza);
204 if ok and not c2s then 202 if ok and not c2s then
205 stanza:tag("archived", { xmlns = xmlns_mam, by = jid_bare(orig_to), id = id }):up(); 203 stanza:tag("archived", { xmlns = xmlns_mam, by = store_user.."@"..host, id = id }):up();
206 end 204 end
207 else 205 else
208 module:log("debug", "Not archiving stanza: %s", stanza:top_tag()); 206 module:log("debug", "Not archiving stanza: %s", stanza:top_tag());
209 end 207 end
210 end 208 end