netgescon-day0/scripts/ops/windows/watch-netgescon-panasonic-tapi-events.vbs

282 lines
7.2 KiB
Plaintext

Option Explicit
Const TE_TAPIOBJECT = 1
Const TE_ADDRESS = 2
Const TE_CALLNOTIFICATION = 4
Const TE_CALLSTATE = 8
Const TE_CALLMEDIA = 16
Const TE_CALLHUB = 32
Const TE_CALLINFOCHANGE = 64
Const TE_PRIVATE = 128
Const TE_REQUEST = 256
Const TE_AGENT = 512
Const TE_AGENTSESSION = 1024
Const TE_QOSEVENT = 2048
Const TE_AGENTHANDLER = 4096
Const TE_ACDGROUP = 8192
Const TE_QUEUE = 16384
Const TE_DIGITEVENT = 32768
Const TE_GENERATEEVENT = 65536
Const TAPIMEDIATYPE_AUDIO = 8
Dim g_tapi
Dim g_logFile
Dim g_watchList
Dim g_cookieMap
Sub Main()
Dim args
Set args = WScript.Arguments
If args.Count < 1 Then
WScript.Echo "Uso: cscript //nologo watch-netgescon-panasonic-tapi-events.vbs 201,205,206 [logfile]"
WScript.Quit 1
End If
g_watchList = "," & Trim(args.Item(0)) & ","
If args.Count >= 2 Then
g_logFile = args.Item(1)
Else
g_logFile = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") & "\\netgescon-tapi-events.log"
End If
Set g_cookieMap = CreateObject("Scripting.Dictionary")
LogLine "=== AVVIO WATCHER TAPI ==="
LogLine "Watch list: " & g_watchList
LogLine "Log file: " & g_logFile
Set g_tapi = WScript.CreateObject("TAPI.TAPI", "Tapi_")
g_tapi.Initialize
g_tapi.EventFilter = TE_CALLNOTIFICATION + TE_CALLSTATE + TE_CALLINFOCHANGE + TE_CALLMEDIA + TE_PRIVATE
RegisterAddresses
LogLine "Watcher pronto. Ora fai una chiamata reale su 201, 205 o 206."
LogLine "Premi Ctrl+C per fermare lo script."
Do
WScript.Sleep 1000
Loop
End Sub
Sub RegisterAddresses()
On Error Resume Next
Dim address
Dim name
Dim dial
Dim cookie
For Each address In g_tapi.Addresses
name = SafeProp(address, "AddressName")
dial = SafeProp(address, "DialableAddress")
If ShouldWatch(name, dial) Then
Err.Clear
cookie = g_tapi.RegisterCallNotifications(address, True, False, TAPIMEDIATYPE_AUDIO, 0)
If Err.Number = 0 Then
g_cookieMap(CStr(cookie)) = name & "|" & dial
LogLine "REGISTER OK cookie=" & cookie & " address=" & name & " dial=" & dial
Else
LogLine "REGISTER KO address=" & name & " dial=" & dial & " err=" & Err.Number & " desc=" & Err.Description
Err.Clear
End If
End If
Next
End Sub
Function ShouldWatch(name, dial)
Dim token
token = ""
If Trim(CStr(dial)) <> "" Then
token = Trim(CStr(dial))
Else
token = Trim(CStr(name))
End If
ShouldWatch = (InStr(1, g_watchList, "," & token & ",", vbTextCompare) > 0)
If Not ShouldWatch Then
If Left(UCase(CStr(name)), 3) = "EXT" Then
token = Right(CStr(name), 3)
ShouldWatch = (InStr(1, g_watchList, "," & token & ",", vbTextCompare) > 0)
End If
End If
If Not ShouldWatch Then
If Left(UCase(CStr(name)), 3) = "GRP" Then
token = Right(CStr(name), 3)
ShouldWatch = (InStr(1, g_watchList, "," & token & ",", vbTextCompare) > 0)
End If
End If
End Function
Sub Tapi_Event(ByVal eventType, ByVal pEvent)
On Error Resume Next
Dim objectType
Dim addressName
Dim dialableAddress
Dim stateValue
Dim causeValue
Dim privilegeValue
Dim infoText
objectType = TypeName(pEvent)
addressName = ResolveAddressName(pEvent)
dialableAddress = ResolveDialableAddress(pEvent)
stateValue = SafeProp(pEvent, "State")
causeValue = SafeProp(pEvent, "Cause")
privilegeValue = SafeProp(pEvent, "Privilege")
infoText = ""
If SafeProp(pEvent, "Call") <> "" Then
infoText = ResolveCallInfo(pEvent)
End If
LogLine "EVENT type=" & eventType & " object=" & objectType & " address=" & addressName & " dial=" & dialableAddress & " state=" & stateValue & " cause=" & causeValue & " privilege=" & privilegeValue & " info=" & infoText
End Sub
Function ResolveAddressName(pEvent)
On Error Resume Next
Dim value
value = ""
value = SafeNestedProp(pEvent, "Call", "Address", "AddressName")
If value <> "" Then
ResolveAddressName = value
Exit Function
End If
value = SafeNestedProp(pEvent, "Address", "AddressName", "")
ResolveAddressName = value
End Function
Function ResolveDialableAddress(pEvent)
On Error Resume Next
Dim value
value = ""
value = SafeNestedProp(pEvent, "Call", "Address", "DialableAddress")
If value <> "" Then
ResolveDialableAddress = value
Exit Function
End If
value = SafeNestedProp(pEvent, "Address", "DialableAddress", "")
ResolveDialableAddress = value
End Function
Function ResolveCallInfo(pEvent)
On Error Resume Next
Dim callObj
Dim buffer
buffer = ""
Set callObj = Nothing
Err.Clear
Set callObj = pEvent.Call
If Err.Number <> 0 Then
Err.Clear
ResolveCallInfo = ""
Exit Function
End If
buffer = buffer & "caller=" & SafeCallInfoString(callObj, 14) & ";"
buffer = buffer & "called=" & SafeCallInfoString(callObj, 15) & ";"
buffer = buffer & "connected=" & SafeCallInfoString(callObj, 16) & ";"
ResolveCallInfo = buffer
End Function
Function SafeCallInfoString(callObj, infoId)
On Error Resume Next
Dim value
value = ""
value = callObj.CallInfoString(infoId)
If Err.Number <> 0 Then
Err.Clear
SafeCallInfoString = ""
Else
SafeCallInfoString = Trim(CStr(value))
End If
End Function
Function SafeProp(obj, propName)
On Error Resume Next
Dim value
value = ""
value = CallByName(obj, propName, VbGet)
If Err.Number <> 0 Then
Err.Clear
SafeProp = ""
Else
SafeProp = Trim(CStr(value))
End If
End Function
Function SafeNestedProp(obj, prop1, prop2, prop3)
On Error Resume Next
Dim level1
Dim level2
Dim value
SafeNestedProp = ""
Set level1 = Nothing
Set level2 = Nothing
Err.Clear
Set level1 = CallByName(obj, prop1, VbGet)
If Err.Number <> 0 Then
Err.Clear
Exit Function
End If
If prop3 = "" Then
value = CallByName(level1, prop2, VbGet)
If Err.Number = 0 Then
SafeNestedProp = Trim(CStr(value))
Else
Err.Clear
End If
Exit Function
End If
Set level2 = CallByName(level1, prop2, VbGet)
If Err.Number <> 0 Then
Err.Clear
Exit Function
End If
value = CallByName(level2, prop3, VbGet)
If Err.Number = 0 Then
SafeNestedProp = Trim(CStr(value))
Else
Err.Clear
End If
End Function
Sub LogLine(text)
Dim fso
Dim stream
Dim line
Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso.OpenTextFile(g_logFile, 8, True)
line = NowIso() & " | " & text
stream.WriteLine line
stream.Close
WScript.Echo line
End Sub
Function NowIso()
Dim dt
dt = Now
NowIso = Year(dt) & "-" & Right("0" & Month(dt), 2) & "-" & Right("0" & Day(dt), 2) & " " & Right("0" & Hour(dt), 2) & ":" & Right("0" & Minute(dt), 2) & ":" & Right("0" & Second(dt), 2)
End Function
Main