egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FileReader

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
Slaktarn
Halfop


Joined: 02 May 2007
Posts: 44

PostPosted: Wed Jun 03, 2009 5:01 am    Post subject: FileReader Reply with quote

Just a sampel script that read from a txt file

and output #channel $text
Back to top
View user's profile Send private message
tomekk
Master


Joined: 28 Nov 2008
Posts: 255
Location: Oswiecim / Poland

PostPosted: Wed Jun 03, 2009 5:31 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Slaktarn
Halfop


Joined: 02 May 2007
Posts: 44

PostPosted: Wed Jun 03, 2009 5:46 am    Post subject: Reply with quote

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 Smile 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 Smile
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Jun 03, 2009 6:42 am    Post subject: Reply with quote

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
View user's profile Send private message
tomekk
Master


Joined: 28 Nov 2008
Posts: 255
Location: Oswiecim / Poland

PostPosted: Wed Jun 03, 2009 9:04 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Jun 03, 2009 9:46 am    Post subject: Reply with quote

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
View user's profile Send private message
Slaktarn
Halfop


Joined: 02 May 2007
Posts: 44

PostPosted: Wed Jun 03, 2009 1:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Jun 03, 2009 1:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
Slaktarn
Halfop


Joined: 02 May 2007
Posts: 44

PostPosted: Wed Jun 03, 2009 1:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
tomekk
Master


Joined: 28 Nov 2008
Posts: 255
Location: Oswiecim / Poland

PostPosted: Wed Jun 03, 2009 5:26 pm    Post subject: Reply with quote

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 Smile

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
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Jun 03, 2009 8:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
Slaktarn
Halfop


Joined: 02 May 2007
Posts: 44

PostPosted: Thu Jun 04, 2009 2:20 am    Post subject: Reply with quote

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 Smile where shuld i edit it
Back to top
View user's profile Send private message
tomekk
Master


Joined: 28 Nov 2008
Posts: 255
Location: Oswiecim / Poland

PostPosted: Thu Jun 04, 2009 3:17 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Thu Jun 04, 2009 3:48 pm    Post subject: Reply with quote

Ahh, my apologies. Must have missed that.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber