Mercurial > prosody-modules
comparison mod_message_logging/mod_message_logging.lua @ 1500:045720a5fccd
mod_message_logging: Improve logging when loaded onto a MUC host
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 07 Sep 2014 08:08:09 +0100 |
parents | 92f3b4d81b52 |
children | 4b55110b0aa8 |
comparison
equal
deleted
inserted
replaced
1499:698686dca982 | 1500:045720a5fccd |
---|---|
48 open_files[jid] = nil; | 48 open_files[jid] = nil; |
49 end | 49 end |
50 end | 50 end |
51 module:hook_global("logging-reloaded", close_open_files); | 51 module:hook_global("logging-reloaded", close_open_files); |
52 | 52 |
53 local function write_to_log(log_jid, jid, prefix, body) | |
54 if not body then return; end | |
55 local f = open_files[log_jid]; | |
56 if not f then return; end | |
57 body = body:gsub("\n", "\n "); -- Indent newlines | |
58 f:write(prefix or "", prefix and ": " or "", jid, ": ", body, "\n"); | |
59 f:flush(); | |
60 end | |
61 | |
53 local function handle_incoming_message(event) | 62 local function handle_incoming_message(event) |
54 local origin, stanza = event.origin, event.stanza; | 63 local origin, stanza = event.origin, event.stanza; |
55 local message_type = stanza.attr.type; | 64 local message_type = stanza.attr.type; |
56 | 65 |
57 if message_type == "error" then return; end | 66 if message_type == "error" then return; end |
58 | 67 |
59 local from, to = jid_bare(stanza.attr.from), jid_bare(stanza.attr.to or stanza.attr.from); | 68 local from, to = jid_bare(stanza.attr.from), jid_bare(stanza.attr.to or stanza.attr.from); |
60 local body = stanza:get_child("body"); | |
61 if not body then return; end | |
62 body = body:get_text(); | |
63 | |
64 local f = open_files[to]; | |
65 if not f then return; end | |
66 if message_type == "groupchat" then | 69 if message_type == "groupchat" then |
67 -- Add the nickname | 70 from = from.." <"..select(3, jid_split(stanza.attr.from))..">"; |
68 from = from.." <"..(select(3, jid_split(stanza.attr.from)) or "")..">"; | |
69 end | 71 end |
70 body = body:gsub("\n", "\n "); -- Indent newlines | 72 write_to_log(to, from, "RECV", stanza:get_child_text("body")); |
71 f:write("RECV: ", from, ": ", body, "\n"); | |
72 f:flush(); | |
73 end | 73 end |
74 | 74 |
75 local function handle_outgoing_message(event) | 75 local function handle_outgoing_message(event) |
76 local origin, stanza = event.origin, event.stanza; | 76 local origin, stanza = event.origin, event.stanza; |
77 local message_type = stanza.attr.type; | 77 local message_type = stanza.attr.type; |
78 | 78 |
79 if message_type == "error" or message_type == "groupchat" then return; end | 79 if message_type == "error" then return; end |
80 | 80 |
81 local from, to = jid_bare(stanza.attr.from), jid_bare(stanza.attr.to or origin.full_jid); | 81 local from, to = jid_bare(stanza.attr.from), jid_bare(stanza.attr.to or origin.full_jid); |
82 local body = stanza:get_child("body"); | 82 write_to_log(from, to, "SEND", stanza:get_child_text("body")); |
83 if not body then return; end | |
84 body = body:get_text(); | |
85 | |
86 local f = open_files[from]; | |
87 if not f then return; end | |
88 body = body:gsub("\n", "\n "); -- Indent newlines | |
89 f:write("SEND: ", to, ": ", body, "\n"); | |
90 f:flush(); | |
91 end | 83 end |
92 | 84 |
93 | 85 local function handle_muc_message(event) |
86 local stanza = event.stanza; | |
87 if stanza.attr.type ~= "groupchat" then return; end | |
88 local room = event.room or hosts[select(2, jid_split(stanza.attr.to))].modules.muc.rooms[stanza.attr.to]; | |
89 if not room then return; end | |
90 local nick = select(3, jid_split(room._jid_nick[stanza.attr.from])); | |
91 if not nick then return; end | |
92 write_to_log(room.jid, jid_bare(stanza.attr.from).." <"..nick..">", "MESG", stanza:get_child_text("body")); | |
93 end | |
94 | 94 |
95 function module.add_host(module) | 95 function module.add_host(module) |
96 local host_base_path = get_host_path(module.host); | 96 local host_base_path = get_host_path(module.host); |
97 if not stat(host_base_path) then | 97 if not stat(host_base_path) then |
98 mkdir(host_base_path); | 98 mkdir(host_base_path); |
99 end | 99 end |
100 | 100 |
101 module:hook("message/bare", handle_incoming_message, 1); | 101 if hosts[module.host].modules.muc then |
102 module:hook("message/full", handle_incoming_message, 1); | 102 module:hook("message/bare", handle_muc_message, 1); |
103 | 103 else |
104 module:hook("pre-message/bare", handle_outgoing_message, 1); | 104 module:hook("message/bare", handle_incoming_message, 1); |
105 module:hook("pre-message/full", handle_outgoing_message, 1); | 105 module:hook("message/full", handle_incoming_message, 1); |
106 module:hook("pre-message/host", handle_outgoing_message, 1); | 106 |
107 module:hook("pre-message/bare", handle_outgoing_message, 1); | |
108 module:hook("pre-message/full", handle_outgoing_message, 1); | |
109 module:hook("pre-message/host", handle_outgoing_message, 1); | |
110 end | |
107 | 111 |
108 end | 112 end |
109 | 113 |
110 function module.command(arg) | 114 function module.command(arg) |
111 local command = table.remove(arg, 1); | 115 local command = table.remove(arg, 1); |