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.

Using 'set {my-var}' prob

Help for those learning Tcl or writing their own scripts.
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Using 'set {my-var}' prob

Post by rosc2112 »

I'm working on a rather old script for compressing logs called logzipper.tcl

Right off the top, there is a line that has:

Code: Select all

if {[info exists {logfile-suffix}]}
It's attempting to parse the bot's global configs for the 'logfile-suffix' but the - char is acting as a arithmetic subtract and so the var is coming out as 'logfile' only. The script was written for eggdrop 1.4.

I'm curious if this behaviour is a bug, or is expected? Is there a way to work around it in a clean manner (renaming the var is obviously an option, but perhaps there are other ways to fix this without renaming.)

I've done a bit of searching on the forums and google, but have not seen any similar issues. I'm not sure what to look for beyond the phrase 'logfile-suffix'

tia
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

I should have mentioned I'm using eggdrop 1.6.17.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

you are incorrect, there is nothing wrong with that expression

this will work too, it's equivalent to yours:

Code: Select all

if [info exists logfile-suffix]
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Didn't work for me..
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

rosc2112 wrote:Didn't work for me..
maybe so, but not for the reason you think
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

It's most likely you try to use a global var within a proc/namespace. In that case ::logfile-suffix should solve the issue.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Here's a couple of test scripts I made:

Code: Select all

# testscript 1
set myvar-fu "foo"
bind msg n testfu testfu
proc testfu { nick uhost hand text } {
global myvar-fu
puthelp "PRIVMSG $nick :$myvar-fu"
}

Code: Select all

#testscript 2
bind msg n testfu testfu
proc testfu { nick uhost hand text } {
set myvar2-fu "foo2"
puthelp "PRIVMSG $nick :$myvar2-fu"
}
It didn't matter where the var-fu thing was, in or out of a proc. In both cases, tcl only saw "myvar" or "myvar2" and chops off the -fu :)

test1
Tcl error [testfu]: can't read "myvar": no such variable

test2
Tcl error [testfu]: can't read "myvar2": no such variable

I've not seen any documentation that says vars cannot use a '-' char, the closest related documentation I have seen is regarding glob. I don't know if that applies to this situation.

Here is the code from logzipper.tcl which is what I'm trying to fix:

Code: Select all

proc lz_zip { } {
 global lz lz_recipient botnick {switch-logfiles-at} {keep-all-logs}
 if {[info exists {logfile-suffix}]} {
  global {logfile-suffix} } else {
  set {logfile-suffix} ".%d%b%Y"
 }
None of the vars with - chars work, with or without curly braces..

There is also this line further along:

Code: Select all

 if {${keep-all-logs} == 1} {
  set lz(searchstr) "*[strftime ${logfile-suffix} [expr [unixtime] - 86400]]"} else {
  set lz(searchstr) "*yesterday"
 }
I've temporarily worked around the problem by renaming the vars to not use dashes '-' and just use the values as found in the eggdrop.conf, but that's not very elegant or portable.

I'm not sure I understand what you mean by using ::var-fu , De Kus. Would you show me an example? Thanks for actually being helpful. :)
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

If you stated your problem clearly from the 1st post then you would've solved your problem way back in this topic. Your problem is in using a variable with a '-' in it. It's simple, see this example code:

Code: Select all

set myvar-fu "foo"

bind msg n testfu testfu

proc testfu {nick uhost hand text} {
 global myvar-fu
 if {[info exists myvar-fu]} {
  puthelp "PRIVMSG $nick :${myvar-fu}"
 }
}
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

the issue is something else:

set var $var-var
--> reads from 'var-var'
set var "$var-var"
--> reads from 'var' and adds '-var' as string
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

De Kus wrote:the issue is something else:

set var $var-var
--> reads from 'var-var'
set var "$var-var"
--> reads from 'var' and adds '-var' as string
nah, these are equivalent, with the latter effect
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

logzipper fix

Post by rosc2112 »

Sir_Fz wrote:If you stated your problem clearly from the 1st post then you would've solved your problem way back in this topic. Your problem is in using a variable with a '-' in it. It's simple, see this example code:

Code: Select all

set myvar-fu "foo"

bind msg n testfu testfu

proc testfu {nick uhost hand text} {
 global myvar-fu
 if {[info exists myvar-fu]} {
  puthelp "PRIVMSG $nick :${myvar-fu}"
 }
}
Ok, I think I'm starting to understand how this works (not exactly sure *why* it works this way) but it seems that using myvar-fu with quotes "" changes the behavior (as in, 'set lz(searchstr) "*[strftime ${logfile-suffix} [expr [unixtime] - 86400]]"}' is the section of the original code that was producing the error.

The original script was also trying to set the global 'logfile-suffix' with 'if {[info exists logfile-suffix]}' but this didn't work at all unless I put 'logfile-suffix' into the global section anyway! Silly =)

Apparently the only real fix it needed was to add logfile-suffix to the global var declaration.. dah...

Anyway, here's my fixed version for anyone else that might find it useful:

Code: Select all

# LogZipper             2.0 by Baerchen, May 2001
#                              for eggdrop 1.4.x+
#                       baerchen@germany-chat.net
#
# HISTORY
#
# 2.0.1-rosc    - Updated by Rosc at rosc2112.net to work with eggdrop 1.6.x (April 2006)
#               - Uses bzip2 now, I see no point in making a tarball for a single file.
#               - Added bind msg logzip to run the script manually (mainly for testing purposes)
#
# 2.0
# Baerchen      - Complete rewrite: 1.01f did not really what it was supposed
#                 to do :/ 
#               - Added possibility of sending various (zipped) logs
#                 to various recipients
#               - Now works with keep-all-logs set to 1
#
# 1.01f
# Baerchen      First public release
#
# CONFIGURATION

set lz(logloc) "logs/"
set lz(duration) 90
set lz(sendlogs) 0
set lz_recipient(1) "user@mail log1.log #channel1.log"

# CODEBASE BELOW

bind evnt - logfile lz_wait
proc lz_wait {logfile} {timer 1 lz_zip}
putlog "LogZipper 2.0 by Baerchen (keeping logs for $lz(duration) days)"

bind msg n logzip msg:lz_zip
proc msg:lz_zip {nick uhost hand text} {
lz_zip
}

proc lz_zip { } {
        global lz lz_recipient botnick switch-logfiles-at keep-all-logs logfile-suffix
        if {![info exists logfile-suffix]} {
# set logfile-suffix is only needed as a fall-back, in some off chance that the global var isn't found..
                set logfile-suffix ".%d-%b-%Y"
        }
        set lz(date) [strftime "%b-%d-%Y" [expr [clock seconds] - 86400]]
        set lz(max) [expr [clock seconds] - ($lz(duration) * 86400) + 60]
        set lz(filestomail) ""
        set i 0; set j 0; set k 0
        if {(${keep-all-logs} == 0) && (${switch-logfiles-at} == 2500)} {
                putlog "Logzipper: Invalid configuration: log files do not switch. You might want to correct that."
                return
        }
        if {${keep-all-logs} == 1} {
                set lz(searchstr) "*[strftime ${logfile-suffix} [expr [unixtime] - 86400]]"
        } else {
                set lz(searchstr) "*yesterday"
        }
        set lz(filelist) [glob -directory $lz(logloc) -nocomplain -- $lz(searchstr)]
        foreach e $lz(filelist) {
                if {$e == ""} {continue}
                        set f "[string range $e 0 [string last . $e]]$lz(date).bz2"
                        set lz(zerror) [catch {exec bzip2 -z $e}]
                        if {$lz(zerror)} {
                                putlog "LogZipper: An error occured while trying to zip $e"
                                continue
                        }
                set lz(ferror) [catch {file delete -force $e}]
                if {$lz(ferror)} {
                        putlog "LogZipper: An error occured while trying to delete $e"
                }
                lappend lz(filestomail) [string range $f [expr [string last / $f] +1] end]
                incr i
        }
        set lz(filelist) [glob -directory $lz(logloc) -nocomplain -- *.???-??-????.bz2]
        foreach e $lz(filelist) {
                if {$e == ""} {continue}
                if {[file mtime $e] <= $lz(max)} {
                        set lz(ferror) [catch {file delete -force $e}]
                        if {$lz(ferror)} {
                                putlog "LogZipper: An error occured while trying to delete $e"
                                continue
                        }
                        incr j
                }
        }
        set lz_status "LogZipper: zipped $i logs, deleted $j expired logs. "
        if {$lz(sendlogs)} {
                foreach e $lz(filestomail) {
                        if {$e == ""} {continue}
                        foreach el [array names lz_recipient] {
                                set rec [lindex $lz_recipient($el) 0]
                                set what [lrange $lz_recipient($el) 1 end]
                                foreach ele $what {
                                        if {[string match -nocase *$ele* $e]} {
                                                set lz(merror) [catch { exec uuencode $lz(logloc)$e $e | mail $rec }]
                                                if {$lz(merror)} {
                                                        putlog "LogZipper: An error occured while trying to uuencode or 
                                                        continue
                                                }
                                                incr k
                                        }
                                }  
                        }
                }
                append lz_status "Sent $k logs via email."
        }
        putlog "$lz_status"
}
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Either add it to global or use ${::logfile-suffix}.
Z
Zircon
Op
Posts: 191
Joined: Mon Aug 21, 2006 4:22 am
Location: Montreal

Post by Zircon »

Hi rosc2112

I have a question about ur updated logzipper.tcl for 1.6.17. I used ur version of this script. It s zippin the file, but it doent send it coz it looks for wrong file.

I edited the script to make these settings :

set lz(logloc) "logs/quizz/culture/"
set lz(duration) 1
set lz(sendlogs) 1
set lz_recipient(1) "zircon@gmail.com culture"

and in the config file i have these :

logfile jpk #culture "logs/quizz/culture/culture"
set keep-all-logs 1
set logfile-suffix ".%Y%b%d.log"

When i make the test manually with messaging the bot with logzip, i get these messages in the party line

LogZipper: An error occured while trying to uuencode or mail culture.2007Feb04.Feb-04-2007.bz2

LogZipper: zipped 1 logs, deleted 0 expired logs. Sent 0 logs via email.

So the script made the zip of culture.2007Feb04.log to culture.2007Feb04.log.bz2 as it should be, but couldnt sent it in a mail, cause it was trying to send the file culture.2007Feb04.Feb-04-2007.bz2, which doesnt exist.

Please, do u have some idea to solve this problem, to make the script send the right file ?

Thanks in advance Smile

PSssttt : by the way, what to do to make the script leave the input file there ( the unzipped file ), and not delete it ?
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

I dont use the mail feature, sorry. Try adding debug "putcmdlog" stuff in the script to get the real error messages that came from your shell, apparently there is a problem with mail on your shell, not the script.
Z
Zircon
Op
Posts: 191
Joined: Mon Aug 21, 2006 4:22 am
Location: Montreal

Post by Zircon »

Hello rosc2112

Thanks for replying. I think that the mail feature is working, coz i receive a mail, but empty, no file attached too.

The problem is that the script is trying to send the file culture.2007Feb04.Feb-04-2007.bz2 which doesnt exist. The right file to send is culture.2007Feb04.log.bz2.

My problem is that when the script want to send the file, it adds "Feb-04-2007" at the end in the name of the right file.

Thanks
Post Reply