#by wiebe @ Quakenet



#help restoremode pub
bind pubm mn|mn "% ${botnet-nick} help restoremode" c_restoremode:help:pub

proc c_restoremode:help:pub { nick uhost handle chan text } {
  lappend output "Usage: restoremode <vho>"
  lappend output "Ops, halfops and/or voices all users with the flags for it."
  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 restoremode msg
bind msgm mn|mn "help restoremode" c_restoremode:help:msg

proc c_restoremode:help:msg { nick uhost handle text } {
  lappend output "Usage: restoremode <channel> <vho>"
  lappend output "Ops, halfops and/or voices all users with the flags for it."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#restoremode pub
bind pubm mn|mn "% ${botnet-nick} restoremode" c_restoremode:pub
bind pubm mn|mn "% ${botnet-nick} restoremode *" c_restoremode:pub

proc c_restoremode:pub { nick uhost handle chan text } {
  set restoremode [lindex [split $text] 2]
  if { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } elseif { ![string match {[vho]} $restoremode] } {
    lappend output "Usage: restoremode <channel> <vho>"
  } elseif { ![c_restoremode:parse $chan $restoremode] } {
    lappend output "Failed to restore any modes on $chan"
  } else {    lappend output "Restored modes on $chan"
  }
  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]]"
}



#restoremode msg
bind msg mn|mn restoremode c_restoremode:msg

proc c_restoremode:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set restoremode [lindex [split $text] 1]
  if { ![validchan $chan] && ![string equal $chan ""] } {
    lappend output "No access or unknown channel $chan"
  } elseif { [string equal $chan ""] || [string equal $restoremode ""] } {
    lappend output "Usage: restoremode <channel> <vho>"
  } elseif { ![matchattr $handle mn|mn $chan] } {
    lappend output "No access or unknown channel $chan"
  } elseif { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"  } elseif { ![string match {[vho]} $restoremode] } {
    lappend output "Usage: restoremode <channel> <vho>"
  } elseif { ![c_restoremode:parse $chan $restoremode] } {
    lappend output "Failed to restore any modes on $chan"
  } else {
    lappend output "Restored modes on $chan"
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#parse modes
proc c_restoremode:parse { chan restoremode } {
  set m 0
  foreach n [chanlist $chan] {
    set h [nick2hand $n]
    if { [string match *o* $restoremode] && [botisop $chan] } {
      if { [matchattr $h omn|omn $chan] && ![matchattr $h d|d $chan] } {
        if { ![isop $n $chan] } {
          pushmode $chan +o $n
          set m 1
        }
      }
    } elseif { [string match *h* $restoremode] && [botisop $chan] } {
      if { [matchattr $h lomn|lomn $chan] && ![matchattr $h r|r $chan] } {
        if { ![isop $n $chan] && ![ishalfop $n $chan] } {
          #pushmode $chan +h $n
          #set m 1
        }
      }
    } elseif { [string match *v* $restoremode] } {
      if { [matchattr $h vlomn|vlomn $chan] && ![matchattr $h q|q $chan] } {
        if { ![isop $n $chan] && ![ishalfop $n $chan] && ![isvoice $n $chan] } {
          pushmode $chan +v $n
          set m 1
        }
      }
    }
  }
  if { $m } {
    flushmode $chan
    return 1
  } else { return 0 }
}

