bash.tcl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #
  2. # June 26 2010
  3. # by horgh
  4. #
  5. # Requires Tcl 8.5+ and tcllib
  6. #
  7. # Must .chanset #channel +bash
  8. #
  9. # Usage: !bash [optional search terms]
  10. # If search terms are not provided, fetch random quotes.
  11. #
  12. # Keeps fetched quotes in memory until displayed, including all results per
  13. # search term
  14. #
  15. package require http
  16. package require htmlparse
  17. namespace eval bash {
  18. variable trigger !bash
  19. variable line_length 400
  20. variable max_lines 10
  21. variable useragent "Mozilla/5.0 (compatible; Y!J; for robot study; keyoshid)"
  22. variable output_cmd putserv
  23. setudef flag bash
  24. bind pub -|- $bash::trigger bash::handler
  25. variable url http://bash.org/?
  26. variable list_regexp {<p class="quote">.*?<p class="qt">.*?</p>}
  27. variable quote_regexp {<p class="quote">.*?<b>#(.*?)</b>.*?class="qa".*?</a>\((.*?)\)<a.*?<p class="qt">(.*?)</p>}
  28. if {![info exists random_quotes]} {
  29. variable random_quotes []
  30. }
  31. if {![info exists search_quotes]} {
  32. variable search_quotes []
  33. }
  34. }
  35. proc bash::quote_output {chan quote} {
  36. set number [dict get $quote number]
  37. set rating [dict get $quote rating]
  38. set quote [htmlparse::mapEscapes [dict get $quote quote]]
  39. set quote [regsub -all -- {<br />} $quote ""]
  40. $bash::output_cmd "PRIVMSG $chan :#\002${number}\002 (Rating: ${rating})"
  41. foreach line [split $quote \n] {
  42. if {[incr count] > $bash::max_lines} {
  43. $bash::output_cmd "PRIVMSG $chan :Output truncated. ${bash::url}${number}"
  44. break
  45. } else {
  46. foreach subline [bash::split_line $bash::line_length $line] {
  47. $bash::output_cmd "PRIVMSG $chan :$subline"
  48. }
  49. }
  50. }
  51. }
  52. proc bash::handler {nick uhost hand chan argv} {
  53. if {![channel get $chan bash]} { return }
  54. if {$argv == ""} {
  55. if {[catch {bash::random $chan} result]} {
  56. $bash::output_cmd "PRIVMSG $chan :Error: $result"
  57. return
  58. } else {
  59. bash::quote_output $chan $result
  60. }
  61. } else {
  62. if {[catch {bash::search $argv $chan} result]} {
  63. $bash::output_cmd "PRIVMSG $chan :Error: $result"
  64. return
  65. } else {
  66. bash::quote_output $chan $result
  67. }
  68. }
  69. }
  70. proc bash::random {} {
  71. if {![llength $bash::random_quotes]} {
  72. $bash::output_cmd "PRIVMSG $chan :Fetching new random quotes..."
  73. set bash::random_quotes [bash::fetch ${bash::url}random1]
  74. }
  75. set quote [lindex $bash::random_quotes 0]
  76. set bash::random_quotes [lreplace $bash::random_quotes 0 0]
  77. return $quote
  78. }
  79. proc bash::search {query} {
  80. if {![dict exists $bash::search_quotes $query]} {
  81. $bash::output_cmd "PRIVMSG $chan :Fetching results..."
  82. set url ${bash::url}[http::formatQuery search $query sort 0 show 25]
  83. dict set bash::search_quotes $query [bash::fetch $url]
  84. }
  85. set quotes [dict get $bash::search_quotes $query]
  86. set quote [lindex $quotes 0]
  87. set quotes [lreplace $quotes 0 0]
  88. # Remove key if no more quotes after removal of one, else set quotes to remaining
  89. if {![llength $quotes]} {
  90. dict unset bash::search_quotes $query
  91. } else {
  92. dict set bash::search_quotes $query $quotes
  93. }
  94. return $quote
  95. }
  96. proc bash::fetch {url} {
  97. putlog "Fetching new bash.org quotes: $url"
  98. http::config -useragent $bash::useragent
  99. set token [http::geturl $url -timeout 10000]
  100. set data [http::data $token]
  101. set ncode [http::ncode $token]
  102. http::cleanup $token
  103. if {$ncode != 200} {
  104. error "HTTP fetch error $ncode: $data"
  105. }
  106. return [bash::parse $data]
  107. }
  108. proc bash::parse {html} {
  109. set quotes []
  110. foreach raw_quote [regexp -all -inline -- $bash::list_regexp $html] {
  111. if {[regexp $bash::quote_regexp $raw_quote -> number rating quote]} {
  112. lappend quotes [list number $number rating $rating quote $quote]
  113. } else {
  114. error "Parse error"
  115. }
  116. }
  117. return $quotes
  118. }
  119. # by fedex
  120. proc bash::split_line {max str} {
  121. set last [expr {[string length $str] -1}]
  122. set start 0
  123. set end [expr {$max -1}]
  124. set lines []
  125. while {$start <= $last} {
  126. if {$last >= $end} {
  127. set end [string last { } $str $end]
  128. }
  129. lappend lines [string trim [string range $str $start $end]]
  130. set start $end
  131. set end [expr {$start + $max}]
  132. }
  133. return $lines
  134. }
  135. putlog "bash.tcl loaded"