clix/sendfilecontent.lua

Sun, 15 Nov 2020 20:45:50 +0100

author
Kim Alvefur <zash@zash.se>
date
Sun, 15 Nov 2020 20:45:50 +0100
changeset 140
8815232cbbeb
parent 103
45843df81db0
child 164
fafdcde2e2eb
permissions
-rw-r--r--

clix.watch_pep: Take PEP node as positional argument

Thanks MattJ

102
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
1 return function (opts, arg)
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
2 if opts.short_help then
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
3 print("Send file content");
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
4 return;
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
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
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
7 local function on_connect(conn)
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
8 local function send_message(text)
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
9 conn:send(verse.message({ to = opts.to,
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
10 type = opts.type or (opts.chatroom and "groupchat") or "chat" })
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
11 :body(text));
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
12 end
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
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
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
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
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
28 end
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
29 clix_connect(opts, on_connect);
f1166645a56c new sendfilecontent plugin
Marc Fauser
parents:
diff changeset
30 end

mercurial