Executing Lua every time data is stored

Whenever a channel's protocol calls source.store or source.storebinary, the Lua core will allow an “onrecord” event for that channel.

This gives the opportunity to analyze, modify, and split the data.

The “onrecord” hook is made by assigning: x.chnl[#].src.onrecord
(where # is the channel number).

e.g.

function my_store(txt, chnl, tag)
  mem.write(chnl, txt)
end

x.chnl[1].src.onrecord = my_store
Caution

Once the hook is assigned it becomes the responsibility of the onrecord function to store the data!

Lua will pass three parameters to the onrecord function:

Type Description
string The data to store
integer The channel number
string The data tag (as passed to source.store*)

e.g.

function my_channel1(txt, c, tag)
 -- txt is the string
 -- c is the channel number
 -- tag is the tag string
 mem.write(c, txt) -- perform a simple write
end

x.chnl[1].src.onrecord = my_channel1

functionmy_channel2(txt, c, tag)
 -- Read from channel 2 and write into channel 3
 sources[3]:write(tag..':'..txt)
end

x.chnl[2].src.onrecord = my_channel2

Of course, standard Lua applies.
So you can define a named function and link to this directly.

For example, you may wish to provide your customer with a complex script that does some special functions. You can define the function inside your encrypted OEM script, and allow the user to reference this in their script:

Inside the OEM script:

function our_special_function(txt, c, tag)
  -- Hidden functionality can go here...
  mem.write(c, tag..':'..txt)
end

Inside the OEM script or the normal script:

x.chnl[1].src.onrecord = our_special_function

This way the source of “our_special_function” can be hidden by encryption, yet the customer can make use of that code. Obviously they will need to be instructed about the name of the function etc.