| View previous topic :: View next topic |
| Author |
Message |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Wed Mar 23, 2011 7:15 pm Post subject: adding a search |
|
|
My script is as follows:
| Code: | bind pub - !shoelace pub_shoelace
proc pub_shoelace {nick mask hand channel args} {
global shoelace
putserv "PRIVMSG $channel :\[RANDOM IMAGE\] [lindex $shoelace [rand [llength $shoelace]]]"
}
set shoelace {
"1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg"
"2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg"
"3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg"
etc etc etc etc } |
Basically all it does is randomly pulls a link from 'shoelace'. and it works wonderfully. My question is, how can I possibly add a function to where, if I execute '!shoelace keyword' it tries to match the keyword to a link in 'shoelace' and putserv the relevant link, if not, else if to a random link.
Any input would be great.
Thanks! |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Mar 24, 2011 7:01 am Post subject: |
|
|
You could use the lsearch command to search through your list for a given pattern.
PS. I took the liberty of moving this thread to the "Scripting Help" forum. DS _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Thu Mar 24, 2011 6:34 pm Post subject: |
|
|
I added this line of code to search for the argument
| Code: | | putserv "PRIVMSG $channel :\[LOCATED IMAGE\] [lindex $shoelace [rand [lsearch -all $shoelace $args]]]" |
But it is returning this error...
| Code: | | [15:30:35] Tcl error [pub_shoelace]: random limit must be greater than zero |
I'm fairly new to TCL, so again, any guidance is appreciated. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Mar 24, 2011 6:51 pm Post subject: |
|
|
First off, I doubt you'd like to use rand with the search-result; didn't you search for one specific item within the list? If you were thinking of picking a random item from the search result list (since you used -all), that won't work at all; "rand" expects a single integer, and returns a value between 0 and the provided integer minus one.
Secondly, using "args" as the last argument name will cause issues with this code. Use something different such as "text" or "data", which is not handled in a special manner by tcl.
I'd first check if the user provided any text:
| Code: | if {$text != ""} {
#user supplied a search-term
set hits [lsearch -all -- $shoelace $text]
#test if we found any..
if {[llength $hits] > 0} {
#We've got atleast one match, pick a random one if we've got multiple ones
set item [rand [llength $hits]]
} else {
#No hits, pick a random line from the list
set item [rand [llength $shoelace]]
}
} else {
#No search-term supplied, pick a random item
set item [rand [llength $shoelace]]
} |
Now that I've got the index of either a random result, use it to extract the line from the list and print it..
| Code: | | puthelp "PRIVMSG $channel :[lindex $shoelace $item]" |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Thu Mar 24, 2011 7:33 pm Post subject: |
|
|
Thanks nml375, I love how you set that up. It works beautifully. The only problem I'm seeing now is the matching.
| Code: | set shoelace {
"1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.j$
"2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg"
"3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg"
"4 of 3918 http://shoelace.org/pics/2011/03-02-2011/6e3733137623ec36878d18dd4c8993ed.jpg"
"5 of 3918 http://shoelace.org/pics/2011/03-02-2011/523px-Michael_Pacher_004.jpg"
"6 of 3918 http://shoelace.org/pics/2011/03-02-2011/tattoo.jpg"
"7 of 3918 http://shoelace.org/pics/2011/03-02-2011/310765_ShayMaria.jpg"
"8 of 3918 http://shoelace.org/pics/2011/03-02-2011/far.jpg"
"9 of 3918 http://shoelace.org/pics/2011/03-02-2011/McDuffie.jpg"
"10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg"
"11 of 3918 http://shoelace.org/pics/2011/03-02-2011/day.jpg"
"12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg"
"13 of 3918 http://shoelace.org/pics/2011/03-02-2011/1256244960-1245496056908.jpg" |
Attempting to fetch the image link for "far.jpg". It's pulling random image links
| Code: | <@stream> !test far
<oldskewl> 3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg
<@stream> !test far.jpg
<oldskewl> 10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg
<@stream> !test far.jpg
<oldskewl> 10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg
<@stream> !test far.jpg
<oldskewl> 12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg
<@stream> !test far.jpg
<oldskewl> 16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg |
|
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Thu Mar 24, 2011 7:39 pm Post subject: |
|
|
I'm guessing the issue lies within the following:
| Code: | #test if we found any..
if {[llength $hits] > 0} {
#We've got atleast one match, pick a random one if we've got multiple ones
set item [rand [llength $hits]]
} else {
#No hits, pick a random line from the list
set item [rand [llength $shoelace]]
}
} else {
#No search-term supplied, pick a random item
set item [rand [llength $shoelace]]
}
putserv "PRIVMSG $channel :[lindex $shoelace $item]"
}
|
If it's finding the match, it's not printing it to channel, rather it continues down the code to
| Code: | putserv "PRIVMSG $channel :[lindex $shoelace $item]"
}
|
In which it's just pulling a random link due to
| Code: | } else {
#No hits, pick a random line from the list
set item [rand [llength $shoelace]]
|
But I am definitely no expert. |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Thu Mar 24, 2011 10:40 pm Post subject: |
|
|
| Code: | | set hits [lsearch -all -- $shoelace $text] |
The code nml375 gave does work just fine. In using only the -all switch, it will be assumed a "glob" match. Problem is, there are no wildcards (* or ?) to facilitate a proper glob match. Meaning to get nml375's code to work right, you need to issue your request like this, including wildcards in your text:
!shoelace *far*
To get around this limitation, and have the script assume wildcards * begin and end the string. Change the code above to look like it is below. Once you do this, the following will work like it does above, without having to use *'s within your text.
!shoelace far
| Code: | | set hits [lsearch -all -- $shoelace *$text*] |
_________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Thu Mar 24, 2011 10:58 pm Post subject: |
|
|
Didn't seem to work for me.
My code full code is as follows:
| Code: | bind pub - !test pub_shoelace
proc pub_shoelace {nick mask hand channel text} {
global shoelace
if {$text != ""} {
#user supplied a search-term
set hits [lsearch -all $shoelace *$text*]
#test if we found any..
if {[llength $hits] > 0} {
#We've got atleast one match, pick a random one if we've got multiple ones
set item [rand [llength $hits]]
} else {
#No hits, pick a random line from the list
set item [rand [llength $shoelace]]
}
} else {
#No search-term supplied, pick a random item
set item [rand [llength $shoelace]]
}
putserv "PRIVMSG $channel :[lindex $shoelace $item]"
}
set shoelace {
"1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.j$
"2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg"
"3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg"
"4 of 3918 http://shoelace.org/pics/2011/03-02-2011/6e3733137623ec36878d18dd4c8993ed.jpg"
"5 of 3918 http://shoelace.org/pics/2011/03-02-2011/523px-Michael_Pacher_004.jpg"
"6 of 3918 http://shoelace.org/pics/2011/03-02-2011/tattoo.jpg"
"7 of 3918 http://shoelace.org/pics/2011/03-02-2011/310765_ShayMaria.jpg"
"8 of 3918 http://shoelace.org/pics/2011/03-02-2011/far.jpg"
"9 of 3918 http://shoelace.org/pics/2011/03-02-2011/McDuffie.jpg"
"10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg"
"11 of 3918 http://shoelace.org/pics/2011/03-02-2011/day.jpg"
"12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg"
"13 of 3918 http://shoelace.org/pics/2011/03-02-2011/1256244960-1245496056908.jpg"
"14 of 3918 http://shoelace.org/pics/2011/03-02-2011/AVvFQ.jpg"
"15 of 3918 http://shoelace.org/pics/2011/03-02-2011/Blizzy.jpg"
"16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg"
"17 of 3918 http://shoelace.org/pics/2011/03-02-2011/DSC05480.jpg"
"18 of 3918 http://shoelace.org/pics/2011/03-02-2011/F1EjB.jpg"
"19 of 3918 http://shoelace.org/pics/2011/03-02-2011/HRguidetoconversation.jpg"
"20 of 3918 http://shoelace.org/pics/2011/03-02-2011/Lol_wut_completely.jpg"
"21 of 3918 http://shoelace.org/pics/2011/03-02-2011/MJ3eN.jpg"
"22 of 3918 http://shoelace.org/pics/2011/03-02-2011/Meanwhile_In_Uganda.jpg"
}
|
However, I get the following return results:
| Code: | <@stream> !test day
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg
<@stream> !test *day*
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg
<@stream> !test
<oldskewl> 16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg
<@stream> !test day
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg
<@stream> !test McDuffie
<oldskewl> 1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg |
|
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Thu Mar 24, 2011 11:33 pm Post subject: |
|
|
I guess I spoke too soon. HAW
nml375's code has one major flaw within it.
| Code: | #We've got atleast one match, pick a random one if we've got multiple ones
set item [rand [llength $hits]] |
The part here that sets item is to blame. This checks length of "hits" which will be the amount of positions found that hold your results. It then picks a random number from 0 to length. This is where it fails. It should apply that to a lindex to get the position from $hits which would then work correctly.
| Code: | bind pub - !shoelace pub_shoelace
proc pub_shoelace {nick mask hand channel text} {
global shoelace
if {[string length [string trim $text]]} {
#user supplied a search-term
set hits [lsearch -all $shoelace *[string trim $text]*]
#test if we found any..
if {[llength $hits] > 0} {
#We've got atleast one match, pick a random one if we've got multiple ones
set item [lindex $hits [rand [llength $hits]]] ; set status "SEARCH FOUND"
} else {
#No hits, pick a random line from the list
set item [rand [llength $shoelace]] ; set status "RANDOMIZED"
}
} else {
#No search-term supplied, pick a random item
set item [rand [llength $shoelace]] ; set status "RANDOMIZED"
}
putserv "PRIVMSG $channel :\[$status\] [lindex $shoelace $item]"
}
set shoelace {
{1 of 3918 http://shoelace.org/pics/2011/03-02-2011/02212011_earthquake_christchurch_InstantBikeramp.jpg}
{2 of 3918 http://shoelace.org/pics/2011/03-02-2011/2irqlon.jpg}
{3 of 3918 http://shoelace.org/pics/2011/03-02-2011/2vjr8mx.jpg}
{4 of 3918 http://shoelace.org/pics/2011/03-02-2011/6e3733137623ec36878d18dd4c8993ed.jpg}
{5 of 3918 http://shoelace.org/pics/2011/03-02-2011/523px-Michael_Pacher_004.jpg}
{6 of 3918 http://shoelace.org/pics/2011/03-02-2011/tattoo.jpg}
{7 of 3918 http://shoelace.org/pics/2011/03-02-2011/310765_ShayMaria.jpg}
{8 of 3918 http://shoelace.org/pics/2011/03-02-2011/far.jpg}
{9 of 3918 http://shoelace.org/pics/2011/03-02-2011/McDuffie.jpg}
{10 of 3918 http://shoelace.org/pics/2011/03-02-2011/311123_attagirl.jpg}
{11 of 3918 http://shoelace.org/pics/2011/03-02-2011/day.jpg}
{12 of 3918 http://shoelace.org/pics/2011/03-02-2011/1204982164-1204976762649.jpg}
{13 of 3918 http://shoelace.org/pics/2011/03-02-2011/1256244960-1245496056908.jpg}
{14 of 3918 http://shoelace.org/pics/2011/03-02-2011/AVvFQ.jpg}
{15 of 3918 http://shoelace.org/pics/2011/03-02-2011/Blizzy.jpg}
{16 of 3918 http://shoelace.org/pics/2011/03-02-2011/CNipv.jpg}
{17 of 3918 http://shoelace.org/pics/2011/03-02-2011/DSC05480.jpg}
{18 of 3918 http://shoelace.org/pics/2011/03-02-2011/F1EjB.jpg}
{19 of 3918 http://shoelace.org/pics/2011/03-02-2011/HRguidetoconversation.jpg}
{20 of 3918 http://shoelace.org/pics/2011/03-02-2011/Lol_wut_completely.jpg}
{21 of 3918 http://shoelace.org/pics/2011/03-02-2011/MJ3eN.jpg}
{22 of 3918 http://shoelace.org/pics/2011/03-02-2011/Meanwhile_In_Uganda.jpg}
} |
I've tested this, it works perfectly. Also use { } to enclose your quasi-list of shoelace url's. This will avoid many problems that arise from hand-crafted lists.
_________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Thu Mar 24, 2011 11:44 pm Post subject: |
|
|
Thanks so much speechles, it's absolutely brilliant.
What would the difficulty be to seperate the links into an external file such as links.txt?
Would I just do something like this
| Code: | set file "scripts/links.txt"
set shoelace [open $file r] |
Thanks |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Fri Mar 25, 2011 12:01 am Post subject: |
|
|
Where the list presently is...
| Code: | set shoelace {
{etc}
{etc}
} |
Change that entire section that sets shoelace to look exactly like the code below:
| Code: | set shoefile [open "scripts/links.txt" r]
set shoelace [split [read -nonewline $shoefile] "\n"]
close $shoefile ; unset shoefile |
It will now read the shoelace list from your file. It isn't very difficult to do at all.  _________________ speechles' eggdrop tcl archive
Last edited by speechles on Fri Mar 25, 2011 12:05 am; edited 1 time in total |
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Fri Mar 25, 2011 12:05 am Post subject: |
|
|
| Thanks man. You're the bestestestest. I'm trying to get adjusted to it, because tcl seems pretty damn fun. |
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
|
| Back to top |
|
 |
streamish Voice
Joined: 03 Dec 2010 Posts: 21
|
Posted: Fri Mar 25, 2011 12:47 am Post subject: |
|
|
| I actually haven't even tried yet, just figured I would ask lol |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Fri Mar 25, 2011 2:40 am Post subject: |
|
|
It wouldn't quite work that way. File handlers are not the same as http socket handlers.
A very basic way to accomplish it would be as follows:
| Code: | set shoefile [open "scripts/links.txt" r]
set shoelace [split [read -nonewline $shoefile] "\n"]
close $shoefile ; unset shoefile |
Change what you see above, to look like it does below.
| Code: | package require http
if [catch {set shoefile [::http::geturl "http://www.whateverdomain.com/links.txt" -timeout 5000]} error]} {
putlog "ERROR: Cannot create link list ( $error )"
set shoelace [list] ; unset shoefile
} else {
set shoelace [split [http::data $shoefile] "\n"]
::http::cleanup $shoefile ; unset shoefile
} |
This of course doesn't account for redirects, cookies, secure sockets, gzip, other types of http transaction errors, etc. This method simply assumes your http call will succeed with a code 200 always. We will assume it always does.  _________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
|