# $Id: Cari.tcl,v 1.11 2003/08/24 14:23:29 peter Exp $

# Cari script for the eggdrop
# version 0.4, 24/08/2003, by Peter Postma <peter@webdeveloping.nl>
#
# Changelog:
# 0.4: (24/08/2003)
#  - added perline option
#  - some code tweaks
# 0.3: (09/07/2003)
#  - added several configuration options
#

### Configuration settings ###

# the triggers: [seperate with spaces]
set Cari(triggers) "!google"

# flags needed to use the trigger [default=everyone]
set Cari(flags) "-|-"

# channels where the bot doesn't respond to triggers [seperate with spaces]
set Cari(nopub) "" 

# flood protection: seconds between use of the triggers
# to disable: set it to 0
set Cari(antiflood) 11

# method to send the messages:
# 0 = Private message
# 1 = Public message 
# 2 = Private notice 
# 3 = Public notice
set Cari(method) 1

# show how many results? 1, 2 or 3??
set Cari(results) 10

# show every result on a new line?  0=no 1=yes
set Cari(perline) 0

### End Configuration settings ###


### Begin Tcl code ###

set Cari(version) 0.4

if {[catch { package require http } err]} {
  putlog "Cannot load [file tail [info script]]: Problem loading the http package: $err"
  return 1
}

if {[info tclversion] < 8.1} {
  putlog "Cannot load [file tail [info script]]: You need at least Tcl version 8.1 and you have Tcl version [info tclversion]."
  return 1
}

foreach trigger [split $Cari(triggers)] {
  bind pub $Cari(flags) $trigger Cari:pub
}
catch { unset trigger }

proc Cari:output {chan nick output} {
  global Cari

  switch $Cari(method) {
    0 { putquick "PRIVMSG $nick :$output" }
    1 { putquick "PRIVMSG $chan :$output" }
    2 { putquick "NOTICE $nick :$output" }
    3 { putquick "NOTICE $chan :$output" }
    default { putquick "PRIVMSG $chan :$output" }
  }
}

proc Cari:pub {nick uhost hand chan text} {
  global lastbind Cari

  if {[lsearch -exact $Cari(nopub) [string tolower $chan]] >= 0} { return 0 }

  if {[string length [string trim [lindex $text 0]]] == 0} {
    putquick "NOTICE $nick :Usage: $lastbind <keywords>"
    return 0
  }

  if {[info exists Cari(floodprot)]} {
    set diff [expr [clock seconds] - $Cari(floodprot)]
    if {$diff < $Cari(antiflood)} {
      putquick "NOTICE $nick :Trigger has just been used! Please wait [expr $Cari(antiflood) - $diff] seconds..."
      return 0
    }
    catch { unset diff }
  }
  set Cari(floodprot) [clock seconds]

  regsub -all { } [join $text] {+} search
  set Cari(url) "http://www.google.co.id/search?q=$search"
  set Cari(page) [http::config -useragent "Mozilla"]
  if {[catch {set Cari(page) [http::geturl $Cari(url) -timeout 15000]} msg]} {
    putquick "NOTICE $nick :Can't connect ($msg)"
    return 0
  }
  set Cari(data) [http::data $Cari(page)]

  if {$Cari(results) >= 1} {
    regexp -nocase {related:(.*?)>} $Cari(data) t link1
  }
  if {$Cari(results) >= 2} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link2
  }
  if {$Cari(results) >= 3} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link3
  }
  if {$Cari(results) >= 4} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link4
  }
  if {$Cari(results) >= 5} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link5
  }
  if {$Cari(results) >= 6} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link6
  }
  if {$Cari(results) >= 7} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link7
  }
  if {$Cari(results) >= 8} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link8
  }
  if {$Cari(results) >= 9} {
    regexp -nocase {related:.*?>.*?related:(.*?)>} $Cari(data) t link9
  }
  if {$Cari(results) >= 10} {
    regexp -nocase {related:.*?>.*?related:.*?>.*?related:(.*?)>} $Cari(data) t link10
  }

  if {$Cari(perline) == 1} {
    set separator "\n"
  } else {
    set separator "-"
  }

  if {[info exists link10]} {
    set output "http://$link1 $separator http://$link2 $separator http://$link3 http://$link4 $separator http://$link5 $separator http://$link6 $separator http://$link7 $separator http://$link8 $separator http://$link9 $separator http://$link10"
  } elseif {[info exists link2]} {
    set output "http://$link1 $separator http://$link2"
  } elseif {[info exists link1]} {
    set output "http://$link1"
  } else {
    set output "Kagak AdE sAy..Pokokna...cari.....aja....ndiri....daghhhh...."
  }

  regsub -all {%26} $output {\&} output
  regsub -all {%3F} $output {?} output
  regsub -all {%3D} $output {=} output 

  if {$Cari(perline) == 1} {
    foreach line [split $output \n] {
      Cari:output $chan $nick [string trim $line]
    }
  } else { 
    Cari:output $chan $nick [string trim $output]
  }

  catch { unset output separator t link1 link2 link3 link4 link5 link6 link7 link8 link9 link10}
  catch { http::cleanup $Cari(page) }

  return 0
}

putlog "Cari script $Cari(version) loaded!"