Eggdrop Tcl Binds

Disclaimer: This method of binding is officially unsupported by the Eggheads community. The reason for this is because it may not work in future versions of Eggdrop. However, I still find it useful.

In some cases, you may want to bind multiple types of binds to a single proc. However, few bind types have compatible parameter lists. For our example, we bind a pub command and a msg command to the same proc. The examples on this page were inspired by an egghelp.org post by dq.

# Traditional Way
 
bind pub n|- ${trigger}join pub:join
proc pub:join {nick uhost hand chan text} {
  dojoin $nick $text
}
 
bind msg n ${trigger}join msg:join
proc msg:join {nick uhost hand text} {
  dojoin $nick $text
}
 
proc dojoin {nick chan} {
  set sChan [split $chan]
  if {![llength $sChan] || [validchan [set chan [lindex $sChan 0]]]} {
    puthelp "NOTICE $nick :Please use $::lastbind #channel"
  } else {
    channel add $chan
  }
}

As you can see, that is a bit wordy. A shorter way to define this might be as follows.

# New Way
 
bind pub n|- ${trigger}join {dojoin $_pub1 $_pub5; return 1; #}
bind msg n ${trigger}join {dojoin $_msg1 $_msg4; #}
proc dojoin {nick chan} {
  set sChan [split $chan]
  if {![llength $sChan] || [validchan [set chan [lindex $sChan 0]]]} {
    puthelp "NOTICE $nick :Please use $::lastbind #channel"
  } else {
    channel add $chan
  }
}

To understand how this works, it's important to understand how Eggdrop calls a bound function. First, the Eggdrop sets some global variables. In this case, they're $_pub1 through $_pub5 for pub binds, and $_msg1 through $_msg4 for msg binds. They're not always this simple, though, so see the reference table below for others. Second, eggdrop executes the code at the end of the bind, passing the variables it just set. By actually using these globals instead of waiting for them to be passed in, we can save a lot of code. This is sort of like the concept of anonymous functions in Java.

TypeGlobals
MSGnick user@hosthandletext
$_msg1$_msg2 $_msg3$_msg4
DCChandleidx text
$_dcc1$_dcc2$_dcc3
FILhandleidx text
$_fil1$_fil2$_fil3
PUBnick user@hosthandlechanneltext
$_pub1$_pub2 $_pub3$_pub4 $_pub5
MSGMnick user@hosthandle text
$_msgm1$_msgm2 $_msgm3$_msgm4
PUBMnick user@hosthandle channeltext
$_pubm1$_pubm2 $_pubm3$_pubm4$_pubm5
NOTCnick user@hosthandle text dest
$_notc1$_notc2 $_notc3$_notc4$_notc5
JOINnick user@hosthandlechannel
$_jp1$_jp2 $_jp3 $_jp4
PARTnickuser@hosthandlechannelmsg
$_p1$_p2 $_p3 $_p4 $_p5
SIGNnick user@hosthandle channelreason
$_stnm1$_stnm2 $_stnm3$_stnm4$_stnm5
TOPCnick user@hosthandle channeltopic
$_stnm1$_stnm2 $_stnm3$_stnm4$_stnm5
KICKnick user@hosthandle channeltarget reason
$_kick1$_kick2 $_kick3$_kick4$_kick5$_kick6
NICKnick user@hosthandle channelnewnick
$_stnm1$_stnm2 $_stnm3$_stnm4$_stnm5