Posted by
| Nick Gammon
Australia (22,973 posts) bio
Forum Administrator |
Message
| With the new functionality of OnPluginPacketReceived it is now possible to "fool" MUSHclient into thinking prompt lines have a newline after them, even if they don't.
This can be useful for situations where you want a trigger to match a prompt line the moment it arrives, which it won't normally because trigger matching happens when a newline is received.
The way it is done is this:
- The plugin below receives all incoming text from the MUD.
- It batches the packets into lines, as sometimes lines are split over packets
- All of the lines, except the last one, are just reassembled into normal lines with a newline between them.
- The final line (one with no newline after it), is tested against a regular expression which should be set up to match your prompt.
In my test case the prompt was:
(ESC)[0m<1000/1000hp 100/100m 110/110mv 2000/315816750xp>
The very first character is the "Escape" character (hex 1B) which is used in this case to reset colours to the default. (ESC[0m). Since we are expecting that we build it into the regular expression.
Note that in the regular expression we have to use two backslashes, as the first is used by Lua to indicate that the second is a "literal" backslash. This gets passed to the regular expression engine as \x1B to indicate the "escape" character.
If the regular expression matches, then the final (partial) line is also returned to MUSHclient (with a newline after it) thus effectively adding the newline to the prompt line.
- The lines (saved into table t) are concatenated back into a single string with a newline between each table entry, and returned as the function result.
To use this, just copy below the line, and paste into a text window (notepad window). Change the regular expression (line 3) to match your prompt line, save as Add_Newline_To_Prompt.xml and then install it as a plugin.
All being well, your prompt lines should now automatically end with a newline, and thus match triggers immediately.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient[
<!ENTITY prompt_regexp "^\\x1B\\[0m<.+hp .+m .+mv .+xp> $" >
]>
<!-- MuClient version 3.59 -->
<!-- Plugin "Add_Newline_To_Prompt" generated by Plugin Wizard -->
<muclient>
<plugin
name="Add_Newline_To_Prompt"
author="Nick Gammon"
id="8316c19c35a9ebdb46055874"
language="Lua"
purpose="Adds a newline to prompt lines"
date_written="2004-12-13 12:08:00"
requires="3.59"
version="1.0"
>
<description trim="y">
Detects prompt lines without a newline, and if found, adds one.
Change ENTITY line on 3rd line of plugin to be a regular expression
that matches your prompt.
</description>
</plugin>
<!-- Script -->
<script>
re = rex.new ("&prompt_regexp;")
<![CDATA[
partial = "" -- partial line from last time through
function OnPluginPacketReceived (s)
-- add packet to what we already have (excluding carriage-returns)
partial = partial .. string.gsub (s, "\r", "")
t = {} -- table of lines to be returned
-- iterate over each line
partial = string.gsub (partial, "(.-)\n",
function (line)
table.insert (t, line)
return "" -- added for MUSHclient 3.80+
end)
-- look for prompt
if (re:match (partial)) then
table.insert (t, partial)
partial = ""
end -- if found prompt
if table.getn (t) > 0 then
table.insert (t, "") -- to get final linefeed
end -- if
-- return table of lines, concatenated with newlines between each one
return table.concat (t, "\n")
end -- function OnPluginPacketReceived
]]>
</script>
</muclient>
[EDIT] Amended on 22 October 2006 to make a change so that this plugins works with MUSHclient 3.80 onwards. The amended line is in bold (return "") and the surrounding lines have been reformatted a bit. See this forum post for a discussion:
http://www.gammon.com.au/forum/?id=7430&page=999
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|