Software /
code /
prosody-modules
Annotate
mod_archive_muc/mod_archive_muc.lua @ 476:2c85635318a5
mod_archive_muc: Fixed a nil global access.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 17 Nov 2011 20:24:46 +0500 |
parent | 243:6202ce4d12d6 |
child | 477:bbe4df968099 |
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 | |
243
6202ce4d12d6
mod_archive: added some config options.
shinysky<shinysky1986(AT)gmail.com>
parents:
240
diff
changeset
|
18 local AUTO_MUC_ARCHIVING_ENABLED = module:get_option_boolean("auto_muc_archiving_enabled", true); |
237 | 19 |
476
2c85635318a5
mod_archive_muc: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents:
243
diff
changeset
|
20 local NULL = {}; |
2c85635318a5
mod_archive_muc: Fixed a nil global access.
Waqas Hussain <waqas20@gmail.com>
parents:
243
diff
changeset
|
21 |
237 | 22 module:add_feature("urn:xmpp:archive#preferences"); |
23 module:add_feature("urn:xmpp:archive#management"); | |
24 | |
25 ------------------------------------------------------------ | |
26 -- Utils | |
27 ------------------------------------------------------------ | |
240
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
28 local function trim(s) |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
29 return (string.gsub(s, "^%s*(.-)%s*$", "%1")) |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
30 end |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
31 |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
32 local function clean_up(t) |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
33 for i = #t, 1, -1 do |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
34 if type(t[i]) == 'table' then |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
35 clean_up(t[i]); |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
36 elseif type(t[i]) == 'string' and trim(t[i]) == '' then |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
37 table.remove(t, i); |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
38 end |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
39 end |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
40 end |
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
41 |
237 | 42 local function load_prefs(node, host) |
43 return st.deserialize(dm.load(node, host, PREFS_DIR)); | |
44 end | |
45 | |
46 local function store_prefs(data, node, host) | |
240
ef0b580f434d
mod_archive_muc: clean up '\n ' in preference stanza
shinysky<shinysky1986(AT)gmail.com>
parents:
238
diff
changeset
|
47 clean_up(data); |
237 | 48 dm.store(node, host, PREFS_DIR, st.preserialize(data)); |
49 end | |
50 | |
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
|
51 local date_time = datetime.datetime; |
237 | 52 |
53 local function match_jid(rule, id) | |
54 return not rule or jid.compare(id, rule); | |
55 end | |
56 | |
57 local function is_earlier(start, coll_start) | |
58 return not start or start <= coll_start; | |
59 end | |
60 | |
61 local function is_later(endtime, coll_start) | |
62 return not endtime or endtime >= coll_start; | |
63 end | |
64 | |
65 ------------------------------------------------------------ | |
66 -- Preferences | |
67 ------------------------------------------------------------ | |
68 local function preferences_handler(event) | |
69 local origin, stanza = event.origin, event.stanza; | |
70 module:log("debug", "-- Enter muc preferences_handler()"); | |
71 module:log("debug", "-- muc pref:\n%s", tostring(stanza)); | |
72 if stanza.attr.type == "get" then | |
73 local data = load_prefs(origin.username, origin.host); | |
74 if data then | |
75 origin.send(st.reply(stanza):add_child(data)); | |
76 else | |
77 origin.send(st.reply(stanza)); | |
78 end | |
79 elseif stanza.attr.type == "set" then | |
80 local node, host = origin.username, origin.host; | |
81 if stanza.tags[1] and stanza.tags[1].name == 'prefs' then | |
82 store_prefs(stanza.tags[1], node, host); | |
83 origin.send(st.reply(stanza)); | |
84 local user = bare_sessions[node.."@"..host]; | |
85 local push = st.iq({type="set"}); | |
86 push:add_child(stanza.tags[1]); | |
87 for _, res in pairs(user and user.sessions or NULL) do -- broadcast to all resources | |
88 if res.presence then -- to resource | |
89 push.attr.to = res.full_jid; | |
90 res.send(push); | |
91 end | |
92 end | |
93 end | |
94 end | |
95 return true; | |
96 end | |
97 | |
98 ------------------------------------------------------------ | |
99 -- Archive Management | |
100 ------------------------------------------------------------ | |
101 local function management_handler(event) | |
102 module:log("debug", "-- Enter muc management_handler()"); | |
103 local origin, stanza = event.origin, event.stanza; | |
104 local node, host = origin.username, origin.host; | |
105 local data = dm.list_load(node, host, ARCHIVE_DIR); | |
106 local elem = stanza.tags[1]; | |
107 local resset = {} | |
108 if data then | |
109 for i = #data, 1, -1 do | |
110 local forwarded = st.deserialize(data[i]); | |
111 local res = (match_jid(elem.attr["with"], forwarded.tags[2].attr.from) | |
112 or match_jid(elem.attr["with"], forwarded.tags[2].attr.to)) | |
113 and is_earlier(elem.attr["start"], forwarded.tags[1].attr["stamp"]) | |
114 and is_later(elem.attr["end"], forwarded.tags[1].attr["stamp"]); | |
115 if res then | |
116 table.insert(resset, forwarded); | |
117 end | |
118 end | |
119 for i = #resset, 1, -1 do | |
120 local res = st.message({to = stanza.attr.from, id=st.new_id()}); | |
121 res:add_child(resset[i]); | |
122 origin.send(res); | |
123 end | |
124 end | |
125 origin.send(st.reply(stanza)); | |
126 return true; | |
127 end | |
128 | |
129 ------------------------------------------------------------ | |
130 -- Message Handler | |
131 ------------------------------------------------------------ | |
132 local function is_in(list, jid) | |
133 for _,v in ipairs(list) do | |
134 if match_jid(v:get_text(), jid) then -- JID Matching | |
135 return true; | |
136 end | |
137 end | |
138 return false; | |
139 end | |
140 | |
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
|
141 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
|
142 return rom.is_contact_subscribed(node, host, jid.bare(id)); |
237 | 143 end |
144 | |
145 local function apply_pref(node, host, jid) | |
146 local pref = load_prefs(node, host); | |
147 if not pref then | |
243
6202ce4d12d6
mod_archive: added some config options.
shinysky<shinysky1986(AT)gmail.com>
parents:
240
diff
changeset
|
148 return AUTO_MUC_ARCHIVING_ENABLED; |
237 | 149 end |
150 local always = pref:child_with_name('always'); | |
151 if always and is_in(always, jid) then | |
152 return true; | |
153 end | |
154 local never = pref:child_with_name('never'); | |
155 if never and is_in(never, jid) then | |
156 return false; | |
157 end | |
158 local default = pref.attr['default']; | |
159 if default == 'roster' then | |
160 return is_in_roster(node, host, jid); | |
161 elseif default == 'always' then | |
162 return true; | |
163 elseif default == 'never' then | |
164 return false; | |
165 end | |
243
6202ce4d12d6
mod_archive: added some config options.
shinysky<shinysky1986(AT)gmail.com>
parents:
240
diff
changeset
|
166 return AUTO_MUC_ARCHIVING_ENABLED; |
237 | 167 end |
168 | |
169 local function store_msg(msg, node, host) | |
170 local forwarded = st.stanza('forwarded', {xmlns='urn:xmpp:forward:tmp'}); | |
171 forwarded:tag('delay', {xmlns='urn:xmpp:delay',stamp=date_time()}):up(); | |
172 forwarded:add_child(msg); | |
173 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(forwarded)); | |
174 end | |
175 | |
176 local function msg_handler(data) | |
177 module:log("debug", "-- Enter muc msg_handler()"); | |
178 local origin, stanza = data.origin, data.stanza; | |
179 local body = stanza:child_with_name("body"); | |
180 if body then | |
181 local from_node, from_host = jid.split(stanza.attr.from); | |
182 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
|
183 if um.user_exists(from_node, from_host) and apply_pref(from_node, from_host, stanza.attr.to) then |
237 | 184 store_msg(stanza, from_node, from_host); |
185 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
|
186 if um.user_exists(to_node, to_host) and apply_pref(to_node, to_host, stanza.attr.from) then |
237 | 187 store_msg(stanza, to_node, to_host); |
188 end | |
189 end | |
190 | |
191 return nil; | |
192 end | |
193 | |
194 -- Preferences | |
195 module:hook("iq/self/urn:xmpp:archive#preferences:prefs", preferences_handler); | |
196 -- Archive management | |
197 module:hook("iq/self/urn:xmpp:archive#management:query", management_handler); | |
198 | |
199 module:hook("message/bare", msg_handler, 20); | |
200 |