Comparison

mod_archive_muc/mod_archive_muc.lua @ 477:bbe4df968099

mod_archive_muc: A little refactoring to improve code search.
author Waqas Hussain <waqas20@gmail.com>
date Thu, 17 Nov 2011 20:26:49 +0500
parent 476:2c85635318a5
comparison
equal deleted inserted replaced
476:2c85635318a5 477:bbe4df968099
5 -- COPYING file in the source package for more information. 5 -- COPYING file in the source package for more information.
6 -- 6 --
7 7
8 local st = require "util.stanza"; 8 local st = require "util.stanza";
9 local dm = require "util.datamanager"; 9 local dm = require "util.datamanager";
10 local jid = require "util.jid"; 10 local jid_compare, jid_split, jid_bare = require "util.jid".compare, require "util.jid".bare, require "util.jid".split;
11 local datetime = require "util.datetime"; 11 local datetime = require "util.datetime".datetime;
12 local um = require "core.usermanager"; 12 local user_exists = require "core.usermanager".user_exists;
13 local rom = require "core.rostermanager"; 13 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
14 14
15 local PREFS_DIR = "archive_muc_prefs"; 15 local PREFS_DIR = "archive_muc_prefs";
16 local ARCHIVE_DIR = "archive_muc"; 16 local ARCHIVE_DIR = "archive_muc";
17 17
18 local AUTO_MUC_ARCHIVING_ENABLED = module:get_option_boolean("auto_muc_archiving_enabled", true); 18 local AUTO_MUC_ARCHIVING_ENABLED = module:get_option_boolean("auto_muc_archiving_enabled", true);
46 local function store_prefs(data, node, host) 46 local function store_prefs(data, node, host)
47 clean_up(data); 47 clean_up(data);
48 dm.store(node, host, PREFS_DIR, st.preserialize(data)); 48 dm.store(node, host, PREFS_DIR, st.preserialize(data));
49 end 49 end
50 50
51 local date_time = datetime.datetime;
52
53 local function match_jid(rule, id) 51 local function match_jid(rule, id)
54 return not rule or jid.compare(id, rule); 52 return not rule or jid_compare(id, rule);
55 end 53 end
56 54
57 local function is_earlier(start, coll_start) 55 local function is_earlier(start, coll_start)
58 return not start or start <= coll_start; 56 return not start or start <= coll_start;
59 end 57 end
136 end 134 end
137 end 135 end
138 return false; 136 return false;
139 end 137 end
140 138
141 local function is_in_roster(node, host, id)
142 return rom.is_contact_subscribed(node, host, jid.bare(id));
143 end
144
145 local function apply_pref(node, host, jid) 139 local function apply_pref(node, host, jid)
146 local pref = load_prefs(node, host); 140 local pref = load_prefs(node, host);
147 if not pref then 141 if not pref then
148 return AUTO_MUC_ARCHIVING_ENABLED; 142 return AUTO_MUC_ARCHIVING_ENABLED;
149 end 143 end
155 if never and is_in(never, jid) then 149 if never and is_in(never, jid) then
156 return false; 150 return false;
157 end 151 end
158 local default = pref.attr['default']; 152 local default = pref.attr['default'];
159 if default == 'roster' then 153 if default == 'roster' then
160 return is_in_roster(node, host, jid); 154 return is_contact_subscribed(node, host, jid_bare(jid));
161 elseif default == 'always' then 155 elseif default == 'always' then
162 return true; 156 return true;
163 elseif default == 'never' then 157 elseif default == 'never' then
164 return false; 158 return false;
165 end 159 end
166 return AUTO_MUC_ARCHIVING_ENABLED; 160 return AUTO_MUC_ARCHIVING_ENABLED;
167 end 161 end
168 162
169 local function store_msg(msg, node, host) 163 local function store_msg(msg, node, host)
170 local forwarded = st.stanza('forwarded', {xmlns='urn:xmpp:forward:tmp'}); 164 local forwarded = st.stanza('forwarded', {xmlns='urn:xmpp:forward:tmp'});
171 forwarded:tag('delay', {xmlns='urn:xmpp:delay',stamp=date_time()}):up(); 165 forwarded:tag('delay', {xmlns='urn:xmpp:delay',stamp=datetime()}):up();
172 forwarded:add_child(msg); 166 forwarded:add_child(msg);
173 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(forwarded)); 167 dm.list_append(node, host, ARCHIVE_DIR, st.preserialize(forwarded));
174 end 168 end
175 169
176 local function msg_handler(data) 170 local function msg_handler(data)
177 module:log("debug", "-- Enter muc msg_handler()"); 171 module:log("debug", "-- Enter muc msg_handler()");
178 local origin, stanza = data.origin, data.stanza; 172 local origin, stanza = data.origin, data.stanza;
179 local body = stanza:child_with_name("body"); 173 local body = stanza:child_with_name("body");
180 if body then 174 if body then
181 local from_node, from_host = jid.split(stanza.attr.from); 175 local from_node, from_host = jid_split(stanza.attr.from);
182 local to_node, to_host = jid.split(stanza.attr.to); 176 local to_node, to_host = jid_split(stanza.attr.to);
183 if um.user_exists(from_node, from_host) and apply_pref(from_node, from_host, stanza.attr.to) then 177 if user_exists(from_node, from_host) and apply_pref(from_node, from_host, stanza.attr.to) then
184 store_msg(stanza, from_node, from_host); 178 store_msg(stanza, from_node, from_host);
185 end 179 end
186 if um.user_exists(to_node, to_host) and apply_pref(to_node, to_host, stanza.attr.from) then 180 if user_exists(to_node, to_host) and apply_pref(to_node, to_host, stanza.attr.from) then
187 store_msg(stanza, to_node, to_host); 181 store_msg(stanza, to_node, to_host);
188 end 182 end
189 end 183 end
190 184
191 return nil; 185 return nil;