if {![info exists greetfile]} {
   set greetfile "greetfile"
   set greetsetfile "greetset"
}

bind pub - !greet pub_greet
bind pub - `greet pub_greet
bind pub - .greet pub_greet
bind join - *!*@* set_greet
bind msg - greet greet_set
bind dcc - greet greet_set_dcc


proc greet:replace { nick chan line } {
  global botnick
  if {[string match "*%nick%*" $line]} { regsub -all %nick% $line $nick line }
  if {[string match "*%chan%*" $line]} { regsub -all %chan% $line $chan line }
  return $line
}


proc set_greet {nick uhost hand chan} {
  global random_greet setgreet botnick greet:replace

 if {$botnick != $nick} {
  if [channel get $chan greet] {
  if {[info exists setgreet([string tolower $chan])] && [lindex $setgreet([string tolower $chan]) 0] != ""} {
    set i $setgreet([string tolower $chan]) 
    puthelp "NOTICE $nick :[greet:replace $nick $chan $i]"

   } {

     foreach i [array names random_greet] {
        if {[lsearch -exact [string tolower [channels]] [string tolower $i]] != -1} {
            if {[lindex $random_greet([string tolower $i]) 0] != ""} {
              set greet [lindex $random_greet($i) [rand [llength $random_greet($i)]]]
                if {$chan == $i} {
                  puthelp "NOTICE $nick :$greet
                }
            }
}
        } {
            unset random_greet($i)
          }
      }
   }
 }
}

proc greet_show_pub {nick uhost hand channel arg} {
  global random_greet setgreet botnick 


  if {![matchattr $hand m|m $channel]} {
     putserv "PRIVMSG $channel :Sorry, you are not authorized to use this command."
     return 0
  }

  if {[info exists setgreet([string tolower $channel])] && [lindex $setgreet([string tolower $channel]) 0] != ""} {
    set i $setgreet([string tolower $channel]) 
       putserv "PRIVMSG $channel :\002Greet Set to:\002  $i"
       putserv "PRIVMSG $channel :Use GREET SET $channel OFF if you want to turn it off."
   
    }
    putserv "PRIVMSG $channel :Type \002/msg $botnick greet HELP\002 for help."

  return 1
}


proc greet_show {nick uhost hand arg} {
  global random_greet setgreet botnick 

  set channel [lindex $arg 0]
  if {$channel == ""} {
     putserv "PRIVMSG $nick :Usage: greet <channel>"
     return 0
  }

    if {[lsearch -exact [string tolower [channels]] [string tolower $channel]] == -1} {
     putserv "PRIVMSG $nick :I am not on ${channel}."
     return 0
  }

    if {![matchattr $hand m|m $channel]} {
     putserv "PRIVMSG $nick :Sorry, you are not authorized to use this command."
     return 0
  }

  if {[info exists setgreet([string tolower $channel])] && [lindex $setgreet([string tolower $channel]) 0] != ""} {
    set i $setgreet([string tolower $channel]) 
       putserv "PRIVMSG $nick :\002Greet Set to:\002  $i"
       putserv "PRIVMSG $nick :Use GREET SET $channel OFF if you want to turn it off."
   
    }
  

  putserv "PRIVMSG $nick :Type \002/msg $botnick greet HELP\002 for help."

  return 1
}

proc greet_show_dcc {hand idx arg} {
  global random_greet setgreet botnick 

  set channel [lindex $arg 0]
  if {$channel == ""} {
     putidx $idx "Usage: greet <channel>"
     return 0
  }

  
  if {[lsearch -exact [string tolower [channels]] [string tolower $channel]] == -1} {
     putidx $idx "I am not on ${channel}."
     return 0
  }

  if {![matchattr $hand m|m $channel]} {
     putidx $idx "Sorry, you are not authorized to use this command."
     return 0
  }

  putidx $idx "Greets for \002$channel\002:"

  if {[info exists setgreet([string tolower $channel])] && [lindex $setgreet([string tolower $channel]) 0] != ""} {
    set i $setgreet([string tolower $channel]) 
       putidx $idx "\002Greet Set to:\002  $i"
       putidx $idx "Use GREET SET $channel OFF if you want to turn it off."
       }


 
  putidx $idx "Type \002.greet HELP\002 for help."

  return 1
}

proc greet_set_pub {nick uhost hand channel arg} {
  global setgreet

  if {![matchattr $hand m|m $channel]} {
     putserv "PRIVMSG $channel :Sorry, you are not authorized to use this command."
     return 0
  }
  if {[string tolower [lindex $arg 1]] != "-off"} {
    putserv "PRIVMSG $channel :Greet set to: [lrange $arg 1 end]"
    set setgreet([string tolower $channel]) [lrange $arg 1 end]
  } {
    putserv "PRIVMSG $channel :Greet set off $channel"
    set setgreet([string tolower $channel]) [lrange "" 1 end]
  }
  
  putlog "Writing greets file..."
  greet_write
  
  return 1

}


proc greet_set {nick uhost hand arg} {
  global setgreet

  set channel [lindex $arg 1]

  if {$channel == ""} {
     putserv "PRIVMSG $nick :Usage: GREET SET <channel> <greet>"
     putserv "PRIVMSG $nick :Or:    GREET SET <channel> OFF"
     return 0
  }

  if {[lsearch -exact [string tolower [channels]] [string tolower $channel]] == -1} {
     putserv "PRIVMSG $nick :I am not on ${channel}."
     return 0
  }

  if {![matchattr $hand m|m $channel]} {
     putserv "PRIVMSG $nick :Sorry, you are not authorized to use this command."
     return 0
  }

  if {[string tolower [lindex $arg 1]] != "-off"} {
    putserv "PRIVMSG $nick :Greet set to: [lrange $arg 1 end]"
    set setgreet([string tolower $channel]) [lrange $arg 1 end]
  } {
    putserv "PRIVMSG $nick :Greet set off $channel"
    set setgreet([string tolower $channel]) [lrange "" 1 end]
  }
  
  putlog "Writing greets file..."
  greet_write
  
  return 1

}

proc greet_set_dcc {hand idx arg} {
  global setgreet

  set channel [lindex $arg 2]

  if {$channel == ""} {
     putidx $idx "Usage: GREET SET <channel> <greet>"
     putidx $idx "Or:    GREET SET <channel> OFF"
     return 0
  }

  if {[lsearch -exact [string tolower [channels]] [string tolower $channel]] == -1} {
     putidx $idx "I am not on ${channel}."
     return 0
  }

  if {![matchattr $hand m|m $channel]} {
     putidx $idx "Sorry, you are not authorized to use this command."
     return 0
  }

  if {[string tolower [lindex $arg 2]] != "-off"} {
    putidx $idx "Greet set to: [lrange $arg 1 end]"
    set setgreet([string tolower $channel]) [lrange $arg 2 end]
  } {
    putidx $idx "Greet set off $channel"
    set setgreet([string tolower $channel]) [lrange "" 2 end]
  }
  
  
  putlog "Writing greets file..."
  greet_write
  
  return 1

}
proc greet_load {} {
   global random_greet setgreet greetfile greetsetfile

   if {[info exists random_greet]} {unset random_greet}
   if {[catch {set fd [open $greetfile r]}] != 0} {return 0}
   while {![eof $fd]} {
      set inp [gets $fd]
      if {[eof $fd]} {break}
      if {[string trim $inp " "] == ""} {continue}
      set channel [lindex $inp 0]
      lappend random_greet([string tolower $channel]) [lrange $inp 1 end]
   }
   close $fd

   if {[info exists setgreet]} {unset setgreet}
   if {[catch {set fd [open $greetsetfile r]}] != 0} {return 0}
   while {![eof $fd]} {
      set inp [gets $fd]
      if {[eof $fd]} {break}
      if {[string trim $inp " "] == ""} {continue}
      set channel [lindex $inp 0]
      lappend setgreet([string tolower $channel]) [lrange $inp 1 end]
   }
   close $fd



   return 1
}

proc greet_write {} {
   global greetfile random_greet setgreet greetsetfile

   set fd [open $greetfile w]
   foreach channel [array names random_greet] {
      if {[lsearch -exact [string tolower [channels]] [string tolower $channel]] != -1} {
         if {[lindex $random_greet([string tolower $channel]) 0] != ""} {
            foreach i $random_greet([string tolower $channel]) {
               puts $fd "$channel $i"
            }
         }
      }
   }
   close $fd

   set fd [open $greetsetfile w]
   foreach channel [array names setgreet] {
      if {[lsearch -exact [string tolower [channels]] [string tolower $channel]] != -1} {
         if {[lindex $setgreet([string tolower $channel]) 0] != ""} {
            set i $setgreet([string tolower $channel]) 
               puts $fd "$channel $i"
         }
      }
   }
   close $fd

   return 1
}


proc msg_greet {nick uhost hand arg} {
  switch [string tolower [lindex $arg 0]] {
    "set"       {set r [greet_set $nick $uhost $hand $arg]}
    "help"      {set r [greet_help $nick $uhost $hand $arg]}
    default     {set r [greet_show $nick $uhost $hand $arg]}
  }
  return $r
}

proc pub_greet {nick uhost hand channel arg} {
  switch [string tolower [lindex $arg 0]] {
    "set"       {set r [greet_set_pub $nick $uhost $hand $channel $arg]}
    "help"      {set r [greet_help_pub $nick $uhost $hand $channel $arg]}
    default     {set r [greet_show_pub $nick $uhost $hand $channel $arg]}
  }
  return $r
}


proc dcc_greet {hand idx arg} {
  switch [string tolower [lindex $arg 0]] {
    "set"       {set r [greet_set_dcc $hand $idx $arg]}
    "help"      {set r [greet_help_dcc $hand $idx $arg]}
    default     {set r [greet_show_dcc $hand $idx $arg]}
  }
  return $r
}


greet_load

if {![info exists set_greet_running] && ![info exists random_greet] && ![info exists setgreet]} {
  set set_greet_running 1
}