#by wiebe @ QuakeNet



#help invite msg
bind msgm mn|fvlomn "help invite" c_invite:help:msg

proc c_invite:help:msg { nick uhost handle text } {
  lappend output "Usage: invite \[<chan1> <chan2> .. <chanN>\]"
  lappend output "Without params, invites you to all channels you have access on."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#invite msg
bind msg mn|fvlomn invite c_invite:msg

proc c_invite:msg { nick uhost handle text } {
  if { [string equal $text ""] } { set text [channels] }
  set text [lsort [split $text]]
  set chans ""
  foreach chan $text {
    if { ![validchan $chan] } { continue }
    if { ![botisop $chan] && ![botishalfop $chan] } { continue }
    if { ![matchattr $handle mn|fvlomn $chan] } { continue }
    if { [onchan $nick $chan] } { continue }
    lappend chans $chan
  }
  if { [string equal $chans ""] } {
    lappend output "Unable to send any invites."
  } else {
    lappend output "Inviting you to [llength $chans] channel(s). This may take a while, do NOT change nick."
    if { [catch {set x [cnotice $nick $output]} error] || !$x } {
      foreach t $output { puthelp "NOTICE $nick :$t" }
    }
    unset output
    set chans2 ""
    foreach chan $chans {
      puthelp "INVITE $nick $chan"
      if { [string length $chan] > "10" } { set chan [string range $chan 0 9].. }
      lappend chans2 $chan
    }
    set chans [join $chans2]
    if { [string length $chans] > "400" } {
      set chans "[string range $chans 0 399]"
      set chans "[join [lrange [split $chans] 0 end-1]] (truncated)"
    }
    lappend output "Invited you to: $chans"
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

