#
# mircscripts.org search script in TCL by myggan
# creds to Robb (robb@tclcoder.org)
#
# search syntax: ms [keyword[+keyword]] [all|script|addon|code|dll|theme|tutorial|misc]
#
# examples:
#
# ms away+system addon
# ms myggan
# ms
#
 
#script name bugfix by decapitator

#by wiebe
# chanmode +c fix by wiebe (for eggdrop v1.6.17)
# added help proc, made to fit with c_<command>.tcl structure
# logging, use cnotice if possible



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

proc c_ms:help:pub { nick uhost handle chan text } {
  lappend output "Usage: ms \[keyword\[+keyword\]\] \[all|script|addon|code|dll|theme|tutorial|misc\]"
  lappend output "Searches www.mircscripts.org for entries matching the search parameters."
  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]]"
}



#ms pub
bind pubm fvlomn|fvlomn  "% ${botnet-nick} ms" c_ms:pub
bind pubm fvlomn|fvlomn  "% ${botnet-nick} ms *" c_ms:pub
 
proc c_ms:pub { nick uhost handle chan text } {
  set text2 [join [lrange [split $text] 2 end]]
  if { [string equal $text2 ""] } {
    lappend output "Usage: ms \[keyword\[+keyword\]\] \[all|script|addon|code|dll|theme|tutorial|misc\]"
    if { [catch {set x [cnotice $nick $output]} error] || !$x } {
      foreach t $output { puthelp "NOTICE $nick :$t" }
    }
  } else {
    msEvent $nick $uhost $handle $chan $text2
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#bind pub fvlomn|fvlomn ?ms msEvent
proc msEvent {nick uhost handle channel text} {
  msConnect $channel [lindex [split $text] 0] [lindex [split $text] 1]
}
proc msConnect {chan query {type all}} {
  set socket [socket -async www.mircscripts.org 80]
  fconfigure $socket -buffering line -buffersize 2000
  set msAuthor ""
  set msTitle ""
  set msType ""
  set msUrl ""
  set msCount 0
  puts $socket "GET /archive.php?stype=$type&squery=$query&sorder=file_date&ssort=desc&perpage=3 HTTP/1.1"
  puts $socket "Host: www.mircscripts.org"
  puts $socket "User-Agent: Mozilla"
  puts $socket "Connection: close"
  puts $socket ""
  flush $socket
  while { ![eof $socket] } {
    set data [gets $socket]
    regexp {<th class="site(?:Lite|Body)">(.+?)<\x2Fth>} $data _ msType
    if { [string length $msType] > 0 } {
      if { [regexp {<\x2Ftr>$} $data] } {
        regsub -all {<.+?>} $msTitle "" msTitle
        set msg "$msTitle: \037http://www.mircscripts.org/$msUrl\037 ($msType) by: \002$msAuthor\002"
        if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } { set msg [stripcodes bcru $msg] }
        puthelp "PRIVMSG $chan :$msg"
        set msAuthor ""
        set msTitle ""
        set msType ""
        set msUrl ""
        incr msCount
      } else {
        regexp {\x2Fusers\x2F(.+?)"} $data _ msAuthor
        regexp {\x2F(comments\.php\?id=(\d+))"} $data _ msUrl
	if {$msTitle==""} {
	        regexp -nocase -- {^.*<a\shref="/comments\.php\?id=[0-9]+">(.+?)</a>.*} $data _ msTitle
	}
      }
    }
  }
  close $socket
  if { !$msCount } { puthelp "PRIVMSG $chan :No matches found for $query" }
}

