| View previous topic :: View next topic |
| Author |
Message |
Slaktarn Halfop
Joined: 02 May 2007 Posts: 44
|
Posted: Wed Jun 03, 2009 5:01 am Post subject: FileReader |
|
|
Just a sampel script that read from a txt file
and output #channel $text |
|
| Back to top |
|
 |
tomekk Master

Joined: 28 Nov 2008 Posts: 255 Location: Oswiecim / Poland
|
Posted: Wed Jun 03, 2009 5:31 am Post subject: |
|
|
not tested ;p, try:
| Code: | 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 ..."
|
|
|
| Back to top |
|
 |
Slaktarn Halfop
Joined: 02 May 2007 Posts: 44
|
Posted: Wed Jun 03, 2009 5:46 am Post subject: |
|
|
| tomekk wrote: | not tested ;p, try:
| Code: | 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  |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Jun 03, 2009 6:42 am Post subject: |
|
|
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: | ...
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: | ...
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". _________________ NML_375, idling at #eggdrop@IrcNET
Last edited by nml375 on Wed Jun 03, 2009 9:55 am; edited 4 times in total |
|
| Back to top |
|
 |
tomekk Master

Joined: 28 Nov 2008 Posts: 255 Location: Oswiecim / Poland
|
Posted: Wed Jun 03, 2009 9:04 am Post subject: |
|
|
script is checking file mod time every X seconds, try it:
| Code: | # 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"
|
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Jun 03, 2009 9:46 am Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Slaktarn Halfop
Joined: 02 May 2007 Posts: 44
|
Posted: Wed Jun 03, 2009 1:28 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Jun 03, 2009 1:31 pm Post subject: |
|
|
It's because of the incorrect join I mentioned in my previous post.
| Code: | set last_line [join [lindex $get_data end]]
###should be
set last_line [lindex $get_data end] |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Slaktarn Halfop
Joined: 02 May 2007 Posts: 44
|
Posted: Wed Jun 03, 2009 1:33 pm Post subject: |
|
|
| nml375 wrote: | It's because of the incorrect join I mentioned in my previous post.
| Code: | 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 |
|
| Back to top |
|
 |
tomekk Master

Joined: 28 Nov 2008 Posts: 255 Location: Oswiecim / Poland
|
Posted: Wed Jun 03, 2009 5:26 pm Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Jun 03, 2009 8:37 pm Post subject: |
|
|
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: | | if {![info exists ::last_file_mod]} {set ::last_file_mod 0} |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Slaktarn Halfop
Joined: 02 May 2007 Posts: 44
|
Posted: Thu Jun 04, 2009 2:20 am Post subject: |
|
|
| 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: | | if {![info exists ::last_file_mod]} {set ::last_file_mod 0} |
|
How do you mean where shuld i edit it |
|
| Back to top |
|
 |
tomekk Master

Joined: 28 Nov 2008 Posts: 255 Location: Oswiecim / Poland
|
Posted: Thu Jun 04, 2009 3:17 am Post subject: |
|
|
| 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: | | if {![info exists ::last_file_mod]} {set ::last_file_mod 0} |
|
it is initialized, before timer proc
| Code: | ############################################################
set last_file_mod 0
|
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Jun 04, 2009 3:48 pm Post subject: |
|
|
Ahh, my apologies. Must have missed that. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|