Advertisement
Ford

Urban Dictionary for eggdrop

Jun 16th, 2012
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 8.58 KB | None | 0 0
  1. ##############################################################################################
  2. ##  ##  urbandictionary.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help ##  ##
  3. ##############################################################################################
  4. ## To use this script you must set channel flag +ud (ie .chanset #chan +ud)                 ##
  5. ##############################################################################################
  6. ##############################################################################################
  7. ##  ##                             Start Setup.                                         ##  ##
  8. ##############################################################################################
  9. namespace eval urbandictionary {
  10. ## Edit logo to change the logo displayed at the start of the line                      ##  ##
  11.   variable logo "\017\00308,07\002UD\017"
  12. ## Edit textf to change the color/state of the text shown                               ##  ##
  13.   variable textf "\017\00304"
  14. ## Edit linkf to change the color/state of the links                                    ##  ##
  15.   variable linkf "\017\037\00304"
  16. ## Edit tagf to change the color/state of the Tags:                                     ##  ##
  17.   variable tagf "\017\002"
  18. ## Edit line1, line2, line3, line4 to change what is displayed on each line             ##  ##
  19. ## Valid items are: word, definition, example, author, link                             ##  ##
  20. ## Do not remove any variables here! Just change them to "" to suppress display         ##  ##
  21.   variable line1 "word"
  22.   variable line2 "definition"
  23.   variable line3 "example"
  24.   variable line4 "author link"
  25. ## Edit cmdchar to change the !trigger used to for this script                          ##  ##
  26.   variable cmdchar "!"
  27. ##############################################################################################
  28. ##  ##                           End Setup.                                              ## ##
  29. ##############################################################################################
  30.   setudef flag ud
  31.   bind pub -|- [string trimleft $urbandictionary::cmdchar]ud urbandictionary::main
  32.   bind pub -|- [string trimleft $urbandictionary::cmdchar]slang urbandictionary::main
  33. }
  34. proc urbandictionary::main {nick host hand chan text} {
  35.   if {[lsearch -exact [channel info $chan] +ud] != -1} {
  36.     set text [strip $text]
  37.     set number ""
  38.     set definition ""
  39.     set example ""
  40.     set word ""
  41.     set term ""
  42.     set class ""
  43.     set udurl ""
  44.     set page ""
  45.     set count 1
  46.     set item 1
  47.     if {[regexp {^(?:[\d]{1,}\s)(.*)} $text match term]} {
  48.       set term [urlencode $term]
  49.       set item [expr {[lindex $text 0] % 7}]
  50.       set page [expr {int(ceil(double([lindex $text 0]) / double(7)))}]
  51.       set page "&page=${page}"
  52.       set udurl "/define.php?term=${term}${page}"
  53.     } elseif {[lindex $text 0] != "${urbandictionary::cmdchar}ud"} {
  54.       set term [urlencode $text]
  55.       set udurl [iif $term "/define.php?term=${term}" "/random.php"]
  56.       set class ""
  57.     }
  58.     set udsite "www.urbandictionary.com"
  59.     if {[catch {set udsock [socket -async $udsite 80]} sockerr] && $udurl != ""} {
  60.       return 0
  61.     } else {
  62.       puts $udsock "GET $udurl HTTP/1.0"
  63.       puts $udsock "Host: $udsite"
  64.       puts $udsock "User-Agent: Opera 9.6"
  65.       puts $udsock ""
  66.       flush $udsock
  67.       while {![eof $udsock]} {
  68.         set udvar " [gets $udsock] "
  69.         regexp -nocase {<div\sclass="([^"]*)">} $udvar match class
  70.        if {$class == "definition" && $count == $item} {
  71.          if {[regexp -nocase {<div\sclass="definition">(.*?)<\/div>} $udvar match definition]} {
  72.            set definition [striphtml $definition]
  73.            if {[regexp -nocase {<div class="example">(.*)} $udvar match example]} {
  74.              set class "example"
  75.              set example [striphtml $example]
  76.            }
  77.          } else {
  78.            set definition "$definition [striphtml $udvar]"
  79.          }
  80.        } elseif {[string match -nocase "*<td class='word'>*" $udvar]} {
  81.          set class "word"
  82.        } elseif {$class == "word" && $count == $item} {
  83.          set word [striphtml $udvar]
  84.          set class ""
  85.        } elseif {$class == "example" && $count == $item} {
  86.          if {[regexp -nocase {<div class="example">(.*)(?:<\/div>)?} $udvar match example]} {
  87.            regexp -nocase {(.*)<div class="example">} $udvar match definitionend
  88.            set definition "$definition [striphtml $definitionend]"
  89.            set example [striphtml $example]
  90.          } else {
  91.            set example "$example [striphtml $udvar]"
  92.          }
  93.        } elseif {[regexp -nocase {class="author">(.*)<\/a>} $udvar match author]} {
  94.          if {$count == $item} {
  95.            set wordfix [string map {" " +} [string trimleft [string trimright $word " "] " "]]
  96.            set word "${urbandictionary::tagf}Word: ${urbandictionary::textf}[striphtml $word]"
  97.            set link "${urbandictionary::tagf}Link: ${urbandictionary::linkf}http://www.urbandictionary.com/define.php?term=${wordfix}${page}\017"
  98.            set author "${urbandictionary::tagf}Author: ${urbandictionary::textf}[striphtml $author]"
  99.            set definition "${urbandictionary::tagf}Definition: ${urbandictionary::textf}[striphtml $definition]"
  100.            set example "${urbandictionary::tagf}Example: ${urbandictionary::textf}[striphtml $example]"
  101.            if {$urbandictionary::line1 != ""} {
  102.              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line1 {$\1}]]
  103.            }
  104.            if {$urbandictionary::line2 != ""} {
  105.              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line2 {$\1}]]
  106.            }
  107.            if {$urbandictionary::line3 != ""} {
  108.              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line3 {$\1}]]
  109.            }
  110.            if {$urbandictionary::line4 != ""} {
  111.              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line4 {$\1}]]
  112.            }
  113.            close $udsock
  114.            return 0
  115.          } else {
  116.            incr count
  117.          }
  118.        } elseif {[regexp -nocase {Location:\s(.*)} $udvar match redirect]} {
  119.          regexp {term\=(.*)} $redirect match udredir
  120.          urbandictionary::main $nick $host $hand $chan $udredir
  121.          break
  122.          return 0
  123.        } elseif {[string match -nocase "*<div id='not_defined_yet'>*" $udvar] || [string match -nocase "*</body>*" $udvar]} {
  124.          putserv "PRIVMSG $chan :Nothing found!"
  125.          close $udsock
  126.          return
  127.        }
  128.      }
  129.    }
  130.  }
  131. }
  132. proc urbandictionary::striphtml {string} {
  133.  return [string map {&quot; \" &lt; < &rt; >} [regsub -all {(<[^<^>]*>)} $string ""]]
  134. }
  135. proc urbandictionary::replacestring {string found replace} {
  136.  set found [escape $found]
  137.  putlog "found: $found"
  138.  return [regsub -all $found $string $replace]
  139.  
  140. }
  141. proc urbandictionary::escape {string} {
  142.  return [subst [regsub -all {([\[\]\(\)\{\}\.\?\:\^])} $string "\\1"]]
  143. }
  144. proc urbandictionary::iif {test do elsedo} {
  145.   if {$test != 0 && $test != ""} {
  146.     return $do
  147.   } else {
  148.     return "$elsedo"
  149.   }
  150. }
  151. proc urbandictionary::urlencode {string} {
  152.  regsub -all {^\{|\}$} $string "" string
  153.  return [subst [regsub -nocase -all {([^a-z0-9\+])} $string {%[format %x [scan "\\&" %c]]}]]
  154. }
  155. proc urbandictionary::strip {text} {
  156.  regsub -all {\002|\031|\015|\037|\017|\003(\d{1,2})?(,\d{1,2})?} $text "" text
  157.    return $text
  158. }
  159. proc urbandictionary::msg {chan logo textf text} {
  160.  set text [textsplit $text 50]
  161.  set counter 0
  162.  while {$counter <= [llength $text]} {
  163.    if {[lindex $text $counter] != ""} {
  164.      putserv "PRIVMSG $chan :${logo} ${textf}[string map {\\\" \"} [lindex $text $counter]]"
  165.     }
  166.     incr counter
  167.   }
  168. }
  169. proc urbandictionary::textsplit {text limit} {
  170.   set text [split $text " "]
  171.   set tokens [llength $text]
  172.   set start 0
  173.   set return ""
  174.   while {[llength [lrange $text $start $tokens]] > $limit} {
  175.     incr tokens -1
  176.     if {[llength [lrange $text $start $tokens]] <= $limit} {
  177.       lappend return [join [lrange $text $start $tokens]]
  178.       set start [expr $tokens + 1]
  179.       set tokens [llength $text]
  180.     }
  181.   }
  182.   lappend return [join [lrange $text $start $tokens]]
  183.   return $return
  184. }
  185. putlog "\002*Loaded* \00308,07\002UrbanDictionary\002\003 \002by Ford_Lawnmower irc.GeekShed.net #Script-Help"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement