Mon, 23 Nov 2020 23:52:31 +0100
clix.watch_pep: Move invocation syntax out of short help
So it doesn't clutter the command listing
102 | 1 | return function (opts, arg) |
2 | if opts.short_help then | |
3 | print("Send file content"); | |
4 | return; | |
5 | end | |
103
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
6 | |
102 | 7 | local function on_connect(conn) |
8 | local function send_message(text) | |
9 | conn:send(verse.message({ to = opts.to, | |
10 | type = opts.type or (opts.chatroom and "groupchat") or "chat" }) | |
11 | :body(text)); | |
12 | end | |
13 | 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
|
14 | 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
|
15 | 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
|
16 | 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
|
17 | else |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
18 | 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
|
19 | f:close() |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
20 | 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
|
21 | send_message(content); |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
22 | else |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
23 | 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
|
24 | end |
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
25 | end |
102 | 26 | end |
103
45843df81db0
sendfilecontent: Fix indentation and show error if file cannot be opened
Matthew Wild <mwild1@gmail.com>
parents:
102
diff
changeset
|
27 | conn:close(); |
102 | 28 | end |
29 | clix_connect(opts, on_connect); | |
30 | end |