Lua macro-regexp

Skip to end of metadata
Go to start of metadata

Вот еще решение, с использованием Lua, на котором написан макрос, реализующий поиск по regexp'ам, но не перловым (PCRE), а Lua'вским (LPEG).

Вот диалплан:

exten => _X.,1,Macro(regexp,^%d%d%d$)
exten => _X.,2,Verbose(We passwd regexp, let's do smth)

Можно каскадировать макросы друг за другом.

Вот кусок из extensions.lua:

-- Verbose levels
DEBUG = 10
VERBOSE = 3


extensions = {}
extensions['macro-regexp'] = {}

extensions['macro-regexp']['s'] = function()
   local pattern = channel.arg1:get()
   local called = channel["CALLERID(dnid)"]:get()
   if not called:match(pattern) then
      app.verbose(DEBUG, string.format("False, number %q does not match pattern %q, hanging up", called, pattern))
      app.hangup()
   else
      app.verbose(DEBUG, string.format("True, number %q matches pattern %q, hanging up", called, pattern))
   end
end
Ну и логи:

  == Using SIP RTP CoS mark 5
    -- Executing [111@users:1] Macro("SIP/101-00000042", "regexp,^%d%d%d$") in new stack
    -- Executing [s@macro-regexp:1] verbose("SIP/101-00000042", "10,True, number "111" matches pattern "^%d%d%d$", hanging up")
       > True, number "111" matches pattern "^%d%d%d$", hanging up
    -- Executing [111@users:2] Verbose("SIP/101-00000042", "We passed regexp, let's do smth") in new stack
[Jul 16 22:49:41] WARNING[5845]: app_verbose.c:101 verbose_exec: 'We passed regexp' is not a verboser number
 let's do smth
    -- Auto fallthrough, channel 'SIP/101-00000042' status is 'UNKNOWN'
  == Using SIP RTP CoS mark 5
    -- Executing [1111@users:1] Macro("SIP/101-00000043", "regexp,^%d%d%d$") in new stack
    -- Executing [s@macro-regexp:1] verbose("SIP/101-00000043", "10,False, number "1111" does not match pattern "^%d%d%d$", hanging up")
       > False, number "1111" does not match pattern "^%d%d%d$", hanging up
    -- Executing [s@macro-regexp:1] hangup("SIP/101-00000043", "")
  == Spawn extension (macro-regexp, s, 1) exited non-zero on 'SIP/101-00000043' in macro 'regexp'
  == Spawn extension (users, 1111, 1) exited non-zero on 'SIP/101-00000043'

*CLI>
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.