#########
#
# MootBot - Channel Management Module
# Copyright 2007 Andrew Williams
#
#########
#
#########
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation; either version 2.1 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#########

################################################################################
# DO NOT EDIT DO NOT EDIT DO NOT EDIT  DO NOT EDIT DO NOT EDIT DO NOT EDIT     #
#                         EDIT MOOTBOT_CONFIG.TCL			       #
################################################################################

# Bind Herder Commands (+H flag) 
bind msg H "joinchan" msg:joinchan
bind msg H "leavechan" msg:leavechan

# Bind Herdmaster Commands (+M)
bind msg M addherder msg:addherder
bind msg M remherder msg:remherder

# Herders can be created by Herdmasters
# Herdmasters can only be created by owners/botops (+M flag)

# Join channel on request from a Herder
proc msg:joinchan {nick hostmask handle text} {
   set chan [lindex [split $text] 0]

   if {[string index $chan 0] == "#"} {
        addChannel $chan
	putquick "PRIVMSG $nick :Joined $chan"
   } else {
 	putquick "PRIVMSG $nick :Please specify a valid channel"
   }
}

# Leave on request of a herder
proc msg:leavechan {nick hostmask handle text} {
   set chan [lindex [split $text] 0]

   if {[string index $chan 0] == "#"} {
         killChannel $chan
	 putquick "PRIVMSG $nick :Left $chan"
   } else {
 	putquick "PRVIMSG $nick :Please specify a valid channel"
   }
}

proc msg:addherder {nick hostmask handle text} {
   set user [lindex [split $text] 0]

   if {![validuser $user]} {
      putquick "PRIVMSG $nick :Please specify a valid user"
   } else {
         chattr $user +H
         putquick "PRIVMSG $nick :$user has been added as a botherder"
	 putquick "PRIVMSG $user :You are now a Bot Herder (+H) on MootBot"

   }
}

proc msg:remherder {nick hostmask handle text} {
   set user [lindex [split $text] 0]

   if {![validuser $user]} {
      putquick "PRIVMSG $nick :Please specify a valid user"
   } else {
      if {![matchattr $user H]} {
         putquick "PRIVMSG $nick :$user is not a botherder"
      } else {
         chattr $user -H
         putquick "PRIVMSG $nick :$user has been removed as a botherder"
      }
   }
}

############# API Commands-ish

proc addChannel {channel} {
   channel add $channel
   savechannels
}

proc killChannel {channel} {
   channel remove $channel
   savechannels
}
