resubmit.lua

Verified
Added by iNTERFACEWARE

Resubmit a logged message to an Iguana channel using the unique reference number (refmsgid).

Source Code
local message = {}
 
local function Split(s, d)        
   local t = {}        
   local i = 0        
   local f        
   local match = '(.-)' .. d .. '()'        
   if string.find(s, d) == nil then        
      return {s}        
   end        
   for sub, j in string.gfind(s, match) do        
      i = i + 1        
      t[i] = sub        
      f = j        
   end        
   if i~= 0 then        
      t[i+1]=string.sub(s,f)        
   end        
   return t        
end
 
function message.resubmit(T)
   local Url = T.url
   local User = T.user
   local Password = T.password
   local RefId = T.refid
   local Channel = T.channel
   local Message = T.message
   local Live = T.live or false
   if not Url or not User or not Password or
      not RefId or not Channel or not Message then
      error('Parameters url, user, password, refid, channel and message are required.',2)
   end
 
   -- We login first
   local Success, Result = pcall(net.http.post, {url=Url..'resubmit_message', 
         parameters={Message=Message, 
            RefMsgId=RefId, 
            RequestId=1, 
            Destination=Channel},
         auth={username=User, password=Password},live=true})
   if not Success then
      error(Report, 2)
   end
   if not Live then 
      return 'Not running in editor - pass in live=true'
   end
   return Result
end
 
function message.resubmit_with_cookie(T)
   local Url = T.url
   local User = T.user
   local Password = T.password
   local RefId = T.refid
   local Channel = T.channel
   local Message = T.message
   local Live = T.live or false
   if not Url or not User or not Password or
      not RefId or not Channel or not Message then
      error('Parameters url, user, password, refid, channel and message are required.',2)
   end
 
   -- We login first
   local Success,Result, _, Headers = pcall(net.http.get,{url=Url..'login.html', 
      parameters={username=User, password=Password },live=true})
   if not Success then
      error(Result, 2)
   end
   -- That gives us our session cookie which we use for our
   -- login credentials.  The login interface was not quite
   -- meant for an API - but the problem is solvable.
   local SessionId = Headers["Set-Cookie"]
   if not SessionId then
      error('Username and/or password wrong',2)
   end
   SessionId = Split(SessionId,' ')[1]
   trace(SessionId)
   
   local Success, Result = pcall(net.http.post, {url=Url..'resubmit_message', 
         parameters={Message=Message, 
            RefMsgId=RefId, 
            RequestId=1, 
            Destination=Channel},
         headers={Cookie=SessionId}, live=Live})
   if not Success then
      error(Report, 2)
   end
   if not Live then 
      return 'Not running in editor - pass in live=true'
   end
   return Result
end
 
local resubmitStatusHelp = {
       Title="resubmit.resubmit",
       Usage=[[resubmit.resubmit{url=<value>, user=<value>, password=<value>,
   refid=<value>, message=<value>, channel=<value> [, live=<value>]}]],
       Desc=[[This function utilizes an existing Iguana web service API call to resubmit a message to a specific channel.
   Code using this function should expect that this web service call may periodically fail since it's going over the network.]],
       Returns={
          {Desc="The response data from the HTTP request (string)."},
       },
       ParameterTable=true,
       Parameters={
           {url={Desc='The root URL for the target Iguana server. i.e. http://localhost:6543/'}},
           {user={Desc='User name to login with.'}},
           {password={Desc='Password to login with.'}},
           {refid={Desc='Unique log reference ID of the message you are resubmitting.'}},
           {message={Desc='Value of the message to be resubmitted.'}},
           {channel={Desc='Unique name of the channel you are resubmitting to.'}},
           {live={Desc='Resubmit the message while in the editor.', Opt=true}},
       },
       Examples={
           [[local Result = resubmit.resubmit{url='http://localhost:6543/', user='admin', password='password'}
       refid='20130310-14420', message='Some message', channel='Some channel'}]]
       },
       SeeAlso={
           {
               Title="Resubmit module",
               Link="http://wiki.interfaceware.com/1362.html"
           }
       }
   }
 
help.set{input_function=message.resubmit, help_data=resubmitStatusHelp}
return message
Description
Resubmit a logged message to an Iguana channel using the unique reference number (refmsgid).
Attachments
Usage Details

When writing Translator scripts, a useful operation to have on hand is the ability to resubmit a logged message to an Iguana channel. This module uses the same web service hook that Iguana’s own user interface uses to resubmit a message.

To use the resubmit module effectively, you’ll need to know the unique reference number for the message (or refmsgid). You can get this information by querying the logs.  To learn more, see Obtaining a Direct Link to a Logged Message.

How to use resubmit.lua:

  • Add it to your shared modules in any Translator project
  • Add local resubmit = require 'resubmit' at the top of your script
  • Use the resubmit.resubmit{} function to resubmit a message

Here is an example using it in main():

local resubmit = require 'resubmit'

function main(Data)
   -- Caveat emptor - the refid needs to be for a valid message.  I have an admin
   -- user with user = 'kermit' and password = 'kermit' (replace with your own admin user details)
   -- replace SomeCondition with a condition (boolean) to specify when a message is to be repeated
   
   if SomeCondition then
      resubmit.resubmit{user='kermit', password='kermit', url='http://localhost:6543/', 
         refid='20130310-14420', message='MSH exploded', channel='My Channel'}
   end
end

———- and here is a test module ————-

local message = {}

local function Split(s, d)        
   local t = {}        
   local i = 0        
   local f        
   local match = '(.-)' .. d .. '()'        
   if string.find(s, d) == nil then        
      return {s}        
   end        
   for sub, j in string.gfind(s, match) do        
      i = i + 1        
      t[i] = sub        
      f = j        
   end        
   if i~= 0 then        
      t[i+1]=string.sub(s,f)        
   end        
   return t        
end

function message.resubmit(T)
   local Url = T.url
   local User = T.user
   local Password = T.password
   local RefId = T.refid
   local Channel = T.channel
   local Message = T.message
   local Live = T.live or false
   if not Url or not User or not Password or
      not RefId or not Channel or not Message then
      error('Parameters url, user, password, refid, channel and message are required.',2)
   end
   
   -- We login first
   local Success, Result = pcall(net.http.post, {url=Url..'resubmit_message', 
         parameters={Message=Message, 
            RefMsgId=RefId, 
            RequestId=1, 
            Destination=Channel},
         auth={username=User, password=Password},live=true})
   if not Success then
      error(Report, 2)
   end
   if not Live then 
      return 'Not running in editor - pass in live=true'
   end
   return Result
end

function message.resubmit_with_cookie(T)
   local Url = T.url
   local User = T.user
   local Password = T.password
   local RefId = T.refid
   local Channel = T.channel
   local Message = T.message
   local Live = T.live or false
   if not Url or not User or not Password or
      not RefId or not Channel or not Message then
      error('Parameters url, user, password, refid, channel and message are required.',2)
   end
   
   -- We login first
   local Success,Result, _, Headers = pcall(net.http.get,{url=Url..'login.html', 
         parameters={username=User, password=Password },live=true})
   if not Success then
      error(Result, 2)
   end
   -- That gives us our session cookie which we use for our
   -- login credentials.  The login interface was not quite
   -- meant for an API - but the problem is solvable.
   local SessionId = Headers["Set-Cookie"]
   if not SessionId then
      error('Username and/or password wrong',2)
   end
   SessionId = Split(SessionId,' ')[1]
   trace(SessionId)
   
   local Success, Result = pcall(net.http.post, {url=Url..'resubmit_message', 
         parameters={Message=Message, 
            RefMsgId=RefId, 
            RequestId=1, 
            Destination=Channel},
         headers={Cookie=SessionId}, live=Live})
   if not Success then
      error(Report, 2)
   end
   if not Live then 
      return 'Not running in editor - pass in live=true'
   end
   return Result
end

local resubmitStatusHelp = {
   Title="resubmit.resubmit",
   Usage=[[resubmit.resubmit{url=<value>, user=<value>, password=<value>,
   refid=<value>, message=<value>, channel=<value> [, live=<value>]}]],
   Desc=[[This function utilizes an existing Iguana web service API call to resubmit a message to a specific channel.
   Code using this function should expect that this web service call may periodically fail since it's going over the network.]],
   Returns={
      {Desc="The response data from the HTTP request (string)."},
   },
   ParameterTable=true,
   Parameters={
      {url={Desc='The root URL for the target Iguana server. i.e. http://localhost:6543/'}},
      {user={Desc='User name to login with.'}},
      {password={Desc='Password to login with.'}},
      {refid={Desc='Unique log reference ID of the message you are resubmitting.'}},
      {message={Desc='Value of the message to be resubmitted.'}},
      {channel={Desc='Unique name of the channel you are resubmitting to.'}},
      {live={Desc='Resubmit the message while in the editor.', Opt=true}},
   },
   Examples={
      [[local Result = resubmit.resubmit{url='http://localhost:6543/', user='admin', password='password'}
      refid='20130310-14420', message='Some message', channel='Some channel'}]]
   },
   SeeAlso={
      {
         Title="Resubmit module",
         Link="http://wiki.interfaceware.com/1362.html"
      }
   }
}

help.set{input_function=message.resubmit, help_data=resubmitStatusHelp}
return message