Software /
code /
prosody
Annotate
plugins/mod_scansion_record.lua @ 9365:acb316319dc0
mod_scansion_record: Generate a header with hostname and timestamp
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 22 Sep 2018 14:38:40 +0200 |
parent | 9364:62f9127ab493 |
child | 9366:9472b4044fc6 |
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 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local full_jids = {}; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
9355
a31e0d549b74
mod_scansion_record: Import util.filters
Kim Alvefur <zash@zash.se>
parents:
9354
diff
changeset
|
7 local filters = require "util.filters"; |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 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
|
9 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
|
10 local dm = require "util.datamanager"; |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
9361
3620f7b3517b
mod_scansion_record: Split up construction of filename for reuse of parts later
Kim Alvefur <zash@zash.se>
parents:
9360
diff
changeset
|
12 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
|
13 local record_date = os.date("%Y%b%d"):lower(); |
9364
62f9127ab493
mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents:
9363
diff
changeset
|
14 local header_file = dm.getpath(record_id, "scansion", record_date, "sch", true); |
9361
3620f7b3517b
mod_scansion_record: Split up construction of filename for reuse of parts later
Kim Alvefur <zash@zash.se>
parents:
9360
diff
changeset
|
15 local record_file = dm.getpath(record_id, "scansion", record_date, "scs", true); |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
9364
62f9127ab493
mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents:
9363
diff
changeset
|
17 local head = io.open(header_file, "w"); |
9357
0ae20fb3d4c6
mod_scansion_record: Rename main file handle
Kim Alvefur <zash@zash.se>
parents:
9356
diff
changeset
|
18 local scan = io.open(record_file, "w"); |
9346
2946f2bd2bb5
mod_scansion_record: Open log file
Kim Alvefur <zash@zash.se>
parents:
9345
diff
changeset
|
19 |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local function record(string) |
9357
0ae20fb3d4c6
mod_scansion_record: Rename main file handle
Kim Alvefur <zash@zash.se>
parents:
9356
diff
changeset
|
21 scan:write(string); |
9348
e1de3245c696
mod_scansion_record: Finish function for writing data
Kim Alvefur <zash@zash.se>
parents:
9347
diff
changeset
|
22 end |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 |
9364
62f9127ab493
mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents:
9363
diff
changeset
|
24 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
|
25 head:write(string); |
62f9127ab493
mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents:
9363
diff
changeset
|
26 end |
62f9127ab493
mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents:
9363
diff
changeset
|
27 |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 local function record_event(session, event) |
9351
b37f131d68c2
mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents:
9350
diff
changeset
|
29 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
|
30 end |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local function record_stanza(stanza, session, verb) |
9349
7e16e4ecd090
mod_scansion_record: Add missing )
Kim Alvefur <zash@zash.se>
parents:
9348
diff
changeset
|
33 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
|
34 end |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 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
|
37 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
|
38 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
|
39 end |
9351
b37f131d68c2
mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents:
9350
diff
changeset
|
40 return stanza; |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 record_stanza(stanza, session, "receives"); |
dc55d7d2798b
mod_scansion_record: Avoid recording the resource binding stanza
Kim Alvefur <zash@zash.se>
parents:
9358
diff
changeset
|
47 end |
9358
bbc24fc734da
mod_scansion_record: Only record stanzas in the default namespace
Kim Alvefur <zash@zash.se>
parents:
9357
diff
changeset
|
48 end |
9351
b37f131d68c2
mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents:
9350
diff
changeset
|
49 return stanza; |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 module:hook("resource-bind", function (event) |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 local session = event.session; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 if not users[session.username] then |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 users[session.username] = { |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 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
|
57 devices = {}; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 n_devices = 0; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 }; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local user = users[session.username]; |
9354
b042aa047d80
mod_scansion_record: Correctly retrieve resource
Kim Alvefur <zash@zash.se>
parents:
9353
diff
changeset
|
62 local device = user.devices[session.resource]; |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 if not device then |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 user.n_devices = user.n_devices + 1; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 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
|
66 user.devices[session.resource] = device; |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 end |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 session.scansion_character = user.character; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 session.scansion_device = device; |
9350
07fd880905a3
mod_scansion_record: Remove extra "
Kim Alvefur <zash@zash.se>
parents:
9349
diff
changeset
|
70 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
|
71 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 full_jids[session.full_jid] = session.scansion_id; |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 module:log("warn", "Connected: %s's %s", user.character, device); |
9352
0fdb70ad9fcc
mod_scansion_record: Record session connecting
Kim Alvefur <zash@zash.se>
parents:
9351
diff
changeset
|
75 record_event(session, "connects"); |
9344
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 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
|
78 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
|
79 end); |
d36fa7d164c0
mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 |
9365
acb316319dc0
mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents:
9364
diff
changeset
|
81 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
|
82 |
acb316319dc0
mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents:
9364
diff
changeset
|
83 record[[ |
acb316319dc0
mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents:
9364
diff
changeset
|
84 ----- |
acb316319dc0
mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents:
9364
diff
changeset
|
85 |
acb316319dc0
mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents:
9364
diff
changeset
|
86 ]] |
acb316319dc0
mod_scansion_record: Generate a header with hostname and timestamp
Kim Alvefur <zash@zash.se>
parents:
9364
diff
changeset
|
87 |
9360
0444e321b757
mod_scansion_record: Hook correct shutdown event
Kim Alvefur <zash@zash.se>
parents:
9359
diff
changeset
|
88 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
|
89 record("# recording ended on "..dt.datetime().."\n"); |
9363
c4ecaaf66e76
mod_scansion_record: Log name of scanison log on shutdown
Kim Alvefur <zash@zash.se>
parents:
9362
diff
changeset
|
90 module:log("info", "Scansion recording available in %s", record_file); |
9362
31317e8edbbc
mod_scansion_record: Fix missed rename of file handle
Kim Alvefur <zash@zash.se>
parents:
9361
diff
changeset
|
91 scan:close(); |
9364
62f9127ab493
mod_scansion_record: Open a related file for header entries
Kim Alvefur <zash@zash.se>
parents:
9363
diff
changeset
|
92 head:close() |
9347
9479523e209a
mod_scansion_record: and close it on shutdown
Kim Alvefur <zash@zash.se>
parents:
9346
diff
changeset
|
93 end); |