#by wiebe @ QuakeNet



#help gbanlist pub

bind pubm omn|omn "% ${botnet-nick} help gbanlist" c_gbanlist:help:pub

proc c_gbanlist:help:pub { nick uhost handle chan text } {
  lappend output "Usage: gbanlist <banmask> \[<reason>\]"
  lappend output "Shows up to 5 matching bans on the internal global banlist."
  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]]"
}




#help gbanlist msg

bind msgm omn|omn "help gbanlist" c_gbanlist:help:msg

proc c_gbanlist:help:msg { nick uhost handle text } {
  lappend output "Usage: gbanlist <banmask> \[<reason>\]"
  lappend output "Shows up to 5 matching bans on the internal global banlist."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#gbanlist pub
bind pubm omn|omn "% ${botnet-nick} gbanlist" c_gbanlist:pub
bind pubm omn|omn "% ${botnet-nick} gbanlist *" c_gbanlist:pub

proc c_gbanlist:pub { nick uhost handle chan text } {
  set match [join [lrange [split $text] 2 end]]
  if { ![string equal $match ""] } {
    set output [c_gbanlist:gbanlist $match]
  } else {
    lappend output "Usage: gbanlist <banmask> \[<reason>\]"
  }
  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]]"
}



#gbanlist msg
bind msg omn|omn gbanlist c_gbanlist:msg

proc c_gbanlist:msg { nick uhost handle text } {
  set match $text
  if { [string equal $text ""] } {
    c_gbanlist:help:msg $nick $uhost $handle $text
  } else {
    set output [c_gbanlist:gbanlist $match]
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#returns bans to show
proc c_gbanlist:gbanlist { match } {
  set banmatch [lindex [split $match] 0]
  set commatch [join [lrange [split $match] 1 end]]
  if { [string equal $commatch ""] } { set commatch * }

  regsub -all {\[} $banmatch {\\[} banmatch
  regsub -all {\]} $banmatch {\\]} banmatch
  regsub -all {\\} $banmatch {\\\\} banmatch
  regsub -all {\[} $commatch {\\[} commatch
  regsub -all {\]} $commatch {\\]} commatch
  regsub -all {\\} $commatch {\\\\} commatch

  set x 0
  foreach ban [banlist] {
    set mask [lindex $ban 0]
    set comment [lindex $ban 1]
    if { [string equal $comment \{\}] } { set comment "<none>" }
    if { [isbansticky $mask] } { set mask "$mask   (sticky)" }
    if { [string match -nocase $banmatch $mask] && [string match -nocase $commatch $comment] } {
      incr x 1
      if { [string equal $x 6] } {
        lappend output "Too many matching bans, restrict your query."
        break
      }
      set expire [lindex $ban 2]
      if { [string equal $expire 0] } {
        set expire never
      } else {
        set expire "[clock format $expire -format %d/%m/%y] (in [c_gbanlist:ts $expire])"
        
      }
      set time [lindex $ban 3]
      set time "[clock format $time -format %d/%m/%y] ([c_gbanlist:ts $time] ago)"
      set lactive [lindex $ban 4]
      if { [string equal $lactive 0] } {
        set lactive never
      } else {
        set lactive "[clock format $lactive -format %d/%m/%y] ([c_gbanlist:ts $lactive] ago)"
      }
      set creator [lindex $ban 5]
      set ban "BAN $x: $mask"
      set ban "$ban [string repeat " " [expr 40 - [string length $ban]]]"
      set expire "\017EXPIRE: $expire"
      set expire "$expire [string repeat " " [expr 35 - [string length $expire]]]"
      set creator "\017BY: $creator"
      set creator "$creator [string repeat " " [expr 23 - [string length $creator]]]"
      set time "\017ADDED: $time"
      set time "$time [string repeat " " [expr 35 - [string length $time]]]"
      set lactive "\017USED: $lactive"
      set lactive "$lactive [string repeat " " [expr 35 - [string length $lactive]]]"
      set comment "\017REASON: $comment"
      lappend output "$ban $expire $creator $time $lactive $comment"
    }
  }
  if { [string equal $x 0] } {
    lappend output "No global bans found matching banmask \"$banmatch\" with reason \"$commatch\""
  } else {
    lappend output "End of global bans matching banmask \"$banmatch\" with reason \"$commatch\""
  }
  return $output
}



#give timestamp, returns duration since/to in Xy Xw Xd Xh Xm Xs format
proc c_gbanlist:ts { ts } {
  if { ![string is digit $ts] } { return 0 }
  if { $ts > [unixtime] } {
    set ts [duration [expr $ts - [unixtime]]]
  } else {
    set ts [duration [expr [unixtime] - $ts]]
  }
  set ts [string map [list " seconds" "s" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " second" "s" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " minutes" "m" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " minute" "m" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " hours" "h" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " hour" "h" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " days" "d" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " day" "d" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " weeks" "w" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " week" "w" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " years" "y" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " year" "y" "search" "replace" "search" "replace"] $ts]
  return [join [lrange [split $ts] 0 1]]
}

