Annotate

plugins/mod_scansion_record.lua @ 9353:02ae04efab0f

mod_scansion_record: Tweak filename
author Kim Alvefur <zash@zash.se>
date Sat, 22 Sep 2018 14:05:59 +0200
parent 9352:0fdb70ad9fcc
child 9354:b042aa047d80
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
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
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local id = require "util.id";
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
9353
02ae04efab0f mod_scansion_record: Tweak filename
Kim Alvefur <zash@zash.se>
parents: 9352
diff changeset
9 local record_file = require "util.datamanager".getpath(id.medium():lower(), module.host, os.date("%Y%b%d"):lower(), "scs", true);
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
9346
2946f2bd2bb5 mod_scansion_record: Open log file
Kim Alvefur <zash@zash.se>
parents: 9345
diff changeset
11 local fh = io.open(record_file, "w");
2946f2bd2bb5 mod_scansion_record: Open log file
Kim Alvefur <zash@zash.se>
parents: 9345
diff changeset
12
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local function record(string)
9348
e1de3245c696 mod_scansion_record: Finish function for writing data
Kim Alvefur <zash@zash.se>
parents: 9347
diff changeset
14 fh:write(string);
e1de3245c696 mod_scansion_record: Finish function for writing data
Kim Alvefur <zash@zash.se>
parents: 9347
diff changeset
15 end
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local function record_event(session, event)
9351
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
18 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
19 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local function record_stanza(stanza, session, verb)
9349
7e16e4ecd090 mod_scansion_record: Add missing )
Kim Alvefur <zash@zash.se>
parents: 9348
diff changeset
22 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
23 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local function record_stanza_in(stanza, session)
9351
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
26 record_stanza(stanza, session, "sends")
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
27 return stanza;
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local function record_stanza_out(stanza, session)
9351
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
31 record_stanza(stanza, session, "receives")
b37f131d68c2 mod_scansion_record: Complete stanza recording functions
Kim Alvefur <zash@zash.se>
parents: 9350
diff changeset
32 return stanza;
9344
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 module:hook("resource-bind", function (event)
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local session = event.session;
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if not users[session.username] then
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 users[session.username] = {
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 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
40 devices = {};
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 n_devices = 0;
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 end
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local user = users[session.username];
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local device = user.devices[event.resource];
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if not device then
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 user.n_devices = user.n_devices + 1;
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 device = devices[user.n_devices] or ("device"..id.short());
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 user.devices[event.resource] = device;
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 session.scansion_character = user.character;
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 session.scansion_device = device;
9350
07fd880905a3 mod_scansion_record: Remove extra "
Kim Alvefur <zash@zash.se>
parents: 9349
diff changeset
53 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
54
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 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
56
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 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
58 record_event(session, "connects");
9344
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 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
61 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
62 end);
d36fa7d164c0 mod_scansion_record: Unfinished code dump from Matthew
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
9347
9479523e209a mod_scansion_record: and close it on shutdown
Kim Alvefur <zash@zash.se>
parents: 9346
diff changeset
64 module:hook_global("server-shutdown", function ()
9479523e209a mod_scansion_record: and close it on shutdown
Kim Alvefur <zash@zash.se>
parents: 9346
diff changeset
65 fh:close();
9479523e209a mod_scansion_record: and close it on shutdown
Kim Alvefur <zash@zash.se>
parents: 9346
diff changeset
66 end);