Progress has been made!
- reading the file
- separating out the user names from the message
In preparation for further error messa–I mean, progress–each line now has a UID (date to the second and the position in the file it was read–if it isn’t unique Something Went Wrong™). Username has been stripped of surrounding <>'s and symbols. And look, there’s the rest of the message!
The code was very hacky and I’m sure it can be streamlined considerably. I also experimented with functions. They’re my little buddies from a past era.
Gotta love the little guys!
-- https://stackoverflow.com/questions/42062779/convert-string-to-timestamp
-- https://www.lua.org/pil/22.1.html
-- Assuming a date pattern like: yyyy-mm-dd hh:mm:ss
local function grab_time(raw_message)
local functn = "grab_time " -- debug
local raw_time = string.sub(raw_message, 2, 21) -- msg_time = from character 2 to 21
local raw_time = raw_time:gsub("[^%w%s]+", "") -- remove non-alphanumerics (still needs work)
local msg_time = raw_time:gsub("%s+", "") -- remove the spaces
print(scriptn .. functn .. "msg_time " .. msg_time) -- debug
return msg_time -- return the time string -- can this be combined with above?
end
-- [2022-03-28 14:33:08] <@PokemonCommunityGame> Abra has been caught by: wyrdewyn
-- 1---------------------22 ------------------- I did a lot of counting.
local function grab_user(raw_message)
local functn = "grab_user "
local end_of_name_area = 3
local msg_after_date = string.sub(raw_message, 23) -- grab message after the date.
if string.find(msg_after_date, "<", 1, true) == 1 then -- if the first character is a < then it is a user name
local end_of_name_area = string.find(msg_after_date, ">", 2, true) -- find out how many characters it takes to get to >
local user_name_area = string.sub(msg_after_date, 1, end_of_name_area) -- snip out the letters between
local cleaned_name = user_name_area:gsub("[^%w%s]+", "")
print(scriptn .. functn .. "cleaned_name " .. cleaned_name)
return cleaned_name
else
return "SYSTEM"
end
end
-- https://stackoverflow.com/questions/62647752/lua-string-drop-non-alphanumeric-or-space
-- customer_input , _ = customer_input:gsub("[^%w%s]+", "");
-- https://stackoverflow.com/questions/12117965/lua-how-to-check-if-a-string-contains-only-numbers-and-letters
-- https://stackoverflow.com/questions/10460126/how-to-remove-spaces-from-a-string-in-lual
local function grab_body(raw_message)
local functn = "grab_body "
if string.find(raw_message, "<", 1, true) == 23 then -- if < at 23, then start of a username.
local end_of_name_area = string.find(raw_message, ">", 24, true) + 1 -- find end of the user name.
print(scriptn .. functn .. "end_of_name_area: " .. end_of_name_area) -- debug
local msg_body = string.sub(raw_message, end_of_name_area) -- after username is the message.
print(scriptn .. functn .. "msg_body " .. msg_body)
return msg_body
else
local msg_body = string.sub(raw_message, 23) -- no < means a status message.
return msg_body
end
end
the check_log()
function grew a bit, but most of it is debug messages.
local function check_log(self, handle) -- the master function for log reading.
local functn = "check_log "
if log_accessed == 0 then -- should only be donce once, when timer first activates.
-- file_length = check_file_size(filename) -- check what the file length is when first opened.
local logfile = io.input(filename, "r") -- access in read mode
file_length = logfile:seek("end") -- length of file when first accssed
print(scriptn .. functn .. "file_length " .. file_length) -- debug
-- log_accessed = 0
log_accessed = log_accessed + 1 -- increment and hopfully not read the same line again
print(scriptn .. functn .. "log_accessed 1 " .. log_accessed) -- debug
else
-- length_alpha = check_file_size(filename) -- log_accessed lets me reuse the function
local logfile = io.input(filename, "r") -- access in read mode
length_alpha = logfile:seek("end") -- length of file when first accssed
-- print(scriptn .. functn .. "file_length 2 " .. file_length) -- debug
-- log_accessed = 0
log_accessed = log_accessed + 1 -- to prevent an update to file size too soon
-- print(scriptn .. functn .. "log_accessed 2 " .. log_accessed) -- debug
if length_alpha > file_length then -- if the file size is larger, there's new lines to read
-- tail_log(length_alpha) -- function to do the reading
-- -- testing the log print.
-- local logfile = io.input(filename, "r") -- done above, don't need?
logfile:seek("set", file_length) -- "set" the "pointer" to file_length
if logfile then -- double check it is not "nil"
for i=1,seek_lines do -- start iterating though a possible number of lines (some might have been added)
local log_lines = io.read("*l") -- I thnk this reads by "next line to "end of line". If doesn't do what I want, then there's the line() call to try.
file_length = logfile:seek() -- set file length to the new EOF?
if not log_lines then -- if log_lines is empty, stop
break -- stop reading
end
print(scriptn .. functn .. "log lines" .. log_lines) -- debug & print the log_lines
-- figure out the message UUID. Timestamp + file_length will be plenty specific.
raw_msg = log_lines
local msg_time = grab_time(raw_msg)
local msg_user = grab_user(raw_msg)
local msg_body = grab_body(raw_msg)
local msg_uid = msg_time .. "-" .. file_length -- not sure I want this to be a number so using a dash
--msg.post("go#test_http", "log_parsed", {line = log_lines}) -- send messge to the GUI node.
print(msg_uid .. "##".. msg_user .. "##" .. msg_body)
msg.post("go#test_http", "log_message", {uid = msg_uid, user = msg_user, body = msg_body}) -- send messge to the GUI node.
end
end
file_length = length_alpha -- after reading, increase the stored filesize.
-- print(scriptn .. functn .. "file_length 3 " .. file_length) -- debug
end
end
end
Meanwhile, back in the gui_script
there is a little friend in the on_messages
function on_message(self, message_id, message, sender)
if message_id == hash("log_parsed") then -- check message ID
local log_window_node = gui.get_node("text_log_window") -- get the node info
gui.set_text(log_window_node, message.line) -- print message to text node
end
if message_id == hash("log_message") then -- check message ID
local log_window_node = gui.get_node("text_log_window") -- get node info
local glue_message = message.uid .. ": " .. message.user .. ": " .. message.body -- our new friend from readchatlog
-- {uid = msg_uid, user = msg_user, body = msg_body} -- my memory is terrible
gui.set_text(log_window_node, glue_message) -- print message to text node
end -- go, little buddy, go!
end
I think I figured out how to get more than one message to show up at a time: duct tape!