Software /
code /
verse
Annotate
plugins/vcard_update.lua @ 224:7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 02 Nov 2011 01:17:54 +0100 |
parent | 196:eb9d69d3f0b5 |
child | 229:279c0e89c3b3 |
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) |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local photo = vCard and vCard:get_child("PHOTO"); |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local binval = photo and photo:get_child("BINVAL"); |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local data = binval and binval:get_text(); |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 if data then |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 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
|
39 x_vcard_update = verse.stanza("x", { xmlns = xmlns_vcard_update }) |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 :tag("photo"):text(hash); |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 stream:resend_presence() |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 else |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 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
|
45 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 end |
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 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
|
49 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 --[[ 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
|
51 -- 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
|
52 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
|
53 _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
|
54 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
|
55 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
|
56 if vCard_ then |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 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
|
58 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
|
59 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 if callback then |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 return callback(event, ...); |
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 end |
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 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 -- 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
|
72 stream:get_vcard(nil, function(response) |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 -- FIXME Picking out the vCard element should be done in get_vcard() |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 if response.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
|
75 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
|
76 if vCard then |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 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
|
78 end |
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 |