Annotate

plugins/vcard_update.lua @ 242:ab4773b0ef5e

verse: Add default log handler for errors
author Matthew Wild <mwild1@gmail.com>
date Sun, 27 Nov 2011 23:00:15 +0000
parent 229:279c0e89c3b3
child 244:6dbd61ecd5d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 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
2
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- MMMmmmm.. hacky
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local ok, fun = pcall(function() return require("util.hashes").sha1; end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 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
6 ok, fun = pcall(function() return require("util.sha1").sha1; end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 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
8 error("Could not find a sha1()")
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local sha1 = fun;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 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
14 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
15 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
16 return unb64;
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 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
19 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
20 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
21 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
22 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local unb64 = fun;
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 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
27 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
28 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
29
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 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
32
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 function update_vcard_photo(vCard)
229
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
34 local data;
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
35 for i=1,#vCard do
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
36 if vCard[i].name == "PHOTO" then
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
37 data = vCard[i][1];
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
38 break
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
39 end
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
40 end
196
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 if data then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 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
43 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
44 :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
45
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 stream:resend_presence()
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 else
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 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
49 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 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
53
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 --[[ 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
55 -- 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
56 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
57 _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
58 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
59 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
60 if vCard_ then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 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
62 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
63 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 if callback then
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 return callback(event, ...);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 --]]
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 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
72 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
73 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
74 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
75 -- 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
76 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
77 if response then
279c0e89c3b3 plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents: 196
diff changeset
78 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
79 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 stream:event("ready");
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 return true;
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end, 3);
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 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
86 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
87 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
88 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 end, 10);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 --[[
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 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
93 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
94 local photo_hash = x_vcard_update and x_vcard_update:get_child("photo");
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 :get_child("photo"):get_text(hash);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 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
97 -- 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
98 end
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end);
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 --]]
eb9d69d3f0b5 plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 end