clix.coro.lua

changeset 0
ae83411a89c9
equal deleted inserted replaced
-1:000000000000 0:ae83411a89c9
1 require "verse"
2 require "verse.client"
3
4 local command = arg[1];
5
6 if not command then
7 print("Command Line XMPP, available commands:");
8 for module in pairs(package.preload) do
9 if module:match("^clix%.") then
10 local m = require(module);
11 m{ "--short-help" };
12 end
13 end
14 return 0;
15 end
16
17 local ok, m = pcall(require, "clix."..command);
18 if not ok then
19 print("Error running command '"..command.."' (run with --debug to see full error)");
20 if arg[2] == "--debug" then
21 print(m);
22 end
23 return 1;
24 end
25
26 if type(m) ~= "function" then
27 print(command.." is not a valid command");
28 return 1;
29 end
30
31 local accounts = { default = {} };
32 local current_account;
33 for line in io.lines(os.getenv("HOME").."/.clix") do
34 line = line:match("^%s*(.-)%s*$");
35 if line:match("^%[") then
36 current_account = line:match("^%[(.-)%]");
37 accounts[current_account] = {};
38 if not current_account then -- This is the first defined account
39 accounts.default = accounts[current_account];
40 end
41 elseif current_account then
42 local k,v = line:match("^(%w+)%s*[:=]%s*(.+)$");
43 accounts[current_account or "default"][k] = v;
44 end
45 end
46
47 function clix_connect(opts)
48 local account = accounts[opts.account or "default"];
49 if not (account and account.jid) then
50 io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n");
51 return nil;
52 end
53
54 local conn = verse.new();
55 local co_verse = coroutine.create(verse.loop);
56 function conn:go(...) return coroutine.resume(co_verse, ...); end
57 conn:hook("authentication-failure", function (err)
58 io.stderr:write("Authentication failure (",err.condition or "unknown error", ")", err.text and (": "..err.text) or "", "\n");
59 coroutine.yield(nil, "authentication-failure");
60 end);
61 conn:hook("binding-success", function () io.stderr:write("Connected: ", tostring(conn), "\n"); coroutine.yield(conn); end);
62 conn:hook("binding-failure", function (err)
63 io.stderr:write("Authentication failure (",err.condition or "unknown error", ")", err.text and (": "..err.text) or "", "\n");
64 coroutine.yield(nil, "authentication-failure");
65 end);
66 -- Optional config parameters
67 conn.connect_host = account.address;
68 conn.connect_port = account.port;
69 -- Connect!
70 conn:connect_client(account.jid, account.password);
71 return coroutine.resume(co_verse);
72 end
73
74 table.remove(arg,1);
75
76 local short_opts = { v = "verbose", t = "to", f = "from", e = "type", a = "account", p = "password" }
77 local opts = {};
78
79 for i, opt in ipairs(arg) do
80 if opt:match("^%-") and opt ~= "--" then
81 local name = opt:match("^%-%-?([^%s=]+)()")
82 name = (short_opts[name] or name):gsub("%-+", "_");
83 if name:match("^no_") then
84 name = name:sub(4, -1);
85 opts[name] = false;
86 else
87 opts[name] = opt:match("=(.*)$") or true;
88 end
89 else
90 -- Remove all the handled args from the arg array
91 for n=1,(i-1) do
92 table.remove(arg, n);
93 end
94 break;
95 end
96 end
97
98 return m(opts, arg) or 0;
99

mercurial