## LiVE.tcl checks for a file in your public_html directory
## every x seconds and if it exists it'll show the contents
## of it in the channel you specify.
## 
## Why would you need such a thing? Well.. it can be handy
## if you want to send messages to a channel through webpages.
## All you got to do is built a form that will create this
## text file, and the bot will show the contents to the channel
## and then delete the file
##
## Created by xiv (xiv@innocent.com) - version 1.0
## Please feel free to enhance this file and make better versions
## (as long as you give me credit!) :)
##
## Commands : !webstop - will stop the counter, and thus this script
##            !webstart - will reset the counter
##            !webstat - will tell you if weblive is on or off

## Full path where the file is situated - don't forget to chmod 777 that dir ##
set file "/myhome/public_html/out/irc.out"

## Is it activated when booted up? ##
set status 1

## Timer when bot checks if the file exists (in seconds) ##
set cl_timer 5

## Channel text will be sent to
set channel "#mychannel"

##### DO NOT CHANGE BELOW ######

# Binds
bind pub m|m !webstop pub_stopweb
bind pub m|m !webstart pub_startweb
bind pub -|- !webstat pub_statweb

proc gettext {} {
   global status file channel cl_timer
   if {$status == 1} {
     if {![file exists $file]} {
        utimer $cl_timer gettext
        return 0
     }
     set count "0"
     set df [open $file r]
     while {![eof $df]} {
       set def [gets $df]
       if {$def != ""} {
         incr count
         putserv "PRIVMSG $channel :$def"
       }
     }
     close $df
     exec rm -f $file
  }
  utimer $cl_timer gettext
}

proc pub_stopweb {nick uhost hand chan arg} {
  global status
  if {$status == 1} {
    set status 0
    foreach timer [timers] {
      if {[string match *gettext* $timer]} {
        killtimer [lindex $timer 2]
	}
    }
    putserv "PRIVMSG $chan :WebLiVe turned OFF"
    return 1
  }
  putserv "PRIVMSG $chan :WebLiVe already turned OFF"
}

proc pub_startweb {nick uhost hand chan arg} {
  global status file cl_timer
  if {$status == 1} {
    putserv "PRIVMSG $chan :WebLiVe already turned ON"
    return 0
  }
  if {[file exists $file]} {
    exec rm -f $file
  }
  set status 1
  utimer $cl_timer gettext
  putserv "PRIVMSG $chan :WebLiVe turned ON"
}

proc pub_statweb {nick uhost hand chan arg} {
  global status
  if {$status == 1} {
    putserv "PRIVMSG $chan :WebLiVe is currenly ON"
    return 0
  }
  if {$status == 0} {
    putserv "PRIVMSG $chan :WebLiVe is currenly OFF"
    return 0
  }
}

if {$status == 1} {
  utimer $cl_timer gettext
}
