
# Pub News4
# by Ernst <ernst@studbox.uni-stuttgart.de>
# Ernst's eggdrop page:  http://www.sodre.net/ernst/eggdrop/
# 17-Aug-1998. Released: 9-May-1999

# Read news created with news4.tcl in a public chan

# !news      - shows all new news
# !news all  - shows all news
# !news <nr> - shows news item number <nr>

# Bind this to the flags you want people to have to use the !news command:
#  -|- means everyone can access
#  o|o means any OP can access it (channel of global)
#  n|- means only global owner can access 
#  ....
bind pub -|- !news news_pubread

set news_publastread 0

# Read the channel news in public
proc news_pubread { nick uhost hand chan arg} {
  global news botnick news_biggest_ts
  set chan [string tolower $chan]
  if {![info exist news($chan)]} {
    putlog "$nick tried to read !news in $chan, but there are no news here!"
    return 0
  }
  set arg [string trim $arg]
  if {$arg != ""} {
    if {[string compare [string tolower $arg] "all"] == 0} {
      if {[news_thereisnews $chan]} {
        putnotc $chan "News in $chan at [date] ..."
        news_print $chan 0 0 0 $chan
        putnotc $chan "--- end ---"
      } {
        putnotc $chan "Sorry, there are no news in $chan!"
      }
      return 1
    }
    set havearg [scan $arg "%d" which]
    if {$havearg} {
      if {($which < 1) || ($which > [news_count $chan])} {
        putnotc $nick "The item number must be between 1 and [news_count $chan]"
        return 0
      } else {
        news_print $chan 0 $which 0 $chan
        return 1
      }
    } {
      putlog "$nick gave unknown argument to !news: $arg"
      return 0
    }
  }
  # By the time we are here, it must have no arguments - display only new ones!
  if {[news_thereisnews $chan]} {
    global news_publastread
    if {$news_publastread >= $news_biggest_ts($chan)} {
      putnotc $chan "Sorry, there are no new news in $chan!"
    } else {
      putnotc $chan "New news in $chan at [date] [time] ..."
      news_print $chan 0 0 $news_publastread $chan
      putnotc $chan "--- end ---"
      set news_publastread $news_biggest_ts($chan)
    }
  } else {
    putnotc $chan "Sorry, there are no new news in $chan!"
  }
  return 1
}

