Software /
code /
verse
Annotate
plugins/jingle_ft.lua @ 485:c9a144591649
component: Avoid adding to the global stream metatable
This allows component and client connections to be made side-by-side.
Previous to this change, loading this connection module would break the
ability to make client connections, due to overriding stream methods such as
:reopen() and :reset().
A next step would be to share the methods that the two connection modules have
in common.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 23 Mar 2023 18:56:32 +0000 |
parent | 380:0891b4e27766 |
rev | line source |
---|---|
250 | 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 |
379
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
6 local xmlns_jingle_ft = "urn:xmpp:jingle:apps:file-transfer:4"; |
102
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); |
380 | 12 |
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
|
13 local ft_content = { type = "file" }; |
380 | 14 |
102
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 |
380 | 22 |
102
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 |
380 | 25 |
102
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) |
379
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
28 local file_tag = description_tag:get_child("file"); |
102
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local file = { |
379
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
30 name = file_tag:get_child_text("name"); |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
31 size = tonumber(file_tag:get_child_text("size")); |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
32 desc = file_tag:get_child_text("desc"); |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
33 date = file_tag:get_child_text("date"); |
102
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 }; |
380 | 35 |
102
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 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
|
37 end); |
380 | 38 |
102
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 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
|
40 -- Return <description/> |
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 local date; |
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 if file_info.timestamp then |
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 end |
a167d3248842
verse.plugins.jingle_ft: Jingle File Transfer application profile
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return verse.stanza("description", { xmlns = xmlns_jingle_ft }) |
379
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
46 :tag("file") |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
47 :tag("name"):text(file_info.filename):up() |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
48 :tag("size"):text(tostring(file_info.size)):up() |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
49 :tag("date"):text(date):up() |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
50 :tag("desc"):text(file_info.description):up() |
d80d27234e38
plugins.jingle_ft: Update to version 0.16
Kim Alvefur <zash@zash.se>
parents:
250
diff
changeset
|
51 :up(); |
102
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 |
380 | 57 |
102
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); |
380 | 60 |
102
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); |
380 | 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 |