Software /
code /
clix
Annotate
clix.lua @ 2:fd77e75c4891
clix: Make more use of Verse's new logging controls
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 17:52:01 +0000 |
parent | 0:ae83411a89c9 |
child | 4:ead275885948 |
rev | line source |
---|---|
0 | 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, on_connect) | |
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 | |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
53 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
|
54 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
|
55 conn.log.debug = opts.verbose; |
0 | 56 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
|
57 conn:error("Authentication failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or "")); |
0 | 58 end); |
2
fd77e75c4891
clix: Make more use of Verse's new logging controls
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
59 conn:hook("binding-success", function () conn:debug("Connected: "..tostring(conn)); return on_connect(conn); end); |
0 | 60 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
|
61 conn:error("Resource binding failure ("..(err.condition or "unknown error")..")"..(err.text and (": "..err.text) or "")); |
0 | 62 end); |
63 conn:hook("disconnected", function (info) | |
64 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
|
65 conn:warn("Disconnecting: %s", tostring(info.reason)); |
0 | 66 end |
67 verse.quit(); | |
68 end); | |
69 -- Optional config parameters | |
70 conn.connect_host = account.address; | |
71 conn.connect_port = account.port; | |
72 -- Connect! | |
73 conn:connect_client(account.jid, account.password); | |
74 return verse.loop(); | |
75 end | |
76 | |
77 table.remove(arg,1); | |
78 | |
79 local short_opts = { v = "verbose", t = "to", f = "from", e = "type", a = "account", p = "password" } | |
80 local opts = {}; | |
81 | |
82 local args_handled_up_to; | |
83 for i, opt in ipairs(arg) do | |
84 if opt:match("^%-") and opt ~= "--" then | |
85 local name = opt:match("^%-%-?([^%s=]+)()") | |
86 name = (short_opts[name] or name):gsub("%-+", "_"); | |
87 if name:match("^no_") then | |
88 name = name:sub(4, -1); | |
89 opts[name] = false; | |
90 else | |
91 opts[name] = opt:match("=(.*)$") or true; | |
92 end | |
93 else | |
94 args_handled_up_to = i-1; | |
95 break; | |
96 end | |
97 end | |
98 | |
99 -- Remove all the handled args from the arg array | |
100 for n=(args_handled_up_to or #arg),1,-1 do | |
101 table.remove(arg, n); | |
102 end | |
103 | |
104 return m(opts, arg) or 0; | |
105 |