annotate mod_archive/mod_archive.lua @ 188:5e8ea3733dc6

mod_archive: there can be only one note/previous/next/x element in a collection.
author shinysky<shinysky1986(AT)gmail.com>
date Sun, 27 Jun 2010 15:04:24 +0800
parents 670c99e96c52
children 18a0e5c0bb62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
1 -- Prosody IM
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
2 -- Copyright (C) 2010 Dai Zhiwei
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
3 --
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
6 --
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
7
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
8 local st = require "util.stanza";
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
9 local dm = require "util.datamanager";
178
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
10 local jid = require "util.jid";
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
11
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
12 local PREFS_DIR = "archive_prefs";
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
13 local ARCHIVE_DIR = "archive";
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
14
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
15 module:add_feature("urn:xmpp:archive");
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
16 module:add_feature("urn:xmpp:archive:auto");
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
17 module:add_feature("urn:xmpp:archive:manage");
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
18 module:add_feature("urn:xmpp:archive:manual");
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
19 module:add_feature("urn:xmpp:archive:pref");
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
20
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
21 ------------------------------------------------------------
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
22 -- Utils
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
23 ------------------------------------------------------------
182
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
24 local function load_prefs(node, host)
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
25 return st.deserialize(dm.load(node, host, PREFS_DIR));
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
26 end
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
27
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
28 local function store_prefs(data, node, host)
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
29 dm.store(node, host, PREFS_DIR, st.preserialize(data));
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
30 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
31
182
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
32 local function store_msg(msg, node, host, isfrom)
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
33 local body = msg:child_with_name("body");
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
34 local thread = msg:child_with_name("thread");
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
35 local data = dm.list_load(node, host, ARCHIVE_DIR);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
36 local tag = (isfrom and "from") or "to";
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
37 if data then
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
38 for k, v in ipairs(data) do
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
39 -- <chat with='juliet@capulet.com/chamber'
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
40 -- start='1469-07-21T02:56:15Z'
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
41 -- thread='damduoeg08'
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
42 -- subject='She speaks!'
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
43 -- version='1'>
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
44 -- <from secs='0'><body>Art thou not Romeo, and a Montague?</body></from>
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
45 -- <to secs='11'><body>Neither, fair saint, if either thee dislike.</body></to>
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
46 -- <from secs='7'><body>How cam'st thou hither, tell me, and wherefore?</body></from>
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
47 -- <note utc='1469-07-21T03:04:35Z'>I think she might fancy me.</note>
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
48 -- </chat>
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
49 local collection = st.deserialize(v);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
50 if collection.attr["thread"] == thread:get_text() then
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
51 -- TODO figure out secs
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
52 collection:tag(tag, {secs='1'}):add_child(body);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
53 local ver = tonumber(collection.attr["version"]) + 1;
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
54 collection.attr["version"] = tostring(ver);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
55 data[k] = collection;
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
56 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
57 return;
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
58 end
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
59 end
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
60 end
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
61 -- not found, create new collection
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
62 -- TODO figure out start time
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
63 local collection = st.stanza('chat', {with = isfrom and msg.attr.to or msg.attr.from, start='2010-06-01T09:56:15Z', thread=thread:get_text(), version='0'});
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
64 collection:tag(tag, {secs='0'}):add_child(body);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
65 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(collection));
178
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
66 end
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
67
187
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
68 local function save_result(collection)
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
69 local save = st.stanza('save', {xmlns='urn:xmpp:archive'});
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
70 local chat = st.stanza('chat', collection.attr);
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
71 save:add_child(chat);
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
72 return save;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
73 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
74
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
75 ------------------------------------------------------------
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
76 -- Preferences
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
77 ------------------------------------------------------------
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
78 local function preferences_handler(event)
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
79 local origin, stanza = event.origin, event.stanza;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
80 module:log("debug", "-- Enter preferences_handler()");
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
81 module:log("debug", "-- pref:\n%s", tostring(stanza));
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
82 if stanza.attr.type == "get" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
83 local data = load_prefs(origin.username, origin.host);
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
84 if data then
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
85 origin.send(st.reply(stanza):add_child(data));
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
86 else
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
87 local reply = st.reply(stanza):tag('pref', {xmlns='urn:xmpp:archive'});
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
88 reply:tag('default', {otr='concede', save='false', unset='true'}):up();
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
89 reply:tag('method', {type='auto', use='concede'}):up();
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
90 reply:tag('method', {type='local', use='concede'}):up();
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
91 reply:tag('method', {type='manual', use='concede'}):up();
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
92 reply:tag('auto', {save='false'}):up();
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
93 origin.send(reply);
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
94 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
95 elseif stanza.attr.type == "set" then
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
96 local node, host = origin.username, origin.host;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
97 local data = load_prefs(node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
98 if not data then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
99 data = st.stanza('pref', {xmlns='urn:xmpp:archive'});
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
100 data:tag('default', {otr='concede', save='false'}):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
101 data:tag('method', {type='auto', use='concede'}):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
102 data:tag('method', {type='local', use='concede'}):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
103 data:tag('method', {type='manual', use='concede'}):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
104 data:tag('auto', {save='false'}):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
105 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
106 local elem = stanza.tags[1].tags[1]; -- iq:pref:xxx
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
107 if not elem then return false end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
108 -- "default" | "item" | "session" | "method"
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
109 elem.attr["xmlns"] = nil; -- TODO why there is an extra xmlns attr?
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
110 if elem.name == "default" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
111 local setting = data:child_with_name(elem.name)
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
112 for k, v in pairs(elem.attr) do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
113 setting.attr[k] = v;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
114 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
115 -- setting.attr["unset"] = nil
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
116 elseif elem.name == "item" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
117 local found = false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
118 for child in data:children() do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
119 -- TODO bare JID or full JID?
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
120 if child.name == elem.name and child.attr["jid"] == elem.attr["jid"] then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
121 for k, v in pairs(elem.attr) do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
122 child.attr[k] = v;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
123 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
124 found = true;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
125 break;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
126 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
127 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
128 if not found then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
129 data:tag(elem.name, elem.attr):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
130 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
131 elseif elem.name == "session" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
132 local found = false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
133 for child in data:children() do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
134 if child.name == elem.name and child.attr["thread"] == elem.attr["thread"] then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
135 for k, v in pairs(elem.attr) do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
136 child.attr[k] = v;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
137 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
138 found = true;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
139 break;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
140 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
141 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
142 if not found then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
143 data:tag(elem.name, elem.attr):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
144 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
145 elseif elem.name == "method" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
146 local newpref = stanza.tags[1]; -- iq:pref
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
147 for _, e in ipairs(newpref.tags) do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
148 -- if e.name ~= "method" then continue end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
149 local found = false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
150 for child in data:children() do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
151 if child.name == "method" and child.attr["type"] == e.attr["type"] then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
152 child.attr["use"] = e.attr["use"];
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
153 found = true;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
154 break;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
155 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
156 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
157 if not found then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
158 data:tag(e.name, e.attr):up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
159 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
160 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
161 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
162 store_prefs(data, node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
163 origin.send(st.reply(stanza));
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
164 local user = bare_sessions[node.."@"..host];
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
165 local push = st.iq({type="set"});
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
166 push = push:tag('pref', {xmlns='urn:xmpp:archive'});
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
167 if elem.name == "method" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
168 for child in data:children() do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
169 if child.name == "method" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
170 push:add_child(child);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
171 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
172 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
173 else
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
174 push:add_child(elem);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
175 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
176 push = push:up();
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
177 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
178 if res.presence then -- to resource
172
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
179 push.attr.to = res.full_jid;
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
180 res.send(push);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
181 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
182 end
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
183 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
184 return true;
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
185 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
186
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
187 local function itemremove_handler(event)
187
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
188 -- TODO use 'assert' to check imcoming stanza?
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
189 -- or use pcall() to catch exceptions?
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
190 local origin, stanza = event.origin, event.stanza;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
191 if stanza.attr.type ~= "set" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
192 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
193 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
194 local elem = stanza.tags[1].tags[1];
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
195 if not elem or elem.name ~= "item" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
196 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
197 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
198 local node, host = origin.username, origin.host;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
199 local data = load_prefs(node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
200 if not data then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
201 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
202 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
203 for i, child in ipairs(data) do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
204 if child.name == "item" and child.attr["jid"] == elem.attr["jid"] then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
205 table.remove(data, i)
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
206 break;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
207 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
208 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
209 store_prefs(data, node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
210 origin.send(st.reply(stanza));
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
211 return true;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
212 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
213
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
214 local function sessionremove_handler(event)
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
215 local origin, stanza = event.origin, event.stanza;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
216 if stanza.attr.type ~= "set" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
217 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
218 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
219 local elem = stanza.tags[1].tags[1];
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
220 if not elem or elem.name ~= "session" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
221 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
222 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
223 local node, host = origin.username, origin.host;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
224 local data = load_prefs(node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
225 if not data then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
226 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
227 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
228 for i, child in ipairs(data) do
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
229 if child.name == "session" and child.attr["thread"] == elem.attr["thread"] then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
230 table.remove(data, i)
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
231 break;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
232 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
233 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
234 store_prefs(data, node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
235 origin.send(st.reply(stanza));
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
236 return true;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
237 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
238
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
239 local function auto_handler(event)
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
240 -- event.origin.send(st.error_reply(event.stanza, "cancel", "feature-not-implemented"));
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
241 local origin, stanza = event.origin, event.stanza;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
242 if stanza.attr.type ~= "set" then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
243 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
244 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
245 local elem = stanza.tags[1];
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
246 local node, host = origin.username, origin.host;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
247 local data = load_prefs(node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
248 if not data then
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
249 return false;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
250 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
251 local setting = data:child_with_name(elem.name)
172
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
252 for k, v in pairs(elem.attr) do
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
253 setting.attr[k] = v;
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
254 end
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
255 store_prefs(data, node, host);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
256 origin.send(st.reply(stanza));
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
257 return true;
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
258 end
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
259
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
260 local function chat_handler(event)
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
261 module:log("debug", "-- stanza:\n%s", tostring(event.stanza));
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
262 return true;
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
263 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
264
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
265 local function list_handler(event)
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
266 module:log("debug", "-- stanza:\n%s", tostring(event.stanza));
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
267 return true;
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
268 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
269
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
270 local function modified_handler(event)
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
271 module:log("debug", "-- stanza:\n%s", tostring(event.stanza));
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
272 return true;
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
273 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
274
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
275 local function remove_handler(event)
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
276 module:log("debug", "-- stanza:\n%s", tostring(event.stanza));
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
277 return true;
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
278 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
279
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
280 local function retrieve_handler(event)
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
281 module:log("debug", "-- stanza:\n%s", tostring(event.stanza));
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
282 return true;
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
283 end
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
284
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
285 local function save_handler(event)
187
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
286 local origin, stanza = event.origin, event.stanza;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
287 if stanza.attr.type ~= "set" then
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
288 return false;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
289 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
290 local elem = stanza.tags[1].tags[1];
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
291 if not elem or elem.name ~= "chat" then
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
292 return false;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
293 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
294 local node, host = origin.username, origin.host;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
295 local data = dm.list_load(node, host, ARCHIVE_DIR);
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
296 if data then
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
297 for k, v in ipairs(data) do
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
298 local collection = st.deserialize(v);
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
299 if collection.attr["with"] == elem.attr["with"]
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
300 and collection.attr["start"] == elem.attr["start"] then
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
301 -- TODO check if there're duplicates
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
302 for newchild in elem:children() do
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
303 if type(newchild) == "table" then
188
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
304 if newchild.name == "from" or newchild.name == "to" then
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
305 collection:add_child(newchild);
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
306 elseif newchild.name == "note" or newchild.name == "previous" or newchild.name == "next" or newchild.name == "x" then
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
307 local found = false;
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
308 for i, c in ipairs(collection) do
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
309 if c.name == newchild.name then
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
310 found = true;
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
311 collection[i] = newchild;
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
312 break;
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
313 end
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
314 end
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
315 if not found then
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
316 collection:add_child(newchild);
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
317 end
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
318 end
187
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
319 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
320 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
321 local ver = tonumber(collection.attr["version"]) + 1;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
322 collection.attr["version"] = tostring(ver);
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
323 collection.attr["subject"] = elem.attr["subject"];
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
324 origin.send(st.reply(stanza):add_child(save_result(collection)));
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
325 data[k] = collection;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
326 dm.list_store(node, host, ARCHIVE_DIR, st.preserialize(data));
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
327 return true;
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
328 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
329 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
330 end
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
331 -- not found, create new collection
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
332 elem.attr["version"] = "0";
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
333 origin.send(st.reply(stanza):add_child(save_result(elem)));
188
5e8ea3733dc6 mod_archive: there can be only one note/previous/next/x element in a collection.
shinysky<shinysky1986(AT)gmail.com>
parents: 187
diff changeset
334 -- TODO check if elem is valid(?)
187
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
335 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(elem));
670c99e96c52 mod_archive: first commit for manual archiving, need polishing
shinysky<shinysky1986(AT)gmail.com>
parents: 182
diff changeset
336 -- TODO unsuccessful reply
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
337 return true;
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
338 end
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
339
178
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
340 ------------------------------------------------------------
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
341 -- Message Handler
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
342 ------------------------------------------------------------
172
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
343 local function msg_handler(data)
182
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
344 -- TODO if not auto_archive_enabled then return nil;
172
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
345 module:log("debug", "-- Enter msg_handler()");
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
346 local origin, stanza = data.origin, data.stanza;
178
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
347 local body = stanza:child_with_name("body");
182
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
348 local thread = stanza:child_with_name("thread");
172
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
349 module:log("debug", "-- msg:\n%s", tostring(stanza));
178
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
350 if body then
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
351 module:log("debug", "-- msg body:\n%s", tostring(body));
182
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
352 -- TODO mapping messages and conversations to collections if no thread
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
353 if thread then
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
354 module:log("debug", "-- msg thread:\n%s", tostring(thread));
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
355 -- module:log("debug", "-- msg body text:\n%s", body:get_text());
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
356 local from_node, from_host = jid.split(stanza.attr.from);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
357 local to_node, to_host = jid.split(stanza.attr.to);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
358 -- FIXME only archive messages of users on this host
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
359 if from_host == "localhost" then
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
360 store_msg(stanza, from_node, from_host, true);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
361 end
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
362 if to_host == "localhost" then
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
363 store_msg(stanza, to_node, to_host, false);
43d9e0944276 mod_archive: now auto archiving is almost done.
shinysky<shinysky1986(AT)gmail.com>
parents: 178
diff changeset
364 end
178
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
365 end
62f47a93b5b7 mod_archive: Now we can archive messages, even though not in organized way.
shinysky<shinysky1986(AT)gmail.com>
parents: 172
diff changeset
366 end
172
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
367 return nil;
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
368 end
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
369
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
370 module:hook("iq/self/urn:xmpp:archive:pref", preferences_handler);
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
371 module:hook("iq/self/urn:xmpp:archive:itemremove", itemremove_handler);
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
372 module:hook("iq/self/urn:xmpp:archive:sessionremove", sessionremove_handler);
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
373 module:hook("iq/self/urn:xmpp:archive:auto", auto_handler);
165
fd8d76daad97 mod_archive: preference handling is done.
shinysky<shinysky1986(AT)gmail.com>
parents: 159
diff changeset
374 -- module:hook("iq/self/urn:xmpp:archive:chat", chat_handler);
159
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
375 module:hook("iq/self/urn:xmpp:archive:list", list_handler);
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
376 module:hook("iq/self/urn:xmpp:archive:modified", modified_handler);
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
377 module:hook("iq/self/urn:xmpp:archive:remove", remove_handler);
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
378 module:hook("iq/self/urn:xmpp:archive:retrieve", retrieve_handler);
9a37898f4f7c mod_archive: Added features: Determining Server Support & Determining Preferences
shinysky<shinysky1986(AT)gmail.com>
parents: 157
diff changeset
379 module:hook("iq/self/urn:xmpp:archive:save", save_handler);
157
86c28405c5da initial commitment of mod_archive
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
380
172
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
381 module:hook("message/full", msg_handler, 10);
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
382 module:hook("message/bare", msg_handler, 10);
ac826882a8cf mod_archive: The element auto MAY include a 'scope' attribute; Added message hook.
shinysky<shinysky1986(AT)gmail.com>
parents: 165
diff changeset
383