-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_logger.lua
More file actions
161 lines (139 loc) · 4.13 KB
/
Copy pathchat_logger.lua
File metadata and controls
161 lines (139 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
--// vim:ts=4:sw=4:noet
--// chat_logger.lua -- Log all hub chat to timestamped daily files
--//
--// Usage:
--// Copy to your scripts directory, or run directly:
--// eispy lua eval-file examples/lua/chat_logger.lua
--//
--// Logs are written to <config_dir>/logs/<hub_address>/<date>.log
--// Each line is timestamped: [HH:MM:SS] <Nick> message
--//
--// Chat commands (type in any hub):
--// /log on Enable logging for the current hub
--// /log off Disable logging for the current hub
--// /log status Show whether logging is on or off
--// /log path Show the current log file path
if not chatlog then
chatlog = {}
chatlog.enabled = {} -- keyed by hub URL
end
-- Ensure the log directory exists (best-effort with os.execute)
local function ensureDir(path)
os.execute('mkdir -p "' .. path .. '"')
end
-- Sanitise hub address for use as a directory name
local function sanitiseAddress(addr)
return addr:gsub("[:/\\%?%*\"|<>]", "_")
end
-- Get the log file path for a hub on a given date
local function logPath(hub)
local base = DC():GetConfigPath() .. "logs/"
local dir = base .. sanitiseAddress(hub:getAddress()) .. "/"
local file = os.date("%Y-%m-%d") .. ".log"
return dir, dir .. file
end
-- Append a line to the log file
local function writeLine(hub, line)
local url = hub:getUrl()
if not chatlog.enabled[url] then return end
local dir, path = logPath(hub)
ensureDir(dir)
local f = io.open(path, "a")
if f then
f:write(line .. "\n")
f:close()
end
end
-- Format a timestamped chat line
local function fmtChat(nick, text)
return os.date("[%H:%M:%S]") .. " <" .. nick .. "> " .. text
end
local function fmtSystem(text)
return os.date("[%H:%M:%S]") .. " *** " .. text
end
---------------------------------------------------------------------------
-- Listeners
---------------------------------------------------------------------------
-- NMDC public chat
dcpp:setListener("chat", "chatlog",
function(hub, user, text)
writeLine(hub, fmtChat(user:getNick(), text))
end
)
-- ADC public chat
dcpp:setListener("adcChat", "chatlog",
function(hub, user, text, me_msg)
if me_msg then
writeLine(hub, fmtChat(user:getNick(), "* " .. text))
else
writeLine(hub, fmtChat(user:getNick(), text))
end
end
)
-- NMDC private messages
dcpp:setListener("pm", "chatlog",
function(hub, user, text)
writeLine(hub, os.date("[%H:%M:%S]") .. " [PM] <" .. user:getNick() .. "> " .. text)
end
)
-- ADC private messages
dcpp:setListener("adcPm", "chatlog",
function(hub, user, text)
writeLine(hub, os.date("[%H:%M:%S]") .. " [PM] <" .. user:getNick() .. "> " .. text)
end
)
-- Log connect / disconnect
dcpp:setListener("connected", "chatlog",
function(hub)
-- Auto-enable logging for every hub by default
local url = hub:getUrl()
if chatlog.enabled[url] == nil then
chatlog.enabled[url] = true
end
writeLine(hub, fmtSystem("Connected to " .. hub:getUrl()))
end
)
dcpp:setListener("disconnected", "chatlog",
function(hub)
writeLine(hub, fmtSystem("Disconnected from " .. hub:getUrl()))
end
)
-- User join/part
dcpp:setListener("userConnected", "chatlog",
function(hub, user)
writeLine(hub, fmtSystem(user:getNick() .. " joined"))
end
)
dcpp:setListener("userQuit", "chatlog",
function(hub, nick)
writeLine(hub, fmtSystem(nick .. " left"))
end
)
---------------------------------------------------------------------------
-- Chat commands
---------------------------------------------------------------------------
dcpp:setListener("ownChatOut", "chatlog",
function(hub, text)
local cmd = text:match("^/log%s+(%S+)")
if not cmd then return nil end
local url = hub:getUrl()
if cmd == "on" then
chatlog.enabled[url] = true
hub:addLine("*** Chat logging enabled")
return 1
elseif cmd == "off" then
chatlog.enabled[url] = false
hub:addLine("*** Chat logging disabled")
return 1
elseif cmd == "status" then
local state = chatlog.enabled[url] and "on" or "off"
hub:addLine("*** Chat logging is " .. state)
return 1
elseif cmd == "path" then
local _, path = logPath(hub)
hub:addLine("*** Log file: " .. path)
return 1
end
end
)
DC():PrintDebug(" ** Loaded chat_logger.lua **")