Code: Select all
--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
-- Client side replacer script by Hungarista, 2007. 01. 11. --
-- Respect to ']['phoon for his help and his nick :o) --
-- Usage: --
-- /addreplace (/ar) <text> <new_text> --
-- /delreplace (/dr) <text> --
-- /listreplace (/lr) --
-- /replace <on/off> --
--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
dofile( DC():GetAppPath() .. "/scripts/libsimplepickle.lua" )
sReplace = "on"
if loadfile(DC():GetAppPath().."/scripts/replaces.txt") then
dofile(DC():GetAppPath().."/scripts/replaces.txt")
else
tReplaces = {}
end
dcpp:setListener("ownChatOut","replacer",
function(hub,text)
text = DC():FromUtf8(text)
local tCmds = {
["help"] = function(hub)
hub:addLine("(replacer.lua) /addreplace <text> <new_text>, /delreplace <text>, /listreplace, /replace <on/off> (currently: "..sReplace..")")
return 1
end,
["addreplace"] = function(hub)
local _,_, from, to = string.find(text, "^/%S+%s+(%S+)%s(.+)")
if from and to then
if tReplaces[from] then
hub:addLine("<replacer> This expression is already replaced to "..tReplaces[from])
else
tReplaces[from] = to
Save()
hub:addLine("<replacer> "..from.." will be replaced now with: "..tReplaces[from])
end
else
hub:injectChat("<replacer> Usage: /addreplace <text> <new_text>")
end
return 1
end,
["delreplace"] = function(hub)
local _,_, from = string.find(text, "^/%S+%s+(%S+)")
if from then
if tReplaces[from] then
tReplaces[from] = nil
Save()
hub:addLine("<replacer> "..from.." has been deleted from the list.")
else
hub:addLine("<replacer> "..from.." is not on the list.")
end
else
hub:addLine("<replacer> Usage: /delreplace <text>")
end
return 1
end,
["listreplace"] = function(hub)
local msg = "Currently added replaces:\r\n\t\t"..string.rep("Z",42).."\r\n\t\t"..
"Z From: \t\t\tTo: \t\t\tZ\r\n\t\t"..string.rep("Z",42).."\r\n"
for key in pairs(tReplaces) do
msg = msg.."\t\t "..key.."\t\t\t"..tReplaces[key].."\r\n"
end
msg = msg.."\t\t"..string.rep("Z",42)
hub:addLine("<replacer> "..msg)
return 1
end,
["replace"] = function(hub)
local _,_,param = string.find(text,"^/replace (%S+)")
if string.lower(param) == "on" then
sReplace = "on"
hub:addLine("<replacer> Automatic replacing has been turned on.")
elseif string.lower(param) == "off" then
sReplace = "off"
hub:addLine("<replacer> Automatic replacing has been turned off.")
else
hub:addLine("<replacer> Valid parameters: on/off")
end
return 1
end,
}
tCmds["ar"] = tCmds["addreplace"] tCmds["dr"] = tCmds["delreplace"] tCmds["lr"] = tCmds["listreplace"]
local _,_,cmd = string.find(text,"^/(%S+)")
if tCmds[cmd] then
return tCmds[cmd](hub)
end
local bSend = true
if sReplace == "on" then
for k in pairs(tReplaces) do
if string.find(text,k) then
text = string.gsub(text,Escape(k),Escape(tReplaces[k]))
bSend = false
end
end
if string.find(text,"%-") then
for k in pairs(tReplaces) do
if string.find(text,"-"..tReplaces[k],1,true) then
text = string.gsub(text,"%-"..Escape(tReplaces[k]),Escape(k))
end
end
end
if not bSend then
if string.find(text,"^/me ") then
return 1
else
if hub:getProtocol() == "adc" then
text = DC():ToUtf8(text)
hub:sendChat(text)
else
hub:sendChat(text)
end
end
return 1
end
end
end
)
function Escape(text)
text = string.gsub(text,"%%","%%%%")
text = string.gsub(text,"%(","%%%(")
text = string.gsub(text,"%)","%%%)")
text = string.gsub(text,"%.","%%%.")
text = string.gsub(text,"%+","%%%+")
text = string.gsub(text,"%-","%%%-")
text = string.gsub(text,"%*","%%%*")
text = string.gsub(text,"%?","%%%?")
text = string.gsub(text,"%[","%%%[")
text = string.gsub(text,"%^","%%%^")
text = string.gsub(text,"%$","%%%$")
return text
end
function Save()
pickle.store(DC():GetAppPath().."/scripts/replaces.txt", { tReplaces = tReplaces })
end
DC():PrintDebug( " *** Loaded replacer.lua *** " )