clix/sendfilecontent.lua

Wed, 01 Dec 2021 17:25:25 +0100

author
Kim Alvefur <zash@zash.se>
date
Wed, 01 Dec 2021 17:25:25 +0100
changeset 164
fafdcde2e2eb
parent 103
45843df81db0
permissions
-rw-r--r--

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

mercurial