#-- ADDON INIT ----------------------------------------------------------------------#
sitebot:addversion "Invite v3.4"
set addon(invite) "3.4"
set addonBuild(invite) "12"
set addonConf(invite) "defMsgChan announceInvite"
set addonConfInfo(invite) "#-- INVITE ADDON CONF -----------------------------------------------------------------#"

if {![info exists enabled(invite)]} { set enabled(invite) 1 }
if {![info exists invite(defMsgChan)]} {set invite(defMsgChan) "#xXx"}
if {![info exists invite(announceInvite)]} {set invite(announceInvite) "1"}

#-- INVITE SCRIPT -------------------------------------------------------------------#
if $enabled(invite) {
  if !$sitebot(limboBot) { bind msg - !invite sitebot:invite }

  proc sitebot:ftpinvite { arg } { global sitebot enabled invite leafbots
    foreach channel $invite(defMsgChan) { if ![botisop $channel] { hubsend chan 5 $channel PRIVMSG [replacevar invite_not_op 0 0] ; re$
    foreach {nick username group} $arg {}
    set output [replacevar invite "nickname username group" "$nick $username $group"]
    foreach channel $invite(defMsgChan) {
      if $invite(announceInvite) { hubsend chan 5 $channel PRIVMSG $output }
      puthelp "INVITE $nick $channel"
    }
    foreach leaf [lsort [array names leafbots]] {
      if $enabled(debug) { putlog "Leaf Invite msg: $leaf, $nick, $username, $group, $invite(announceInvite)" }
      putbot $leaf "leafinvite $nick $username $group $invite(announceInvite)"
    }
  }

  proc sitebot:invite { nick uhost hand arg } { global sitebot invite
    if { [llength $arg] >= 2 } {
      foreach channel $invite(defMsgChan) { if ![botisop $channel] { hubsend chan 5 $channel PRIVMSG [replacevar invite_not_op 0 0] ; $
      set username [lindex $arg 0]; set password [lindex $arg 1]; set group ""
      set result [exec "$sitebot(glftpdDir)/bin/passchk" $username $password "$sitebot(glftpdDir)/etc/passwd"]
      set userFile "$sitebot(glftpdDir)/ftp-data/users/$username"

      if {[file exists $userFile]} {
        foreach line [split [exec "/bin/cat" $userFile] "\n"] {
          if {![string compare [lindex $line 0] "FLAGS"]} {
            set flags [lindex $line 1]; break;
          }
        }
        if {[string match "*6*" "$flags"] != "0" } { set result "DELLED" }

        if {![string compare $result "MATCH"]} {
          foreach line [split [exec "/bin/cat" $userFile] "\n"] {
            if { ! [string compare [lindex $line 0] "GROUP"] } {
              set group [lrange $line 1 end]; break;
            }
          }
          if { [lindex $arg 2] == "" } {
            set output [replacevar msginvite "user group nick" "$username $group $nick"]
            foreach channel $invite(defMsgChan) {
              if $invite(announceInvite) { hubsend chan 5 $channel PRIVMSG $output }
              puthelp "INVITE $nick $channel"
            }
          }
          if { [lindex $arg 2] != "" } {
            foreach channel [array get invite] {
              if { [string match "*group-*" "[string tolower [lindex $arg 3]]"] } { break }
              if { [string match "[string tolower [lindex $arg 2]]" "[string tolower $channel]"] } {
                set output [replacevar msginvite "user group nick" "$username $group $nick"]
                if $invite(announceInvite) { hubsend chan 5 $invite($channel) PRIVMSG $output }
                puthelp "INVITE $nick $invite($channel)"
              }
            }
          }
          foreach grp $group {
            if { [info exists invite(group-$grp)] } {
              set output [replacevar msginvite "user group nick" "$username $group $nick"]
              foreach channel $invite(group-$grp) { if $invite(announceInvite) { hubsend chan 5 $channel PRIVMSG $output } }
              foreach channel $invite(group-$grp) { puthelp "INVITE $nick $channel" }
            }
          }
        } elseif {![string compare $result "NOMATCH"]} {
          set output [replacevar badmsginvite "nick uhost user" "$nick $uhost $username"]
          foreach channel $invite(defMsgChan) { if $invite(announceInvite) { hubsend chan 5 $channel PRIVMSG $output } }
        } elseif {![string compare $result "DELLED"]} {
          set output [replacevar delmsginvite "nick uhost user" "$nick $uhost $username"]
          foreach channel $invite(defMsgChan) { if $invite(announceInvite) { hubsend chan 5 $channel PRIVMSG $output } }
        } else {
          set output [replacevar badmsginvite "nick uhost user" "$nick $uhost $username"]
          foreach channel $invite(defMsgChan) { if $invite(announceInvite) { hubsend chan 5 $channel PRIVMSG $output } }
        }
      }
    }
  }
} {
  if !$sitebot(limboBot) {
    set valid [binds sitebot:invite]
    if {$valid != ""} { unbind msg - !invite sitebot:invite }
  }
}
