Software /
code /
verse
Annotate
plugins/vcard_update.lua @ 251:e44ddcd84234
fixed sending of leave message
author | mva <mva@mva.name> |
---|---|
date | Fri, 02 Dec 2011 11:27:38 +0600 |
parent | 250:a5ac643a7fd6 |
child | 380:0891b4e27766 |
rev | line source |
---|---|
250 | 1 local verse = require "verse"; |
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 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- MMMmmmm.. hacky |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 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
|
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 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
|
9 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
|
10 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
|
11 end |
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 local sha1 = fun; |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 return unb64; |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 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 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
|
22 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
|
23 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
|
24 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local unb64 = fun; |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 |
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 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
|
34 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 function update_vcard_photo(vCard) |
229
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
36 local data; |
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
37 for i=1,#vCard do |
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
38 if vCard[i].name == "PHOTO" then |
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
39 data = vCard[i][1]; |
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
40 break |
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
41 end |
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
42 end |
196
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 if data then |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 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
|
45 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
|
46 :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
|
47 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 stream:resend_presence() |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 else |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 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
|
51 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 end |
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 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
|
55 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 --[[ 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
|
57 -- 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
|
58 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
|
59 _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
|
60 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
|
61 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
|
62 if vCard_ then |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 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
|
64 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
|
65 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if callback then |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 return callback(event, ...); |
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 end); |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 --]] |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 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
|
74 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
|
75 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
|
76 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
|
77 -- 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
|
78 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
|
79 if response then |
279c0e89c3b3
plugins.vcard_update: Update for util.vcard changes.
Kim Alvefur <zash@zash.se>
parents:
196
diff
changeset
|
80 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
|
81 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 stream:event("ready"); |
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 return true; |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 end, 3); |
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-out", function(presence) |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 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
|
89 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
|
90 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 end, 10); |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 --[[ |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 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
|
95 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
|
96 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
|
97 :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
|
98 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
|
99 -- 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
|
100 end |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 end); |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 --]] |
eb9d69d3f0b5
plugins.vcard_update: Support for XEP-153 avatar hash in presence broadcasts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 end |