File

clix/archive.lua @ 75:8d5c99f46cb8

clix.archive: New plugin, a MAM browser
author Kim Alvefur <zash@zash.se>
date Mon, 25 Jun 2012 02:39:41 +0200
child 77:22be057cf590
line wrap: on
line source

local bare_jid = require"util.jid".bare;
return function (opts, arg)
	if opts.short_help then
		print("Fetch archived messages");
		return;
	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

	print"opts"
	for k,v in pairs(opts) do
		print("",k,v)
	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(nil, 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(nil, opts, handle_results);
	end
	clix_connect(opts, on_connect);
end