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 

"Tip of the day"
Goto page Previous  1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Tcl FAQ
View previous topic :: View next topic  
Author Message
slennox
Owner


Joined: 22 Sep 2001
Posts: 593

PostPosted: Sat Jul 02, 2005 4:54 pm    Post subject: Reply with quote

I agree--let's call it the Tcl FAQ Smile The big posts appearing here should have their own topics, but I suppose having to request a mod create the topic discourages this. So I've created a new FAQ Maintainers group whose members can create new topics. The members added are people who've made large posts here:

^DooM^
Sir_Fz
greenbear
Linux
sKy
demond

You should be able to split a topic you've posted here into a separate one (if you can't, let me know and I'll check the permissions).
Back to top
View user's profile Send private message Visit poster's website
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sat Jul 02, 2005 6:21 pm    Post subject: Reply with quote

Nice, that's a good idea. And it works fine with me, I can split Smile
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
sKy
Op


Joined: 14 Apr 2005
Posts: 194
Location: Germany

PostPosted: Sun Jul 03, 2005 7:30 pm    Post subject: Reply with quote

Oh well, it is like some kind of 'honours' that you put my name with all those other profis in 1 group. My knownage about tcl is only 10 of 100 percent and my english isn`t good too. But i have some experience with forum moderation. I like this communty and try to support if i can.
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sun Jul 03, 2005 7:40 pm    Post subject: Reply with quote

When using if-else statements you should know that you can't use more than 1 else. For example:
Code:
if {[botisop $chan]} {
 pushmode $chan -o $nick
} else {
 puthelp "PRIVMSG $chan :I'm not oped."
}

But you can use several elseifs, for example:
Code:
if {[botisop $chan]} {
 pushmode $chan -o $nick
} elseif {[isop $other $chan]} {
 puthelp "PRIVMSG $other :please deop $nick on $chan."
} elseif {[isop $other2 $chan]} {
 puthelp "PRIVMSG $other2 :please deop $nick on $chan."
} else {
 puthelp "PRIVMSG $chan :neither me, $other or $other2 are oped on $chan."
}

_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Wed Jul 06, 2005 3:20 pm    Post subject: Reply with quote

In an if statement, || stands for 'or' and && stands for 'and'. So this statement
Code:
if {[botisop $chan] || [isop $nick $chan]} {
 putlog "Either the bot or $nick is oped on $chan."
}

is satisfied if either the bot or $nick is oped on $chan (atleast 1 of the conditions should be satisfied so if both are oped it's satisfied as well), if neither are oped then it will not putlog anything. While this statement
Code:
if {[botisop $chan] && [isop $nick $chan]} {
 putlog "Both the bot and $nick are oped on $chan."
}

is satisfied if both the bot and $nick are oped on $chan (all conditions should be satisfied), if neither or only 1 is oped then it will not putlog anything.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Wed Jul 13, 2005 10:14 pm    Post subject: Reply with quote

it should be noted that Tcl implements so-called "lazy evaluation" of boolean expressions (just like C/C++ and other languages); what does that mean?

if you happen to have (pseudo code):
Code:

if {<expr1> || <expr2>} {
   ...
}

and <expr1> evaluates to non-zero value (i.e. true), <expr2> will not be evaluated at all (because if's result doesn't depend on it, and will always be true, regardless of <expr2> being zero or non-zero) and execution will proceed within if's body; similarly, if you have:
Code:

if {<expr1> && <expr2>} {
   ...
}

if's body will be immediately skipped if <expr1> evaluates to zero (<expr2> will not be evaluated since the if's result doesn't depend on it)

that makes possible constructions like this:
Code:

if {[info exists foo] && ($foo == $bar)} {
   # do something
}

which will check whether $foo==$bar only if foo exists
Back to top
View user's profile Send private message Visit poster's website
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Wed Jul 27, 2005 10:30 am    Post subject: Reply with quote

Quote:
<owner> .set errorInfo
<bot> What? You need '.help'

That means dcc .set command is unbound. To make it work, go to your .conf file and change the line that says:
Quote:
unbind dcc n set *dcc:set

to
Quote:
#unbind dcc n set *dcc:set

And that applies to the tcl command as well (the bind that's before it).

Remember that if you have must-be-owner set to 1 then you should put your handle in the owner variable
Quote:
set owner "<your handle>"


PS: All those variables are documented, read them.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
sKy
Op


Joined: 14 Apr 2005
Posts: 194
Location: Germany

PostPosted: Tue Aug 09, 2005 10:41 pm    Post subject: recovering tcl files Reply with quote

Once a day i have deleted a .tcl file by error. This wasn`t the end of days becuase it was still in the memory.

Code:
# the new file
set ::proc_recover(setting,file) recoverfile.tcl

proc recover { pattern } {
   set file [open $::proc_recover(setting,file) a]
   foreach proc [lsort [info commands]] {
      if { [string match -nocase $pattern $proc] || [string equal -nocase $pattern $proc] } {
         puts $file "\n"
         puts $file "############################################################################################################################"
         puts $file "# $proc"
         puts $file "############################################################################################################################"
         puts $file "\n"
         puts $file "proc $proc \{ [info args $proc] \} \{"
         puts $file "[info body $proc]"
         puts $file "\}"
      }
   }
   close $file
}

# .tcl recover $pattern
# the pattern must be the exact procname or match it (wildcards possible)


Perhaps you want to change somethings to you own needs. The only thing you miss will be binds, global variables and comments.

Quote:
.tcl foreach var [lsort [info globals]] { putlog $var }
.tcl foreach bind [binds] { putlog $bind }
.tcl foreach bind [binds pub] { putlog $bind }


If you restarted the bot after then you have to google for "undelete" (depends on your platform).
Back to top
View user's profile Send private message
awyeah
Revered One


Joined: 26 Apr 2004
Posts: 1580
Location: Switzerland

PostPosted: Wed Aug 10, 2005 9:39 pm    Post subject: Reply with quote

A lot of people I have seen request simple AI scripts which they can use on their bots and configure themselves later.

Basically what we need to do here is to bind on specific keywords in the text or match on all and let the bot spit on a random reply from a list.

(1) For this the 'rand' function can help.

Code:

set reply [rand 5]

#This would return a random integer between 0 and 4 for the variable $reply.

(2) First check the length of your list so that the random number generated doesn't exceed the length of your list, which 'llength' does:

Code:

set reply [rand [llength $replies]]

#This would return a random integer between limits of your list for the variable $reply.


(3) Then we can continue to retrieve an element from the list by using lindex.

Code:

set reply [lindex $replies [rand [llength $replies]]]

#This would retrieve a random element from the list and save it in the variable $reply.


Code:

#In short here is an example script:

set replies {
{hi!}
{how are you?}
{whats up}
}

bind pubm - "*" auto:reply

proc auto:reply {nick uhost hand chan text} {
 global replies
 putserv "PRIVMSG $chan :[lindex $replies [rand [llength $replies]]]"
}

_________________
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Sun Sep 11, 2005 2:58 pm    Post subject: Reply with quote

you can embed inline, instead of the proc name argument of [bind], any code that doesn't depend on arguments passed by [bind]:
Code:

bind evnt - init-server {putserv "privmsg nickserv :id password";#}

;# at the end does the trick - it forces any arguments passed by [bind] to be ignored as comment

Tcl supports declaring variable number of arguments with keyword args, which allows you to use the same proc for several binds that would call it with different number of arguments:
Code:

bind msg - hi foo
bind pub - hi foo
proc foo {nick args} {
   putserv "privmsg $nick :hello goober"
}
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Wed Sep 14, 2005 8:07 pm    Post subject: Re: recovering tcl files Reply with quote

sKy wrote:
Code:

         puts $file "proc $proc \{ [info args $proc] \} \{"
         puts $file "[info body $proc]"
         puts $file "\}"

Try this instead:
Code:
proc printproc proc {
   set args {}
   foreach arg [info args $proc] {
      if {[info default $proc $arg val]} {
         lappend args [list $arg $val]
      } {
         lappend args [list $arg]
      }
   }
   list proc $proc $args [info body $proc]
}
+
Code:
puts $file [printproc $proc]

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
Linux
Halfop


Joined: 04 Apr 2004
Posts: 71
Location: Under The Sky

PostPosted: Sat Dec 03, 2005 2:48 am    Post subject: Match characters Reply with quote

Match characters

Bindings allow match characters in the arguments. Here are few special characters:

? matches any single character

* matches 0 or more characters of any type

% matches 0 or more non-space characters (can be used to match a single word)

~ matches 1 or more space characters (can be used for whitespace between words)
_________________
I'm an idiot, At least this one [bug] took about 5 minutes to find...
Back to top
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Wed Dec 14, 2005 4:49 am    Post subject: Reply with quote

while most people already learned they have to split the string text argument provided by many eggdrop binds into Tcl list first before using list commands on it, very few are aware that after using [split] extra white space will end up being empty list element(s), which will screw up argument indexing

here is one neat solution to this problem, from Tcler's wiki:

String to list: [split $s] alone operates on each instance of the splitchar (default:space), so sequences of spaces will produce empty list elements. [eval list $s] collapses whitespace sequences in one, but errors on unbalanced braces etc. The following proc should join the best of both worlds:

Code:

 proc string2list s {
        if [catch {eval list $s} res] {
                set res [list]
                foreach i [split $s] {
                        if {$i!=""} {lappend res $i}
                }
        }
        set res
 } ;#RS
 % string2list {a   b c     d}
 a b c d
 % string2list "a   b c  {"
 a b c \{
 % string2list {unbalanced "}
 unbalanced {"}

_________________
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code
Back to top
View user's profile Send private message Visit poster's website
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri May 26, 2006 8:21 am    Post subject: Reply with quote

demond wrote:
Code:

 proc string2list s {
        if [catch {eval list $s} res] {
                set res [list]
                foreach i [split $s] {
                        if {$i!=""} {lappend res $i}
                }
        }
        set res
 } ;#RS
 % string2list {a   b c     d}
 a b c d
 % string2list "a   b c  {"
 a b c \{
 % string2list {unbalanced "}
 unbalanced {"}

Try 'string2list {[exit]}' using that proc Razz The catch doesn't make any sense... RS must have created that proc before he learned Tcl Razz
Here's how i'd do it:
Code:
proc string2list s {
   set res [list]
   foreach i [split $s] {
      if {$i!=""} {lappend res $i}
   }
   set res
}


Edit: heh..i managed to provide a false solution the first time Razz Anyway here's another way to do the same ting:
Code:
proc string2list s {
   split [eval concat [split $s]]
}

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
De Kus
Revered One


Joined: 15 Dec 2002
Posts: 1361
Location: Germany

PostPosted: Fri May 26, 2006 10:12 am    Post subject: Reply with quote

Code:
proc string2list {s {c "\n\t "}} {
   set res [list]
   foreach i [split $s $c] {
      if {$i!=""} {lappend res $i}
   }
   set res
}

If you want to improove split, why not at least include all features of split? Smile
I don't know exactly which characters are thread as whitespaces, but I am these 3 are the most common one Very Happy.

Edit: sorry, I wondered why you would need, since lappend creates if neccessary, but didn't think on empty strings Very Happy.

Edit3: final one doesnt work with $c.
_________________
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...


Last edited by De Kus on Sat May 27, 2006 4:53 am; edited 4 times in total
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Tcl FAQ All times are GMT - 4 Hours
Goto page Previous  1, 2, 3, 4, 5  Next
Page 3 of 5

 
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