[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Plugins
. . -> [Subject]  zMUD style gag plugin - Please post code suggestions...
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

zMUD style gag plugin - Please post code suggestions...

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Onoitsu2   USA  (246 posts)  [Biography] bio
Date Fri 02 Jun 2006 05:09 PM (UTC)  quote  ]
Message
Anyone that has used zmud knows that you can gag just part of a line, yet Mushclient only allows for the omitting of an entire line based upon a match. This plugin allows you to match multiple gags within a packet, and remove it from the line entirely without removing ANSI codes, and without omitting the line entirely from output.

The method used to remove the word or phrase is "DIRTY" and not very streamlined, but it works. Perhaps someone whom is a better coder can improve upon this rudementary plugin.

Upon install it shows the help information, and you can match things like this even.

addgagword testing---
from something like this
12345testing---67890testing---

It DOES NOT use regular expressions, I did that so that it does not match on things like this
addgagword ---
and gag the following, even though it is 1 more than wanted to match
----

if someone can improve this to use regular expressions properly in the "medthod" I described previously, then please HAVE AT IT! :)

Plugin posted below:
[Go to top] top

Posted by Onoitsu2   USA  (246 posts)  [Biography] bio
Date Reply #1 on Fri 02 Jun 2006 05:11 PM (UTC)  quote  ]

Amended on Fri 02 Jun 2006 05:12 PM (UTC) by Onoitsu2

Message
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- MuClient version 3.70 -->

<muclient>
<plugin
   name="ZmudGagger"
   author="Onoitsu2"
   id="b9794f6c141a300dbe5c4836"
   language="Lua"
   purpose="Gags words duplicating the method Zmud uses"
   save_state="y"
   date_written="2006-06-01"
   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 Zmud in its gagging method.

THIS PLUGIN IS EXTREMELY CASE SENSITIVE AND IF THE WORD HAS A SPACE AFTER IT
YOU MUST ADD IT AS WELL, THIS DOES NOT USE REGULAR EXPLRESSIONS TO MATCH!
example:
To Match: "Brother Phillip *"
To Gag: "Phillip "
Produces: "Brother *"

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

]]>
    </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[
gags = ""

function listgag(sName, sLine, wildcards)
count = 0
	local gaglist = gags
	if gaglist == "" or gaglist == nil then
	ColourNote("white", "red", "No gags in Your Gag Word List")
	return
	end
	print ("")
	for gag in string.gfind(gaglist, "[^||]+") do
		if count == 0 then
			ColourNote("white", "blue", "-- Your Gag Word List --")
		end
		ColourNote("white", "blue", gag)
		count = count + 1
	end
	if count > 0 then
		ColourNote("white", "blue", count .. " gag(s)")
	else
		ColourNote("white", "red", "No gags in your gag word list")
	end
	ColourNote("white", "black", "")
end

function addgag(sName, sLine, wildcards)
local gaglist = gags
gagname = wildcards[1]
if gaglist == "" then
	gaglist = wildcards[1]
	ColourNote("white", "blue", "Added Gag: " .. gagname)
else
	for gag in string.gfind(gaglist, "[^||]+") do
	if gag == gagname then
		ColourNote("white", "red", "Gag " .. gagname ..  " already in Your Gag Word List")
		return
	end
	end
	gaglist = gaglist .. "||" .. gagname
	ColourNote("white", "blue", "Added Gag: " .. gagname)
end
gags = gaglist
SaveState()
end -- addgag

function removegag(sName,sLine,wildcards)
local gaglist = gags
gwanted = wildcards[1]
gfound = 0
gnewlist = ""
count = 0
for gag in string.gfind(gaglist, "[^||]+") do
	if gag == gwanted then
		gfound = 1
	else
		if count > 0 then
			gnewlist = gnewlist .. "||"
		end
	count = count + 1
	gnewlist = gnewlist .. gag
	end
end
if gfound == 0 then
  ColourNote("white", "red", "Gag " .. gwanted .. " not in Your Gag Word List")
else
  gags = gnewlist
  SaveState()
  ColourNote("white", "green",  "Gag " .. gwanted .. " removed from Your Gag Word List")
end
end -- removegag

function OnPluginPacketReceived(packet)
local gaglist = gags
local packettemp = packet
local pos = 1
local before = ""
local after = ""
	if gaglist == "" or gaglist == nil then
	else
	for gag in string.gfind(gaglist, "[^||]+") do
	local lengthgag = string.len(gag)
		--string.gsub(packettemp,gag,"")
		pos = string.find(packettemp,gag,1,1)
		--Note(pos)
		while pos ~= nil
		do
		if pos == nil then
		else
		if pos == 1 or pos == 0 then
		before = string.sub(packettemp,0,pos)
		else
		before = string.sub(packettemp,1,pos-1)
		end
		after = string.sub(packettemp,pos+lengthgag)
		packettemp = before .. after
		pos = string.find(packettemp,gag,1,1)
		--Note(pos)
		end
		end
	end
	end
	packet = packettemp
	
    return packet
end

function OnPluginInstall()
OnHelp()
end

function OnHelp(sName, sLine, wildcards)
  Note(world.GetPluginInfo(world.GetPluginID(), 3))
end

]]>
</script>


</muclient>
[Go to top] top

Posted by Nick Gammon   Australia  (19,597 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Tue 06 Jun 2006 10:20 PM (UTC)  quote  ]
Message
If you close the world and re-open it, it doesn't remember the gagged words. You are doing SaveState but not moving the gaglist to a MUSHclient variable.

You can fix this by changing the first line of the script from:


gags = ""


to:


gags = GetVariable ("gags") or ""


Also add this function to the script to copy the local gags variable to the MUSHclient gags variable, prior to the state file being written:


function OnPluginSaveState ()
  SetVariable ("gags", gags)
end -- OnPluginSaveState


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (19,597 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Wed 07 Jun 2006 01:20 AM (UTC)  quote  ]

Amended on Wed 18 Aug 2010 06:38 AM (UTC) by Nick Gammon

Message
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.

Template:saveplugin=Gagger To save and install the Gagger plugin do this:
  1. Copy between the lines below (to the Clipboard)
  2. Open a text editor (such as Notepad) and paste the plugin into it
  3. Save to disk on your PC, preferably in your plugins directory, as Gagger.xml
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Gagger.xml (which you just saved in step 3) as a plugin
  7. 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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,040 views.

It is now over 60 days since the last post. This thread is closed.   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]