-- chat_history - Channel-based history for Chat System
-- (component of Chat System)
-- Template attachment: chat_system
-- Created by: Patrick Ferland
-- Created on: 06/02/2008
-- Revision History
-- 06/02/2008 P.Ferland - Cleanup and documentation
-- 06/20/2008 J.McNab - Added styling of chat history and login history length
-- 07/17/2008 P.Ferland - Unpersisted history until fixed (stylesheet can't contain large tables)
-- 07/22/2008 P.Ferland - Commented attach/detach object blocks
-- 07/28/2008 P.Ferland - Persisted history - chat is no longer a stylesheet object!
-- 07/29/2008 P.Ferland - Replaced "-" prefix with system coloration
-- 09/02/2008 P.Ferland - Changed hardcoded buffer sizes to MAX_SEND_BUFFER_SIZE
-- 10/08/2008 P.Ferland - OX friendliness pass
-- Constants
WORLD_HISTORY_SCRIPT = '11302:24'
HISTORY_SAVE_TICK = 900000 -- 15-minute save tick
-- Script properties
Define Properties()
script_description = "Chat History"
script_long_description = "Manage how your world chat system remembers and displays public chat"
chat_system_history_enabled = 1
ExposeProperty('chat_system_history_enabled', 'Enable chat channel history?', 'checkbox')
PersistProperty('chat_system_history_enabled')
chat_system_history_length = 500
ExposeProperty('chat_system_history_length', 'Number of lines to keep per channel', 'int')
SetPropRange('chat_system_history_length', 0, 5000)
chat_system_login_history_length = 10
ExposeProperty('chat_system_login_history_length', 'Number of lines of history to show on login', 'int')
SetPropRange('chat_system_login_history_length', 0, 100)
chat_system_history_web = 0
ExposeProperty('chat_system_history_web', 'Allow web view of chat history?', 'checkbox')
PersistProperty('chat_system_history_web')
chat_system_history_web_secure = 1
ExposeProperty('chat_system_history_web_secure', 'Require password for web chat history?', 'checkbox')
PersistProperty('chat_system_history_web_secure')
chat_system_history_web_password = ''
ExposeProperty('chat_system_history_web_password', 'Web chat history password')
PersistProperty('chat_system_history_web_password')
chat_system_history = {}
PersistProperty('chat_system_history')
chat_system_history_style = ''
ExposeProperty('chat_system_history_style', 'URL for the location of the stylesheet for your chat history page')
PersistProperty('chat_system_history_style')
IncludeScript('11302:12') -- chat_utility
end
-- Commands
-- no commands
-- Triggers
-- System trigger: called when script is attached (in builder or at instantiation)
Trigger attach()
SendTo(self, 'chat_system_register_command', 0,
'history', 'Display 10 lines of history (or /history [number] between 1-1000 for [number] lines of history)', 0, 0)
-- Begin tick to save chat system history
SendTo(self, 'chat_system_history_save', HISTORY_SAVE_TICK)
end
-- System trigger: called when script is detached
Trigger detach()
SendTo(self, 'chat_system_unregister_command', 0, 'history')
end
-- Save chat history
Trigger chat_system_history_save_tick()
if (self.chat_system_history_enabled == 1) then
SaveToDb(self)
end
SendTo(self, 'chat_system_history_save', HISTORY_SAVE_TICK)
end
-- Core "chat system" trigger for sending any message
-- Handling this allows us to process the raw message. This means that history is NOT profanity-filtered, colored, etc.
Trigger chat_system_send_message(endpoint, message, channel, sender, command)
-- Quit if chat history is disabled
if (self.chat_system_history_enabled ~= 1) then
return
end
-- Above all else, be polite. Do NOT store user channel histories!
if (string.find(channel, '/user/') ~= nil) then
return
end
local channel_history = self.chat_system_history[channel]
local last_message = ''
if (channel_history ~= nil) then
while ( (#channel_history >= self.chat_system_history_length) and (#channel_history > 0) ) do
table.remove(channel_history, 1)
end
last_message = channel_history[#channel_history].msg
else
self.chat_system_history[channel] = {}
end
if (message ~= last_message) then
table.insert( self.chat_system_history[channel], chat_system_history_line(message) )
end
end
-- /history [lines] command
-- Displays last 10 (or [lines], if provided and a number 1-1000 ) lines of history for current channel
Trigger chat_msg_history(sender, message)
local channel = chat_system_channel_name('user', sender.name)
local history = self.chat_system_history[sender.chat_system_current_channel]
-- Quit if chat history is disabled
if (self.chat_system_history_enabled == 1) then
local lines = 10
local arguments = {}
if ( (message ~= nil) and (message ~= '') ) then
local num = tonumber(message)
if (num ~= nil) then
if (num < 1) then
lines = 1
elseif (num > 1000 ) then
lines = 1000
else
lines = num
end
end
end
if (history ~= nil) then
if (#history < lines) then
lines = #history
end
local message = chat_system_format_system_message('- Displaying ' .. lines .. ' lines of history:
')
local pre = ''
local i = 0
for i = #history - lines + 1, #history do
message = message .. pre .. chat_system_format_system_message('[' .. history[i].time .. '] ' .. history[i].msg)
pre = '
'
end
if (string.len(message) > 0) then
chat_system_send_to_subscribers(self, sender, channel, message, 'history')
end
else
local message = 'No history for channel'
chat_system_send_to_subscribers(self, sender, channel, chat_system_format_system_message(message), 'history')
end
else
local message = 'No history is available'
chat_system_send_to_subscribers(self, sender, channel, chat_system_format_system_message(message), 'history')
end
chat_system_set_callback(sender, CHAT_STATUS_CODES.OK, 'OK')
end
-- Local functions
-- Generate a history line
function chat_system_history_line(message)
local history_line = { time = chat_system_history_timestamp(), msg = message }
return history_line
end
-- Generate timestamp
function chat_system_history_timestamp()
return os.date("%x %X")
end