####################################################
# by wiebe @ QuakeNet
#
####################################################


####################################################
# invite:help:msg
####################################################
bind msgm fvlomn|fvlomn "help invite" invite:help:msgm
proc invite:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "invite: usage invite \[<chan1> <chan2> .. <chanN>\]"
  lappend o "invite: invites you to all or the given channels."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "invite: " } }
  putcmdlog "($n!$u) !$h! help invite"
  return 1
}


####################################################
# invite:msg
####################################################
bind msg fvlomn|fvlomn invite invite:msg
proc invite:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  invite:invite $n $h $t
  return 1
}


####################################################
# invite:invite
####################################################
proc invite:invite { n h t } {
  set s ""; set u ""; set f ""
  if {![string equal $t ""]} {
    set t [split $t]
    foreach c $t {
      if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
      if {![validchan $c] || ![matchattr $h omn|fvlomn $c]} { set u [invite:add $u $c]
      } elseif {![botisop $c] && ![botishalfop $c]} { set f [invite:add $f $c]
      } elseif {[onchan $n $c] && ![onchansplit $n $c]} { set f [invite:add $f $c]
      } else { set s [invite:add $s $c] }
    }
  } else {
    foreach c [channels] {
      if {![matchattr $h -|fvlomn $c]} { continue }
      if {![botisop $c] && ![botishalfop $c]} { continue }
      if {[onchan $n $c] && ![onchansplit $n $c]} { continue }
      lappend s $c
    }
  }
  set u [join [lsort -dictionary $u]]; set f [join [lsort -dictionary $f]]; set o ""
  if {![string equal $u ""]} { lappend o "invite: no access or unknown channel(s): $u" }
  if {![string equal $f ""]} { lappend o "invite: unable to send invite for channel(s): $f" }
  if {[string equal $s ""]} { lappend o "invite: no channels found to invite you to."
  } elseif {[llength $s] > 2} {
    lappend o "invite: inviting you to [llength $s] channel(s), this will take a while - do NOT change nick."
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "invite: " } }
  if {[string equal $s ""]} { return 0 }
  set s [lsort -dictionary $s]; unset o
  foreach c $s {
    puthelp "INVITE $n $c"
    if {[string length $c] > 15} { lappend z "[string range $c 0 15].." } else { lappend z $c }
  }
  lappend o "invite: invited you to channel(s): [join $z]"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "invite: " } }
}


####################################################
# invite:add
####################################################
proc invite:add { l i } {
  if {[string equal [lsearch -exact $l $i] "-1"]} { lappend l $i }; return $l
}


####################################################
# invite:join
####################################################
bind join -|- * invite:join; bind rejn -|- * invite:join
proc invite:join { n u h c } {
  if {![validchan $c]} { return 0 }
  if {![validuser $h]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  if {[isbotnick $n]} { return 0 }
  if {[string equal $h *]} { return 0 }
  if {![invite:comchan $n]} { return 0 }
  foreach d [channels] {
    if {![botisop $d] && ![botishalfop $d]} { continue }
    if {[onchan $n $d] && ![onchansplit $n $d]} { continue }
    if {![matchattr $h omn|fvlomn $d]} { continue }
    if {![matchattr $h I|I $d]} { continue }
    puthelp "INVITE $n $d"
  }
}


####################################################
# invite:comchan
####################################################
proc invite:comchan { n } {
  set x 0
  foreach c [channels] {
    if {[onchan $n $c] && ![onchansplit $n $c]} { incr x }
    if {$x > 1} { return 0 }
  }
  if {[string equal $x 1]} { return 1 }
  return 0
}


####################################################
# set info for userflag.tcl
####################################################
set ::userflagdb(invite) {
  "invite.tcl: I=auto invite user"
}


####################################################
# set info for script.tcl
####################################################
set ::scriptdb(invite) {
  "provides invite command for known users to invite themselves to channels the bot is on, auto invite users that have joined the first common channel with the bot to all channels they have access to and have +I (chan or global) for."
}

