File

clix/archive.lua @ 79:a19ccb8f1ef9

clix.archive: Use --to specify where to direct the query
author Kim Alvefur <zash@zash.se>
date Mon, 25 Jun 2012 23:57:54 +0200
parent 78:bed3cfce7d60
child 80:ae77d9c41f4f
line wrap: on
line source

local bare_jid = require"util.jid".bare;
local parse_datetime = require"util.datetime".parse;
return function (opts, arg)
	if opts.short_help then
		print("Fetch archived messages");
		return;
	end

	if opts.start then
		opts.start = parse_datetime(opts.start);
	end
	if opts["end"] then
		opts["end"] = parse_datetime(opts["end"]);
	end
	local selfjid;
	local function print_message(m)
		-- TODO Roster lookup
		-- TODO Timestamp
		local a = m.attr;
		if bare_jid(a.from) == selfjid then
			print("To ".. bare_jid(a.to));
		else
			print("From ".. bare_jid(a.from));
		end
		print(m:get_child_text"body");
		print"";
	end

	local function on_connect(conn)
		if opts.debug then
			conn:hook("stanza-out", print);
			conn:hook("stanza", print);
		end
		selfjid = bare_jid(conn.jid);
		local function handle_results(ok, result)
			for i=1,#result do
				print_message(result[i].message);
			end
			if result.last then
				io.stderr:write "--More--"
				if io.read"*l" then
					opts.after = result.last;
					conn:query_archive(opts.to, opts, handle_results);
					return
				end
			end
			conn:close();
		end
		conn:add_plugin"archive";
		-- TODO conn:add_plugin"roster";
		-- c.roster:fetch(function()
		conn:query_archive(opts.to, opts, handle_results);
	end
	clix_connect(opts, on_connect);
end