mkv_check.tcl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. namespace eval ::ngBot::plugin::MkvCheck {
  2. variable ns [namespace current]
  3. variable np [namespace qualifiers [namespace parent]]
  4. variable listenEvents { MKV_FAIL MKV_PASS }
  5. variable scriptFile [info script]
  6. variable scriptName ${ns}::LogEvent
  7. ### START - ngBot methods
  8. #
  9. proc init {args} {
  10. variable np
  11. variable ns
  12. variable listenEvents
  13. variable ${np}::msgtypes
  14. variable ${np}::precommand
  15. variable ${np}::variables
  16. variable scriptName
  17. variable scriptFile
  18. set theme_file [file normalize "[pwd]/[file rootname $scriptFile].zpt"]
  19. if {[file isfile $theme_file]} {
  20. ${np}::loadtheme $theme_file true
  21. }
  22. # register the event handlers
  23. foreach event $listenEvents {
  24. lappend msgtypes(SECTION) $event
  25. set variables($event) "%pf %mkv_name %mkv_expected %mkv_received %mkv_formatted_expected %mkv_formatted_received"
  26. lappend precommand($event) $scriptName
  27. }
  28. putlog "mkv_check.tcl version 1.0 loaded"
  29. return
  30. }
  31. proc deinit {args} {
  32. variable np
  33. variable ${np}::precommand
  34. variable ${np}::msgtypes
  35. variable scriptName
  36. variable listenEvents
  37. # remove event handler
  38. foreach event $listenEvents {
  39. set idx [lsearch $msgtypes(SECTION) $event]
  40. set msgtypes(SECTION) [lreplace $msgtypes(SECTION) $idx $idx]
  41. if {[info exists precommand($event)] && [set pos [lsearch -exact $precommand($event) $scriptName]] != -1} {
  42. set precommand($event) [lreplace $precommand($event) $pos $pos]
  43. }
  44. }
  45. namespace delete [namespace current]
  46. }
  47. proc LogEvent {event section logData} {
  48. variable np
  49. variable ns
  50. if {[lindex $logData 2] == 0} {
  51. lappend logData "unknown"
  52. } else {
  53. lappend logData [format_bytes [lindex $logData 2]]
  54. }
  55. lappend logData [format_bytes [lindex $logData 3]]
  56. ${np}::sndall $event $section [${np}::ng_format $event $section $logData]
  57. return 0
  58. }
  59. #
  60. ### END -- ngBot methods
  61. proc format_bytes {amount} {
  62. foreach dec {0 0 1 2 2 2 2} unit {B KB MB GB TB PB EB} {
  63. if {abs($amount) >= 1024} {
  64. set amount [expr {double($amount) / 1024.0}]
  65. } else {
  66. break
  67. }
  68. }
  69. return [format "%.*f%s" $dec $amount $unit]
  70. }
  71. }