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