slang.tcl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #
  2. # slang.tcl - June 24 2010
  3. # by horgh
  4. #
  5. # Requires Tcl 8.5+ and tcllib
  6. #
  7. # Made with heavy inspiration from perpleXa's urbandict script!
  8. #
  9. # Must .chanset #channel +ud
  10. #
  11. # Uses is.gd to shorten long definition URL if isgd.tcl package present
  12. #
  13. package require htmlparse
  14. package require http
  15. package require tls
  16. ::http::register https 443 [list ::tls::socket -ssl2 0 -ssl3 0 -tls1 1]
  17. namespace eval ::ud {
  18. # set this to !ud or whatever you want
  19. variable trigger "slang"
  20. # maximum lines to output
  21. variable max_lines 1
  22. # approximate characters per line
  23. variable line_length 400
  24. # show truncated message / url if more than one line
  25. variable show_truncate 1
  26. # toggle whether we store raw response data.
  27. # this will store the response from an http request to urbandictionary.com
  28. # in files for debugging.
  29. # NOTE: enabling this will cause a file to be created for every request
  30. # the script makes, so these can pile up quickly!
  31. variable store_responses 0
  32. # the directory to store responses if store_responses is on.
  33. # this is under your eggdrop directory.
  34. # files under this directory will be named with unix timestamps
  35. # (microseconds).
  36. variable store_responses_dir slang_responses
  37. variable output_cmd "putserv"
  38. variable client "Mozilla/5.0 (compatible; Y!J; for robot study; keyoshid)"
  39. variable url http://www.urbandictionary.com/define.php
  40. variable url_random http://www.urbandictionary.com/random.php
  41. # regex to find the word
  42. variable word_regex {<a class="word" href=.*?>(.*?)</a>}
  43. variable list_regex {<div class='def-panel' data-defid='[0-9]+?'>.*?<div class='def-footer'>}
  44. variable def_regex {<div class='def-panel' data-defid='([0-9]+?)'>.*?<div class='meaning'>(.*?)</div>}
  45. setudef flag ud
  46. bind pub -|- $::ud::trigger ::ud::handler
  47. # 0 if isgd package is present
  48. variable isgd_disabled [catch {package require isgd}]
  49. }
  50. # write a console log message.
  51. proc ::ud::log {msg} {
  52. if {[string length $msg] == 0} {
  53. return
  54. }
  55. putlog "slang.tcl $msg"
  56. }
  57. proc ::ud::handler {nick uhost hand chan argv} {
  58. if {![channel get $chan ud]} { return }
  59. set argv [string trim $argv]
  60. set argv [split $argv]
  61. if {[string is digit [lindex $argv 0]]} {
  62. set number [lindex $argv 0]
  63. set query [join [lrange $argv 1 end]]
  64. } else {
  65. set query [join $argv]
  66. set number 1
  67. }
  68. set query [string trim $query]
  69. if {[llength $argv] == 1 && [string is digit [lindex $argv 0]]} {
  70. $::ud::output_cmd "PRIVMSG $chan :Usage: $::ud::trigger \[#\] <query> (or just $::ud::trigger for random definition)"
  71. return
  72. }
  73. if {$query == ""} {
  74. ::ud::log "Performing random query..."
  75. if {[catch {::ud::get_random} result]} {
  76. $::ud::output_cmd "PRIVMSG $chan :Error: $result"
  77. return
  78. }
  79. ::ud::output $chan $result
  80. } else {
  81. ::ud::log "Fetching definition $number of $query..."
  82. if {[catch {::ud::get_def $query $number} result]} {
  83. $::ud::output_cmd "PRIVMSG $chan :Error: $result"
  84. return
  85. }
  86. ::ud::output $chan $result
  87. }
  88. }
  89. proc ::ud::output {chan def_dict} {
  90. set output 0
  91. foreach line [::ud::split_line $::ud::line_length [dict get $def_dict definition]] {
  92. if {[incr output] > $::ud::max_lines} {
  93. if {$::ud::show_truncate} {
  94. $::ud::output_cmd "PRIVMSG $chan :Output truncated. [::ud::def_url $def_dict]"
  95. }
  96. break
  97. }
  98. $::ud::output_cmd "PRIVMSG $chan :$line"
  99. }
  100. }
  101. proc ::ud::get_random {} {
  102. set result [::ud::http_fetch $::ud::url_random ""]
  103. set word [dict get $result word]
  104. set defs_html [dict get $result definitions]
  105. if {[llength $defs_html] < 1} {
  106. error "Failure finding random definition."
  107. }
  108. return [::ud::parse $word [lindex $defs_html 0]]
  109. }
  110. proc ::ud::get_def {query number} {
  111. set page [expr {int(ceil($number / 7.0))}]
  112. set number [expr {$number - (($page - 1) * 7)}]
  113. set http_query [http::formatQuery term $query page $page]
  114. set result [::ud::http_fetch $::ud::url $http_query]
  115. set word [dict get $result word]
  116. set defs_html [dict get $result definitions]
  117. if {[llength $defs_html] < $number} {
  118. error "[llength $defs_html] definitions found."
  119. }
  120. return [::ud::parse $word [lindex $defs_html [expr {$number - 1}]]]
  121. }
  122. # store an http request response (if enabled).
  123. proc ::ud::store_response {data} {
  124. if {!$::ud::store_responses} {
  125. return
  126. }
  127. # ensure the directory to store the responses exists.
  128. if {![file isdirectory $::ud::store_responses_dir]} {
  129. # mkdir raises an error if it fails.
  130. file mkdir $::ud::store_responses_dir
  131. }
  132. # make the filename that we will store to.
  133. set base [clock microseconds]
  134. set path [file join $::ud::store_responses_dir $base]
  135. # write out the response
  136. set f [open $path w]
  137. puts -nonewline $f $data
  138. close $f
  139. ::ud::log "stored response to $path"
  140. }
  141. proc ::ud::http_fetch {url http_query} {
  142. http::config -useragent $::ud::client
  143. set token [http::geturl $url -timeout 20000 -query $http_query]
  144. set data [http::data $token]
  145. set ncode [http::ncode $token]
  146. set meta [http::meta $token]
  147. http::cleanup $token
  148. # Follow redirects
  149. if {[regexp -- {30[01237]} $ncode]} {
  150. set new_url [dict get $meta Location]
  151. return [::ud::http_fetch $new_url $http_query]
  152. }
  153. if {$ncode != 200} {
  154. error "HTTP fetch error. Code: $ncode"
  155. }
  156. # we may be storing responses for debugging.
  157. if {[catch {::ud::store_response $data} result]} {
  158. putlog "Problem storing response: $result"
  159. }
  160. return [::ud::parse_word_and_definitions $data]
  161. }
  162. # parse a response from a file.
  163. # this is primarily for debugging purposes. we can pass this function
  164. # a stored response file to try to parse it.
  165. proc ::ud::parse_response_file {path} {
  166. set f [open $path]
  167. set data [read -nonewline $f]
  168. close $f
  169. return [::ud::parse_word_and_definitions $data]
  170. }
  171. # first pass parsing - we pull out the word and the definitions from
  172. # the page.
  173. # we return a dictionary with keys 'word' and 'definitions' on success.
  174. # on failure, we raise an error.
  175. proc ::ud::parse_word_and_definitions {data} {
  176. # pull out the word.
  177. if {![regexp -- $::ud::word_regex $data -> word]} {
  178. error "Word not found. No definitions or parsing problem!"
  179. }
  180. set word [string trim $word]
  181. set definitions [regexp -all -inline -- $::ud::list_regex $data]
  182. set definition_count [llength $definitions]
  183. if {$definition_count == 0} {
  184. error "No definitions found"
  185. }
  186. return [list word $word definitions $definitions]
  187. }
  188. proc ::ud::parse {word raw_definition} {
  189. if {![regexp $::ud::def_regex $raw_definition -> number definition]} {
  190. error "Could not parse definition's HTML"
  191. }
  192. set definition [htmlparse::mapEscapes $definition]
  193. set definition [regsub -all -- {<.*?>} $definition ""]
  194. set definition [regsub -all -- {\n+} $definition " "]
  195. set definition [string tolower $definition]
  196. set definition [string trim $definition]
  197. return [list number $number word $word definition "$word is $definition"]
  198. }
  199. proc ::ud::def_url {def_dict} {
  200. set word [dict get $def_dict word]
  201. set number [dict get $def_dict number]
  202. set raw_url ${::ud::url}?[http::formatQuery term $word defid $number]
  203. if {$::ud::isgd_disabled} {
  204. return $raw_url
  205. } else {
  206. if {[catch {isgd::shorten $raw_url} shortened]} {
  207. return "$raw_url (is.gd error)"
  208. } else {
  209. return $shortened
  210. }
  211. }
  212. }
  213. # by fedex
  214. proc ::ud::split_line {max str} {
  215. set last [expr {[string length $str] -1}]
  216. set start 0
  217. set end [expr {$max -1}]
  218. set lines []
  219. while {$start <= $last} {
  220. if {$last >= $end} {
  221. set end [string last { } $str $end]
  222. }
  223. lappend lines [string trim [string range $str $start $end]]
  224. set start $end
  225. set end [expr {$start + $max}]
  226. }
  227. return $lines
  228. }
  229. putlog "slang.tcl loaded"