Wed, 06 Jan 2010 18:19:47 +0000
clix.lua: Add shebang for people who don't use squish
4
ead275885948
clix.lua: Add shebang for people who don't use squish
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
1 | #!/usr/bin/env lua |
0 | 2 | require "verse" |
3 | require "verse.client" | |
4 | ||
5 | local command = arg[1]; | |
6 | ||
7 | if not command then | |
8 | print("Command Line XMPP, available commands:"); | |
9 | for module in pairs(package.preload) do | |
10 | if module:match("^clix%.") then | |
11 | local m = require(module); | |
12 | m{ "--short-help" }; | |
13 | end | |
14 | end | |
15 | return 0; | |
16 | end | |
17 | ||
18 | local ok, m = pcall(require, "clix."..command); | |
19 | if not ok then | |
20 | print("Error running command '"..command.."' (run with --debug to see full error)"); | |
21 | if arg[2] == "--debug" then | |
22 | print(m); | |
23 | end | |
24 | return 1; | |
25 | end | |
26 | ||
27 | if type(m) ~= "function" then | |
28 | print(command.." is not a valid command"); | |
29 | return 1; | |
30 | end | |
31 | ||
32 | local accounts = { default = {} }; | |
33 | local current_account; | |
34 | for line in io.lines(os.getenv("HOME").."/.clix") do | |
35 | line = line:match("^%s*(.-)%s*$"); | |
36 | if line:match("^%[") then | |
37 | current_account = line:match("^%[(.-)%]"); | |
38 | accounts[current_account] = {}; | |
39 | if not current_account then -- This is the first defined account | |
40 | accounts.default = accounts[current_account]; | |
41 | end | |
42 | elseif current_account then | |
43 | local k,v = line:match("^(%w+)%s*[:=]%s*(.+)$"); | |
44 | accounts[current_account or "default"][k] = v; | |
45 | end | |
46 | end | |
47 | ||
48 | function clix_connect(opts, on_connect) | |
49 | local account = accounts[opts.account or "default"]; | |
50 | if not (account and account.jid) then | |
51 | io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n"); | |
52 | return nil; | |
53 | end | |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
54 | verse.set_logger(function () end); |
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
55 | local conn = verse.new(verse.logger()); |
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
56 | conn.log.debug = opts.verbose; |
0 | 57 | conn:hook("authentication-failure", function (err) |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
58 | conn:error("Authentication failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or "")); |
0 | 59 | end); |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
60 | conn:hook("binding-success", function () conn:debug("Connected: "..tostring(conn)); return on_connect(conn); end); |
0 | 61 | conn:hook("binding-failure", function (err) |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
62 | conn:error("Resource binding failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or "")); |
0 | 63 | end); |
64 | conn:hook("disconnected", function (info) | |
65 | if info.reason then | |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
66 | conn:warn("Disconnecting: %s", tostring(info.reason)); |
0 | 67 | end |
68 | verse.quit(); | |
69 | end); | |
70 | -- Optional config parameters | |
71 | conn.connect_host = account.address; | |
72 | conn.connect_port = account.port; | |
73 | -- Connect! | |
74 | conn:connect_client(account.jid, account.password); | |
75 | return verse.loop(); | |
76 | end | |
77 | ||
78 | table.remove(arg,1); | |
79 | ||
80 | local short_opts = { v = "verbose", t = "to", f = "from", e = "type", a = "account", p = "password" } | |
81 | local opts = {}; | |
82 | ||
83 | local args_handled_up_to; | |
84 | for i, opt in ipairs(arg) do | |
85 | if opt:match("^%-") and opt ~= "--" then | |
86 | local name = opt:match("^%-%-?([^%s=]+)()") | |
87 | name = (short_opts[name] or name):gsub("%-+", "_"); | |
88 | if name:match("^no_") then | |
89 | name = name:sub(4, -1); | |
90 | opts[name] = false; | |
91 | else | |
92 | opts[name] = opt:match("=(.*)$") or true; | |
93 | end | |
94 | else | |
95 | args_handled_up_to = i-1; | |
96 | break; | |
97 | end | |
98 | end | |
99 | ||
100 | -- Remove all the handled args from the arg array | |
101 | for n=(args_handled_up_to or #arg),1,-1 do | |
102 | table.remove(arg, n); | |
103 | end | |
104 | ||
105 | return m(opts, arg) or 0; | |
106 |