-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_logger.lua
More file actions
218 lines (183 loc) · 6.07 KB
/
url_logger.lua
File metadata and controls
218 lines (183 loc) · 6.07 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
-- url_logger.lua
-- @description: Logs URLs posted in channels with timestamps
-- @author: RustIRC Contributors
-- @version: 1.0.0
--[[
URL Logger Script for RustIRC
Features:
- Detects URLs in chat messages
- Stores URLs with timestamps and channel info
- Provides commands to view recent URLs
- Supports filtering by channel
- Configurable buffer size
Usage:
/urls [count] - Show last N URLs (default: 10)
/urls clear - Clear URL log
/urls search <text> - Search URLs containing text
Configuration:
Edit the CONFIG table below to customize behavior
]]--
-- Configuration
local CONFIG = {
enabled = true,
max_urls = 500, -- Maximum URLs to store
show_notifications = false, -- Notify on URL detection
excluded_channels = {}, -- Channels to ignore
}
-- State
local url_log = {}
-- URL pattern (basic HTTP/HTTPS detection)
local URL_PATTERN = "https?://[%w-_%.%?%.:/%+=&%%#~]+"
---Helper function to add URL to log
---@param url string The URL to log
---@param channel string Channel where URL was posted
---@param nick string Nickname who posted it
local function log_url(url, channel, nick)
local entry = {
url = url,
channel = channel,
nick = nick,
timestamp = os.time(),
date = os.date("%Y-%m-%d %H:%M:%S"),
}
table.insert(url_log, entry)
-- Maintain buffer size
if #url_log > CONFIG.max_urls then
table.remove(url_log, 1)
end
-- Optional notification
if CONFIG.show_notifications then
irc.notify("URL Posted", url)
end
irc.log("debug", string.format("Logged URL from %s in %s: %s", nick, channel, url))
end
---Check if channel is excluded
---@param channel string Channel to check
---@return boolean
local function is_excluded(channel)
for _, excluded in ipairs(CONFIG.excluded_channels) do
if channel == excluded then
return true
end
end
return false
end
---Format URL entry for display
---@param entry table URL log entry
---@return string
local function format_entry(entry)
return string.format("[%s] <%s/%s> %s",
entry.date,
entry.channel,
entry.nick,
entry.url
)
end
-- Event Handler: Message received
function irc.on_message(event)
if not CONFIG.enabled then return end
if event.type ~= "message" then return end
if not event.params or #event.params < 2 then return end
local channel = event.params[1]
local message = event.params[#event.params]
-- Skip excluded channels
if is_excluded(channel) then return end
-- Extract nickname from message prefix if available
-- Note: In the actual IRC integration, the nickname would be parsed from
-- the message prefix. For this example script, we use a placeholder.
local nick = event.prefix and event.prefix:match("^([^!]+)") or "unknown"
-- Find all URLs in message
for url in message:gmatch(URL_PATTERN) do
log_url(url, channel, nick)
end
end
-- Custom Commands
irc.commands = irc.commands or {}
---Command: View recent URLs
irc.commands.urls = function(args)
local subcmd = args[1] or "list"
if subcmd == "clear" then
-- Clear URL log
local count = #url_log
url_log = {}
irc.print(string.format("Cleared %d URLs from log", count))
return
end
if subcmd == "search" then
-- Search URLs
if not args[2] then
irc.print("Usage: /urls search <text>")
return
end
local search_term = args[2]:lower()
local matches = {}
for _, entry in ipairs(url_log) do
if entry.url:lower():find(search_term, 1, true) then
table.insert(matches, entry)
end
end
if #matches == 0 then
irc.print("No URLs found matching: " .. args[2])
else
irc.print(string.format("Found %d URLs matching '%s':", #matches, args[2]))
for _, entry in ipairs(matches) do
irc.echo(format_entry(entry))
end
end
return
end
-- Default: List recent URLs
local count = tonumber(subcmd) or 10
count = math.min(count, #url_log)
if #url_log == 0 then
irc.print("No URLs logged yet")
return
end
irc.print(string.format("Last %d URLs (total: %d):", count, #url_log))
local start = math.max(#url_log - count + 1, 1)
for i = start, #url_log do
irc.echo(format_entry(url_log[i]))
end
end
---Command: Configure URL logger
irc.commands.urlconfig = function(args)
if #args == 0 then
-- Show current configuration
irc.print("URL Logger Configuration:")
irc.echo(" enabled: " .. tostring(CONFIG.enabled))
irc.echo(" max_urls: " .. CONFIG.max_urls)
irc.echo(" notifications: " .. tostring(CONFIG.show_notifications))
irc.echo(" current URLs: " .. #url_log)
return
end
local setting = args[1]:lower()
local value = args[2]
if setting == "enable" then
CONFIG.enabled = true
irc.print("URL logging enabled")
elseif setting == "disable" then
CONFIG.enabled = false
irc.print("URL logging disabled")
elseif setting == "notifications" then
CONFIG.show_notifications = value == "on" or value == "true"
irc.print("Notifications: " .. tostring(CONFIG.show_notifications))
elseif setting == "maxurls" and value then
local new_max = tonumber(value)
if new_max and new_max > 0 then
CONFIG.max_urls = new_max
irc.print("Max URLs set to: " .. new_max)
-- Trim if needed
while #url_log > CONFIG.max_urls do
table.remove(url_log, 1)
end
else
irc.print("Invalid number for maxurls")
end
else
irc.print("Usage: /urlconfig [enable|disable|notifications on/off|maxurls <num>]")
end
end
-- Initialization
irc.print("URL Logger v1.0.0 loaded")
irc.print("Commands: /urls [count|clear|search], /urlconfig")
irc.log("info", string.format("URL Logger initialized (max: %d URLs)", CONFIG.max_urls))