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

####################################################
# say:help:pubm
####################################################
bind pubm omn|omn "% ${botnet-nick} help say" say:help:pubm
proc say:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "say: usage say \[<chan>\] <text>"
  lappend o "say: says text"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "say: " } }
  putloglev c $c "help: $n $u $h $c say"
  return 1
}


####################################################
# say:help:msg
####################################################
bind msgm omn|omn "help say" say:help:msgm
proc say:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "say: usage say <chan> <text>"
  lappend o "say: says text on chan"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "say: " } }
  putcmdlog "($n!$u) !$h! help say"
  return 1
}


####################################################
# say:pubm
####################################################
bind pubm omn|omn "% ${botnet-nick} say" say:pubm
bind pubm omn|omn "% ${botnet-nick} say *" say:pubm
proc say:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  set o [say:say $h $t $c $n]
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "say: " } }
  putloglev c $c "say: $n $u $h $c $t"
  return 1
}


####################################################
# say:msg
####################################################
bind msg omn|omn say say:msg
proc say:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  set o [say:say $h $t "" $n]
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "say: " } }
  return 1
}


####################################################
# say:dcc
####################################################
bind dcc -|- say say:dcc
proc say:dcc { h i t } {
  if {![valididx $i]} { return 0 }
  set c [lindex [split [console $i]] 0]
  if {![validchan $c]} { set c "" }
  set o [say:say $h $t $c ""]
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# say:say
####################################################
proc say:say { h t c n } {
  set t [split $t]; set d [lindex $t 0]; set m ""; set o ""
  if {[string equal $c ""] || [string match {[#&]*} $d]} {
    set c $d; set t [join [lrange $t 1 end]]
  }
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  set t [string trimright [join $t] " "]
  if {[validchan $c]} { set m [lindex [split [getchanmode $c]] 0] }
  if {[string equal $c ""] || [string equal $t ""]} {
    lappend o "say: usage say \[<chan>\] <message>"
  } elseif {![validchan $c] || ![matchattr $h omn|omn $c]} {
    lappend o "say: no access or unknown channel $c"
  } elseif {![botonchan $c]} {
    lappend o "say: I am not on $c"
  } elseif {[string match *m* $m] && ![botisop $c] && ![botishalfop $c] && ![botisvoice $c]} {
    lappend o "say: channel $c is moderated"
  } else {
    if {[string match *c* $m] } { set t [stripcodes bcru $t] }
    if {[string equal [info procs privmsg] ""]} { puthelp "PRIVMSG $c :$t"
    } else { privmsg $c $t puthelp }
    if {![onchan $n $c]} { lappend o "say: said '$t\017' on $c" }
  }
  return $o
}


set scriptdb(say) {
  "provides say command for sending sayions to channels through the bot"
}

