-
Notifications
You must be signed in to change notification settings - Fork 4
/
ymsgfilter.tcl
executable file
·75 lines (58 loc) · 1.47 KB
/
ymsgfilter.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env tclsh
#
# Yate Message Filter/Dumper
# - dump all messages meeting certain criteria -
#
# Author: Ben Fuhrmannek <[email protected]>
# Date: 2013-11-30
#
# Copyright (c) 2013-2014, Ben Fuhrmannek
# All rights reserved.
package require Tcl 8.5
## cmd opts
package require cmdline
set options {
{H.arg "localhost" "connect to host"}
{P.arg "5039" "connect to port"}
{m.arg "" "message filter, e.g. user.register"}
{kv.arg "" "key/value match filter, e.g. username=1234"}
}
set usage "$::argv0 <options>..."
if {[catch {
array set cfg [::cmdline::getoptions argv $options $usage]
} e]} {
puts stderr $e
exit 1
}
## YGI setup
set auto_path [linsert $auto_path 0 [file dirname [info script]]]
package require ygi
proc dump_msg {_id processed name retvalue kv} {
puts "---------> new message: $name | $processed | $retvalue"
foreach {k v} $kv {
puts [format "| %18s %s" $k $v]
}
}
## watch handler function
proc watch_handler {args} {
if {$::cfg(kv) eq ""} {
dump_msg {*}$args
return
}
lassign $args _id processed name retvalue kv
lassign [split $::cfg(kv) "="] key value
if {![dict exists $kv $key]} {return}
if {[dict get $kv $key] ne $value} {return}
dump_msg {*}$args
}
## start.
::ygi::start_tcp $cfg(H) $cfg(P)
# set ::ygi::debug true
## first command to socket must be connect
::ygi::connect global
## log or console message
::ygi::log "start"
## install watch handler(s)
::ygi::watch $cfg(m) watch_handler
## never stop
::ygi::loop_forever