clix/sendfilecontent.lua

changeset 103
45843df81db0
parent 102
f1166645a56c
child 164
fafdcde2e2eb
equal deleted inserted replaced
102:f1166645a56c 103:45843df81db0
1 return function (opts, arg) 1 return function (opts, arg)
2 if opts.short_help then 2 if opts.short_help then
3 print("Send file content"); 3 print("Send file content");
4 return; 4 return;
5 end 5 end
6
6 local function on_connect(conn) 7 local function on_connect(conn)
7 local function send_message(text) 8 local function send_message(text)
8 conn:send(verse.message({ to = opts.to, 9 conn:send(verse.message({ to = opts.to,
9 type = opts.type or (opts.chatroom and "groupchat") or "chat" }) 10 type = opts.type or (opts.chatroom and "groupchat") or "chat" })
10 :body(text)); 11 :body(text));
11 end 12 end
12 if opts.file then 13 if opts.file then
13 local f = io.open(opts.file, "rb") 14 local f, err = io.open(opts.file, "rb")
14 local content = f:read("*all") 15 if not f then
15 f:close() 16 conn:error("Unable to open file: %s", err);
16 if content:len() < 2000 then 17 else
17 send_message(content); 18 local content = f:read("*all")
18 else 19 f:close()
19 conn:error("File size too large. Cannot send file"); 20 if content:len() < 2000 then
20 end 21 send_message(content);
21 conn:close(); 22 else
23 conn:error("File size too large. Cannot send file");
24 end
25 end
22 end 26 end
27 conn:close();
23 end 28 end
24 clix_connect(opts, on_connect); 29 clix_connect(opts, on_connect);
25 end 30 end

mercurial