#by wiebe @ QuakeNet



#help cycle msg
bind msgm mn "help cycle" c_cycle:help:msg

proc c_cycle:help:msg { nick uhost handle text } {
  lappend output "Usage: cycle \[<channel1> <channel2> .. <channelN>\]"
  lappend output "Makes the bot rejoin all or the given channels."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#cycle msg
bind msg mn cycle c_cycle:msg

proc c_cycle:msg { nick uhost handle text } {
  set output ""
  set chans $text
  set md5 [string range [md5 [string tolower "$nick!$uhost $handle [channels]"]] 0 9]
  set chans2 [join [lrange [split $text] 0 end-1]]
  set md51 [string range [md5 [string tolower "$nick!$uhost $handle $chans"]] 0 9]
  set md52 [string range [md5 [string tolower "$nick!$uhost $handle $chans2"]] 0 9]
  set md53 [lindex [split $text] end]
  if { ![botonchan] } {
    lappend output "I am not on any channels at the moment."
  } elseif { [string equal $chans ""] } {
    lappend output "WARNING: this will make the bot cycle all of its channels (possibly losing ops). If you are sure you want to do this, use: /msg $::botnick cycle $md5"
  } elseif { [string equal $md5 $chans] } {
    puthelp "NOTICE $nick :Cycling all channels, this may take a while."
    puthelp "JOIN 0"
  } elseif { [string equal $md52 $md53] } {
    set partchans ""
    set failchans ""
    foreach chan [split $chans2] {
      if { [validchan $chan] && [botonchan $chan] } {
        lappend partchans $chan
      } else {
        lappend failchans $chan
      }
    }
    if { ![string equal $partchans ""] } {
      puthelp "NOTICE $nick :Cycling [join $partchans]"
      puthelp "PART [join $partchans ,] :cycling channel."
    }
    if { ![string equal $failchans ""] } {
      puthelp "NOTICE $nick :Failed to cycle for [join $failchans]"
    }
  } else {
    lappend output "WARNING: this will make the bot cycle the given channels (possibly losing ops). If you are sure you want to do this, use: /msg $::botnick cycle $chans $md51"  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

