Annotate

plugins/mod_scansion_record.lua @ 9570:ce403b6470f8

util.serialization: Encode non-fatal error in way that can be restored It could previously encode to eg `{ [nil] = ... }` which doesn't get decoded
author Kim Alvefur <zash@zash.se>
date Sat, 27 Oct 2018 12:56:01 +0200
parent 9373:1a69803d5d5d
child 9618:d6e6e1fb7ef0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local names = { "Romeo", "Juliet", "Mercutio", "Tybalt", "Benvolio" };
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local devices = { "", "phone", "laptop", "tablet", "toaster", "fridge", "shoe" };
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local users = {};
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
9355
a31e0d549b74 mod_scansion_record: Import util.filters
Kim Alvefur <zash@zash.se>
parents: 9354
diff changeset
5 local filters = require "util.filters";
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local id = require "util.id";
9365
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
7 local dt = require "util.datetime";
9361
3620f7b3517b mod_scansion_record: Split up construction of filename for reuse of parts later
Kim Alvefur <zash@zash.se>
parents: 9360
diff changeset
8 local dm = require "util.datamanager";
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
9361
3620f7b3517b mod_scansion_record: Split up construction of filename for reuse of parts later
Kim Alvefur <zash@zash.se>
parents: 9360
diff changeset
10 local record_id = id.medium():lower();
3620f7b3517b mod_scansion_record: Split up construction of filename for reuse of parts later
Kim Alvefur <zash@zash.se>
parents: 9360
diff changeset
11 local record_date = os.date("%Y%b%d"):lower();
9369
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
12 local header_file = dm.getpath(record_id, "scansion", record_date, "scs", true);
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
13 local record_file = dm.getpath(record_id, "scansion", record_date, "log", true);
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
9364
62f9127ab493 mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents: 9363
diff changeset
15 local head = io.open(header_file, "w");
9369
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
16 local scan = io.open(record_file, "w+");
9346
2946f2bd2bb5 mod_scansion_record: Open log file
Kim Alvefur <zash@zash.se>
parents: 9345
diff changeset
17
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local function record(string)
9357
0ae20fb3d4c6 mod_scansion_record: Rename main file handle
Kim Alvefur <zash@zash.se>
parents: 9356
diff changeset
19 scan:write(string);
9348
e1de3245c696 mod_scansion_record: Finish function for writing data
Kim Alvefur <zash@zash.se>
parents: 9347
diff changeset
20 end
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
9364
62f9127ab493 mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents: 9363
diff changeset
22 local function record_header(string)
62f9127ab493 mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents: 9363
diff changeset
23 head:write(string);
62f9127ab493 mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents: 9363
diff changeset
24 end
62f9127ab493 mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents: 9363
diff changeset
25
9368
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
26 local function record_object(class, name, props)
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
27 head:write(("[%s] %s\n"):format(class, name));
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
28 for k,v in pairs(props) do
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
29 head:write(("\t%s: %s\n"):format(k, v));
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
30 end
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
31 head:write("\n");
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
32 end
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
33
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local function record_event(session, event)
9351
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
35 record(session.scansion_id.." "..event.."\n\n");
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local function record_stanza(stanza, session, verb)
9349
7e16e4ecd090 mod_scansion_record: Add missing )
Kim Alvefur <zash@zash.se>
parents: 9348
diff changeset
39 record(session.scansion_id.." "..verb..":\n\t"..tostring(stanza).."\n\n");
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local function record_stanza_in(stanza, session)
9358
bbc24fc734da mod_scansion_record: Only record stanzas in the default namespace
Kim Alvefur <zash@zash.se>
parents: 9357
diff changeset
43 if stanza.attr.xmlns == nil then
bbc24fc734da mod_scansion_record: Only record stanzas in the default namespace
Kim Alvefur <zash@zash.se>
parents: 9357
diff changeset
44 record_stanza(stanza, session, "sends")
bbc24fc734da mod_scansion_record: Only record stanzas in the default namespace
Kim Alvefur <zash@zash.se>
parents: 9357
diff changeset
45 end
9351
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
46 return stanza;
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local function record_stanza_out(stanza, session)
9358
bbc24fc734da mod_scansion_record: Only record stanzas in the default namespace
Kim Alvefur <zash@zash.se>
parents: 9357
diff changeset
50 if stanza.attr.xmlns == nil then
9359
dc55d7d2798b mod_scansion_record: Avoid recording the resource binding stanza
Kim Alvefur <zash@zash.se>
parents: 9358
diff changeset
51 if not (stanza.name == "iq" and stanza:get_child("bind", "urn:ietf:params:xml:ns:xmpp-bind")) then
dc55d7d2798b mod_scansion_record: Avoid recording the resource binding stanza
Kim Alvefur <zash@zash.se>
parents: 9358
diff changeset
52 record_stanza(stanza, session, "receives");
dc55d7d2798b mod_scansion_record: Avoid recording the resource binding stanza
Kim Alvefur <zash@zash.se>
parents: 9358
diff changeset
53 end
9358
bbc24fc734da mod_scansion_record: Only record stanzas in the default namespace
Kim Alvefur <zash@zash.se>
parents: 9357
diff changeset
54 end
9351
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
55 return stanza;
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 module:hook("resource-bind", function (event)
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local session = event.session;
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 if not users[session.username] then
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 users[session.username] = {
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 character = table.remove(names, 1) or id.short();
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 devices = {};
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 n_devices = 0;
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 };
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 local user = users[session.username];
9354
b042aa047d80 mod_scansion_record: Correctly retrieve resource
Kim Alvefur <zash@zash.se>
parents: 9353
diff changeset
68 local device = user.devices[session.resource];
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 if not device then
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 user.n_devices = user.n_devices + 1;
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 device = devices[user.n_devices] or ("device"..id.short());
9354
b042aa047d80 mod_scansion_record: Correctly retrieve resource
Kim Alvefur <zash@zash.se>
parents: 9353
diff changeset
72 user.devices[session.resource] = device;
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 session.scansion_character = user.character;
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 session.scansion_device = device;
9350
07fd880905a3 mod_scansion_record: Remove extra "
Kim Alvefur <zash@zash.se>
parents: 9349
diff changeset
76 session.scansion_id = user.character..(device ~= "" and "'s "..device or device);
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
9370
7872abb9fc1f mod_scansion_record: Record the full identity including device name
Kim Alvefur <zash@zash.se>
parents: 9369
diff changeset
78 record_object("Client", session.scansion_id, {
9368
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
79 jid = session.full_jid,
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
80 password = "password",
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
81 });
0c5177738ffd mod_scansion_record: Record connected clients as scansion Objects declarations
Kim Alvefur <zash@zash.se>
parents: 9367
diff changeset
82
9372
0e685b7ec039 mod_scansion_record: Lower log message to 'info' level
Kim Alvefur <zash@zash.se>
parents: 9371
diff changeset
83 module:log("info", "Connected: %s", session.scansion_id);
9352
0fdb70ad9fcc mod_scansion_record: Record session connecting
Kim Alvefur <zash@zash.se>
parents: 9351
diff changeset
84 record_event(session, "connects");
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 filters.add_filter(session, "stanzas/in", record_stanza_in);
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 filters.add_filter(session, "stanzas/out", record_stanza_out);
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end);
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89
9373
1a69803d5d5d mod_scansion_record: Record disconnection
Kim Alvefur <zash@zash.se>
parents: 9372
diff changeset
90 module:hook("resource-unbind", function (event)
1a69803d5d5d mod_scansion_record: Record disconnection
Kim Alvefur <zash@zash.se>
parents: 9372
diff changeset
91 local session = event.session;
1a69803d5d5d mod_scansion_record: Record disconnection
Kim Alvefur <zash@zash.se>
parents: 9372
diff changeset
92 if session.scansion_id then
1a69803d5d5d mod_scansion_record: Record disconnection
Kim Alvefur <zash@zash.se>
parents: 9372
diff changeset
93 record_event(session, "disconnects");
1a69803d5d5d mod_scansion_record: Record disconnection
Kim Alvefur <zash@zash.se>
parents: 9372
diff changeset
94 end
1a69803d5d5d mod_scansion_record: Record disconnection
Kim Alvefur <zash@zash.se>
parents: 9372
diff changeset
95 end)
1a69803d5d5d mod_scansion_record: Record disconnection
Kim Alvefur <zash@zash.se>
parents: 9372
diff changeset
96
9365
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
97 record_header("# mod_scansion_record on host '"..module.host.."' recording started "..dt.datetime().."\n\n");
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
98
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
99 record[[
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
100 -----
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
101
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
102 ]]
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
103
9360
0444e321b757 mod_scansion_record: Hook correct shutdown event
Kim Alvefur <zash@zash.se>
parents: 9359
diff changeset
104 module:hook_global("server-stopping", function ()
9365
acb316319dc0 mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents: 9364
diff changeset
105 record("# recording ended on "..dt.datetime().."\n");
9369
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
106 module:log("info", "Scansion recording available in %s", header_file);
9366
9472b4044fc6 mod_scansion_record: Close files in the cleanup stage of shutdown (fixes use after close)
Kim Alvefur <zash@zash.se>
parents: 9365
diff changeset
107 end);
9472b4044fc6 mod_scansion_record: Close files in the cleanup stage of shutdown (fixes use after close)
Kim Alvefur <zash@zash.se>
parents: 9365
diff changeset
108
9472b4044fc6 mod_scansion_record: Close files in the cleanup stage of shutdown (fixes use after close)
Kim Alvefur <zash@zash.se>
parents: 9365
diff changeset
109 prosody.events.add_handler("server-cleanup", function ()
9369
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
110 scan:seek("set", 0);
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
111 for line in scan:lines() do
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
112 head:write(line, "\n");
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
113 end
9362
31317e8edbbc mod_scansion_record: Fix missed rename of file handle
Kim Alvefur <zash@zash.se>
parents: 9361
diff changeset
114 scan:close();
9369
dd452f932b67 mod_scansion_record: Merge header and log into one file at shutdown
Kim Alvefur <zash@zash.se>
parents: 9368
diff changeset
115 os.remove(record_file);
9364
62f9127ab493 mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents: 9363
diff changeset
116 head:close()
9347
9479523e209a mod_scansion_record: and close it on shutdown
Kim Alvefur <zash@zash.se>
parents: 9346
diff changeset
117 end);