Mon, 22 Nov 2021 18:47:31 +0100
clix.moderate: Add --reason to help text
148
27a9f28724d3
clix.archive: Add 'irc' output format, useful for MUC export
Kim Alvefur <zash@zash.se>
parents:
147
diff
changeset
|
1 | local jid_split = require"util.jid".split; |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
2 | local datetime = require "util.datetime"; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
3 | local st = require "util.stanza"; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
4 | local uuid = require"util.uuid".generate; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
5 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
6 | return function(opts, arg) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
7 | if opts.help then |
162 | 8 | print("clix moderate --room=room@muc.example.com") |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
9 | print("\t--start=timestamp") |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
10 | print("\t--end=timestamp") |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
11 | print("\t--from=nickname") |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
12 | print("\t--body-contains=\"some spam\"") |
163
801fcbe4a642
clix.moderate: Add --reason to help text
Kim Alvefur <zash@zash.se>
parents:
162
diff
changeset
|
13 | print("\t--reason=\"spam\"") |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
14 | print("\t--dry-run") |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
15 | return 0; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
16 | elseif opts.short_help or arg[1] or not opts.room then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
17 | print("Remove messages from a MUC"); |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 | return; |
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 | end |
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 | |
159
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
21 | local function parse_datetime(s) |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
22 | if s:match("^%d%d:") then |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
23 | s = datetime.date().."T"..s; |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
24 | end |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
25 | if #s < 20 then |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
26 | s = s .. ("0000-01-01T00:00:00Z"):sub(#s+1) |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
27 | end |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
28 | return datetime.parse(s) |
68e09745d928
clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents:
148
diff
changeset
|
29 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
30 | |
78 | 31 | if opts.start then |
32 | opts.start = parse_datetime(opts.start); | |
33 | end | |
34 | if opts["end"] then | |
35 | opts["end"] = parse_datetime(opts["end"]); | |
36 | end | |
148
27a9f28724d3
clix.archive: Add 'irc' output format, useful for MUC export
Kim Alvefur <zash@zash.se>
parents:
147
diff
changeset
|
37 | |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
38 | local function matches(message) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
39 | local nick = select(3, jid_split(message.attr.from)); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
40 | if opts.from and opts.from ~= nick then return end |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
41 | local body = message:get_child_text("body"); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
42 | if opts.body_contains and not string.find(body or "", opts.body_contains, 1, true) then return end |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
43 | if opts.body_match and not string.find(body or "", opts.body_match) then return end |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
44 | return true; |
147
0bc82a318c45
clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents:
146
diff
changeset
|
45 | end |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 | |
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 | local function on_connect(conn) |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
48 | local waiting = {}; -- to keep track of outstanding queries |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
49 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
50 | local function done(with) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
51 | waiting[with] = nil; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
52 | if next(waiting) == nil then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
53 | conn:close(); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
54 | end |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
56 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
57 | local function moderate(id) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
58 | waiting[id] = true; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
59 | -- TODO maybe queue and send the next request when a response comes in? |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
60 | local mod_iq = st.iq({ id = uuid(); type = "set"; to = opts.room }) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
61 | :tag("apply-to", { xmlns = "urn:xmpp:fasten:0"; id = id }) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
62 | :tag("moderate", { xmlns = "urn:xmpp:message-moderate:0" }) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
63 | :tag("retract", { xmlns = "urn:xmpp:message-retract:0" }):up() |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
64 | if opts.reason then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
65 | mod_iq:tag("reason", { xmlns = "urn:xmpp:message-moderate:0" }):text(opts.reason):up() |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
67 | mod_iq:reset(); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
68 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
69 | if opts.dry_run then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
70 | done(id); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
71 | conn:debug("Would send: %s", mod_iq); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
72 | return; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
73 | end |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
74 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
75 | return conn:send_iq(mod_iq, function (ret) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
76 | if ret.attr.type == "error" then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
77 | local t, cond, msg = ret:get_error(); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
78 | conn:error("Retracting message with id %s failed: %s(%s, %s)", msg or "", t, cond); |
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
|
79 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
80 | done(id); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
81 | end); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
82 | end |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
83 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
84 | local function handle_results(result, err) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
85 | if not result then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
86 | conn:error("Archive query failed: %s", err); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
87 | return done(true); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
88 | end |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
89 | for _, item in ipairs(result) do |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
90 | if matches(item.message) then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
91 | conn:info("Moderate %s", item.message:top_tag()) |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
92 | moderate(item.id); |
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
|
93 | else |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
94 | conn:debug("Skip %s", item.message:top_tag()) |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 | end |
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
97 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
98 | if result.complete == nil then -- COMPAT verse |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
99 | result.complete = opts.after == nil or result[1] == nil; |
117
c6b5d2039f25
clix.archive: Show number of items if available
Kim Alvefur <zash@zash.se>
parents:
116
diff
changeset
|
100 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
101 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
102 | if not result.complete then |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
103 | -- Proceed to the next page |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
104 | opts.after = result.last; |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
105 | return conn:query_archive(opts.room, opts, handle_results); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
106 | else |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
107 | -- All done, just wait for any outstanding moderation queries to complete |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
108 | done(true); |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
109 | end |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
111 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
112 | waiting[true] = true; -- for the archive query |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
113 | conn:query_archive(opts.room, opts, handle_results); |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 | end |
160
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
115 | |
6c1953fbe0fa
clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents:
159
diff
changeset
|
116 | clix_connect(opts, on_connect, { "archive" }); |
75
8d5c99f46cb8
clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 | end |