54
|
1 |
|
|
2 |
-- USER AVATAR (XEP-0084) |
|
3 |
|
|
4 |
-- library |
|
5 |
|
|
6 |
require 'lm' |
|
7 |
require 'sha1' |
|
8 |
require 'base64' |
|
9 |
require 'pubsub' |
|
10 |
require 'pep' |
|
11 |
|
|
12 |
-- public |
|
13 |
|
|
14 |
avatar = { } |
|
15 |
|
|
16 |
-- TODO 'temporary disabling' |
|
17 |
-- however I cannot see a method to enable it back without republishing avatar :( |
|
18 |
-- this requires client to know, what is published on the server now |
|
19 |
-- maybe we can do that by requesting item without payload from server |
|
20 |
|
|
21 |
function avatar.publish ( conn, data, success, fail, height, width ) |
|
22 |
local id = sha1.digest ( data ) |
|
23 |
pep.publish ( conn, 'urn:xmpp:avatar:data', |
|
24 |
{ id = id, |
|
25 |
data = { xmlns = 'urn:xmpp:avatar:data', |
|
26 |
base64.encode ( data ), |
|
27 |
}, |
|
28 |
}, |
|
29 |
function () |
|
30 |
pep.publish ( conn, 'urn:xmpp:avatar:metadata', |
|
31 |
{ id = id, |
|
32 |
info = { bytes = data:len (), id = id, type = 'image/png', height = height, width = width }, |
|
33 |
}, success, fail ) |
|
34 |
end, fail ) |
|
35 |
end |
|
36 |
|
|
37 |
function avatar.publish_url ( conn, url, iid, size, id, mtype, success, fail, height, width ) |
|
38 |
pep.publish ( conn, 'urn:xmpp:avatar:metadata', |
|
39 |
{ id = iid, |
|
40 |
info = { bytes = size, id = id, type = mtype, url = url, height = height, width = width }, |
|
41 |
}, success, fail ) |
|
42 |
end |
|
43 |
|
|
44 |
function avatar.get ( conn, from, id, success, fail ) |
|
45 |
pubsub.retrieve ( conn, from, 'urn:xmpp:avatar:data', |
|
46 |
function ( from, node, item ) |
|
47 |
local data = item:child ( 'data' ) |
|
48 |
if data then |
|
49 |
success ( data:value () ) |
|
50 |
else |
|
51 |
-- XXX |
|
52 |
end |
|
53 |
end, fail, nil, { id } ) |
|
54 |
end |
|
55 |
|
|
56 |
-- mcabber |
|
57 |
|
|
58 |
pep.handlers['urn:xmpp:avatar:metadata'] = |
|
59 |
function ( from, node, data ) |
|
60 |
if not main.yesno ( main.option ( 'lua_pep_notification' ) ) then |
|
61 |
return true |
|
62 |
end |
|
63 |
local item = data:children () |
|
64 |
while item do |
|
65 |
main.print_info ( from, ('Avatar: %s [%s] %s bytes, %sx%s %s'):format ( |
|
66 |
item:attribute ( 'id' ), |
|
67 |
item:attribute ( 'type' ), |
|
68 |
item:attribute ( 'bytes' ), |
|
69 |
item:attribute ( 'width' ) or '?', |
|
70 |
item:attribute ( 'height' ) or '?', |
|
71 |
item:attribute ( 'url' ) or '' ) ) |
|
72 |
item = item:next () |
|
73 |
end |
|
74 |
end |
|
75 |
|
|
76 |
main.command ( 'avatar', |
|
77 |
function ( args ) |
|
78 |
local action = args[1] |
|
79 |
if action == 'get' then |
|
80 |
local who |
|
81 |
if args.t then |
|
82 |
who = args.t |
|
83 |
else |
|
84 |
who = main.current_buddy () |
|
85 |
end |
|
86 |
avatar.get ( lm.connection.bless ( main.connection () ), who, args[2], |
|
87 |
function ( data ) |
|
88 |
local h = io.open ( args[3], 'w' ) |
|
89 |
if h then |
|
90 |
h:write ( data ) |
|
91 |
h:close () |
|
92 |
main.print_info ( who, 'Avatar saved to ' .. args[3] ) |
|
93 |
else |
|
94 |
print ( 'Cannot open file for writing ' .. args[3] ) |
|
95 |
end |
|
96 |
end, |
|
97 |
function ( mesg ) |
|
98 |
main.print_info ( who, 'Error obtaining avatar: ' .. mesg ) |
|
99 |
end ) |
|
100 |
else |
|
101 |
local file = action |
|
102 |
if action == 'set' then |
|
103 |
file = args[2] |
|
104 |
end |
|
105 |
local h = io.open ( file ) |
|
106 |
if h then |
|
107 |
data = h:read ( '*a' ) |
|
108 |
h:close () |
|
109 |
avatar.publish ( lm.connection.bless ( main.connection () ), data, |
|
110 |
function () |
|
111 |
print ( 'Avatar published' ) |
|
112 |
end, |
|
113 |
function ( mesg ) |
|
114 |
print ( 'Avatar publishing error: ' .. mesg ) |
|
115 |
end ) |
|
116 |
else |
|
117 |
print ( 'Cannot open file ' .. file ) |
|
118 |
end |
|
119 |
end |
|
120 |
end, true, 'file' ) |
|
121 |
|
|
122 |
commands_help['avatar'] = '[-t jid] get id filename | [set] filename\n\nGet action tries to get from server avatar with specified id and save it to \'filename\'.\nSet action publishes avatar to server. File must be a PNG image.' |
|
123 |
|
|
124 |
main.add_feature ( 'urn:xmpp:avatar:metadata+notify' ) |
|
125 |
|
|
126 |
-- vim: se ts=4: -- |