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