Changeset

0:ae83411a89c9

Initial commit
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 Jan 2010 03:50:37 +0000
parents
children 1:58c694492459
files clix.coro.lua clix.lua clix/message.lua clix/version.lua squishy
diffstat 5 files changed, 246 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clix.coro.lua	Wed Jan 06 03:50:37 2010 +0000
@@ -0,0 +1,99 @@
+require "verse"
+require "verse.client"
+
+local command = arg[1];
+
+if not command then
+	print("Command Line XMPP, available commands:");
+	for module in pairs(package.preload) do
+		if module:match("^clix%.") then
+			local m = require(module);
+			m{ "--short-help" };
+		end
+	end
+	return 0;
+end
+
+local ok, m = pcall(require, "clix."..command);
+if not ok then
+	print("Error running command '"..command.."' (run with --debug to see full error)");
+	if arg[2] == "--debug" then
+		print(m);
+	end
+	return 1;
+end
+
+if type(m) ~= "function" then
+	print(command.." is not a valid command");
+	return 1;
+end
+
+local accounts = { default = {} };
+local current_account;
+for line in io.lines(os.getenv("HOME").."/.clix") do
+	line = line:match("^%s*(.-)%s*$");
+	if line:match("^%[") then
+		current_account = line:match("^%[(.-)%]");
+		accounts[current_account] = {};
+		if not current_account then -- This is the first defined account
+			accounts.default = accounts[current_account];
+		end
+	elseif current_account then
+		local k,v = line:match("^(%w+)%s*[:=]%s*(.+)$");
+		accounts[current_account or "default"][k] = v;
+	end
+end
+
+function clix_connect(opts)
+	local account = accounts[opts.account or "default"];
+	if not (account and account.jid) then
+		io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n");
+		return nil;
+	end
+	
+	local conn = verse.new();
+	local co_verse = coroutine.create(verse.loop);
+	function conn:go(...) return coroutine.resume(co_verse, ...); end
+	conn:hook("authentication-failure", function (err)
+		io.stderr:write("Authentication failure (",err.condition or "unknown error", ")", err.text and (": "..err.text) or "", "\n");
+		coroutine.yield(nil, "authentication-failure");
+	end);
+	conn:hook("binding-success", function () io.stderr:write("Connected: ", tostring(conn), "\n"); coroutine.yield(conn); end);
+	conn:hook("binding-failure", function (err)
+		io.stderr:write("Authentication failure (",err.condition or "unknown error", ")", err.text and (": "..err.text) or "", "\n");
+		coroutine.yield(nil, "authentication-failure");
+	end);
+	-- Optional config parameters
+	conn.connect_host = account.address;
+	conn.connect_port = account.port;
+	-- Connect!
+	conn:connect_client(account.jid, account.password);
+	return coroutine.resume(co_verse);
+end
+
+table.remove(arg,1);
+
+local short_opts = { v = "verbose", t = "to", f = "from", e = "type", a = "account", p = "password" }
+local opts = {};
+
+for i, opt in ipairs(arg) do
+        if opt:match("^%-") and opt ~= "--" then
+                local name = opt:match("^%-%-?([^%s=]+)()")
+                name = (short_opts[name] or name):gsub("%-+", "_");
+                if name:match("^no_") then
+                        name = name:sub(4, -1);
+                        opts[name] = false;
+                else
+                        opts[name] = opt:match("=(.*)$") or true;
+                end
+        else
+        	-- Remove all the handled args from the arg array
+		for n=1,(i-1) do
+			table.remove(arg, n);
+		end
+		break;
+        end
+end
+
+return m(opts, arg) or 0;
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clix.lua	Wed Jan 06 03:50:37 2010 +0000
@@ -0,0 +1,104 @@
+require "verse"
+require "verse.client"
+
+local command = arg[1];
+
+if not command then
+	print("Command Line XMPP, available commands:");
+	for module in pairs(package.preload) do
+		if module:match("^clix%.") then
+			local m = require(module);
+			m{ "--short-help" };
+		end
+	end
+	return 0;
+end
+
+local ok, m = pcall(require, "clix."..command);
+if not ok then
+	print("Error running command '"..command.."' (run with --debug to see full error)");
+	if arg[2] == "--debug" then
+		print(m);
+	end
+	return 1;
+end
+
+if type(m) ~= "function" then
+	print(command.." is not a valid command");
+	return 1;
+end
+
+local accounts = { default = {} };
+local current_account;
+for line in io.lines(os.getenv("HOME").."/.clix") do
+	line = line:match("^%s*(.-)%s*$");
+	if line:match("^%[") then
+		current_account = line:match("^%[(.-)%]");
+		accounts[current_account] = {};
+		if not current_account then -- This is the first defined account
+			accounts.default = accounts[current_account];
+		end
+	elseif current_account then
+		local k,v = line:match("^(%w+)%s*[:=]%s*(.+)$");
+		accounts[current_account or "default"][k] = v;
+	end
+end
+
+function clix_connect(opts, on_connect)
+	local account = accounts[opts.account or "default"];
+	if not (account and account.jid) then
+		io.stderr:write("The specified account (", opts.account or "default", ") wasn't found in the config file\n");
+		return nil;
+	end
+	
+	local conn = verse.new();
+	conn:hook("authentication-failure", function (err)
+		io.stderr:write("Authentication failure (",err.condition or "unknown error", ")", err.text and (": "..err.text) or "", "\n");
+	end);
+	conn:hook("binding-success", function () io.stderr:write("Connected: ", tostring(conn), "\n"); return on_connect(conn); end);
+	conn:hook("binding-failure", function (err)
+		io.stderr:write("Authentication failure (",err.condition or "unknown error", ")", err.text and (": "..err.text) or "", "\n");
+	end);
+	conn:hook("disconnected", function (info)
+		if info.reason then
+			io.stderr:write("Disconnecting: ", tostring(info.reason), "\n");
+		end
+		verse.quit();
+	end);
+	-- Optional config parameters
+	conn.connect_host = account.address;
+	conn.connect_port = account.port;
+	-- Connect!
+	conn:connect_client(account.jid, account.password);
+	return verse.loop();
+end
+
+table.remove(arg,1);
+
+local short_opts = { v = "verbose", t = "to", f = "from", e = "type", a = "account", p = "password" }
+local opts = {};
+
+local args_handled_up_to;
+for i, opt in ipairs(arg) do
+        if opt:match("^%-") and opt ~= "--" then
+                local name = opt:match("^%-%-?([^%s=]+)()")
+                name = (short_opts[name] or name):gsub("%-+", "_");
+                if name:match("^no_") then
+                        name = name:sub(4, -1);
+                        opts[name] = false;
+                else
+                        opts[name] = opt:match("=(.*)$") or true;
+                end
+        else
+        	args_handled_up_to = i-1;
+		break;
+        end
+end
+
+-- Remove all the handled args from the arg array
+for n=(args_handled_up_to or #arg),1,-1 do
+	table.remove(arg, n);
+end
+
+return m(opts, arg) or 0;
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clix/message.lua	Wed Jan 06 03:50:37 2010 +0000
@@ -0,0 +1,10 @@
+return function (opts, arg)
+	if #arg == 0 or opts.help then
+		return 0;
+	end
+	local function on_connect(conn)
+		conn:send(verse.message({ to = opts.to, type = opts.type or "chat" }):body(table.concat(arg, " ")));
+		conn:close();
+	end
+	clix_connect(opts, on_connect);
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clix/version.lua	Wed Jan 06 03:50:37 2010 +0000
@@ -0,0 +1,28 @@
+return function (opts, arg)
+	if #arg == 0 or opts.help then
+		return 0;
+	end
+	local conn;
+	local function on_reply(reply)
+		if not reply.error then
+			if reply.name then
+				print("Software:", reply.name);
+			end
+			if reply.version then
+				print("Version:", reply.version);
+			end
+			if reply.platform then
+				print("Platform:", reply.platform);
+			end
+		else
+			print("Error requesting version ("..(reply.condition or "unknown")..")"..(reply.text and (": "..reply.text) or ""));
+		end
+		conn:close();
+	end
+	local function on_connect(_conn)
+		conn = _conn;
+		conn:add_plugin("version");
+		conn:query_version(arg[1], on_reply);
+	end
+	clix_connect(opts, on_connect);
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/squishy	Wed Jan 06 03:50:37 2010 +0000
@@ -0,0 +1,5 @@
+Module "clix.message" "clix/message.lua"
+Module "clix.version" "clix/version.lua"
+
+Main "clix.lua"
+Output "clix"