| View previous topic :: View next topic |
| Author |
Message |
alkoleht Voice
Joined: 26 Jun 2011 Posts: 6
|
Posted: Thu Feb 28, 2013 3:18 pm Post subject: need help with lists |
|
|
Hi. I am pretty noob when it comes to TCL but i am trying to learn.
I am trying to create a script that will take user msg to bot, does some matching and relays it to chan.
here is how it looks in reality:
1. let's say i have a list of words:
| Code: |
set word {
flower
bee
house
}
|
2. user MSG-s to bot, ether just one word or more text, doesnt matter.
example:
| Code: |
/msg bot house is tv show
|
3. script then takes the first word of imput and matches that to $word . also no matter if the input is CAPS or lower case or MiXeD
4. if first word matches then say in chan:
| Code: |
<bot> $nick sayd $word
|
if the first word does not match $word list then it will say the full imput into the chan.
so example:
| Code: |
<user>/msg <bot> house is tv show
<bot> <user> says word [b]house[/b]
<user>/msg <bot> there is a house in tv right now
<bot> <user> says: there is a house in tv right now
|
i know it may sound strange but that is something i want to create.
any help with this would be appreciated
Thank you |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Thu Feb 28, 2013 4:09 pm Post subject: |
|
|
Try this code and tell if this is what you wanted
| Code: |
bind MSGM - * check:msgm
set temp(chan) "#channel"
set temp(words) {
"word1"
"word2"
}
proc check:msgm {nick uhost hand arg} {
global temp
foreach a $temp(words) {
if {[string match -nocase $a [lindex [split $arg] 0]]} {
putserv "PRIVMSG $temp(chan) :$nick said \002[lindex [split $arg] 0]\002"
return
} else {
putserv "PRIVMSG $temp(chan) :$nick said: $arg"
return
}
}
}
|
_________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
alkoleht Voice
Joined: 26 Jun 2011 Posts: 6
|
Posted: Thu Feb 28, 2013 4:19 pm Post subject: |
|
|
awesome, it works. thank you.
i was going completely different approach to this problem before but this solution works just perfect. Much appreciated |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Thu Feb 28, 2013 4:22 pm Post subject: |
|
|
No problem, glad i could help _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
LoKii Voice
Joined: 21 Oct 2009 Posts: 34
|
Posted: Sun Mar 10, 2013 8:49 am Post subject: |
|
|
forgive me for hijacking this thread, a small question regarding it.
This code is based on the first word said in the sentence, but if i want to capture any word from the sentence which is in the temp(words) list, how would i go about doing it, if the word is NOT the first word?
Cheers. |
|
| Back to top |
|
 |
Papillon Owner

Joined: 15 Feb 2002 Posts: 724 Location: *.no
|
Posted: Sun Mar 10, 2013 10:11 am Post subject: |
|
|
Madalin:
Instead of using a foreach loop which is slow, why not just use the standard tcl-command lsearch?
LoKii:
In that case I would use a loop on each word said by the user and do a lsearch on the list. Word of warning though, if your channel is very crowded and people are chatty this will be very time-consuming on the bot. I have not been writing tcl-code for years and have forgotten alot, so might be a much more effecient way of doing it if you search around  _________________ Elen sila lúmenn' omentielvo |
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Sun Mar 10, 2013 10:21 am Post subject: |
|
|
I am trying more and more to use lsearch (i never used that in the past) so i have to try use it now in the present yet as i used foreach alot it would be a little weird for me at start but ill try use it. _________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
LoKii Voice
Joined: 21 Oct 2009 Posts: 34
|
Posted: Sun Mar 10, 2013 10:37 am Post subject: |
|
|
Yes, i also discovered that lsearch is the way to go.
What i am trying to accomplish is, i took a script called: forward.tcl written by r0gUe.
His script basicly just relays any msg to the bot to a specified channel.
What i want to do, is to edit it, so that certain messages are not relayed with the same output, due to security, such as pass/ident/auth/addhost etc....
What i have so far is:
| Code: |
bind msgm - "*" proc_msgm
proc proc_msgm {nick uhost hand arg} {
set chan "#prototype"
set line [string trim $arg]
set temp {
"verify"
"ident"
"addhost"
"pass"
}
foreach a $temp [split $arg]{
if {[string match -nocase $a $arg]} {
putserv "PRIVMSG $chan :$nick has used the $a command"
}
}
puthelp "PRIVMSG $chan :$nick messaged me saying: '$line'"
return 1
} |
So I am not too worried about large rooms and such, since its only in the rare cases that someone pms the bot. I just want to make sure that no passwords though go through, which is why i am attempting to change the output in the 'if' statement on certain words.
Cheers |
|
| Back to top |
|
 |
LoKii Voice
Joined: 21 Oct 2009 Posts: 34
|
Posted: Sun Mar 10, 2013 11:01 am Post subject: |
|
|
update:
I got it to work this way:
| Code: | proc proc_msgm {nick uhost hand arg} {
set chan "#prototype"
set line [string trim $arg]
set items [split [string tolower $line]]
set temp {
"verify"
"ident"
"addhost"
"pass"
}
foreach pattern $temp {
foreach match [lsearch -all -inline $items $pattern] {
putserv "PRIVMSG $chan :$nick has just messaged me, using the \002$pattern\002 command."
return 1
}
}
puthelp "PRIVMSG $chan :$nick messaged me saying: '$line'"
return 1
} |
Using the lsearch.
Cheers everyone and sorry again for hijacking this thread  |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Mar 10, 2013 11:35 am Post subject: |
|
|
LoKii,
I'm not sure why you're using a second foreach-loop with lsearch in inline mode as the source. Just test the result from lsearch; if it's 0 or greater, there was a match, while -1 indicates there was no match.
My recommendation is something along these lines:
| Code: | proc proc_msgm {nick host handle text} {
set items [split $text]
set temp [list "verify" "ident" "addhost" "pass"]
set chan "#prototype"
foreach pattern $temp {
if {[lsearch -nocase $items $pattern] >= 0} {
puthelp "PRIVMSG $chan :$nick has just messaged me, using the \002$pattern\002 command."
return 1
}
}
puthelp "PRIVMSG $chan :$nick messaged me saying: '$text'"
return 1
} |
_________________ NML_375, idling at #eggdrop@IrcNET
Last edited by nml375 on Sun Mar 10, 2013 12:06 pm; edited 1 time in total |
|
| Back to top |
|
 |
LoKii Voice
Joined: 21 Oct 2009 Posts: 34
|
Posted: Sun Mar 10, 2013 11:42 am Post subject: |
|
|
@ nml375
Simple, I dont know either. Im a total n00b trying to get my scripts to work the way I imagine.
I will try out your suggestion and let you know in a bit
If I showed you my main script which is around 3000 lines by now, you would prolly shoot me for raping TCL.
Cheers for your help.  |
|
| Back to top |
|
 |
LoKii Voice
Joined: 21 Oct 2009 Posts: 34
|
Posted: Sun Mar 10, 2013 12:03 pm Post subject: |
|
|
Your solution didnt quite work for me.
No matter what i write in a /msg bot , the result is ALWAYS:
| Quote: | | LoKii has just messaged me, using the verify command. |
I dont really understand why yet, but I'm sure I will figure it out once i understand what you meant with using the foreach-loop once.
In the mean time, my code is:
| Code: | proc proc_msgm {nick uhost hand arg} {
global memo
set chan "#prototype"
set line [string trim $arg]
set items [split [string tolower $line]]
set temp {
"verify"
"ident"
"addhost"
"pass"
"info"
"voice"
"halfop"
"invite"
"key"
"op"
"status"
"notes"
}
set danger {
"die"
"jump"
"reset"
}
if {[matchattr $nick n]} {
return 1
}
foreach pattern $temp {
foreach match [lsearch -all -inline $items $pattern] {
putserv "PRIVMSG $chan :$nick has just messaged me, using the \002$pattern\002 command."
return 1
}
}
foreach pattern $danger {
foreach match [lsearch -all -inline $items $pattern] {
putserv "PRIVMSG $chan :\002WARNING!\002 User \002$nick\002 has just attempted to use the \002$pattern\002 command."
putserv "PRIVMSG $chan :Full output is: \002$arg\002."
putserv "PRIVMSG $memo :send lokii Master, a user \002$nick\002 has just attempted to issue the \002$pattern\002 command on me via /msg. His full output was: \002$arg\002."
return 1
}
}
puthelp "PRIVMSG $chan :$nick messaged me saying: '$line'"
return 1
} |
Which works great, except I'm sure that your method is less resource-hungry.
BTW, i got my code based on a code that you have written in: http://forum.egghelp.org/viewtopic.php?t=18997&highlight=lists
Last edited by LoKii on Sun Mar 10, 2013 12:07 pm; edited 1 time in total |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Mar 10, 2013 12:05 pm Post subject: |
|
|
Sorry, made a minor mistake...
Should've tested for greater or equal to zero, not less than zero...
I'll update my previous post in a sec or two... _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
LoKii Voice
Joined: 21 Oct 2009 Posts: 34
|
Posted: Sun Mar 10, 2013 12:12 pm Post subject: |
|
|
| nml375 wrote: | Sorry, made a minor mistake...
Should've tested for greater or equal to zero, not less than zero...
I'll update my previous post in a sec or two... |
Yup, now it works like a charm. I will try to understand the difference between yours and mine, to make sure that in the future, my code will be more effective.
Will convert my version into yours, and show it.
Many thanks for your help everyone  |
|
| Back to top |
|
 |
LoKii Voice
Joined: 21 Oct 2009 Posts: 34
|
Posted: Sun Mar 10, 2013 12:58 pm Post subject: |
|
|
Update:
Final code and works like a charm. Now i got some reading to do to understand the difference.
| Code: | proc proc_msgm {nick host handle text} {
global memo
set items [split $text]
set temp [list "verify" "ident" "addhost" "pass" "info" "voice" "halfop" "invite" "key" "op" "status" "notes"]
set danger [list "die" "jump" "reset"]
set chan "#prototype"
if {[matchattr $nick n]} {
return 1
}
foreach pattern $temp {
if {[lsearch -nocase $items $pattern] >= 0} {
puthelp "PRIVMSG $chan :$nick has just messaged me, using the \002$pattern\002 command."
return 1
}
}
foreach pattern $danger {
if {[lsearch -nocase $items $pattern] >= 0} {
putserv "PRIVMSG $chan :\002WARNING!\002 User \002$nick\002 has just attempted to use the \002$pattern\002 command."
putserv "PRIVMSG $chan :Full output is: \002$text\002."
putserv "PRIVMSG $memo :send lokii Master, a user \002$nick\002 has just attempted to issue the \002$pattern\002 command on me via /msg. His full output was: \002$text\002."
return 1
}
}
puthelp "PRIVMSG $chan :$nick messaged me saying: '$text'"
return 1
} |
Many thanks for your help  |
|
| Back to top |
|
 |
|