Annotate

plugins/vcard_update.lua @ 394:c2e959b60c13

plugins.smacks: explicitly import luasocket which stops setting globals in new versions
author Kim Alvefur <zash@zash.se>
date Thu, 03 Sep 2015 21:46:57 +0200
parent 392:cdea6a28369e
child 395:e86144a4eaa1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 244
diff changeset
1 local verse = require "verse";
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 244
diff changeset
2
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local xmlns_vcard, xmlns_vcard_update = "vcard-temp", "vcard-temp:x:update";
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
392
cdea6a28369e plugins: Use util.hashes instead of util.sha1
Kim Alvefur <zash@zash.se>
parents: 380
diff changeset
5 local sha1 = require("util.hashes").sha1;
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local ok, fun = pcall(function()
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local unb64 = require("util.encodings").base64.decode;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 assert(unb64("SGVsbG8=") == "Hello")
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 return unb64;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 if not ok then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 ok, fun = pcall(function() return require("mime").unb64; end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 if not ok then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 error("Could not find a base64 decoder")
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local unb64 = fun;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 function verse.plugins.vcard_update(stream)
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 stream:add_plugin("vcard");
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 stream:add_plugin("presence");
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local x_vcard_update;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
27 function update_vcard_photo(vCard)
229
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
28 local data;
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
29 for i=1,#vCard do
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
30 if vCard[i].name == "PHOTO" then
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
31 data = vCard[i][1];
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
32 break
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
33 end
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
34 end
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if data then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local hash = sha1(unb64(data), true);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 x_vcard_update = verse.stanza("x", { xmlns = xmlns_vcard_update })
229
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
38 :tag("photo"):text(hash);
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 stream:resend_presence()
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 else
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 x_vcard_update = nil;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 local _set_vcard = stream.set_vcard;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 --[[ TODO Complete this, it's probably broken.
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 -- Maybe better to hook outgoing stanza?
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 function stream:set_vcard(vCard, callback)
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 _set_vcard(vCard, function(event, ...)
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 if event.attr.type == "result" then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 local vCard_ = response:get_child("vCard", xmlns_vcard);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 if vCard_ then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 update_vcard_photo(vCard_);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end -- Or fetch it again? Seems wasteful, but if the server overrides stuff? :/
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if callback then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return callback(event, ...);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 --]]
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 local initial_vcard_fetch_started;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 stream:hook("ready", function(event)
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if initial_vcard_fetch_started then return; end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 initial_vcard_fetch_started = true;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 -- if stream:jid_supports(nil, xmlns_vcard) then TODO this, correctly
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 stream:get_vcard(nil, function(response)
229
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
71 if response then
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
72 update_vcard_photo(response)
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 stream:event("ready");
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 return true;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end, 3);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 stream:hook("presence-out", function(presence)
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 if x_vcard_update and not presence:get_child("x", xmlns_vcard_update) then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 presence:add_child(x_vcard_update);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end, 10);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 --[[
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 stream:hook("presence", function(presence)
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 local x_vcard_update = presence:get_child("x", xmlns_vcard_update);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local photo_hash = x_vcard_update and x_vcard_update:get_child("photo");
244
6dbd61ecd5d0 plugins.vcard_update: get_child_text()
Kim Alvefur <zash@zash.se>
parents: 229
diff changeset
89 :get_child_text("photo");
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 if x_vcard_update then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 -- TODO Cache peoples avatars here
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 --]]
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end