1 |
1 |
2 -- OUT OF BAND DATA (XEP-0066) |
2 -- OUT OF BAND DATA (XEP-0066) |
3 |
3 |
4 -- library |
4 -- library |
5 |
5 |
6 require 'lm' |
6 local lm = require 'lm' |
7 local iq = require 'iq' |
7 local iq = require 'iq' |
8 |
8 |
9 -- public |
9 -- |
10 |
10 |
11 oob = { |
11 local O = { |
12 handler = |
12 handler = |
13 function ( from, url, desc, success, fail ) |
13 function ( from, url, desc, success, fail ) |
14 fail () |
14 fail () |
15 end, |
15 end, |
16 } |
16 } |
17 |
17 |
18 function oob.send ( conn, to, url, success, fail, desc ) |
18 local F = { } |
|
19 |
|
20 function F.send ( conn, to, url, success, fail, desc ) |
19 if desc then |
21 if desc then |
20 desc = { desc } |
22 desc = { desc } |
21 end |
23 end |
22 iq.send ( conn, to, 'set', |
24 iq.send ( conn, to, 'set', |
23 { |
25 { |
26 desc = desc, |
28 desc = desc, |
27 }, |
29 }, |
28 }, success, fail ) |
30 }, success, fail ) |
29 end |
31 end |
30 |
32 |
31 -- private |
33 function F.handler ( handler ) |
|
34 O.handler = handler |
|
35 end |
32 |
36 |
33 local oob_incoming_iq_handler = lm.message_handler.new ( |
37 function F.iq_handler ( conn, mess ) |
34 function ( conn, mess ) |
38 local mtype, smtype = mess:type () |
35 local mtype, smtype = mess:type () |
39 if smtype == 'set' then |
36 if smtype == 'set' then |
40 local query = mess:child ( 'query' ) |
37 local query = mess:child ( 'query' ) |
41 if query and query:attribute ( 'xmlns' ) == 'jabber:iq:oob' then |
38 if query and query:attribute ( 'xmlns' ) == 'jabber:iq:oob' then |
|
39 local from = mess:attribute ( 'from' ) |
|
40 local url = query:child( 'url' ):value () |
|
41 local desc = query:child( 'desc' ) |
|
42 if desc then |
|
43 desc = desc:value () |
|
44 end |
|
45 oob.handler ( from, url, desc, |
|
46 function () |
|
47 conn:send ( lm.message.create { mtype = 'iq-result', to = from, id = mess:attribute ( 'id' ) } ) |
|
48 end, |
|
49 function () -- XXX distinguish download error and reject |
|
50 conn:send ( |
|
51 lm.message.create { mype = 'iq-error', to = from, id = mess:attribute ( 'id' ), |
|
52 -- XXX must we include query here? |
|
53 error = { code = '406', type = 'modify', |
|
54 ['not-acceptable'] = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
55 }, |
|
56 } ) |
|
57 end ) |
|
58 return true |
|
59 end |
|
60 end |
|
61 return false |
|
62 end ) |
|
63 local oob_incoming_message_handler = lm.message_handler.new ( |
|
64 function ( conn, mess ) |
|
65 local x = mess:child ( 'x' ) |
|
66 if x and x:attribute ( 'xmlns' ) == 'jabber:x:oob' then |
|
67 local from = mess:attribute ( 'from' ) |
42 local from = mess:attribute ( 'from' ) |
68 local url = x:child( 'url' ):value () |
43 local url = query:child( 'url' ):value () |
69 local desc = x:child( 'desc' ) |
44 local desc = query:child( 'desc' ) |
70 if desc then |
45 if desc then |
71 desc = desc:value () |
46 desc = desc:value () |
72 end |
47 end |
73 oob.handler ( from, url, desc, |
48 O.handler ( from, url, desc, |
74 function () |
49 function () |
|
50 conn:send ( lm.message.create { mtype = 'iq-result', to = from, id = mess:attribute ( 'id' ) } ) |
75 end, |
51 end, |
76 function () |
52 function () -- XXX distinguish download error and reject |
|
53 conn:send ( |
|
54 lm.message.create { mype = 'iq-error', to = from, id = mess:attribute ( 'id' ), |
|
55 -- XXX must we include query here? |
|
56 error = { code = '406', type = 'modify', |
|
57 ['not-acceptable'] = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' }, |
|
58 }, |
|
59 } ) |
77 end ) |
60 end ) |
|
61 return true |
78 end |
62 end |
79 return false |
63 end |
80 end ) |
64 return false |
|
65 end |
81 |
66 |
82 -- mcabber |
67 function F.message_handler ( conn, mess ) |
|
68 local x = mess:child ( 'x' ) |
|
69 if x and x:attribute ( 'xmlns' ) == 'jabber:x:oob' then |
|
70 local from = mess:attribute ( 'from' ) |
|
71 local url = x:child( 'url' ):value () |
|
72 local desc = x:child( 'desc' ) |
|
73 if desc then |
|
74 desc = desc:value () |
|
75 end |
|
76 O.handler ( from, url, desc, |
|
77 function () |
|
78 end, |
|
79 function () |
|
80 end ) |
|
81 end |
|
82 return false |
|
83 end |
83 |
84 |
84 oob.handler = |
85 return F |
85 function ( from, url, desc, success, fail ) |
|
86 if desc then |
|
87 main.print_info ( from, 'Buddy wants you to download link: ' .. url .. ' (' .. desc .. ')' ) |
|
88 else |
|
89 main.print_info ( from, 'Buddy wants you to download link: ' .. url ) |
|
90 end |
|
91 success () |
|
92 end |
|
93 |
|
94 main.command ( 'oob', |
|
95 function ( args ) |
|
96 local who |
|
97 if args.t then |
|
98 who = args.t |
|
99 else |
|
100 who = main.full_jid () |
|
101 end |
|
102 -- here we can run something external to put file on server and obtain link to it |
|
103 oob.send ( lm.connection.bless ( main.connection () ), who, args[1], |
|
104 function () |
|
105 main.print_info ( who, 'OOB link accepted' ) |
|
106 end, |
|
107 function ( mesg ) |
|
108 main.print_info ( who, 'OOB link refused: ' .. mesg ) |
|
109 end, args[2] ) |
|
110 end, true ) |
|
111 |
|
112 local oob_handler_registered = false |
|
113 |
|
114 hooks_d['hook-post-connect'].oob = |
|
115 function ( args ) |
|
116 local conn = lm.connection.bless ( main.connection () ) |
|
117 conn:handler ( oob_incoming_iq_handler, 'iq', 'normal' ) |
|
118 conn:handler ( oob_incoming_message_handler, 'message', 'normal' ) |
|
119 conn:handler ( oob_incoming_message_handler, 'presence', 'normal' ) |
|
120 oob_handler_registered = true |
|
121 hooks_d['hook-post-connect'].oob = nil |
|
122 hooks_d['hook-quit'].oob = |
|
123 function ( args ) |
|
124 if oob_handler_registered then |
|
125 local conn = lm.connection.bless ( main.connection () ) |
|
126 conn:handler ( oob_incoming_iq_handler, 'iq' ) |
|
127 conn:handler ( oob_incoming_message_handler, 'message' ) |
|
128 conn:handler ( oob_incoming_message_handler, 'presence' ) |
|
129 end |
|
130 end |
|
131 end |
|
132 |
|
133 main.add_feature ( 'jabber:iq:oob' ) |
|
134 main.add_feature ( 'jabber:x:oob' ) |
|
135 |
86 |
136 -- vim: se ts=4: -- |
87 -- vim: se ts=4: -- |