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.

Double Output...

Old posts that have not been replied to for several years.
Locked
e
elajt

Double Output...

Post by elajt »

I got this little problem, the script tells me the message twice, but the $contents variable is only in the first message.

Here is the output:

[02:18:56] <CopyWrong> ?date
[02:18:57] <Elajt> CopyWrong: Sun Oct 27 02:22:44 CET 2002
[02:18:58] <Elajt> CopyWrong:

Here is the source:

Code: Select all

bind pub - ?date pub:date

proc pub:date {nick uhost hand chan text} {
set input [open "|/bin/date" r]

while {![eof $input]} {

        catch {set contents [gets $input]}
        putserv "PRIVMSG $chan :$nick: $contents"   
}} 
*NOTE:
This wont happen if i replaces row 9 with this:
putserv "PRIVMSG $chan :$contents"
*NOTE

How can i stop that?
Thanx!
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: Double Output...

Post by strikelight »

elajt wrote:Here is the source:

Code: Select all

bind pub - ?date pub:date

proc pub:date {nick uhost hand chan text} {
set input [open "|/bin/date" r]

while {![eof $input]} {

        catch {set contents [gets $input]}
        putserv "PRIVMSG $chan :$nick: $contents"   
}} 
*NOTE:
This wont happen if i replaces row 9 with this:
putserv "PRIVMSG $chan :$contents"
*NOTE
The reason for this is because of the irc protocol, not your code...
You can't send a blank message on irc, thus if $contents is empty, you would be attempting to send an empty message to irc, which you can't do.. by adding $nick to the output, the output to server is no longer empty.

The problem lays in the output relayed from the program you are executing... Thus you should check if $contents is empty or not before sending output to server.

Hope this helps.
t
tainted
Master
Posts: 239
Joined: Sun May 12, 2002 8:00 pm
Location: chicago
Contact:

Post by tainted »

Code: Select all

catch {set input [exec /bin/date]}
then just putserv $input, without the while loop. Since date only returns one line, you shouldn't need the while loop at all. (didn't test it myself)

Also, you didn't close the opened file at the end, which will create zombies.
e
elajt

It works now

Post by elajt »

Thanx! I deleted that while loop, so now it works.. :)
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Re: It works now

Post by egghead »

elajt wrote:Thanx! I deleted that while loop, so now it works.. :)
Actually, the problem is (was) due to the fact that you used a "while {![eof $input]}" construction. The eof is only set AFTER the last read. So after a last (unsuccessfull) read, the eof is set and the while loop stops.

If there is a one line file named "testfile.dat", with the one line being "hello world" then running this piece:

Code: Select all

set fileid [open testfile.dat r]
while {![eof $fileid]} { 
   puts "LINE: [gets $fileid]"
} 
close $fileid
will produce as output:
LINE: hello world
LINE:
This file reading business is a very good candidate for the TCL FAQ :)

Additionally and more serious is the fact that you use /bin/date to retrieve the date. On my shell system your code or the [exec /bin/date] against a [clock seconds] combined with a [clock format] runs as:
2400 microseconds per iteration (/bin/date)
23 microseconds per iteration (clock seconds & clock format)
e
elajt

Allright, but there is still one problem....

Post by elajt »

I changed my code to this:

Code: Select all

bind pub - ?date pub:date

proc pub:date {nick uhost hand chan text} {
set input [open "|/bin/date" r]
catch {set contents [gets $input]}
putserv "PRIVMSG $chan :$nick: $contents"
}
But the date will hang up, hm, how should i explain this, hm, if i type ps ax, at my shell, it will display that the program has become defunct:
#elajt: ps ax |grep date
7621 ? Z 0:00 [date <defunct>]

This is without doubt a security risk, anyone knows how to prevent it?

Thanx //Elajt
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Yes.

When you open a application, you can read from it an so on.

Afterwards, the application space needs cleraring.

When using the "open" command to do this, you should treak the file socket, the file as opening a normal file. It needs closing.

As allready mentioned, is there realy a need to to use the "/bin/date" program?

Tcl is much faster with it's builtin code, and it doesn't give you these hectic problems.

Tcl can also be told to return the date the the format you wan't it.

Another sugestion, is to use the "exec" command with programs. This will rmeove much of your issues.
Locked