Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Easy way to add triggers, aliases and timers in Lua

Easy way to add triggers, aliases and timers in Lua

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,100 posts)  Bio   Forum Administrator
Date Fri 23 Jun 2006 03:05 AM (UTC)

Amended on Sat 24 Jun 2006 12:15 AM (UTC) by Nick Gammon

Message
There have been problems in the past with doing the internal AddTrigger script function because it does not support all of the trigger options. The method below lets you add a trigger, timer or alias using ImportXML, and supports all available options.



html_replacements = { 
   ["<"] = "&lt;",
   [">"] = "&gt;",
   ["&"] = "&amp;",
   ['"'] = "&quot;",
   }

-- fix text so that < > & and double-quote are escaped
function fixhtml (s)

  return (string.gsub (tostring (s), '[<>&"]', 
    function (str)
      return html_replacements [str] or str
    end ))

end -- fixhtml


function GeneralAdd (t, which, plural)
 
  assert (type (t) == "table", "Table must be supplied to add a " .. which)

  local k, v
  local xml = {}
  
  local send = fixhtml (t.send or "")  -- send is done differently
  t.send = nil
  
  -- turn into XML options  
  for k, v in pairs (t) do
  
    -- fix true/false to y/n
    if v == true then
      v = "y"
    elseif v == false then
      v = "n"
    end -- if true or false
    
    table.insert (xml, k .. '="' .. fixhtml (v) .. '"')
  end -- for loop
      
  assert (ImportXML (string.format (
          "<%s><%s %s ><send>%s</send></%s></%s>",
             plural,   -- eg. triggers
             which,    -- eg. trigger
             table.concat (xml, "\n"),  -- eg. match="nick"
             send,     -- eg. "go north"
             which,    -- eg. trigger
             plural)   -- eg. triggers
         ) == 1, "Import of " .. which .. " failed") 
  
end -- GeneralAdd 

function LuaAddTrigger (t)
  GeneralAdd (t, "trigger", "triggers")
end -- LuaAddTrigger

function LuaAddAlias (t)
  GeneralAdd (t, "alias", "aliases")
end -- LuaAddAlias 

function LuaAddTimer (t)
  GeneralAdd (t, "timer", "timers")
end -- LuaAddTimer 

function LuaAddMacro (t)
  GeneralAdd (t, "macro", "macros")
end -- LuaAddMacro 



What this does is let you do an LuaAddTrigger (alias or timer) by supplying a table of all the keywords you want to set. The others will take their default values (of zero, false, or an empty string) depending on their type.


These examples below show the general idea:



LuaAddTrigger {  match = "swordfish", 
                regexp = true,
                ['repeat'] = true,   -- repeat is lua keyword
                send = "hi there",
                sequence = 50,
                enabled = true,
              }
              
LuaAddAlias  {  match = "test", 
                send = "sing",
                group = "test group"
              }              
              
LuaAddTimer {  name = "mytimer",
                minute = 5,
                second = 3,
                send "drink water"
              }
              

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,100 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 23 Jun 2006 03:55 AM (UTC)

Amended on Thu 23 Jun 2016 08:14 PM (UTC) by Nick Gammon

Message
Available keywords for triggers are:


back_colour             0 - 15 - which background colour to match on (see below)
bold                    y/n  - see match_bold
clipboard_arg           0 - 10 - which wildcard to copy to the clipboard
colour_change_type      0=both, 1=foreground, 2=background
custom_colour           0=no change, 1 - 16, 17=other
enabled                 y/n - trigger is enabled
expand_variables        y/n - expand variables (like @target)
group                   (string - group name)
ignore_case             y/n - caseless matching
inverse                 y/n - see match_inverse
italic                  y/n - see match_italic
keep_evaluating         y/n - evaluate next trigger in sequence
lines_to_match          0 - 200 - how many lines to match for multi-line triggers
lowercase_wildcard      y/n - make matching wildcards lower-case
make_bold               y/n - display in bold
make_italic             y/n - display in italic
make_underline          y/n - display underlined
match                   (string - what to match)
match_back_colour       y/n - match on value of text colour
match_bold              y/n - match on value of "bold"
match_inverse           y/n - match on value of "inverse"
match_italic            y/n - match on value of "italic"
match_text_colour       y/n - match on value of text colour
multi_line              y/n - multi-line trigger
name                    (string - name/label of trigger)
omit_from_log           y/n - omit matching line from log file
omit_from_output        y/n - omit matching line from output window
one_shot                y/n - trigger is deleted after firing
other_back_colour       (string - name of colour to change to)
other_text_colour       (string - name of colour to change to)
regexp                  y/n - regular expression
repeat                  y/n - repeatedly evaluate on same line
script                  (string - name of function to call)
send                    (multi-line string - what to send)
send_to                 0 - 14 - "send to" location (see below)
sequence                0 - 10000 - sequence in which to check - lower first
sound                   (string - sound file name to play)
sound_if_inactive       y/n - play sound even if world inactive
text_colour             0 - 15 - which text colour to match on (see below)
user                    -2147483647 to 2147483647 - user-defined number
variable                (string - name of variable to send to)





Available keywords for aliases are:


echo_alias              y/n - echo alias itself to output window
enabled                 y/n - alias is enabled
expand_variables        y/n - expand variables (like @target)
group                   (string - group name)
ignore_case             y/n - caseless matching
keep_evaluating         y/n - evaluate next alias in sequence
match                   (string - what to match)
menu                    y/n - add alias to menu that appears if you LH click
name                    (string - name/label of alias)
omit_from_command_history  y/n - omit alias from the command history
omit_from_log           y/n - omit alias from log file
omit_from_output        y/n - omit sent text from the output window
one_shot                y/n - alias is deleted after firing
regexp                  y/n - regular expression
script                  (string - name of function to call)
send                    (multi-line string - what to send)
send_to                 0 - 13 - "send to" location (see below)
sequence                0 - 10000 - sequence number in which to check - lower first
user                    -2147483647 to 2147483647 - user-defined number
variable                (string - name of variable to send to)






Available keywords for timers are:



active_closed           y/n - timer is active when world closed
at_time                 y/n - y=fire *at* time of day, otherwise every interval
enabled                 y/n - timer is enabled
group                   (string - group name)
hour                    hour to fire at (or every hour)
minute                  minute to fire at (or every minute)
name                    (string - name/label of alias)
offset_hour             offset hour - fire at time *plus* this
offset_minute           offset minute - fire at time *plus* this
offset_second           offset second - fire at time *plus* this
omit_from_log           y/n - omit timer from log file
omit_from_output        y/n - omit timer output from log file
one_shot                y/n - delete after firing
script                  (string - name of function to call)
second                  second to fire at (or every second)
send                    (multi-line string - what to send)     
send_to                 0 - 13 - "send to" location (see below)
user                    -2147483647 to 2147483647 - user-defined number
variable                (string - name of variable to send to)





Send-to locations


 0   - send to MUD                  
 1   - put in command window        
 2   - display in output window     
 3   - put in status line           
 4   - new notepad                  
 5   - append to notepad            
 6   - put in log file              
 7   - replace notepad              
 8   - queue it                     
 9   - set a variable               
10   - re-parse as command          
11   - send to MUD as speedwalk     
12   - send to script engine        
13   - send without queuing
14   - send to script engine - after omitting from output (triggers)         


If you are using Lua, you can use built-in variable names rather than numbers, for better documentation.

The predefined names are:


sendto.world = 0
sendto.command = 1
sendto.output = 2
sendto.status = 3
sendto.notepad = 4
sendto.notepadappend = 5
sendto.logfile = 6
sendto.notepadreplace = 7
sendto.commandqueue = 8
sendto.variable = 9
sendto.execute = 10
sendto.speedwalk = 11
sendto.script = 12
sendto.immediate = 13
sendto.scriptafteromit = 14




Colour numbers to use when matching on text/background


 black    = 8 
 red      = 9
 green    = 10
 yellow   = 11
 blue     = 12
 magenta  = 13
 cyan     = 14
 white    = 15




Note: In the Lua code above (LuaAddTrigger and so on), the y/n (boolean) flags can be supplied as either the string value "y" and "n", or the boolean values true and false.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,100 posts)  Bio   Forum Administrator
Date Reply #2 on Sat 24 Jun 2006 12:21 AM (UTC)
Message
Extracting a trigger/alias/timer/macro into a table

The inverse operation of GeneralAdd can be done by the code below. This takes an existing trigger/alias/timer/macro and uses ExportXML and utils.xmlread to convert it back into its component table items. You can then modify one or more of them and then re-import them.


function ItemToTable (which, name)

  local typeconversion = 
     {
     trigger = 0,
     alias = 1,
     timer = 2,
     macro = 3,
 --    variable = 4,
 --   keypad = 5 
     }
     
  local itemtype = assert (typeconversion [which], "Unknown type: " .. which)
  
  local xml = ExportXML (itemtype, name)
  
  -- if not found returns empty string
  assert (xml ~= "", "Can't find " .. which .. ": " .. name)
  
  -- parse back into table entries
  local xmlnodes = assert (utils.xmlread (xml), "Bad XML")
  
  -- all attributes should be a couple of levels down                    
  local result = xmlnodes.nodes [1].nodes [1].attributes
                   
  -- find "send" text
  
  -- another level?
  if xmlnodes.nodes [1].nodes [1].nodes then
    if xmlnodes.nodes [1].nodes [1].nodes [1].name == "send" then
       result.send = xmlnodes.nodes [1].nodes [1].nodes [1].content
    end -- have a "send" field
  end -- have a child node
  
  return result
end -- ItemToTable



As an example, let's extract a trigger called "test" and change a couple of its fields:


-- get trigger "test"
t = ItemToTable ("trigger", "test")

-- change it
t.other_back_colour = "orange"
t.other_text_colour = "purple"

-- put it back
LuaAddTrigger (t)


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,100 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 03 Apr 2007 02:46 AM (UTC)
Message
These functions are now encapsulated into addxml.lua which ships with MUSHclient these days. Thus, without having to add any extra code into your scripts, you can "require" the addxml.lua file, and do it like this:


require "addxml"
addxml.trigger {  match = "swordfish", 
                regexp = true,
                ['repeat'] = true,   -- repeat is lua keyword
                send = "hi there",
                sequence = 50,
                enabled = true,
                name = "boris",
              }


So now the example of exporting a trigger into a table, changing a couple of fields, and re-importing, would look like this:


require "addxml"  -- get extension script

-- get trigger "test"
t = addxml.save ("trigger", "test")

-- change it
t.other_back_colour = "orange"
t.other_text_colour = "purple"

-- put it back
addxml.trigger (t)


- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


31,146 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.