Annotate

plugins/jingle_ft.lua @ 142:ea753bc81a33

verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
author Matthew Wild <mwild1@gmail.com>
date Wed, 15 Sep 2010 16:52:18 +0100
parent 139:ccb8ecde8b09
child 143:641c0a1bb330
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
102
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local ltn12 = require "ltn12";
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 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
4
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 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
6 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
7
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 function verse.plugins.jingle_ft(stream)
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 stream:hook("ready", function ()
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 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
11 end, 10);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local ft_content = { name = "file" };
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 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
16 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
17 self.jingle:hook("connected", function ()
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 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
19 self.jingle:set_sink(sink);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end);
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
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return description;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local ft_mt = { __index = ft_content };
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 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
28 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
29 local file = {
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 name = file_tag.attr.name;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 size = tonumber(file_tag.attr.size);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 };
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 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
35 end);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 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
38 -- Return <description/>
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local date;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if file_info.timestamp then
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 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
42 end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 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
44 :tag("offer")
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 :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
46 name = file_info.filename, -- Mandatory
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 size = file_info.size, -- Mandatory
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 date = date,
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 hash = file_info.hash,
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 })
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 :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
52 end);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 function stream:send_file(to, filename)
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local file, err = io.open(filename);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 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
57
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 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
59 file:seek("set", 0);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local source = ltn12.source.file(file);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
139
ccb8ecde8b09 plugins.jingle_ft: Fix global access
Matthew Wild <mwild1@gmail.com>
parents: 102
diff changeset
63 local jingle = self:jingle(to);
102
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 jingle:offer("file", {
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 filename = filename:match("[^"..dirsep.."]+$");
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 size = file_size;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 });
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 jingle:hook("connected", function ()
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 jingle:set_source(source, true);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end);
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 return jingle;
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
a167d3248842 verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end