#by wiebe @ QuakeNet

#CPRIVMSG <nick> <channel> :<text>
#CNOTICE <nick> <channel> :<text>
#http://ircu.sourceforge.net/release.2.10.02-cprivmsg.html

#this script provides cprivmsg and cnotice procs

#example to use in a script:
#lappend output "message 1"
#lappend output "message 2"
#lappend output "message N"
#if { [catch {set x [cnotice $nick $output]} error] || !$x } {
#  foreach t $output { puthelp "NOTICE $nick :$t" }
#}
#the error checking ensures that this will always work,
#even if you nolonger have this script loaded, the message will be send the normal way

#cprivmsg nick text ?queue?
#queue is puthelp (default), putserv, putquick
#returns 1 if succesful, else returns 0, the message should be send by other means
#text is a list!, so you can send multiple messages with one cnotice command
proc cprivmsg { nick text {queue ""}} {
  if { [string equal $queue ""] } { set queue puthelp }
  if { [string equal [lsearch -exact "puthelp putserv putquick" $queue] -1] } { set queue puthelp }
  if { ![onchan $nick] || (![botisop] && ![botishalfop] && ![botisvoice]) } { return 0 }
  foreach chan [channels] {
    if { ([botisop $chan] || [botishalfop $chan] || [botisvoice $chan]) && [onchan $nick $chan] } {
      foreach t $text {
        if { ![string equal $t ""] } { $queue "CPRIVMSG $nick $chan :$t" }
      }
      return 1
    }
  }
  return 0
}



#cnotice nick text ?queue?
#queue is puthelp (default), putserv, putquick
#returns 1 if succesful, else returns 0, the notice should be send by other means
proc cnotice { nick text {queue ""}} {
  if { [string equal $queue ""] } { set queue puthelp }
  if { [string equal [lsearch -exact "puthelp putserv putquick" $queue] -1] } { set queue puthelp }
  if { ![onchan $nick] || (![botisop] && ![botishalfop] && ![botisvoice]) } { return 0 }
  foreach chan [channels] {
    if { ([botisop $chan] || [botishalfop $chan] || [botisvoice $chan]) && [onchan $nick $chan] } {
      foreach t $text {
        if { ![string equal $t ""] } { $queue "CNOTICE $nick $chan :$t" }
      }
      return 1
    }
  }
  return 0
}

