clix/archive.lua

Mon, 08 Feb 2021 22:57:42 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 08 Feb 2021 22:57:42 +0100
changeset 147
0bc82a318c45
parent 146
ae85457eca8e
child 148
27a9f28724d3
permissions
-rw-r--r--

clix.archive: Add 'raw' output format, useful for exports

75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local bare_jid = require"util.jid".bare;
78
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
2 local parse_datetime = require"util.datetime".parse;
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 return function (opts, arg)
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 if opts.short_help then
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 print("Fetch archived messages");
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 return;
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
114
757647426af2 clix.archive: Add support for paging backwards
Kim Alvefur <zash@zash.se>
parents: 92
diff changeset
9 local reverse = opts.before;
78
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
10 if opts.start then
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
11 opts.start = parse_datetime(opts.start);
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
12 end
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
13 if opts["end"] then
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
14 opts["end"] = parse_datetime(opts["end"]);
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
15 end
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local selfjid;
81
e87f4d0b1a80 clix.archive: Show timestamps
Kim Alvefur <zash@zash.se>
parents: 80
diff changeset
17 local function print_message(i)
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 -- TODO Roster lookup
81
e87f4d0b1a80 clix.archive: Show timestamps
Kim Alvefur <zash@zash.se>
parents: 80
diff changeset
19 local m = i.message;
146
ae85457eca8e clix.archive: Skip printing body-less messages
Kim Alvefur <zash@zash.se>
parents: 118
diff changeset
20 if not m:get_child("body") then return end
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local a = m.attr;
81
e87f4d0b1a80 clix.archive: Show timestamps
Kim Alvefur <zash@zash.se>
parents: 80
diff changeset
22 if i.stamp then
118
8bb289b2b7be clix.archive: Show proper timestamps
Kim Alvefur <zash@zash.se>
parents: 117
diff changeset
23 print(os.date("%Y-%m-%d %H:%M:%S", i.stamp));
81
e87f4d0b1a80 clix.archive: Show timestamps
Kim Alvefur <zash@zash.se>
parents: 80
diff changeset
24 end
80
ae77d9c41f4f clix.archive: The world doesn't revolve around us.
Kim Alvefur <zash@zash.se>
parents: 79
diff changeset
25 if a.to and bare_jid(a.to) ~= selfjid then
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 print("To ".. bare_jid(a.to));
80
ae77d9c41f4f clix.archive: The world doesn't revolve around us.
Kim Alvefur <zash@zash.se>
parents: 79
diff changeset
27 end
ae77d9c41f4f clix.archive: The world doesn't revolve around us.
Kim Alvefur <zash@zash.se>
parents: 79
diff changeset
28 if a.from and bare_jid(a.from) ~= selfjid then
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 print("From ".. bare_jid(a.from));
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 print(m:get_child_text"body");
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 print"";
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
147
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
34 if opts.format == "raw" then
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
35 local st = require"util.stanza";
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
36 local datetime = require"util.datetime".datetime;
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
37 function print_message(i)
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
38 print(st.stanza("forwarded",{xmlns="urn:xmpp:forward:0"})
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
39 :tag("delay", {xmlns="urn:xmpp:delay", stamp=datetime(i.stamp)}):up()
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
40 :add_child(i.message));
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
41 end
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
42 end
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local function on_connect(conn)
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 if opts.debug then
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 conn:hook("stanza-out", print);
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 conn:hook("stanza", print);
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 selfjid = bare_jid(conn.jid);
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local function handle_results(ok, result)
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 for i=1,#result do
81
e87f4d0b1a80 clix.archive: Show timestamps
Kim Alvefur <zash@zash.se>
parents: 80
diff changeset
52 print_message(result[i]);
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
114
757647426af2 clix.archive: Add support for paging backwards
Kim Alvefur <zash@zash.se>
parents: 92
diff changeset
54 if result[reverse and "first" or "last"] then
90
3115bd81b9de clix.archive: Be interactive when --interactive is set. --everything makes it page by itself.
Kim Alvefur <zash@zash.se>
parents: 81
diff changeset
55 local fetch_next = opts.everything;
3115bd81b9de clix.archive: Be interactive when --interactive is set. --everything makes it page by itself.
Kim Alvefur <zash@zash.se>
parents: 81
diff changeset
56 if opts.interactive and not fetch_next then
3115bd81b9de clix.archive: Be interactive when --interactive is set. --everything makes it page by itself.
Kim Alvefur <zash@zash.se>
parents: 81
diff changeset
57 io.stderr:write "--More--"
92
a629b953780c clix.archive: Print a newline if reading a line fails (the user pressed ^D)
Kim Alvefur <zash@zash.se>
parents: 90
diff changeset
58 fetch_next = io.read"*l" or print();
90
3115bd81b9de clix.archive: Be interactive when --interactive is set. --everything makes it page by itself.
Kim Alvefur <zash@zash.se>
parents: 81
diff changeset
59 end
3115bd81b9de clix.archive: Be interactive when --interactive is set. --everything makes it page by itself.
Kim Alvefur <zash@zash.se>
parents: 81
diff changeset
60 if fetch_next then
114
757647426af2 clix.archive: Add support for paging backwards
Kim Alvefur <zash@zash.se>
parents: 92
diff changeset
61 if reverse then
757647426af2 clix.archive: Add support for paging backwards
Kim Alvefur <zash@zash.se>
parents: 92
diff changeset
62 opts.before = result.first and result.first[1];
757647426af2 clix.archive: Add support for paging backwards
Kim Alvefur <zash@zash.se>
parents: 92
diff changeset
63 else
115
1aaf30b457a0 clix.archive: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 114
diff changeset
64 opts.after = result.last;
114
757647426af2 clix.archive: Add support for paging backwards
Kim Alvefur <zash@zash.se>
parents: 92
diff changeset
65 end
79
a19ccb8f1ef9 clix.archive: Use --to specify where to direct the query
Kim Alvefur <zash@zash.se>
parents: 78
diff changeset
66 conn:query_archive(opts.to, opts, handle_results);
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 return
90
3115bd81b9de clix.archive: Be interactive when --interactive is set. --everything makes it page by itself.
Kim Alvefur <zash@zash.se>
parents: 81
diff changeset
68 else
116
3ee83c4801b4 clix.archive: Fix pagination message, rsm.first is a table
Kim Alvefur <zash@zash.se>
parents: 115
diff changeset
69 local whatnext, nextpage = reverse and "before" or "after";
3ee83c4801b4 clix.archive: Fix pagination message, rsm.first is a table
Kim Alvefur <zash@zash.se>
parents: 115
diff changeset
70 if reverse then
3ee83c4801b4 clix.archive: Fix pagination message, rsm.first is a table
Kim Alvefur <zash@zash.se>
parents: 115
diff changeset
71 nextpage = result.first and result.first[1];
3ee83c4801b4 clix.archive: Fix pagination message, rsm.first is a table
Kim Alvefur <zash@zash.se>
parents: 115
diff changeset
72 else
3ee83c4801b4 clix.archive: Fix pagination message, rsm.first is a table
Kim Alvefur <zash@zash.se>
parents: 115
diff changeset
73 nextpage = result.last;
3ee83c4801b4 clix.archive: Fix pagination message, rsm.first is a table
Kim Alvefur <zash@zash.se>
parents: 115
diff changeset
74 end
3ee83c4801b4 clix.archive: Fix pagination message, rsm.first is a table
Kim Alvefur <zash@zash.se>
parents: 115
diff changeset
75 conn:info("Next page: --%s=%s", whatnext, nextpage);
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 end
117
c6b5d2039f25 clix.archive: Show number of items if available
Kim Alvefur <zash@zash.se>
parents: 116
diff changeset
78 if result.count then
c6b5d2039f25 clix.archive: Show number of items if available
Kim Alvefur <zash@zash.se>
parents: 116
diff changeset
79 conn:info("Total: %d items", result.count);
c6b5d2039f25 clix.archive: Show number of items if available
Kim Alvefur <zash@zash.se>
parents: 116
diff changeset
80 end
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 conn:close();
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 conn:add_plugin"archive";
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 -- TODO conn:add_plugin"roster";
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 -- c.roster:fetch(function()
79
a19ccb8f1ef9 clix.archive: Use --to specify where to direct the query
Kim Alvefur <zash@zash.se>
parents: 78
diff changeset
86 conn:query_archive(opts.to, opts, handle_results);
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 clix_connect(opts, on_connect);
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 end

mercurial