Software /
code /
clix
Annotate
clix.lua @ 32:cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
author | Matthew Wild |
---|---|
date | Sat, 29 May 2010 20:58:27 +0100 |
parent | 28:c2998f70dfd4 |
child | 33:f1901c9de891 |
rev | line source |
---|---|
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 |
23
c5f04bdc7c64
clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
12 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type", |
c5f04bdc7c64
clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
13 a = "account", p = "password", r = "resource", o = "presence", c = "chatroom" } |
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
|
14 |
0 | 15 local command = arg[1]; |
16 | |
17 if not command then | |
18 print("Command Line XMPP, available commands:"); | |
19 for module in pairs(package.preload) do | |
20 if module:match("^clix%.") then | |
21 local m = require(module); | |
28
c2998f70dfd4
clix: Pass short_help option to modules correctly
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
22 io.write("\t", module:gsub("^clix%.", ""), ": "); |
c2998f70dfd4
clix: Pass short_help option to modules correctly
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
23 m({ short_help = true }, {}); |
0 | 24 end |
25 end | |
26 return 0; | |
27 end | |
28 | |
29 local ok, m = pcall(require, "clix."..command); | |
30 if not ok then | |
20
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
31 local is_debug; |
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
32 for i=1,#arg do |
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
33 if arg[i] == "--debug" then |
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
34 is_debug = true; break; |
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
35 end |
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
36 end |
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
37 print("Error running command '"..command..(is_debug and "" or "' (run with --debug to see full error)")); |
80cc61512f10
clix: Allow --debug to be anywhere on the command-line
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
38 if is_debug then |
0 | 39 print(m); |
40 end | |
41 return 1; | |
42 end | |
43 | |
44 if type(m) ~= "function" then | |
45 print(command.." is not a valid command"); | |
46 return 1; | |
47 end | |
48 | |
49 local accounts = { default = {} }; | |
50 local current_account; | |
32
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
51 |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
52 local config_path = (os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME").."/.config"); |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
53 local config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix"); |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
54 |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
55 if not config_file then |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
56 print("Unable to open config file... looked for "..config_path.."/.clixrc"); |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
57 if err then print(err); end |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
58 os.exit(1); |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
59 end |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
60 |
cda6a004ff79
clix.lua: Report when config file couldn't be found, change name to .clixrc and comply with fd.o's base directory specifications (now usually ~/.config/.clixrc)
Matthew Wild
parents:
28
diff
changeset
|
61 for line in config_file:lines() do |
0 | 62 line = line:match("^%s*(.-)%s*$"); |
63 if line:match("^%[") then | |
64 current_account = line:match("^%[(.-)%]"); | |
65 accounts[current_account] = {}; | |
66 if not current_account then -- This is the first defined account | |
67 accounts.default = accounts[current_account]; | |
68 end | |
69 elseif current_account then | |
70 local k,v = line:match("^(%w+)%s*[:=]%s*(.+)$"); | |
71 accounts[current_account or "default"][k] = v; | |
72 end | |
73 end | |
74 | |
75 function clix_connect(opts, on_connect) | |
76 local account = accounts[opts.account or "default"]; | |
77 if not (account and account.jid) then | |
78 io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n"); | |
79 return nil; | |
80 end | |
7
6078e8d2b59d
clix: Enable global Verse logger when verbose is enabled
Matthew Wild <mwild1@gmail.com>
parents:
5
diff
changeset
|
81 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
|
82 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
|
83 conn.log.debug = opts.verbose; |
0 | 84 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
|
85 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
|
86 conn:close(); |
0 | 87 end); |
17
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
88 conn:hook("binding-success", function () |
23
c5f04bdc7c64
clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
89 if not opts.quiet then |
c5f04bdc7c64
clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
90 io.stderr:write("clix: connected as ", conn.jid, "\n"); |
c5f04bdc7c64
clix, clix.raw: Move 'Connected as' to clix_connect(), and suppress on -q/--quiet
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
91 end |
21
cdeb02d9546d
clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
92 if opts.chatroom then |
cdeb02d9546d
clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
93 conn:send(verse.presence{to=opts.to.."/"..(opts.nick or "clix")}); |
cdeb02d9546d
clix, clix.send: Support for chatrooms (-c/--chatroom with --nick=somenick)
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
94 end |
17
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
95 if opts.presence then |
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
96 conn:send(verse.presence()); |
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
97 end |
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
98 return on_connect(conn); |
fa9efbef8a0c
Move presence sending into clix_connect(), and control with -o/--presence
Matthew Wild <mwild1@gmail.com>
parents:
15
diff
changeset
|
99 end); |
0 | 100 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
|
101 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
|
102 conn:close(); |
0 | 103 end); |
104 conn:hook("disconnected", function (info) | |
105 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
|
106 conn:warn("Disconnecting: %s", tostring(info.reason)); |
0 | 107 end |
108 verse.quit(); | |
109 end); | |
110 -- Optional config parameters | |
111 conn.connect_host = account.address; | |
112 conn.connect_port = account.port; | |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
113 |
0 | 114 -- Connect! |
15
54314164a2a3
clix: Allow -p/--password to specify the login password
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
115 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
|
116 |
13
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
117 if type(opts.resource) == "string" then |
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
118 conn.resource = opts.resource; |
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
119 end |
751db005032e
clix: Allow specifying the resource with -r/--resource
Matthew Wild <mwild1@gmail.com>
parents:
11
diff
changeset
|
120 |
11
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
121 local ok, ret = pcall(verse.loop); |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
122 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
|
123 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
|
124 return 1; |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
125 end |
a502c905527c
clix: Handle errors (including 'interrupted!') in verse.loop()
Matthew Wild <mwild1@gmail.com>
parents:
8
diff
changeset
|
126 return err or 0; |
0 | 127 end |
128 | |
129 table.remove(arg,1); | |
130 | |
131 local opts = {}; | |
132 | |
133 local args_handled_up_to; | |
134 for i, opt in ipairs(arg) do | |
135 if opt:match("^%-") and opt ~= "--" then | |
136 local name = opt:match("^%-%-?([^%s=]+)()") | |
137 name = (short_opts[name] or name):gsub("%-+", "_"); | |
138 if name:match("^no_") then | |
139 name = name:sub(4, -1); | |
140 opts[name] = false; | |
141 else | |
142 opts[name] = opt:match("=(.*)$") or true; | |
143 end | |
144 else | |
145 args_handled_up_to = i-1; | |
146 break; | |
147 end | |
148 end | |
149 | |
150 -- Remove all the handled args from the arg array | |
151 for n=(args_handled_up_to or #arg),1,-1 do | |
152 table.remove(arg, n); | |
153 end | |
154 | |
155 return m(opts, arg) or 0; | |
156 |