Annotate

plugins/vcard_update.lua @ 464:dc05a8d6dbfb

Fix whitespace
author Matthew Wild <mwild1@gmail.com>
date Thu, 16 Mar 2023 16:47:59 +0000
parent 395:e86144a4eaa1
child 490:6b2f31da9610
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
395
e86144a4eaa1 plugins: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 392
diff changeset
3 -- local xmlns_vcard = "vcard-temp";
e86144a4eaa1 plugins: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 392
diff changeset
4 local xmlns_vcard_update = "vcard-temp:x:update";
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
392
cdea6a28369e plugins: Use util.hashes instead of util.sha1
Kim Alvefur <zash@zash.se>
parents: 380
diff changeset
6 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
7
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 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
9 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
10 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
11 return unb64;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 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
14 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
15 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
16 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
17 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local unb64 = fun;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 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
22 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
23 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
24
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 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
27
395
e86144a4eaa1 plugins: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 392
diff changeset
28 local function update_vcard_photo(vCard)
229
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
29 local data;
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
30 for i=1,#vCard do
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
31 if vCard[i].name == "PHOTO" then
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
32 data = vCard[i][1];
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
33 break
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
34 end
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
35 end
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if data then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 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
38 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
39 :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
40
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 stream:resend_presence()
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 else
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 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
44 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
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?
395
e86144a4eaa1 plugins: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 392
diff changeset
50 local _set_vcard = stream.set_vcard;
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 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
52 _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
53 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
54 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
55 if vCard_ then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 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
57 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
58 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 if callback then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 return callback(event, ...);
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 end
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
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local initial_vcard_fetch_started;
395
e86144a4eaa1 plugins: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 392
diff changeset
67 stream:hook("ready", function()
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 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
69 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
70 -- 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
71 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
72 if response then
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
73 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
74 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 stream:event("ready");
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 return true;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 end, 3);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 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
81 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
82 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
83 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end, 10);
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 --[[
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 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
88 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
89 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
90 :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
91 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
92 -- 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
93 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 --]]
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end