bash.tcl 3.9 KB

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