68
|
1 |
|
|
2 |
local lm = require 'lm' |
|
3 |
local iq = require 'lm.iq' |
|
4 |
|
|
5 |
local F = { } |
|
6 |
local O = { |
|
7 |
handler = |
|
8 |
function ( name ) |
|
9 |
end, |
|
10 |
} |
|
11 |
|
|
12 |
function F.handler ( handler ) |
|
13 |
O.handler = handler |
|
14 |
end |
|
15 |
|
|
16 |
function F.lists ( conn, success, fail ) |
|
17 |
iq.send ( conn, nil, 'get', |
|
18 |
{ |
|
19 |
query = { xmlns = 'jabber:iq:privacy' }, |
|
20 |
}, |
|
21 |
function ( mess ) |
|
22 |
local ret = { } |
|
23 |
local item = mess:child( 'query' ):child () |
|
24 |
while item do |
|
25 |
local tag = item:name () |
|
26 |
local name = item:attribute ( 'name' ) |
|
27 |
if tag == 'list' then |
|
28 |
table.insert ( ret, name ) |
|
29 |
elseif tag == 'active' or tag == 'default' then |
|
30 |
ret[tag] = name |
|
31 |
end |
|
32 |
item = item:next () |
|
33 |
end |
|
34 |
success ( ret ) |
|
35 |
end, fail ) |
|
36 |
end |
|
37 |
|
|
38 |
function F.list ( conn, name, success, fail ) |
|
39 |
iq.send ( conn, nil, 'get', |
|
40 |
{ |
|
41 |
query = { xmlns = 'jabber:iq:privacy', |
|
42 |
list = { name = name }, |
|
43 |
}, |
|
44 |
}, |
|
45 |
function ( mess ) |
|
46 |
local ret = { } |
|
47 |
local item = mess:path( 'query', 'list' ):child () |
|
48 |
while item do |
|
49 |
local q = { |
|
50 |
type = item:attribute ( 'type' ), |
|
51 |
value = item:attribute ( 'value' ), |
|
52 |
action = item:attribute ( 'action' ), |
|
53 |
order = item:attribute ( 'order' ), |
|
54 |
} |
|
55 |
local stanza = item:child () |
|
56 |
if stanza then |
|
57 |
q.stanzas = { } |
|
58 |
while stanza do |
|
59 |
table.insert ( q.stanzas, stanza:name () ) |
|
60 |
stanza = stanza:next () |
|
61 |
end |
|
62 |
end |
|
63 |
item = item:next () |
|
64 |
end |
|
65 |
success ( ret ) |
|
66 |
end, fail ) |
|
67 |
end |
|
68 |
|
|
69 |
-- name may be nil |
|
70 |
function F.active ( conn, name, success, fail ) |
|
71 |
iq.send ( conn, nil, 'set', |
|
72 |
{ |
|
73 |
query = { xmlns = 'jabber:iq:privacy', |
|
74 |
active = { name = name }, |
|
75 |
}, |
|
76 |
}, success, fail ) |
|
77 |
end |
|
78 |
|
|
79 |
-- name may be nil |
|
80 |
function F.default ( conn, name, success, fail ) |
|
81 |
iq.send ( conn, nil, 'set', |
|
82 |
{ |
|
83 |
query = { xmlns = 'jabber:iq:privacy', |
|
84 |
default = { name = name }, |
|
85 |
}, |
|
86 |
}, success, fail ) |
|
87 |
end |
|
88 |
|
|
89 |
function F.set ( conn, name, list, success, fail ) |
|
90 |
local items = { } |
|
91 |
local order = 1 |
|
92 |
for i, item in ipairs ( list ) do |
|
93 |
if list.order then |
|
94 |
order = list.order |
|
95 |
else |
|
96 |
order = order + 1 |
|
97 |
end |
|
98 |
local q = { |
|
99 |
type = list.type, |
|
100 |
value = list.value, |
|
101 |
action = list.action, |
|
102 |
order = order, |
|
103 |
} |
|
104 |
if list.stanzas then |
|
105 |
for j, stanza in ipairs ( list.stanzas ) do |
|
106 |
q[stanza] = { } |
|
107 |
end |
|
108 |
end |
|
109 |
table.insert ( items, q ) |
|
110 |
end |
|
111 |
iq.send ( conn, nil, 'set', |
|
112 |
{ |
|
113 |
query = { xmlns = 'jabber:iq:privacy', |
|
114 |
list = { name = name, |
|
115 |
item = items, |
|
116 |
}, |
|
117 |
}, |
|
118 |
}, success, fail ) |
|
119 |
end |
|
120 |
|
|
121 |
function F.iq_handler ( conn, mess ) |
|
122 |
local query = mess:child ( 'query' ) |
|
123 |
if query and query:attribute ( 'xmlns' ) == 'jabber:iq:privacy' then |
|
124 |
conn:send ( lm.message.create { mtype = 'iq-result', id = mess:attribute ( 'id' ) } ) |
|
125 |
O.handler ( query:child( 'list' ):attribute ( 'name' ) ) |
|
126 |
return true |
|
127 |
end |
|
128 |
return false |
|
129 |
end |
|
130 |
|
|
131 |
return F |
|
132 |
|
|
133 |
-- vim: se ts=4: -- |