Comparison

plugins/jingle_ft.lua @ 102:a167d3248842

verse.plugins.jingle_ft: Jingle File Transfer application profile
author Matthew Wild <mwild1@gmail.com>
date Sat, 21 Aug 2010 15:28:54 +0100
child 139:ccb8ecde8b09
comparison
equal deleted inserted replaced
101:9c5362d393f0 102:a167d3248842
1 local ltn12 = require "ltn12";
2
3 local dirsep = package.config:sub(1,1);
4
5 local xmlns_jingle_ft = "urn:xmpp:jingle:apps:file-transfer:1";
6 local xmlns_si_file_transfer = "http://jabber.org/protocol/si/profile/file-transfer";
7
8 function verse.plugins.jingle_ft(stream)
9 stream:hook("ready", function ()
10 stream:add_disco_feature(xmlns_jingle_ft);
11 end, 10);
12
13 local ft_content = { name = "file" };
14
15 function ft_content:generate_accept(description, options)
16 if options and options.save_file then
17 self.jingle:hook("connected", function ()
18 local sink = ltn12.sink.file(io.open(options.save_file, "w+"));
19 self.jingle:set_sink(sink);
20 end);
21 end
22
23 return description;
24 end
25
26 local ft_mt = { __index = ft_content };
27 stream:hook("jingle/content/"..xmlns_jingle_ft, function (jingle, description_tag)
28 local file_tag = description_tag:get_child("offer"):get_child("file", xmlns_si_file_transfer);
29 local file = {
30 name = file_tag.attr.name;
31 size = tonumber(file_tag.attr.size);
32 };
33
34 return setmetatable({ jingle = jingle, file = file }, ft_mt);
35 end);
36
37 stream:hook("jingle/describe/file", function (file_info)
38 -- Return <description/>
39 local date;
40 if file_info.timestamp then
41 date = os.date("!%Y-%m-%dT%H:%M:%SZ", file_info.timestamp);
42 end
43 return verse.stanza("description", { xmlns = xmlns_jingle_ft })
44 :tag("offer")
45 :tag("file", { xmlns = xmlns_si_file_transfer,
46 name = file_info.filename, -- Mandatory
47 size = file_info.size, -- Mandatory
48 date = date,
49 hash = file_info.hash,
50 })
51 :tag("desc"):text(file_info.description or "");
52 end);
53
54 function stream:send_file(to, filename)
55 local file, err = io.open(filename);
56 if not file then return file, err; end
57
58 local file_size = file:seek("end", 0);
59 file:seek("set", 0);
60
61 local source = ltn12.source.file(file);
62
63 local jingle = c:jingle(to);
64 jingle:offer("file", {
65 filename = filename:match("[^"..dirsep.."]+$");
66 size = file_size;
67 });
68 jingle:hook("connected", function ()
69 jingle:set_source(source, true);
70 end);
71 return jingle;
72 end
73 end