annotate mod_archive_muc/mod_archive_muc.lua @ 237:d900be0dee3e

mod_archive: minor fix; mod_archive_muc: added new module mod_archive_muc; init commit; workable, not fully tested.
author shinysky<shinysky1986(AT)gmail.com>
date Mon, 09 Aug 2010 18:45:51 +0800
parents
children 5343b3ebaffb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
237
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
1 -- Prosody IM
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2010 Dai Zhiwei
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
3 --
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
6 --
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
7
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
8 local st = require "util.stanza";
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
9 local dm = require "util.datamanager";
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
10 local jid = require "util.jid";
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
11 local datetime = require "util.datetime";
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
12
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
13 local PREFS_DIR = "archive_muc_prefs";
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
14 local ARCHIVE_DIR = "archive_muc";
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
15
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
16 local HOST = 'localhost';
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
17
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
18 local AUTO_ARCHIVING_ENABLED = true;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
19
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
20 module:add_feature("urn:xmpp:archive#preferences");
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
21 module:add_feature("urn:xmpp:archive#management");
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
22
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
23 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
24 -- Utils
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
25 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
26 local function load_prefs(node, host)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
27 return st.deserialize(dm.load(node, host, PREFS_DIR));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
28 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
29
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
30 local function store_prefs(data, node, host)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
31 dm.store(node, host, PREFS_DIR, st.preserialize(data));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
32 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
33
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
34 local function date_time(localtime)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
35 return datetime.datetime(localtime);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
36 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
37
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
38 local function match_jid(rule, id)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
39 return not rule or jid.compare(id, rule);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
40 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
41
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
42 local function is_earlier(start, coll_start)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
43 return not start or start <= coll_start;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
44 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
45
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
46 local function is_later(endtime, coll_start)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
47 return not endtime or endtime >= coll_start;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
48 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
49
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
50 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
51 -- Preferences
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
52 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
53 local function preferences_handler(event)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
54 local origin, stanza = event.origin, event.stanza;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
55 module:log("debug", "-- Enter muc preferences_handler()");
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
56 module:log("debug", "-- muc pref:\n%s", tostring(stanza));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
57 if stanza.attr.type == "get" then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
58 local data = load_prefs(origin.username, origin.host);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
59 if data then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
60 origin.send(st.reply(stanza):add_child(data));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
61 else
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
62 origin.send(st.reply(stanza));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
63 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
64 elseif stanza.attr.type == "set" then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
65 local node, host = origin.username, origin.host;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
66 if stanza.tags[1] and stanza.tags[1].name == 'prefs' then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
67 store_prefs(stanza.tags[1], node, host);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
68 origin.send(st.reply(stanza));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
69 local user = bare_sessions[node.."@"..host];
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
70 local push = st.iq({type="set"});
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
71 push:add_child(stanza.tags[1]);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
72 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
73 if res.presence then -- to resource
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
74 push.attr.to = res.full_jid;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
75 res.send(push);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
76 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
77 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
78 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
79 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
80 return true;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
81 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
82
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
83 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
84 -- Archive Management
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
85 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
86 local function management_handler(event)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
87 module:log("debug", "-- Enter muc management_handler()");
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
88 local origin, stanza = event.origin, event.stanza;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
89 local node, host = origin.username, origin.host;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
90 local data = dm.list_load(node, host, ARCHIVE_DIR);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
91 local elem = stanza.tags[1];
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
92 local resset = {}
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
93 if data then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
94 for i = #data, 1, -1 do
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
95 local forwarded = st.deserialize(data[i]);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
96 local res = (match_jid(elem.attr["with"], forwarded.tags[2].attr.from)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
97 or match_jid(elem.attr["with"], forwarded.tags[2].attr.to))
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
98 and is_earlier(elem.attr["start"], forwarded.tags[1].attr["stamp"])
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
99 and is_later(elem.attr["end"], forwarded.tags[1].attr["stamp"]);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
100 if res then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
101 table.insert(resset, forwarded);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
102 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
103 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
104 for i = #resset, 1, -1 do
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
105 local res = st.message({to = stanza.attr.from, id=st.new_id()});
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
106 res:add_child(resset[i]);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
107 origin.send(res);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
108 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
109 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
110 origin.send(st.reply(stanza));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
111 return true;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
112 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
113
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
114 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
115 -- Message Handler
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
116 ------------------------------------------------------------
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
117 local function is_in(list, jid)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
118 for _,v in ipairs(list) do
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
119 if match_jid(v:get_text(), jid) then -- JID Matching
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
120 return true;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
121 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
122 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
123 return false;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
124 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
125
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
126 local function is_in_roster(node, host, jid)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
127 -- TODO
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
128 return true;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
129 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
130
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
131 local function apply_pref(node, host, jid)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
132 local pref = load_prefs(node, host);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
133 if not pref then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
134 return AUTO_ARCHIVING_ENABLED;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
135 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
136 local always = pref:child_with_name('always');
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
137 if always and is_in(always, jid) then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
138 return true;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
139 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
140 local never = pref:child_with_name('never');
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
141 if never and is_in(never, jid) then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
142 return false;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
143 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
144 local default = pref.attr['default'];
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
145 if default == 'roster' then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
146 return is_in_roster(node, host, jid);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
147 elseif default == 'always' then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
148 return true;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
149 elseif default == 'never' then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
150 return false;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
151 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
152 return AUTO_ARCHIVING_ENABLED;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
153 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
154
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
155 local function store_msg(msg, node, host)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
156 local forwarded = st.stanza('forwarded', {xmlns='urn:xmpp:forward:tmp'});
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
157 forwarded:tag('delay', {xmlns='urn:xmpp:delay',stamp=date_time()}):up();
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
158 forwarded:add_child(msg);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
159 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(forwarded));
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
160 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
161
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
162 local function msg_handler(data)
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
163 module:log("debug", "-- Enter muc msg_handler()");
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
164 local origin, stanza = data.origin, data.stanza;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
165 local body = stanza:child_with_name("body");
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
166 if body then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
167 local from_node, from_host = jid.split(stanza.attr.from);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
168 local to_node, to_host = jid.split(stanza.attr.to);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
169 -- FIXME only archive messages of users on this host
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
170 if from_host == HOST and apply_pref(from_node, from_host, stanza.attr.to) then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
171 store_msg(stanza, from_node, from_host);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
172 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
173 if to_host == HOST and apply_pref(to_node, to_host, stanza.attr.from) then
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
174 store_msg(stanza, to_node, to_host);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
175 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
176 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
177
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
178 return nil;
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
179 end
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
180
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
181 -- Preferences
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
182 module:hook("iq/self/urn:xmpp:archive#preferences:prefs", preferences_handler);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
183 -- Archive management
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
184 module:hook("iq/self/urn:xmpp:archive#management:query", management_handler);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
185
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
186 module:hook("message/full", msg_handler, 20);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
187 module:hook("message/bare", msg_handler, 20);
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
188
d900be0dee3e mod_archive: minor fix;
shinysky<shinysky1986(AT)gmail.com>
parents:
diff changeset
189 -- TODO prefs: [1] = "\n ";