Thu, 07 Jan 2010 02:36:52 +0000
clix: Allow -p/--password to specify the login password
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 |
5
7209e1f8e66b
clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
2 | -- Clix -- Command-line XMPP |
7209e1f8e66b
clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
3 | -- Copyright (C) 2008-2010 Matthew Wild |
7209e1f8e66b
clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
4 | -- |
7209e1f8e66b
clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
5 | -- This project is MIT/X11 licensed. Please see the |
7209e1f8e66b
clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
6 | -- COPYING file in the source package for more information. |
7209e1f8e66b
clix.lua, COPYING: Add COPYING file with MIT license, add copyright header to clix.lua
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
7 | -- |
0 | 8 | require "verse" |
9 | require "verse.client" | |
10 | ||
8
df4cb4a73549
clix: Make short_opts table global, to allow commands to add their own short options
Matthew Wild <mwild1@gmail.com>
parents:
7
diff
changeset
|
11 | -- Global to allow commands to add to it |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
12 | short_opts = { v = "verbose", t = "to", f = "from", e = "type", a = "account", p = "password", r = "resource" } |
8
df4cb4a73549
clix: Make short_opts table global, to allow commands to add their own short options
Matthew Wild <mwild1@gmail.com>
parents:
7
diff
changeset
|
13 | |
0 | 14 | local command = arg[1]; |
15 | ||
16 | if not command then | |
17 | print("Command Line XMPP, available commands:"); | |
18 | for module in pairs(package.preload) do | |
19 | if module:match("^clix%.") then | |
20 | local m = require(module); | |
21 | m{ "--short-help" }; | |
22 | end | |
23 | end | |
24 | return 0; | |
25 | end | |
26 | ||
27 | local ok, m = pcall(require, "clix."..command); | |
28 | if not ok then | |
29 | print("Error running command '"..command.."' (run with --debug to see full error)"); | |
30 | if arg[2] == "--debug" then | |
31 | print(m); | |
32 | end | |
33 | return 1; | |
34 | end | |
35 | ||
36 | if type(m) ~= "function" then | |
37 | print(command.." is not a valid command"); | |
38 | return 1; | |
39 | end | |
40 | ||
41 | local accounts = { default = {} }; | |
42 | local current_account; | |
43 | for line in io.lines(os.getenv("HOME").."/.clix") do | |
44 | line = line:match("^%s*(.-)%s*$"); | |
45 | if line:match("^%[") then | |
46 | current_account = line:match("^%[(.-)%]"); | |
47 | accounts[current_account] = {}; | |
48 | if not current_account then -- This is the first defined account | |
49 | accounts.default = accounts[current_account]; | |
50 | end | |
51 | elseif current_account then | |
52 | local k,v = line:match("^(%w+)%s*[:=]%s*(.+)$"); | |
53 | accounts[current_account or "default"][k] = v; | |
54 | end | |
55 | end | |
56 | ||
57 | function clix_connect(opts, on_connect) | |
58 | local account = accounts[opts.account or "default"]; | |
59 | if not (account and account.jid) then | |
60 | io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n"); | |
61 | return nil; | |
62 | end | |
7
6078e8d2b59d
clix: Enable global Verse logger when verbose is enabled
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
63 | verse.set_logger(opts.verbose and print or function () end); |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
64 | 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
|
65 | conn.log.debug = opts.verbose; |
0 | 66 | 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
|
67 | conn:error("Authentication failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or "")); |
14
1e927484c6ec
clix: Close connection after fatal error during login
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
68 | conn:close(); |
0 | 69 | end); |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
70 | conn:hook("binding-success", function () conn:debug("Connected: "..tostring(conn)); return on_connect(conn); end); |
0 | 71 | 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
|
72 | conn:error("Resource binding failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or "")); |
14
1e927484c6ec
clix: Close connection after fatal error during login
Matthew Wild <mwild1@gmail.com>
parents:
13
diff
changeset
|
73 | conn:close(); |
0 | 74 | end); |
75 | conn:hook("disconnected", function (info) | |
76 | 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
|
77 | conn:warn("Disconnecting: %s", tostring(info.reason)); |
0 | 78 | end |
79 | verse.quit(); | |
80 | end); | |
81 | -- Optional config parameters | |
82 | conn.connect_host = account.address; | |
83 | conn.connect_port = account.port; | |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
84 | |
0 | 85 | -- Connect! |
15
54314164a2a3
clix: Allow -p/--password to specify the login password
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
86 | conn:connect_client(account.jid, opts.password or account.password); |
11
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
87 | |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
88 | if type(opts.resource) == "string" then |
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
89 | conn.resource = opts.resource; |
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
90 | end |
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
91 | |
11
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
92 | local ok, ret = pcall(verse.loop); |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
93 | if not ok and not ret:match("interrupted!$") then |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
94 | io.stderr:write("Fatal error: ", ret, "\n"); |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
95 | return 1; |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
96 | end |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
97 | return err or 0; |
0 | 98 | end |
99 | ||
100 | table.remove(arg,1); | |
101 | ||
102 | local opts = {}; | |
103 | ||
104 | local args_handled_up_to; | |
105 | for i, opt in ipairs(arg) do | |
106 | if opt:match("^%-") and opt ~= "--" then | |
107 | local name = opt:match("^%-%-?([^%s=]+)()") | |
108 | name = (short_opts[name] or name):gsub("%-+", "_"); | |
109 | if name:match("^no_") then | |
110 | name = name:sub(4, -1); | |
111 | opts[name] = false; | |
112 | else | |
113 | opts[name] = opt:match("=(.*)$") or true; | |
114 | end | |
115 | else | |
116 | args_handled_up_to = i-1; | |
117 | break; | |
118 | end | |
119 | end | |
120 | ||
121 | -- Remove all the handled args from the arg array | |
122 | for n=(args_handled_up_to or #arg),1,-1 do | |
123 | table.remove(arg, n); | |
124 | end | |
125 | ||
126 | return m(opts, arg) or 0; | |
127 |