Advertisement
lengend

Untitled

Nov 23rd, 2012
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. ;##########################################################
  2. ;# Stand-alone Google link lookup Script
  3. ;# No HTTP-script needed
  4. ;###########################################################
  5.  
  6. set max_links 1
  7. set allowed_chans "d2bs"
  8. set msg_targ 0 ;# 0 = chan, 1 = pv
  9. set google_trigger "!google"
  10. set google_host 173.194.66.104
  11.  
  12. ;# unquote when used on eggdrop
  13. bind pub - $google_trigger google
  14.  
  15. set link_prefix "\00312G\003\00304o\003\00308o\003\00312g\003\00303l\003\00304e\003\00314:\003 "
  16.  
  17. proc shrinkif { string argfront argback } {
  18. set beg [expr [expr [string length [set argfront [string tolower $argfront]]]] - 1]
  19. if {[string tolower [string range $string 0 $beg]] == "$argfront"} { set string [string range $string [expr [expr $beg] + 1] end] }
  20. set end [expr [expr [string length $string]] - [string length [set argback [string tolower $argback]]]]
  21. if {[string tolower [string range $string $end end]] == "$argback"} {
  22. set string [string range $string 0 [expr [expr $end] - 1]] }
  23. ; return $string }
  24.  
  25. proc obg_html_descape { content } {
  26. if {![string match *&* $content]} { return $content }
  27. set escapes {
  28.   \x20 " \x22 & \x26 ' \x27 – \x2D
  29. < \x3C > \x3E ˜ \x7E € \x80 ¡ \xA1
  30. ¢ \xA2 £ \xA3 ¤ \xA4 ¥ \xA5 ¦ \xA6
  31. § \xA7 ¨ \xA8 © \xA9 ª \xAA « \xAB
  32. ¬ \xAC ­ \xAD ® \xAE &hibar; \xAF ° \xB0
  33. ± \xB1 ² \xB2 ³ \xB3 ´ \xB4 µ \xB5
  34. ¶ \xB6 · \xB7 ¸ \xB8 ¹ \xB9 º \xBA
  35. » \xBB ¼ \xBC ½ \xBD ¾ \xBE ¿ \xBF
  36. À \xC0 Á \xC1 Â \xC2 Ã \xC3 Ä \xC4
  37. Å \xC5 Æ \xC6 Ç \xC7 È \xC8 É \xC9
  38. Ê \xCA Ë \xCB Ì \xCC Í \xCD Î \xCE
  39. Ï \xCF Ð \xD0 Ñ \xD1 Ò \xD2 Ó \xD3
  40. Ô \xD4 Õ \xD5 Ö \xD6 × \xD7 Ø \xD8
  41. Ù \xD9 Ú \xDA Û \xDB Ü \xDC Ý \xDD
  42. Þ \xDE ß \xDF à \xE0 á \xE1 â \xE2
  43. ã \xE3 ä \xE4 å \xE5 æ \xE6 ç \xE7
  44. è \xE8 é \xE9 ê \xEA ë \xEB ì \xEC
  45. í \xED î \xEE ï \xEF ð \xF0 ñ \xF1
  46. ò \xF2 ó \xF3 ô \xF4 õ \xF5 ö \xF6
  47. ÷ \xF7 ø \xF8 ù \xF9 ú \xFA û \xFB
  48. ü \xFC ý \xFD þ \xFE ÿ \xFF }
  49. set content [string map $escapes $content] ;
  50. set content [string map -nocase [list "\&rsaquo\;" "ø" "\&lsaquo\;" "ï"] $content] ;
  51. set content [string map [list "\]" "\\\]" "\[" "\\\[" "\$" "\\\$" "\\" "\\\\"] $content] ;
  52. regsub -all -- {&#([[:digit:]]{1,5});} $content {[format %c [string trimleft "\1" "0"]]} content ;
  53. regsub -all -- {&#x([[:xdigit:]]{1,4});} $content {[format %c [scan "\1" %x]]} content ;
  54. regsub -all -- {&#?[[:alnum:]]{2,7};} $content "?" content ;
  55. set content [subst $content] ;
  56. return [string map [list "=\" " "=" "\"" ""] $content] }
  57.  
  58. proc google { nick host hand chan arg } {
  59. global max_links google_host link_prefix allowed_chans msg_targ
  60. set page ""
  61. if {[string map -nocase [list " $chan " ""] " $allowed_chans "] != " $allowed_chans " || $allowed_chans == "*"} {
  62. set search [string map [list + %2B] $arg]
  63. if {$msg_targ == 0} { set msg_trg $chan } else { set msg_trg $nick }
  64. set zoek [string map [list " " "+"] $search]
  65. if {[catch { set socket [socket $google_host 80] } error]} { putlog "ERROR opening socket to $google_host; $error." } elseif {[catch {
  66. puts $socket "GET /search?q=$zoek&hl=en HTTP/1.1"
  67. puts $socket "User-Agent: Opera/9.80 (X11; Linux i686; U; Linux Mint; en) Presto/2.2.15 Version/10.00"
  68. puts $socket "Host: $google_host"
  69. puts $socket "Accept: text/html, text/xml"
  70. puts $socket "Accept-Language: en-US,en;q=0.9"
  71. puts $socket "Accept-Charset: iso-8859-1, utf-8"
  72. puts $socket "Accept-Encoding: deflate"
  73. puts $socket "Connection: close
  74. "
  75. ; flush $socket } error]} { putlog "Write error on writing to $google_host; $error." } else {
  76. if {[eof $socket] != 1} {
  77. set page [obg_html_descape [string map -nocase [list \{ "" \} "" \[ "" \] "" \"> \n> < \n< > >\n] [read $socket]]] } }
  78. catch { close $socket }
  79. if {[set lindex [lsearch $page */images/icons/onebox/calculator-40.gif*]] != -1} {
  80. set page [string map [list "<font size=-2>" "" </font> ""] $page]
  81. set a [lsearch [lrange $page $lindex end] *<b>*]
  82. lappend a [lsearch [lrange $page $lindex end] *</b>*]
  83. putserv "PRIVMSG $msg_trg :$link_prefix\002[lrange [lrange $page $lindex end] [expr [expr [lindex $a 0]] +1] [expr [expr [lindex $a 1]] -1]]\002"
  84. ; } elseif {[set plindex [lsearch [string map -nocase [list ID=IRES id=ires] $page] id=ires*]] != -1} {
  85. set page [lrange $page $plindex end]
  86. set res_cnt -1
  87. while {[set link [lsearch [string map -nocase [list HREF= href=] $page] href=*]] != -1 && [incr res_cnt] < $max_links} {
  88. set page [string map [list [set link [lindex $page $link]] ""] $page]
  89. set link [shrinkif $link href= class=]
  90. if {[string index $link 0] == "/"} { set link http://$google_host$link }
  91. if {$plindex == "-1"} { set link "no results." }
  92. ; putserv "PRIVMSG $msg_trg :$link_prefix$link" } } } }
  93.  
  94. putlog "Google Link Look Up"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement