This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

FileReader

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
S
Slaktarn
Halfop
Posts: 44
Joined: Wed May 02, 2007 1:48 am

FileReader

Post by Slaktarn »

Just a sampel script that read from a txt file

and output #channel $text
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

not tested ;p, try:

Code: Select all

set out_chan "#channel"
set txt_file "myfile.txt"

bind pub -|- !read read_proc

proc read_proc { nick uhost hand chan arg } {
        global out_chan txt_file

        set take_me [open $txt_file r]
        set take_all [split [read $take_me] "\n"]
        close $take_me

        foreach txt_line $take_all {
                if {$txt_line != ""} {
                        putquick "PRIVMSG $out_chan :$txt_line"
                }
        }
}
putlog "read.tcl blah blah ..."
S
Slaktarn
Halfop
Posts: 44
Joined: Wed May 02, 2007 1:48 am

Post by Slaktarn »

tomekk wrote:not tested ;p, try:

Code: Select all

set out_chan "#channel"
set txt_file "myfile.txt"

bind pub -|- !read read_proc

proc read_proc { nick uhost hand chan arg } {
        global out_chan txt_file

        set take_me [open $txt_file r]
        set take_all [split [read $take_me] "\n"]
        close $take_me

        foreach txt_line $take_all {
                if {$txt_line != ""} {
                        putquick "PRIVMSG $out_chan :$txt_line"
                }
        }
}
putlog "read.tcl blah blah ..."
Ths for the quick replay but i wuld like it to read like hmm last line every x second :) or if it possibel maybe everytime the file is changed automatic

Sorry its my fult i shuld be more clear tin the first post whit what i mean :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

You could try to use the tail program along with pipes in tcl... Be wary of zombies though..

Code would look roughly like below. Be aware that this code depends on an external binary (tail).

Code: Select all

...
 set fid [open "| /urs/bin/tail -f /path/to/file" RDONLY]
 fconfigure $fid -blocking 0 -buffering line
 fileevent $fid readable [list readLine $fid]
...

proc readline {socket} {
 while {[gets $socket line] >= 0} {
  puthelp "PRIVMSG #channel :$line"
 }
 if {[eof $socket]} {
  close $socket
 }
}
Edit:
On second thought, that code could easily be adopted to remove the need for the external binary:

Code: Select all

...
 set fid [open "/path/to/file" RDONLY]
 fconfigure $fid -blocking 0 -buffering line
 seek $fid 0 end
 fileevent $fid readable [list readLine $fid]
...

proc readline {socket} {
 while {[gets $socket line] >= 0} {
  puthelp "PRIVMSG #channel :$line"
 }
}
Fixed "seek".
Last edited by nml375 on Wed Jun 03, 2009 9:55 am, edited 4 times in total.
NML_375
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

script is checking file mod time every X seconds, try it:

Code: Select all

# tomekk, http://forum.egghelp.org/viewtopic.php?t=16929

# output channel
set out_chan "#channel" 

# file
set txt_file "myfile.txt" 

# check interval (seconds)
set check_interval 10

############################################################
set last_file_mod 0

proc reader_timer { } {
	global check_interval

	auto_read_proc

	if {[string match *reader_timer* [utimers]] != 1} {
		utimer $check_interval reader_timer
	}
}

proc auto_read_proc { } { 
	global out_chan txt_file last_file_mod

	set file_mtime [file mtime $txt_file]

	set file_handle [open $txt_file r]
	set get_data [split [read $file_handle] "\n"]
	close $file_handle

	set last_line [join [lindex $get_data end]]

	if {$last_line == ""} {
		set last_line [join [lindex $get_data end-1]]
	}

	if {$last_file_mod != $file_mtime} {
		putquick "PRIVMSG $out_chan :$last_line"

		set last_file_mod $file_mtime
	}
}	

if {[string match *reader_timer* [utimers]] != 1} {
	utimer $check_interval reader_timer
}

putlog "auto-reader.tcl loaded"
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

A suggestion, don't join the result from your lindex operation, as you're working on a list of strings (lindex returns the contained item, not a single-entity list).
NML_375
S
Slaktarn
Halfop
Posts: 44
Joined: Wed May 02, 2007 1:48 am

Post by Slaktarn »

Hehe seems to work good but i got a strange error

[19:25] Tcl error in script for 'timer3':
[19:25] list element in quotes followed by ":" instead of space

I have try to search after it on googel but dident find mutch help
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

It's because of the incorrect join I mentioned in my previous post.

Code: Select all

set last_line [join [lindex $get_data end]]
###should be
set last_line [lindex $get_data end]
NML_375
S
Slaktarn
Halfop
Posts: 44
Joined: Wed May 02, 2007 1:48 am

Post by Slaktarn »

nml375 wrote:It's because of the incorrect join I mentioned in my previous post.

Code: Select all

set last_line [join [lindex $get_data end]]
###should be
set last_line [lindex $get_data end]
Still not working but it send on .rehash
Working i guss
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

nml375 wrote:A suggestion, don't join the result from your lindex operation, as you're working on a list of strings (lindex returns the contained item, not a single-entity list).
yeah, lame, my bad :)

i took again part of the source from the another script, there was [join [lrange .... ]] and i forgot,
anyway thank you

cheers !

@Slaktarn
yeah, fix it like nml375 wrote and should be OK
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

One thing strikes me, last_file_mod is not initialized upon startup, making the comparison "$last_file_mod != $file_mtime" possibly fail. I'd consider adding something like this to the script:

Code: Select all

if {![info exists ::last_file_mod]} {set ::last_file_mod 0}
NML_375
S
Slaktarn
Halfop
Posts: 44
Joined: Wed May 02, 2007 1:48 am

Post by Slaktarn »

nml375 wrote:One thing strikes me, last_file_mod is not initialized upon startup, making the comparison "$last_file_mod != $file_mtime" possibly fail. I'd consider adding something like this to the script:

Code: Select all

if {![info exists ::last_file_mod]} {set ::last_file_mod 0}
How do you mean :) where shuld i edit it
User avatar
tomekk
Master
Posts: 255
Joined: Fri Nov 28, 2008 11:35 am
Location: Oswiecim / Poland
Contact:

Post by tomekk »

nml375 wrote:One thing strikes me, last_file_mod is not initialized upon startup, making the comparison "$last_file_mod != $file_mtime" possibly fail. I'd consider adding something like this to the script:

Code: Select all

if {![info exists ::last_file_mod]} {set ::last_file_mod 0}
it is initialized, before timer proc

Code: Select all

############################################################
set last_file_mod 0
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Ahh, my apologies. Must have missed that.
NML_375
Post Reply