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.

Check letters

Help for those learning Tcl or writing their own scripts.
Post Reply
M
McMilan
Voice
Posts: 24
Joined: Fri Mar 13, 2009 3:24 pm

Check letters

Post by McMilan »

Hi.

How can i check what is the first letter of a word?

What i mean is how to check if a word is a channel. Checking if its first letter is a #


Thanks.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

You could use a string command

Code: Select all

if {[string index $chan 0] == "#"} {
  # code here
}
Or possibly two string commands

Code: Select all

if {[string equal [string index $chan 0] "#"]} {
  # code here
}
Or a regexp

Code: Select all

if {[regexp -- {^#} $chan]} {
  # code here
}
Or, if the intended channel is supposed to be one of the bot's channels, an eggdrop tcl command will check if this is true. Generally, all channel names begin with a hash so without the hash it could not be a valid bot channel.

Code: Select all

if {[validchan $chan]} {
  # code here
}

All the above assume the value of the variable chan (referenced by $chan) is the channel name to be checked
I must have had nothing to do
M
McMilan
Voice
Posts: 24
Joined: Fri Mar 13, 2009 3:24 pm

Post by McMilan »

Thanks :D

And, how to check if ! and @ are in a word? This is to check if is a invalid hostname or no.
User avatar
TCL_no_TK
Owner
Posts: 509
Joined: Fri Aug 25, 2006 7:05 pm
Location: England, Yorkshire

Post by TCL_no_TK »

Can use

Code: Select all

 if {![string match "*!*@*.*" $uhost]} {
 #not a valid hostmask
}
:D
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

It would be easy enough to check if ! and @ were present in a string AND in the correct order using regexp or string match commands but my choice would be to generally use a scan command. This is because it can simultaneously seperate out the nick, user and host components for use later in the code. A regexp command could do the same but I prefer scan.

Code: Select all

if {[scan $hostname {%[^!]!%[^@]@%s} nick user host] == 3} {
  # $hostname is in the format nick!user@host
  # additionally $nick, $user and $host can be used here in any further code
} else {
  # $hostname is not in the format nick!user@host
}
Example :-

<arfer> % return [scan "arfer!peter@i.play.for.england.com" {%[^!]!%[^@]@%s} nick user host]
<Baal> 3
<arfer> % return $nick
<Baal> arfer
<arfer> % return $user
<Baal> peter
<arfer> % return $host
<Baal> i.play.for.england.com
I must have had nothing to do
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Be very careful with our use of variable names. That applies to both my code and that of TCL_no_TK.

TCL_no_TK code is exactly correct except that $uhost is frequently used in eggdrop scripts to represent user@host and not nick!user@host in which case it would never pass the string match test.

In my code I use the variable name nick in the scan command. This variable name is widely used in scripts, say for example passed to a proc by a PUB bind. You would therefore have to choose an alternative name if my code was used inside such a proc.
I must have had nothing to do
M
McMilan
Voice
Posts: 24
Joined: Fri Mar 13, 2009 3:24 pm

Post by McMilan »

And how to check if there is no ! and @? because it is for a nickname/hostname ban script.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

From the information given so far, its time to try yourself
I must have had nothing to do
M
McMilan
Voice
Posts: 24
Joined: Fri Mar 13, 2009 3:24 pm

Post by McMilan »

lol

But how can i check if ! and @ there is no?

I dont know if you know mirc scripting, but i wanted something like:

Code: Select all

  if (! !isin $1 && @ !isin $1) {
    msg # It will be used as nickname
  }
  elseif (! isin $1 && @ isin $1) {
    msg # It will be used as hostname
  }
  else {
    msg # Wrong format
  }
i am new in tcl.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

bind pub - !something something

proc something {nick uhost hand chan text} {
  set firstword [lindex [split $text] 0]
  if {![string match "*!*@*" $firstword]} {
    if {[string match "*!*" $firstword] || [string match "*@*" $firstword]} {
      putserv "privmsg $chan :Wrong format" ; return
    }
    putserv "privmsg $chan :It will be used as nickname"
  } else {
    putserv "privmsg $chan :It will be used as hostname"
  }
}
M
McMilan
Voice
Posts: 24
Joined: Fri Mar 13, 2009 3:24 pm

Post by McMilan »

It works like arfer's script. :oops:
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

McMilan wrote:It works like arfer's script. :oops:
Are you high? Perhaps not high enough? It's the same as your crap mIRC code...

Without knowing what-the-F you mean it's clear the blushing your doing indicates to me that any further help you will receive will fall on deaf ears. Good day sir.. :)
M
McMilan
Voice
Posts: 24
Joined: Fri Mar 13, 2009 3:24 pm

Post by McMilan »

:shock:

Look, my mirc scripting code had a if, a elseif and a else. And your tcl script had just a if and a else.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

McMilan wrote::shock:

Look, my mirc scripting code had a if, a elseif and a else. And your tcl script had just a if and a else.
Check again charlie, all 3 of your conditions are there. And the style of code one uses to attain the same results can be dramatic. You've used horrible style, the tcl script doesn't....

Code: Select all

#create the bind for event handling
bind pub - !something something

# create the bound procedure and header
proc something {nick uhost hand chan text} {
  # grab the first word, in mirc this is $1
  set firstword [lindex [split $text] 0]
  # is *!*@* not found in the word?
  if {![string match "*!*@*" $firstword]} {
    # if there is no *!*@* in the word
    # now we need to check them seperately
    # *!* and *@*. if found, reply wrong format
    if {[string match "*!*" $firstword] || [string match "*@*" $firstword]} {
      putserv "privmsg $chan :Wrong format" ; return
    }
    # there is no *!*@*, no *!*, no *@*
    putserv "privmsg $chan :It will be used as nickname"
  } else {
    # there is *!*@*
    putserv "privmsg $chan :It will be used as hostname"
  }
}
Commented it so you can see how this works.
Last edited by speechles on Sun Aug 16, 2009 11:55 am, edited 1 time in total.
M
McMilan
Voice
Posts: 24
Joined: Fri Mar 13, 2009 3:24 pm

Post by McMilan »

Oops... Sorry. :oops: :oops: :oops:

Thanks hehehe
Post Reply