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