####################################################
# by wiebe @ QuakeNet
#
# script provides act command to make the bot do actions on channels
# actions are /me slaps nick, /describe #chan slaps nick etc.
# users with +o and higher (chan or global) can use the command
#
#   in channel:   botnet-nick act [<chan>] <text>
#   in private:   /msg bot act <chan> <text>
#   on partyline: act [<chan>] <text>
#
# script can use: irc.tcl whichchan.tcl script.tcl
#
####################################################


####################################################
# act:help:pubm
####################################################
bind pubm omn|omn "% ${::botnet-nick} help act" act:help:pubm
proc act:help:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "act: usage act \[<chan>\] <text>"
  lappend o "act: performs the specified action"
  act:cnotice $n $o
  putloglev c $c "help: $n $u $h $c act"
  return 1
}


####################################################
# act:help:msg
####################################################
bind msgm omn|omn "help act" act:help:msgm
proc act:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "act: usage act <chan> <text>"
  lappend o "act: performs the specified action on chan"
  act:cnotice $n $o
  putcmdlog "($n!$u) !$h! help act"
  return 1
}


####################################################
# act:pubm
####################################################
bind pubm omn|omn "% ${::botnet-nick} act" act:pubm
bind pubm omn|omn "% ${::botnet-nick} act *" act:pubm
proc act: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 [act:act $h $t $c $n]
  act:cnotice $n $o
  putloglev c $c "act: $n $u $h $c $t"
  return 1
}


####################################################
# act:msg
####################################################
bind msg omn|omn act act:msg
proc act:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  set o [act:act $h $t "" $n]
  act:cnotice $n $o
  return 1
}


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


####################################################
# act:act
####################################################
proc act:act { h t c n } {
  set t [split $t]; set d [lindex $t 0]; set m ""; set o ""
  if {$c == "" || [string match {[#&]*} $d]} { set c $d; set t [join [lrange $t 1 end]] }
  if {![validchan $c] && [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 {$c == "" || $t == ""} {
    lappend o "act: usage act \[<chan>\] <message>"
  } elseif {![validchan $c] || ![matchattr $h omn|omn $c]} {
    lappend o "act: no access or unknown channel $c"
  } elseif {![botonchan $c]} {
    lappend o "act: I am not on $c"
  } elseif {[string match *m* $m] && ![botisop $c] && ![botishalfop $c] && ![botisvoice $c]} {
    lappend o "act: channel $c is moderated"
  } else {
    if {[string match *c* $m] } { set t [stripcodes bcru $t] }
    act:action $c $t
    if {![onchan $n $c]} { lappend o "act: performed action '$t\017' on $c" }
  }
  return $o
}


####################################################
# act:cnotice
####################################################
proc act:cnotice { n o } {
  if {[info procs cnotice] == ""} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "act: " } }
}


####################################################
# act:action
####################################################
proc act:action { c t } {
  if {[info procs action] == ""} { puthelp "PRIVMSG $c :\001ACTION$t\001" } else { action $c $t puthelp }
}


####################################################
# set info for script.tcl
####################################################
set ::scriptdb(act) {
  "Provides act command for sending actions to channels through the bot"
}

