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.

codes reveiw

Help for those learning Tcl or writing their own scripts.
Post Reply
t
timt
Voice
Posts: 9
Joined: Sun Mar 23, 2008 9:49 pm

codes reveiw

Post by timt »

Hi,

I'm new in the world of TCL and i'm reading tips section of this forum as well as different documents/tutorials as well to learn tcl language. Now i'm trying to study different scripts to understand the codes. This forum is the best source and a lot of good coders are here to help the users. I chose 2 excellent excellent scripts for my study to understand the codes:

1: xchannel by demond
2: allprotection by Sir_Fz

I've started xchannel first and i've some silly questions and i hope someone will be able to explain them. Also i think this thread will help other new comers like me as well.

I would appreciate if someone would explain these codes please, i do understand a bit but still confused. this procedure -> proc fixargs {chan text args} is called from all other procedures like for example:

Code: Select all

proc repeat {nick uhost hand chan args} {
   variable repeats
   fixargs chan text $args
...............
Here is the actrual procedure

Code: Select all

proc fixargs {chan text args} {
   upvar $chan xchan; upvar $text xtext
   if {$::lastbind == "**"} {
      set xtext $xchan
      set xchan [lindex $args 0]
   } {
      set n [llength $args]
      set xtext [lindex $args [incr n -1]]
   }
}
why $::lastbind == "**" , what this line will do?
and what's the purpose to have upvar here in the procedure and what's the benefit?

2:

I understood this procedure

Code: Select all

proc maskhost {nuhost {type 0}} {
   scan $nuhost {%[^!]!%[^@]@%s} nick user host
   scan [set mask [::maskhost $nuhost]] {%*[^@]@%s} mhost
   switch $type {
   0 - 1 {return $mask}       ;# *!*foo@*.bar.com
   2 {return *!*@$host}       ;# *!*@moo.bar.com
   3 {return *!*@$mhost}      ;# *!*@*.bar.com
   4 {return *!*$user@$host}  ;# *!*foo@moo.bar.com
   5 {return *!*$user@*}      ;# *!*foo@*
   6 {return $nuhost}
   }
}
but i'm confused when it's set in different procedure like

Code: Select all

set hash [md5 $chan:$what:[maskhost $nick!$uhost]]
i mean what's the purpose to have 'hash' and 'md5' here? what this lines will do?

I think for now its enough for me to digest all this.

Thank you
regards
-timt
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: codes reveiw

Post by user »

timt wrote:why $::lastbind == "**" , what this line will do?
and what's the purpose to have upvar here in the procedure and what's the benefit?
The global variable "lastbind" ("$::varname" is just another way to address global variables) is set by eggdrop (see doc/tcl-commands.doc)

Upvar imports variables from a different scope...in this case the parent scope, so when the variables are changed in the fixargs proc, their values are also changed in the code invoking fixargs.
timt wrote:what's the purpose to have 'hash' and 'md5' here? what this lines will do?
That's just the way demond chose to do it...if you want his reason, ask him :) (I'm guessing the fact that md5-hashes have a fixed and pretty short length is part of the reason)
Have you ever read "The Manual"?
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

demond uses [fixargs] to swap between channel-name and text depending on whether the trigger of the procedure was from a pubm or a notc (so he used upvar to be able to change the value of chan and text). I think if $::lastbind is "**" then it's a notc otherwise it's a pub (you'll have to trace his logic).
t
timt
Voice
Posts: 9
Joined: Sun Mar 23, 2008 9:49 pm

Post by timt »

user and Sir_Fz: Thank you so much, now i'm crystal clear and moving well so far.
t
timt
Voice
Posts: 9
Joined: Sun Mar 23, 2008 9:49 pm

Post by timt »

Hi, I saw the following codes in the "Script Requests" section under topic: Set or remove "Silence". I apologize in advance for my stupid questions but i really want to learn/understand tcl.

By user:

Code: Select all

bind join - "#chan *!cservice@undernet.org" {silence +}
bind part - "#chan *!cservice@undernet.org" {silence -}
bind sign - "#chan *!cservice@undernet.org" {silence -}

proc silence {prefix args} {putserv "SILENCE $prefix*!*@*"}
I know the 2nd argument in the above proc is "args" and it's a special one. I'm trying to understand what's the benefit to have "args" here in the above proc? isn't -> proc silence {prefix} was sufficient?

also as per tcl-commands doc join/part/sign will have 4 arguments so why the above proc has {prefix args}?


By Sir_Fz:

Code: Select all

set swho *!cservice@undernet.org

bind join - "#channelname $swho" silence:control
bind part - "#channelname $swho" silence:control
bind sign - "#channelname $swho" silence:control

proc silence:control {n u h c args} {
 if {[llength $args]} {
  putserv "SILENCE -*!*@*"
 } {
  putserv "SILENCE *!*@*"
 }
}
Same silly question here. Why "args" is used here? isn't {n u h c} were enough because as per tcl-commands.doc "join/part/sign" proc will have 4 arguments.

what's advantage to have args here?

Thanks again.
-timt
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Actually, both "part" and "sign" add a fifth argument, being the reason stated by the user (or server). The use of args in the second example allows us to use the same proc for all three, as it accepts zero or more arguments, adding it to a list.

In the first example, should you only have the argument-list of {prefix}, you'd only get errors stating something similar to "too many arguments when calling silence" or such (silence would only expect one argument, but there would be four or five of them on the commandline). Here, args will "swallow" the additional arguments, since we really don't care for them anyway.
NML_375
t
timt
Voice
Posts: 9
Joined: Sun Mar 23, 2008 9:49 pm

Post by timt »

nml375: very nice, Thank you
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

nml375 wrote:there would be four or five of them
Actually, there would be five or six (since my bound commands have an embedded argument (+/-)), but I guess he got the idea :)
Have you ever read "The Manual"?
t
timt
Voice
Posts: 9
Joined: Sun Mar 23, 2008 9:49 pm

Post by timt »

user: thank you, i got it.
Post Reply