Quote:
if someone can improve this to use regular expressions properly in the "medthod" I described previously, then please HAVE AT IT! :)
OK, that is a nice challenge. :)
This version has a much simpler matching routine. It uses a string.gsub to fix up the gag string to put % in front of non-alphabetic characters, so it can then be used in another string.gsub to fix up the incoming packet.
It also makes the "gags" variable into a table rather than a string, this greatly simplifies going through each one in sequence. The wanted gag strings are saved to the state file as a simple "one gag per line" so you could easily edit the state file if you wanted to add or remove gags manually.
|
To save and install the Gagger plugin do this:
- Copy between the lines below (to the Clipboard)
- Open a text editor (such as Notepad) and paste the plugin into it
- Save to disk on your PC, preferably in your plugins directory, as Gagger.xml
- Go to the MUSHclient File menu -> Plugins
- Click "Add"
- Choose the file Gagger.xml (which you just saved in step 3) as a plugin
- Click "Close"
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- MuClient version 3.70 -->
<muclient>
<plugin
name="Gagger"
author="Nick Gammon"
id="9626e155b19100503893724c"
language="Lua"
purpose="Gags invididual words or strings from the MUD"
save_state="y"
date_written="2006-06-07"
requires="3.70"
version="1.0"
>
<description trim="n">
<![CDATA[
Allows the user to make a gag list, of words to be removed from within a line,
as opposed to gagging the entire line from output, so MUSHclient functions
more like other MUD clients in its gagging method.
Gagging is case and punctuation sensitive.
addgagword <word> - adds a gag word to the gags list
removegagword <word> - removes a gag word from the gags list
listgagwords - lists the gags list
gaghelp - show this help
Original idea by Onoitsu2 on the MUSHclient forum.
Warning - individual packets are gagged, so a word split over packets will get
through. Also, words which are in multiple colours will not match.
]]>
</description>
</plugin>
<!-- Triggers -->
<!-- Variables -->
<aliases>
<alias
match="^addgagword (.*?)$"
enabled="y"
group="gags"
regexp="y"
script="addgag"
sequence="100"
>
</alias>
<alias
match="^listgagwords$"
enabled="y"
group="gags"
regexp="y"
script="listgag"
sequence="100"
>
</alias>
<alias
match="^removegagword (.*?)$"
enabled="y"
group="gags"
regexp="y"
script="removegag"
sequence="100"
>
</alias>
</aliases>
<!-- Script -->
<aliases>
<alias
script="OnHelp"
regexp="y"
group="gags"
match="^gaghelp$"
enabled="y"
>
</alias>
</aliases>
<script>
<![CDATA[
function listgag (sName, sLine, wildcards)
if table.getn (gags) == 0 then
ColourNote("white", "red", "No gags in Your Gag Word List")
return
end -- if no gags
NoteHr ()
ColourNote("white", "blue", "-- Your Gag Word List --")
Note ""
for _, gag in ipairs (gags) do
ColourNote("white", "black", ">",
"white", "blue", gag,
"white", "black", "<")
end -- listing each one
local s = ""
if table.getn (gags) > 1 then
s = "s"
end -- if more than one gag
Note ""
ColourNote("white", "blue", table.getn (gags) .. " gag" .. s)
NoteHr ()
end -- listgag
-- helper function for use with table.foreachi
-- returns a function with "what" as an upvalue
function finder (what)
return function (k, v)
if v == what then
return k
end -- if found
end -- function
end -- finder
function addgag (sName, sLine, wildcards)
local gagname = wildcards [1]
if table.foreachi (gags, finder (gagname)) then
ColourNote("white", "red", "Gag '" .. gagname ..
"' already in Your Gag Word List")
return
end
table.insert (gags, gagname)
ColourNote("white", "blue", "Added Gag: '" .. gagname .. "'")
SaveState()
end -- addgag
function removegag (sName,sLine,wildcards)
local gwanted = wildcards [1]
-- find matching gag, if any
local gfound = table.foreachi (gags, finder (gwanted))
if gfound then
table.remove (gags, gfound)
SaveState()
ColourNote("white", "green", "Gag '" .. gwanted ..
"' removed from Your Gag Word List")
else
ColourNote("white", "red", "Gag '" .. gwanted ..
"' not in Your Gag Word List")
end -- if
end -- removegag
function OnPluginPacketReceived (packet)
local k, v
-- remove each gag word from the packet
for k, v in ipairs (gags) do
packet = string.gsub (packet,
string.gsub (v, "(%W)", "%%%1"), -- make into regexp
"")
end -- for
return packet
end -- OnPluginPacketReceived
function OnPluginInstall ()
DoAfterSpecial (1, "OnHelp()", 12) -- show help after 1 second
-- load existing gags (one per line)
-- (remove carriage-returns from variable)
gags = utils.split (
string.gsub (GetVariable ("gags") or "", "\r", ""),
"\n")
end -- OnPluginInstall
function OnHelp ()
Note(world.GetPluginInfo(world.GetPluginID(), 3))
end -- OnHelp
function OnPluginSaveState ()
table.sort (gags) -- keep in alphabetic order
-- save gags table as one entry per line
SetVariable ("gags", table.concat (gags, "\n"))
end -- OnPluginSaveState
]]>
</script>
</muclient>
As I warn in the help for the plugin, it only works on individual packets, so words split over two packets will not be detected.
Also, as packets may have ANSI colour sequences in them, words which change colour in the middle will not be detected.
|