Wed, 01 Dec 2021 17:25:25 +0100
clix: Import Verse where needed
In the olden days of `module()` this would have been a global, but that
is no longer the convention nor the case.
164 | 1 | local verse = require "verse"; |
102 | 2 | return function (opts, arg) |
3 | if opts.short_help then | |
4 | print("Send file content"); | |
5 | return; | |
6 | end | |
103
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
7 | |
102 | 8 | local function on_connect(conn) |
9 | local function send_message(text) | |
10 | conn:send(verse.message({ to = opts.to, | |
11 | type = opts.type or (opts.chatroom and "groupchat") or "chat" }) | |
12 | :body(text)); | |
13 | end | |
14 | if opts.file then | |
103
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
15 | local f, err = io.open(opts.file, "rb") |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
16 | if not f then |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
17 | conn:error("Unable to open file: %s", err); |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
18 | else |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
19 | local content = f:read("*all") |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
20 | f:close() |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
21 | if content:len() < 2000 then |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
22 | send_message(content); |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
23 | else |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
24 | conn:error("File size too large. Cannot send file"); |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
25 | end |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
26 | end |
102 | 27 | end |
103
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
28 | conn:close(); |
102 | 29 | end |
30 | clix_connect(opts, on_connect); | |
31 | end |