plugins/jingle_ft.lua

changeset 380
0891b4e27766
parent 379
d80d27234e38
equal deleted inserted replaced
379:d80d27234e38 380:0891b4e27766
7 7
8 function verse.plugins.jingle_ft(stream) 8 function verse.plugins.jingle_ft(stream)
9 stream:hook("ready", function () 9 stream:hook("ready", function ()
10 stream:add_disco_feature(xmlns_jingle_ft); 10 stream:add_disco_feature(xmlns_jingle_ft);
11 end, 10); 11 end, 10);
12 12
13 local ft_content = { type = "file" }; 13 local ft_content = { type = "file" };
14 14
15 function ft_content:generate_accept(description, options) 15 function ft_content:generate_accept(description, options)
16 if options and options.save_file then 16 if options and options.save_file then
17 self.jingle:hook("connected", function () 17 self.jingle:hook("connected", function ()
18 local sink = ltn12.sink.file(io.open(options.save_file, "w+")); 18 local sink = ltn12.sink.file(io.open(options.save_file, "w+"));
19 self.jingle:set_sink(sink); 19 self.jingle:set_sink(sink);
20 end); 20 end);
21 end 21 end
22 22
23 return description; 23 return description;
24 end 24 end
25 25
26 local ft_mt = { __index = ft_content }; 26 local ft_mt = { __index = ft_content };
27 stream:hook("jingle/content/"..xmlns_jingle_ft, function (jingle, description_tag) 27 stream:hook("jingle/content/"..xmlns_jingle_ft, function (jingle, description_tag)
28 local file_tag = description_tag:get_child("file"); 28 local file_tag = description_tag:get_child("file");
29 local file = { 29 local file = {
30 name = file_tag:get_child_text("name"); 30 name = file_tag:get_child_text("name");
31 size = tonumber(file_tag:get_child_text("size")); 31 size = tonumber(file_tag:get_child_text("size"));
32 desc = file_tag:get_child_text("desc"); 32 desc = file_tag:get_child_text("desc");
33 date = file_tag:get_child_text("date"); 33 date = file_tag:get_child_text("date");
34 }; 34 };
35 35
36 return setmetatable({ jingle = jingle, file = file }, ft_mt); 36 return setmetatable({ jingle = jingle, file = file }, ft_mt);
37 end); 37 end);
38 38
39 stream:hook("jingle/describe/file", function (file_info) 39 stream:hook("jingle/describe/file", function (file_info)
40 -- Return <description/> 40 -- Return <description/>
41 local date; 41 local date;
42 if file_info.timestamp then 42 if file_info.timestamp then
43 date = os.date("!%Y-%m-%dT%H:%M:%SZ", file_info.timestamp); 43 date = os.date("!%Y-%m-%dT%H:%M:%SZ", file_info.timestamp);
52 end); 52 end);
53 53
54 function stream:send_file(to, filename) 54 function stream:send_file(to, filename)
55 local file, err = io.open(filename); 55 local file, err = io.open(filename);
56 if not file then return file, err; end 56 if not file then return file, err; end
57 57
58 local file_size = file:seek("end", 0); 58 local file_size = file:seek("end", 0);
59 file:seek("set", 0); 59 file:seek("set", 0);
60 60
61 local source = ltn12.source.file(file); 61 local source = ltn12.source.file(file);
62 62
63 local jingle = self:jingle(to); 63 local jingle = self:jingle(to);
64 jingle:offer("file", { 64 jingle:offer("file", {
65 filename = filename:match("[^"..dirsep.."]+$"); 65 filename = filename:match("[^"..dirsep.."]+$");
66 size = file_size; 66 size = file_size;
67 }); 67 });

mercurial