###################################################
# by decapitator @ QuakeNet
#
# modified by wiebe @ QuakeNet
# added support for irc.tcl (privmsg/cnotice)
# added check for chanmode +m, +c and the bot's status on the channel (@%+ or regular)
# used short variable names
# added help proc
# added logging
# made it work with tcl 8.3
####################################################


####################################################
# google:help:pubm
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} help google" google:help:pubm
proc google:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "google: usage google <search>"
  lappend o "google: returns results from google. using this for porn, warez, or other inappropriate searches is not allowed and will get your access removed."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "google: " } }
  putloglev c $c "help: $n $u $h $c google"
  return 1
}


####################################################
# google:help:msg
####################################################
bind msgm fvlomn|fvlomn "help google" google:help:msgm
proc google:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "google: usage google <search>"
  lappend o "google: returns results from google. using this for porn, warez, or other inappropriate searches is not allowed and will get your access removed."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "google: " } }
  putcmdlog "($n!$u) !$h! help google"
  return 1
}


####################################################
# google:pub
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} google" google:pubm
bind pubm fvlomn|fvlomn "% ${botnet-nick} google *" google:pubm
proc google:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "google: usage google <search>"
  } else {
    set o [google:google $t]; set m [lindex [split [getchanmode $c]] 0]
    if {![string match *m* $m] || [botisop $c] || [botishalfop $c] || [botisvoice $c]} {
      if {[string match *c* $m] } { set o [stripcodes bcru $o] }
      if {[string equal [info procs privmsg] ""]} { foreach l $o { puthelp "PRIVMSG $c :$l" }
      } else { foreach l $o { privmsg $c $l puthelp "google: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "google: " } }
  putloglev c $c "google: $n $u $h $c $t"
  return 1
}


####################################################
# google:msg
####################################################
bind msg fvlomn|fvlomn google google:msg
proc google:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} { lappend o "google: usage google <search>"
  } else { set o [google:google $t] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "google: " } }
  return 1
}


####################################################
# google:dcc
####################################################
bind dcc -|- google google:dcc
proc google:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "google: usage google <search>"
    lappend o "google: returns results from google. using this for porn, warez, or other inappropriate searches is not allowed and will get your access removed."
  } else { set o [google:google $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# google:google
####################################################
proc google:google { t } {
	set o ""; set m 3; set d ""
	set s [socket -async www.google.com 80]
	fconfigure $s -buffering line -buffersize 2000
	puts $s "GET /search?hl=en&q=%22[regsub -all -- {\s} $t {%20}]%22 HTTP/1.1"
	puts $s "Host: www.google.com"
	puts $s "User-Agent: Mozilla"
	puts $s "Connection: close"
	puts $s ""

	flush $s

	while {![eof $s]} { append d [gets $s] }
	close $s

	set r [regexp -all -nocase -inline -- {<div\sclass=g.*?><a\shref="(.*?)"\sclass=l.*?>(.*?)</a>} $d]
	if {[llength $r] == 0} {
		lappend o "google: no results found on google for: $t"
	} else {
		for {set i 0} {$i < [expr $m * 3] && $i < [llength $r]} {incr i 3} {
		  set a [regsub -all -nocase -- {<[^>]+>} [lindex $r [expr $i + 2]] {}] - [lindex $r [expr $i + 1]]
			lappend o "google: $a"
		}
	}
	return $o
}


set scriptdb(google) {
  "provides google command for google searches"
}

