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 

read from a file ?

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Gemster
Halfop


Joined: 04 Oct 2010
Posts: 51

PostPosted: Sun Mar 06, 2011 2:02 pm    Post subject: read from a file ? Reply with quote

Hi, how do i make this read from the file and output it ?

Code:

bind raw - 322 channel:list
proc channel:list {from keyword text} {
set cl [open scripts/chanlist.txt a+]
puts $cl "[lindex $text 1]"
close $cl
}

bind pub - ".chanlist" chanlist
proc chanlist {nick uhost hand chan text} {
putserv "list"
}


I need it to msg channel ###1 all lines in that file.

Thanks
Gemster
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Mar 06, 2011 2:52 pm    Post subject: Reply with quote

One simple way of doing it is like this:
Code:
...
proc chanlist {nick uhost hand chan text} {
  if {[catch {open "scripts/chanlist.txt" "RDONLY"} fd]} {
    puthelp "PRIVMSG ###1 :Unable to open the channel-list"
    putlog "Error during open: $fd"
    return 0
  }
  fconfigure $fd -blocking 0
  while {![eof $fd] && ![fblocked $fd]} {
    if {[gets $fd data] > 0} {
      puthelp "PRIVMSG ###1 :$data"
    }
  }
  close $fd
}

Some prefer using read, split, and foreach instead, which also works..

Edit: Fixed typo spotted by caesar
_________________
NML_375, idling at #eggdrop@IrcNET


Last edited by nml375 on Sun Mar 06, 2011 6:38 pm; edited 1 time in total
Back to top
View user's profile Send private message
Gemster
Halfop


Joined: 04 Oct 2010
Posts: 51

PostPosted: Sun Mar 06, 2011 3:10 pm    Post subject: Reply with quote

Thanks nml375, but thats kinda too complicated for me to understand atm lol, im new to tcl scripting.

Is it possible to use the code i posted but will a read command that will msg channel ###1 the output ?

The file is written as a list like this:

#channel1
#channel2
#channel3

Thanks
Gemster
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Mar 06, 2011 4:39 pm    Post subject: Reply with quote

That is what my code will do (provided you use the bindning from your original post, simply replacing the "chanlist" proc).
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Sun Mar 06, 2011 6:02 pm    Post subject: Reply with quote

Shouldn't it be 'fblocked $fd' and not 'fblocked $df'?
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Mar 06, 2011 6:39 pm    Post subject: Reply with quote

caesar:
True, thanks for pointing out the typo. Updated the original post.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Gemster
Halfop


Joined: 04 Oct 2010
Posts: 51

PostPosted: Sun Mar 06, 2011 6:53 pm    Post subject: Reply with quote

I still dont understand that script you posted lol

Ill i want is a command that will read from a file ?

Thanks
Gemster
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Mar 06, 2011 7:05 pm    Post subject: Reply with quote

Add the binding
Code:
bind pub - ".chanlist" chanlist

as I said, and that's exactly what it does
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Gemster
Halfop


Joined: 04 Oct 2010
Posts: 51

PostPosted: Sun Mar 06, 2011 7:14 pm    Post subject: Reply with quote

I know yours does that nml375, but id like to learn this but i cant as i dont understand your script.

The script i posted i made my self and took a few hours as for googleing everything but the examples i seen for "gets" i just cant get them to work.

The main reason i need to learn is that there are a lot of script id like to make but they require a file made and read from , this is why i need to learn myself as simple as possible.

Thanks
Gemster
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Mon Mar 07, 2011 2:24 am    Post subject: Reply with quote

@Gemster : You don't need to google anything, just have a look on Tcl Commands. All the stuff are explained in there.

Also, if you checked the Tcl FAQ you would have found Basic File Operations where you would have found some interesting stuff to read on this topic.

Regarding your first post, you don't need to add " in:
Code:

puts $cl "[lindex $text 1]"

unless you want to add two or more variables, like:
Code:

puts $cl "$bla $blah"

or some text:
Code:

puts $cl "bla blah"


@nml375 : no problem. keep up the good work. Smile
_________________
Once the game is over, the king and the pawn go back in the same box.
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Mon Mar 07, 2011 2:46 pm    Post subject: Reply with quote

Since you're not looking for someone just writing code for you, I believe this thread rather belongs to the "Scripting Help" forum (I'll move it there once I'm done with this post)

As for the code, it's not that complex; I'll walk you through it..
Code:
if {[catch {open "scripts/chanlist.txt" "RDONLY"} fd]} {
  puthelp "PRIVMSG ###1 :Unable to open the channel-list"
  putlog "Error during open: $fd"
  return 0
}

This simply opens the file in a safe manner. "catch" simply catches any errors thrown by open, allowing us to send an error to the person using the command, and logging the actual error message for further investigation.
If the open-command succeeds, a file descriptor is stored in the variable fd instead...

Code:
fconfigure $fd -blocking 0

Sets the file descriptor to "non-blocking" mode; which means that no operations on this descriptor may block (blocking == not returning / completing immediately). Usually not needed to be set explicitly with normal files, but it's a good practice in most cases (makes sure your code won't cause your eggie to stop responding in case of I/O issues)

Code:
while {![eof $fd] && ![fblocked $fd]} {
...
}

Run a loop which checks the EOF (End of File) and fblocked (blocking operation, see above) status of the last operation (in this case; reading a line from the file). EOF will be true if we've reached the end of the file. Fblocked would be true if the last call to "gets" would not return a complete line - not likely to happen with files, but once again good practice (especially if you start reading sockets, serial ports, or pipes in the future).
Simply put, this will make sure we keep on reading the file until we've reached the end of it.

Code:
..
if {[gets fd data] > 0 } {
  ...
}

Try to read one line from the file, and store it in the variable "data". Also check how many characters we actually read (0 means an empty line, anything less than 0 means there was an error, such as EOF). If we've read something, then we should write it to the channel..

Code:
...
puthelp "PRIVMSG ###1 :$data"
...

Send the line of text we've read into the channel ###1.
_________________
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 -> Scripting Help 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