#by wiebe @ QuakeNet



#help chanlist msg
bind msgm mn "help chanlist" c_chanlist:help:msg

proc c_chanlist:help:msg { nick uhost handle text } {
  lappend output "Usage: chanlist <pattern>"
  lappend output "Lists channels added to the bot matching the given pattern. The channels are prefixed with the bots status for them. Where @=op, %=halfop, +=voice, _=regular, !=inactive, *=unable to join, ~=not joined."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#chanlist msg
bind msg mn chanlist c_chanlist:msg

proc c_chanlist:msg { nick uhost handle text } {

  set pattern [lindex [split $text] 0]

#always require a target param
  if { [string equal $pattern ""] } {
    lappend output "Usage: chanlist <pattern>"
  
#show chanlist list
  } else {
    set result ""
    set match $pattern
    set toolong 0
    regsub -all {\[} $match {\\[} match
    regsub -all {\]} $match {\\]} match
    regsub -all {\\} $match {\\\\} match

    foreach chan [lsort [channels]] {
      if { [string match $match $chan] } {
        if { [string length [join $result]] > "350" } {
          lappend output [join $result]
          set result ""
        }
        if { [botisop $chan] } { lappend result @$chan
        } elseif { [botishalfop $chan] } { lappend result %$chan
        } elseif { [botisvoice $chan] } { lappend result +$chan
        } elseif { [channel get $chan inactive] } { lappend result !$chan
        } elseif { [ischanjuped $chan] } { lappend result *$chan
        } elseif { ![botonchan $chan] } { lappend result ~$chan
        } else { lappend result _$chan }
      }
    }
    if { [string equal $result ""] } {
      lappend output "No channels found matching $pattern"
    } else {
      lappend output [join $result]
      lappend output "Where @=op, %=halfop, +=voice, _=regular, !=inactive, *=unable to join, ~=not joined."
      lappend output "End of channel list matching $pattern"
    }
  }

  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

