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