annotate mod_s2s_status/mod_s2s_status.lua @ 5802:f6a2602129c8

mod_csi_battery_saver: mark some presences as important
author tmolitor <thilo@eightysoft.de>
date Wed, 03 Jan 2024 07:53:55 +0100
parents 31c331d05a75
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4791
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local status_out = module:shared("out");
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local errors = require "util.error";
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local function get_session_info(session)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local direction, peer_host = session.direction;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 if direction == "outgoing" then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 peer_host = session.to_host;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 elseif direction == "incoming" then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 peer_host = session.from_host;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 return peer_host, direction, session.id;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local function get_domain_log_out(peer_domain)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local domain_log = status_out[peer_domain];
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if not domain_log then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 domain_log = {};
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 status_out[peer_domain] = domain_log;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
5788
31c331d05a75 mod_s2s_status: Add missing return (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents: 4791
diff changeset
21 return domain_log;
4791
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local function get_connection_record(domain_log, id)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for _, record in ipairs(domain_log) do
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if record.id == id then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return record;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- No record for this connection yet, create it
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local record = { id = id };
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 table.insert(domain_log, 1, record);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return record;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local function log_new_connection_out(peer_domain, id)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local domain_log = get_domain_log_out(peer_domain);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local record = get_connection_record(domain_log, id);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 record.status, record.time_started = "connecting", os.time();
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local function log_successful_connection_out(peer_domain, id)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local domain_log = get_domain_log_out(peer_domain);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local record = get_connection_record(domain_log, id);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 record.status, record.time_connected = "connected", os.time();
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local function log_ended_connection_out(peer_domain, id, reason)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local domain_log = get_domain_log_out(peer_domain);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local record = get_connection_record(domain_log, id);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if record.status == "connecting" then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 record.status = "failed";
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 elseif record.status == "connected" then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 record.status = "disconnected";
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if reason then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local e_reason = errors.new(reason);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 record.error = {
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 type = e_reason.type;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 condition = e_reason.condition;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 text = e_reason.text;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 };
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if not record.error.text and type(reason) == "string" then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 record.error.text = reason;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local now = os.time();
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 record.time_ended = now;
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local function s2sout_established(event)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 local peer_domain, _, id = get_session_info(event.session);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 log_successful_connection_out(peer_domain, id);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local function s2sout_destroyed(event)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local peer_domain, _, id = get_session_info(event.session);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 log_ended_connection_out(peer_domain, id);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local function s2s_created(event)
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local peer_domain, direction, id = get_session_info(event.session);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 if direction == "outgoing" then
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 log_new_connection_out(peer_domain, id);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 module:hook("s2s-created", s2s_created);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 module:hook("s2sout-established", s2sout_established);
b86282953663 mod_s2s_status: Module to track status of s2s connections by domain
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 module:hook("s2sout-destroyed", s2sout_destroyed);