Annotate

plugins/jingle_ft.lua @ 274:a228f3c7808a

plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
author Kim Alvefur <zash@zash.se>
date Tue, 10 Jan 2012 21:08:19 +0100
parent 250:a5ac643a7fd6
child 379:d80d27234e38
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: 143
diff changeset
1 local verse = require "verse";
102
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local ltn12 = require "ltn12";
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local dirsep = package.config:sub(1,1);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local xmlns_jingle_ft = "urn:xmpp:jingle:apps:file-transfer:1";
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local xmlns_si_file_transfer = "http://jabber.org/protocol/si/profile/file-transfer";
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 function verse.plugins.jingle_ft(stream)
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 stream:hook("ready", function ()
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 stream:add_disco_feature(xmlns_jingle_ft);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end, 10);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
143
641c0a1bb330 plugins.jingle_ft: Change content 'name' field to 'type' to avoid confusion with the 'name' attribute of the <content> tag in Jingle
Matthew Wild <mwild1@gmail.com>
parents: 139
diff changeset
14 local ft_content = { type = "file" };
102
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 function ft_content:generate_accept(description, options)
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if options and options.save_file then
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 self.jingle:hook("connected", function ()
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local sink = ltn12.sink.file(io.open(options.save_file, "w+"));
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 self.jingle:set_sink(sink);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return description;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local ft_mt = { __index = ft_content };
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 stream:hook("jingle/content/"..xmlns_jingle_ft, function (jingle, description_tag)
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local file_tag = description_tag:get_child("offer"):get_child("file", xmlns_si_file_transfer);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local file = {
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 name = file_tag.attr.name;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 size = tonumber(file_tag.attr.size);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 };
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return setmetatable({ jingle = jingle, file = file }, ft_mt);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 stream:hook("jingle/describe/file", function (file_info)
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 -- Return <description/>
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local date;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if file_info.timestamp then
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 date = os.date("!%Y-%m-%dT%H:%M:%SZ", file_info.timestamp);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return verse.stanza("description", { xmlns = xmlns_jingle_ft })
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 :tag("offer")
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 :tag("file", { xmlns = xmlns_si_file_transfer,
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 name = file_info.filename, -- Mandatory
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 size = file_info.size, -- Mandatory
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 date = date,
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 hash = file_info.hash,
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 })
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 :tag("desc"):text(file_info.description or "");
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 function stream:send_file(to, filename)
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local file, err = io.open(filename);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if not file then return file, err; end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local file_size = file:seek("end", 0);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 file:seek("set", 0);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local source = ltn12.source.file(file);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
139
ccb8ecde8b09 plugins.jingle_ft: Fix global access
Matthew Wild <mwild1@gmail.com>
parents: 102
diff changeset
64 local jingle = self:jingle(to);
102
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 jingle:offer("file", {
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 filename = filename:match("[^"..dirsep.."]+$");
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 size = file_size;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 });
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 jingle:hook("connected", function ()
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 jingle:set_source(source, true);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return jingle;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end