####################################################
#
# original script:
# mircscripts.org search script in TCL by myggan @ QuakeNet
# creds to Robb (robb@tclcoder.org)
#
####################################################
#
# script name bugfix 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
# add /msg and dcc commands
# changed input, nolonger needed to specify keyword+keyword, keyword keyword will do
#
####################################################


####################################################
# ms:help:pubm
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} help ms" ms:help:pubm
proc ms:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "ms: usage ms <search> \[all|script|addon|code|dll|theme|tutorial|misc\]"
  lappend o "ms: searches www.mircscripts.org for entries matching the search parameters."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "ms: " } }
  putloglev c $c "help: $n $u $h $c ms"
  return 1
}


####################################################
# ms:help:msg
####################################################
bind msgm fvlomn|fvlomn "help ms" ms:help:msgm
proc ms:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "ms: usage ms <search> \[all|script|addon|code|dll|theme|tutorial|misc\]"
  lappend o "ms: searches www.mircscripts.org for entries matching the search parameters."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "ms: " } }
  putcmdlog "($n!$u) !$h! help ms"
  return 1
}


####################################################
# ms:pub
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} ms" ms:pubm
bind pubm fvlomn|fvlomn "% ${botnet-nick} ms *" ms:pubm
proc ms: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 "ms: usage ms <search> \[all|script|addon|code|dll|theme|tutorial|misc\]"
  } else {
    set o [ms:ms $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 "ms: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "ms: " } }
  putloglev c $c "ms: $n $u $h $c $t"
  return 1
}


####################################################
# ms:msg
####################################################
bind msg fvlomn|fvlomn ms ms:msg
proc ms:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} {
    lappend o "ms: usage ms <search> \[all|script|addon|code|dll|theme|tutorial|misc\]"
  } else { set o [ms:ms $t] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "ms: " } }
  return 1
}


####################################################
# ms:dcc
####################################################
bind dcc -|- ms ms:dcc
proc ms:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "ms: usage ms <search> \[all|script|addon|code|dll|theme|tutorial|misc\]"
    lappend o "ms: searches www.mircscripts.org for entries matching the search parameters."
  } else { set o [ms:ms $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# ms:ms
####################################################
proc ms:ms { q } {
  set q [split $q]; set t [lindex $q end]; set t [string tolower $t]
  set l [split "all script addon code dll theme tutorial misc"]
  if {[string equal [lsearch -exact $l $t] "-1"] || [llength $q] < 2} {
    set t "all"; set q [join [lrange $q 0 end] +]
  } else { set q [join [lrange $q 0 end-1] +] }
  set s [socket -async www.mircscripts.org 80]
  fconfigure $s -buffering line -buffersize 2000
  set a ""; set u ""; set v ""; set w ""; set z 0; set o ""
  puts $s "GET /archive.php?stype=$t&squery=$q&sorder=file_date&ssort=desc&perpage=3 HTTP/1.1"
  puts $s "Host: www.mircscripts.org"
  puts $s "User-Agent: Mozilla"
  puts $s "Connection: close"
  puts $s ""
  flush $s
  while {![eof $s]} {
    set d [gets $s]; regexp {<th class="site(?:Lite|Body)">(.+?)<\x2Fth>} $d _ v
    if {[string length $v] > 0} {
      if {[regexp {<\x2Ftr>$} $d]} {
        regsub -all {<.+?>} $u "" u
        lappend o "ms: $u \037http://www.mircscripts.org/$w\037 ($v) by: \002$a\002"
        set a ""; set u ""; set v ""; set w ""; incr z
      } else {
        regexp {\x2Fusers\x2F(.+?)"} $d _ a
        regexp {\x2F(comments\.php\?id=(\d+))"} $d _ w
	if {[string equal $u ""]} {
          regexp -nocase -- {^.*<a\shref="/comments\.php\?id=[0-9]+">(.+?)</a>.*} $d _ u
	}
      }
    }
  }
  close $s
  if {!$z} { lappend o "ms: no results found for '[join [split $q +]]\017' (type: $t)" }
  return $o
}


set scriptdb(ms) {
  "provides ms command for searches on www.mircscripts.org"
}

