Here's my version of the code with comments and debugging info, based on stdragon's code.
Seems to be working great - thank you so much stdragon you saved my sanity.
Note the stdrag_triggerbinds function is called with the decrypted version of $text, so this function below is basically a way to send the arguments comtype nick uhost hand chan text and have this function invoke all binds that would be triggered automtically if this command had been received normally.
It's currently set up only to work with pub,pubm,notc,topc,msg,msgm, but should be easy to modify for others. you pass the comtype argument to indicate what kind of bind you are looking to match. it tries to repsect stacking behavior (did i do it right?)
Any suggestions and bug fixes are welcome, i haven't tested all kinds of mask and flag combinations. Sorry that indenting is not showing here.
thanks again stdragon.
#######################################################
# STDRAGON FUNCTIONS - for iterating through binds to manually trigger them
#
http://forum.egghelp.org/viewtopic.php?p=31744#31744
proc stdrag_triggerbinds {comtype nick uhost hand chan text} {
# this procedure iterates through all registered binds, and manually triggers those that match
# returns a count of the number of triggers executed
# debugging
putlog "DEBUG in triggerbinds: $comtype $nick $uhost $hand $chan $text"
# walk through all binds and look for matches
set triggercount 0
foreach bind [binds] {
# grab the the elements from current bind table entry
foreach {table flags mask hits callback} $bind {}
# debugging
putlog "DEBUG checking against: $table $flags $mask $hits $callback"
# dont retrigger ourselves!
if {$mask == "mcps" || $mask =="+OK"} { continue }
# debugging
putlog "DEBUG at stage 0"
# check if this is one of the kinds of binds we handle (ATTN: check case), if not continue to next entry
if {$table != "pub" && $table != "pubm" && $table != "msg" && $table != "notc" && $table != "topc"} { continue }
# debugging
putlog "DEBUG at stage 1"
# check if this is the kind of bind we want to match based on comtype; if they are equal its good
set commandmatch 0
if {$comtype == $table} { set commandmatch 1 }
if {$comtype == "pub" && $table == "pubm"} { set commandmatch 1 }
if {$comtype == "pubm" && $table == "pub"} { set commandmatch 1 }
if {$comtype == "msg" && $table == "msgm"} { set commandmatch 1 }
if {$comtype == "msgm" && $table == "msg"} { set commandmatch 1 }
# if its not compatible, continue to next table entry
if {$commandmatch == 0} { continue }
# debugging
putlog "DEBUG at stage 2"
# check the flags of the channel if it doesnt match, continue to next bind
if {![string equal "-|-" $flags] && ![matchattr $hand $flags $chan]} { continue }
# now convert the bind mask to a regexp in preparation for comparing it to text
set cmask [stdrag_bind_mask_to_regexp $mask]
# debugging
putlog "DEBUG at stage 3 with converted mask = $mask and converted mask = $cmask and text = $text"
# compare the mask against chan .+ text if its pubm, or again text otherwise
if {$table == "pubm"} {
if {![regexp -nocase -- $cmask "$chan $text"]} {
putlog "DEBUG cmask did not match '$chan $text'"
continue
}
} elseif {$table == "topc"} {
if {![regexp -nocase -- $cmask "$chan $text"]} {
continue
}
} elseif {![regexp -nocase -- $cmask $text]} {
continue
}
# debugging
putlog "DEBUG at stage 4"
# ok we got a matching bind we want to retrigger, so call it
catch { eval $callback [list $nick $uhost $hand $chan $text] }
# debugging
putlog "DEBUG at stage 5"
# increment triggercount
incr triggercount
# should we stop iterating (when we hit a pub or msg we stop, but pubm or mesgm, or notc we dont)
if {$table == "pub" || $table == "msg" } { break }
}
return $triggercount
}
proc stdrag_bind_mask_to_regexp {mask} {
# helper function to convert an eggdrop bind text mask into a regular expression pattern
regsub -all {\.|\(|\+|\^|\$|\\|\[|\|} $mask {\\&} mask
regsub -all "\{" $mask {\\&} mask
regsub -all {\*} $mask {.*} mask
regsub -all {\?} $mask {.} mask
regsub -all {%} $mask {[^\s]+} mask
regsub -all {~} $mask {[\s]+} mask
return "^$mask\$"
}
#######################################################