clix/moderate.lua

Sun, 21 Nov 2021 14:10:27 +0100

author
Kim Alvefur <zash@zash.se>
date
Sun, 21 Nov 2021 14:10:27 +0100
changeset 162
28acd1ca0ffd
parent 160
6c1953fbe0fa
child 163
801fcbe4a642
permissions
-rw-r--r--

clix.moderate: Fix help message

No wonder it didn't work when I tried --to

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
28acd1ca0ffd clix.moderate: Fix help message
Kim Alvefur <zash@zash.se>
parents: 160
diff changeset
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\"")
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
13 print("\t--dry-run")
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
14 return 0;
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
15 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
16 print("Remove messages from a MUC");
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 return;
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
159
68e09745d928 clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents: 148
diff changeset
20 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
21 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
22 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
23 end
68e09745d928 clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents: 148
diff changeset
24 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
25 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
26 end
68e09745d928 clix.archive: Handle partial timestamps (e.g. hh:mm or yyyy-mm-dd)
Kim Alvefur <zash@zash.se>
parents: 148
diff changeset
27 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
28 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
29
78
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
30 if opts.start then
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
31 opts.start = parse_datetime(opts.start);
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
32 end
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
33 if opts["end"] then
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
34 opts["end"] = parse_datetime(opts["end"]);
bed3cfce7d60 clix.archive: Parse timestamps
Kim Alvefur <zash@zash.se>
parents: 77
diff changeset
35 end
148
27a9f28724d3 clix.archive: Add 'irc' output format, useful for MUC export
Kim Alvefur <zash@zash.se>
parents: 147
diff changeset
36
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
37 local function matches(message)
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
38 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
39 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
40 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
41 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
42 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
43 return true;
147
0bc82a318c45 clix.archive: Add 'raw' output format, useful for exports
Kim Alvefur <zash@zash.se>
parents: 146
diff changeset
44 end
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 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
47 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
48
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
49 local function done(with)
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
50 waiting[with] = nil;
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
51 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
52 conn:close();
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
53 end
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
55
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
56 local function moderate(id)
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
57 waiting[id] = true;
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
58 -- 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
59 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
60 :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
61 :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
62 :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
63 if opts.reason then
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
64 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
65 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
66 mod_iq:reset();
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
67
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
68 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
69 done(id);
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
70 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
71 return;
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
72 end
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
73
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
74 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
75 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
76 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
77 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
78 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
79 done(id);
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
80 end);
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
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
83 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
84 if not result then
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
85 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
86 return done(true);
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
87 end
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
88 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
89 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
90 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
91 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
92 else
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
93 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
94 end
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
96
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
97 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
98 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
99 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
100
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
101 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
102 -- 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
103 opts.after = result.last;
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
104 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
105 else
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
106 -- 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
107 done(true);
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
108 end
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
110
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
111 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
112 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
113 end
160
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
114
6c1953fbe0fa clix.moderate: New command to do MUC moderation (XEP-0425)
Kim Alvefur <zash@zash.se>
parents: 159
diff changeset
115 clix_connect(opts, on_connect, { "archive" });
75
8d5c99f46cb8 clix.archive: New plugin, a MAM browser
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end

mercurial