#by decapitator @ QuakeNet

#modified by wiebe @ QuakeNet
# added help proc, made to fit with c_<command>.tcl structure
# logging, use cnotice if possible, notice when no results are found
# made it work with tcl 8.3



#help google pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} help google" c_google:help:pub

proc c_google:help:pub { nick uhost handle chan text } {
  lappend output "Usage: google <search>"
  lappend output "Returns results from google. Using this for porn, warez, or other of such searches is not allowed and will get your access removed."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#google pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} google" c_google:pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} google *" c_google:pub

proc c_google:pub { nick uhost handle chan text } {
  set search [join [lrange [split $text] 2 end]]
  set output ""
  if { ![string equal $search ""] } {
    set r 1
    set socket [socket -async app.www.home.nl 80]
    fconfigure $socket -buffering line -buffersize 2000
    regsub -all -- {\s} $search {%20} t
    puts $socket "GET /zoeken/websites?q=$t&safe=on HTTP/1.1"
    puts $socket "Host: app.www.home.nl"
    puts $socket "User-Agent: Mozilla"
    puts $socket "Connection: close"
    puts $socket ""
    while { ![eof $socket] } {
      set data [gets $socket]
      if { [regexp -nocase -- {<a\sclass="plain"\shref="(.*?)".*?\><b>(\d+\.\s.*?)</a>} $data _ link title] } {
        regsub -all -- {(^[^<]*>|<[^>]*>|<[^>]*$)} $title "" title
        regsub -all -- {(^[^<]*>|<[^>]*>|<[^>]*$)} $link "" link
        set msg "$title - $link"
        if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } {
          set msg [stripcodes bcru $msg]
        }        
        puthelp "PRIVMSG $chan :$msg"
        incr r
      }
      if { [string equal $r 4] } { break }
    }
    close $socket
    if { [string equal $r 1] } {
      if { [string length $search] > "20" } { set search [string range $search 0 19]... }
      lappend output "No results found on google for: $search"
    }
  } else { lappend output "Usage: google <search>" }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}

