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.

escaping special characters in strings

Old posts that have not been replied to for several years.
Locked
d
devilkin

escaping special characters in strings

Post by devilkin »

Hello all,

I'm fiddling a bit with TCL to create a script that allows the bot to trigger on any 'keyword' said in any sentence. Also adding 'add' and 'remove' binds for it.

Works like a charm, tho for some weird reason when using special characters, the bot replies them with { } around them, eg:
<devilkin> !addtextreact meow *purrrrr* ;)
<devilkin> meow
<thebot> *purrrrrrrr* {;)}
Any idea how I can deal with that? Same happens with the strings read from the datafile...

Code: Select all

bind pubm - "*" tr:reacttext
bind pub M !addtextreact tr:addreaction
bind pub M !deltextreact tr:delreaction

#
# Configuration
#
set textReactionsFile "data/textreactions/textreactions.txt"
set textReactionsSaveTimer 30

#
# This needs executing at start to read the current binds
#
if { [file exists $textReactionsFile]} {
        set fp [open $textReactionsFile r]
        while {[eof $fp] == 0} {
                set line [gets $fp]
                set trigger [lindex $line 0]
                set response [lrange $line 1 end]

                set textreactions($trigger) $response
        }
        close $fp
}

#
# Set a timer for saving
#
timer $textReactionsSaveTimer tr:savereactions

#
# Functions
#

proc tr:addreaction { nick uhost hand chan arg } {
        global textreactions

        set trigger [string tolower [lindex $arg 0]]
        set reaction [lrange $arg 1 end]
        set textreactions($trigger) $reaction

        putlog "TEXTTRIGGER: Added '$trigger' => '$reaction'"

}

proc tr:delreaction { nick uhost hand chan arg } {
        global textreactions

        set trigger [string tolower $arg]

        unset textreactions($trigger)
        putlog "TEXTTRIGGER: Deleted '$trigger'"
}

# TODO
proc tr:savereactions {} {
        global textReactionsSaveTimer

        putlog "SAVE"

        timer $textReactionsSaveTimer tr:savereactions

}

proc tr:reacttext {nick uhost hand chan arg } {
        global textreactions

        foreach trigger [array names textreactions] {
                if { [string match -nocase "*$trigger*" $arg] == 1 } {
                        putchan $chan $textreactions($trigger)
                }
        }
}

User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

lindex and lrange expect a list as input and you feed it a string. Use split to turn your string into a list...and join to make the list returned by 'lrange' a string.

Code: Select all

set args [split $arg]
lindex $args N...
Have you ever read "The Manual"?
d
devilkin

Post by devilkin »

Hmmm. okay.

Any hints on how i should save this in the data file?

Currently, there are things in it like

Code: Select all

"bad bot" bad humanoid
so that 'bad bot' is the trigger... Because I'm still getting the {}...
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

devilkin wrote: Any hints on how i should save this in the data file?
as user suggested, joined
d
devilkin

Post by devilkin »

Hmmm, ok.

Even with elaborate experimenting I couldn't get it right... so I changed my script like this, and it works perfectly:

Code: Select all

bind pubm - "*" tr:reacttext
bind pub M !addtextreact tr:addreaction
bind pub M !deltextreact tr:delreaction

#
# Configuration
#
set textReactionsFile "data/textreactions/textreactions.txt"
set textReactionsSaveTimer 30

#
# This needs executing at start to read the current binds
#
if { [file exists $textReactionsFile]} {
        set fp [open $textReactionsFile r]
        while {[eof $fp] == 0} {
                set line [gets $fp]

                if { [string length $line] > 0 } {
                        set splitpos [string first "|" $line]

                        if { $splitpos == -1 } {
                                putlog "TEXTSTRINGS: Invalid entry in datafile: '$line'"
                                exit
                        }
                        set trigger [string range $line 0 [expr $splitpos - 1]]
                        set response [string range $line [expr $splitpos + 1] end]

                        set textreactions($trigger) $response
                        putlog "TEXTSTRINGS: Added '$trigger' => '$response'"
                }
        }
        close $fp
}

#
# Set a timer for saving
#
timer $textReactionsSaveTimer tr:savereactions

#
# Functions
#

proc tr:addreaction { nick uhost hand chan arg } {
        global textreactions

        set splitpos [string first "=" $arg]

        if { $splitpos == -1 } {
                puthelp "NOTICE $nick :You can't do it that way Dave... Correct syntax is !addtextreact trigger = reaction..."
        } else {
                set trigger [string trim [string range $arg 0 [expr $splitpos - 1]]]
                set response [string trim [string range $arg [expr $splitpos + 1] end]]

                set textreactions($trigger) $response

                puthelp "NOTICE $nick :Oookaaay... I've added your weird idea... I'll now respond to it, too ;p"
                putlog "TEXTTRIGGER: Added '$trigger' => '$response' by $nick"
        }
}

proc tr:delreaction { nick uhost hand chan arg } {
        global textreactions

        set trigger [string tolower $arg]

        unset textreactions($trigger)
        putlog "TEXTTRIGGER: Deleted '$trigger'"
        puthelp "NOTICE $nick :Hmm.. What did you say? I think I've forgotten something..."
}

proc tr:savereactions {} {
        global textReactionsSaveTimer textReactionsFile textreactions

        set fp [open $textReactionsFile w]

        foreach trigger [array names textreactions] {
                puts $fp "$trigger|$textreactions($trigger)"
        }

        close $fp

        timer $textReactionsSaveTimer tr:savereactions

        putlog "TEXTSTRINGS: Saving strings..."
}

proc tr:reacttext {nick uhost hand chan arg } {
        global textreactions

        foreach trigger [array names textreactions] {
                if { [string match -nocase "*$trigger*" $arg] == 1 } {
                        putchan $chan $textreactions($trigger)
                }
        }
}
Last edited by devilkin on Wed Nov 17, 2004 4:55 pm, edited 1 time in total.
User avatar
KrzychuG
Master
Posts: 306
Joined: Sat Aug 16, 2003 2:51 pm
Location: Torun, Poland
Contact:

Post by KrzychuG »

Maybe that will help:

Code: Select all

set arg [string map {\\ \\\\ \{ \\\{ \} \\\} \[ \\\[ \] \\\] \" \\\"} $arg]
Que?
Locked