qstat.tcl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. ###################################################################
  2. #
  3. # qstat.tcl
  4. #
  5. # CONFIG:
  6. #
  7. # Here you can change some global variables. You can change the
  8. # name and/or path of the file you want to store the servers in
  9. # and its backup file.
  10. set qstat "/path/to/qstat" ;#path to your qstat binary
  11. set optionsall "-u -default q2s" ;#qstat options for all servers
  12. set optionssingle "-P -u -q2s" ;#qstat options for single server
  13. set file "servers.lst" ;#file to store servers in
  14. set filebak "servers.lst.bak" ;#server backup file
  15. # Here you can change the command names to something you like more.
  16. set addcommand "!addserver" ;#add server to the list
  17. set delcommand "!delserver" ;#remove server from list
  18. set showcommand "!serverlist" ;#show servers in list (no qstat)
  19. set refreshcommand "!refresh" ;#show server stats (qstat)
  20. # END OF CONFIG
  21. ####################################################################
  22. global file
  23. global filebak
  24. global qstat
  25. global optionsall
  26. global optionssingle
  27. set playerlist1 ""
  28. global playerlist1
  29. # this procedure shows the saved matches:
  30. proc show_matches {nick host hand chan arg} {
  31. global file
  32. if {[isop $nick $chan]} {
  33. if {[file exists $file]} {
  34. if {[file size $file] > 0} {
  35. if ![catch {open $file r} input] {
  36. while {[gets $input line] >= 0} {
  37. lappend matches $line
  38. }
  39. close $input
  40. puthelp "PRIVMSG $nick :*** Server List ***:"
  41. for { set i 0 } { $i < [llength $matches] } { incr i } {
  42. puthelp "PRIVMSG $nick :([expr $i +1]) [lindex $matches $i]"
  43. }
  44. } else { puthelp "NOTICE $nick :Error opening file: $input" }
  45. } else { puthelp "PRIVMSG $nick :No servers have been added yet..."}
  46. } else { puthelp "PRIVMSG $nick :No servers have been added yet..."}
  47. }
  48. }
  49. # this procedure deletes saved matches:
  50. proc del_match {nick host hand chan arg} {
  51. global file
  52. global filebak
  53. if {[isop $nick $chan]} {
  54. if {[file exists $file]} {
  55. if {[file size $file] > 0} {
  56. if ![catch {open $file r} input] {
  57. while {[gets $input line] >= 0} {
  58. lappend matches $line
  59. }
  60. if {$arg <= [llength $matches] && $arg != 0} {
  61. close $input
  62. file copy -force $file $filebak
  63. if ![catch {open $file w} output] {
  64. for { set i 0 } { $i < [llength $matches] } { incr i } {
  65. if {[expr $i +1] != $arg} { puts $output "[lindex $matches $i]" }
  66. }
  67. close $output
  68. puthelp "NOTICE $nick :Attempted to delete server number $arg."
  69. putlog "match.tcl: $nick@$chan attempted to delete server number $arg."
  70. } else { puthelp "NOTICE $nick :Error opening file: $output"
  71. putlog "match.tcl: ERROR! Error opening file: $output"}
  72. } else { puthelp "NOTICE $nick :Can't delete servers that don't exist..." }
  73. } else { puthelp "NOTICE $nick :Error opening file: $input"
  74. putlog "match.tcl: ERROR! Error opening file: $input"}
  75. } else { puthelp "NOTICE $nick :Can't delete servers that don't exist..." }
  76. } else { puthelp "NOTICE $nick :Can't delete servers that don't exist..." }
  77. }
  78. }
  79. # this procedure adds matches to the list:
  80. proc add_match {nick host hand chan arg} {
  81. global file
  82. if {[isop $nick $chan]} {
  83. if { $arg != "" } {
  84. set match [regexp {[\d]+.[\d]+.[\d]+.[\d]+:[\d]+} $arg matchl]
  85. if { $match == 1 } {
  86. if ![catch {open $file a} output] {
  87. puts $output "$arg"
  88. close $output
  89. puthelp "NOTICE $nick :Attempted to add server."
  90. putlog "match.tcl: $nick@$chan attempted to add a server to the list"
  91. } else { puthelp "NOTICE $nick :Error opening file: $output"
  92. putlog "match.tcl: ERROR! Error opening file: $output"}
  93. }
  94. } else { puthelp "NOTICE $nick :Can't add empty entry" }
  95. }
  96. }
  97. proc refresh_servers {nick host hand chan arg} {
  98. global qstat
  99. global file
  100. global playerlist1
  101. global optionsall
  102. global optionssingle
  103. if { $arg == "" } {
  104. if {[file exists $file]} {
  105. if {[file size $file] > 0} {
  106. if ![catch {open $file r} input] {
  107. while {[gets $input line] >= 0} {
  108. lappend matches $line
  109. }
  110. close $input
  111. }
  112. if ![catch {open "|$qstat $optionsall -f $file" r} input] {
  113. while {[gets $input line] >= 0} {
  114. formatline $line $chan $matches
  115. }
  116. close $input
  117. } else { puthelp "PRIVMSG $nick :Error refreshing servers: $input " }
  118. } else { puthelp "PRIVMSG $nick :No servers have been added yet..." }
  119. } else { puthelp "PRIVMSG $nick :No servers have been added yet..." }
  120. } else {
  121. set playerlist1 ""
  122. if {[file exists $file]} {
  123. if {[file size $file] > 0} {
  124. if ![catch {open $file r} input] {
  125. while {[gets $input line] >= 0} {
  126. lappend matches $line
  127. }
  128. close $input
  129. }
  130. if ![catch {open "|$qstat $optionssingle [lindex $matches [expr $arg -1]]" r} input] {
  131. while {[gets $input line] >= 0} {
  132. formatone $line $chan $arg
  133. }
  134. if { $playerlist1 != "" } { puthelp "PRIVMSG $chan :$playerlist1" }
  135. close $input
  136. } else { puthelp "NOTICE $nick :Error opening file: $input" }
  137. } else { puthelp "PRIVMSG $chan :No servers have been added yet..."}
  138. } else { puthelp "PRIVMSG $chan :No servers have been added yet..."}
  139. }
  140. }
  141. proc formatline { line chan matches } {
  142. set match [regexp {([\d]+.[\d]+.[\d]+.[\d]+:[\d]+)\
  143. [\s]*([\d]+/[\d]+)[\s]*\
  144. ([\w]+)[\s]*([\d]+)[\s]*/[\s]*[\d]+\
  145. [\s]*([\w]+)[\s]*(.+)} $line matchln address players map ping type name]
  146. #puthelp "PRIVMSG $chan :$match"
  147. if { $match == 1 } {
  148. set number [expr [lsearch $matches $address] +1]
  149. puthelp "PRIVMSG $chan :($number) \0030,1\00307[format "%-21s " $address] \00315[format "%-45s " $name] \0034[format "%-7s " ($players)] \00315[format "%-10s " ($map)]"}
  150. }
  151. proc formatone { line chan arg } {
  152. global playerlist1
  153. set match [regexp {([\d]+.[\d]+.[\d]+.[\d]+:[\d]+)\
  154. [\s]*([\d]+/[\d]+)[\s]*\
  155. ([\w]+)[\s]*([\d]+)[\s]*/[\s]*[\d]+\
  156. [\s]*([\w]+)[\s]*(.+)} $line matchln address players map ping type name]
  157. #puthelp "PRIVMSG $chan :$match"
  158. if { $match == 1 } {
  159. puthelp "PRIVMSG $chan :($arg) \0030,1\00307[format "%-21s " $address] \00315[format "%-45s " $name] \0034[format "%-7s " ($players)] \00315[format "%-10s " ($map)]"
  160. } else {
  161. set match2 [regexp {[\s]*(-*[\d]+)[\s]*frags[\s]*([\d]+)ms[\s]*(.+)} $line matchln2 playerfrags playerping playername]
  162. if { $match2 == 1 } {
  163. if { $playerlist1 != "" } {
  164. set playerlist1 "${playerlist1}, \00307$playername \00315(${playerfrags} frags, \00304${playerping}ms)"
  165. } else { set playerlist1 "\0030,1\00307$playername \00315(${playerfrags} frags, \00304${playerping}ms)"
  166. }
  167. }
  168. }
  169. }
  170. # binds to call the procedures:
  171. bind pub - $showcommand show_matches
  172. bind pub - $addcommand add_match
  173. bind pub - $delcommand del_match
  174. bind pub - $refreshcommand refresh_servers