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.

[resolved] comparing two hosts w/ wildcards

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
krystal
Voice
Posts: 3
Joined: Mon Oct 05, 2009 6:48 am
Location: Spain

[resolved] comparing two hosts w/ wildcards

Post by krystal »

hi there.. new to tcl, but experienced in mirc (and lamp)

want to do something when eggy joins a channel

Code: Select all

#settings from config file
global my-hostname
global my-ip

if {($uhost != my-hostname) && ($uhost != my-ip)}
I know this isn't right.. what's the best way to compare two hosts, and can I bring in these two settings from the config file?
Last edited by krystal on Mon Oct 05, 2009 10:33 am, edited 1 time in total.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

You can use any of the settings in the bot's .conf file.

Generally speaking, a direct test of equality or inequality using == or != respectively can lead to problems because they are case sensitive. This would not be an issue with a numeric IP but may well be a problem with a hostname. A better way would be to use 'string equal' or 'string match' command with the -nocase option. The former would be used to test exact equality.

The second problem with your code is that you are not comparing like for like. The variable uhost is generally used as an argument (variable) name passed to a proc by a bind and represents user@host whilst the global variables my-hostname and my-ip are a host only.

You would always need to use the dollar symbol to bring about variable substitution (replacement of a variable name by its value). So the following is incorrect anyway. It compares the value of the variable named uhost with the string my-hostname.

Code: Select all

if {$uhost != my-hostname} {
I'm not sure I see the point of the code anyway, because generally I would expect a nick to be passed to the same proc and this is easy to test if it is or is not the bot. I had a quick scan through the various bind types and didn't see any that pass user@host but not nick.

Code: Select all

if {![isbotnick $nick]} {
  # code here
}
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 »

Just as an aside, if you did want to use the global variables my-ip and/or my-hostname, I would expect an issue to arise during substitution if simply used as $my-ip and/or $my-hostname. The interpreter will stop at the hyphen assuming the end of the variable name had been reached. They would have to be used as ${my-ip} and/or ${my-hostname}.
I must have had nothing to do
User avatar
krystal
Voice
Posts: 3
Joined: Mon Oct 05, 2009 6:48 am
Location: Spain

Post by krystal »

thanks for your reply. the hyphens did cause an issue, and the brackets fixed that. I was wondering what the best way to compare the uhost to the host so that I know for sure it's my bot joining.

this code works, but I'm open to suggestion if there is a better way.

Code: Select all

set our_chan "#mychan"
bind join - *!*@* join_handler
proc join_handler {nick uhost hand chan} {

  global our_chan
  global {my-hostname}
  global {my-ip}

  # only respond to joining $our_chan
  if {[string tolower $chan] != $our_chan} {
    return 0
  }

  set host [lindex [split $uhost @] 1]
  if {($host == ${my-hostname}) || ($host == ${my-ip})} {
    set msg "it is me"
  } else {
     set msg "it is not me"
  }

  # send msg to channel
  putserv "privmsg $chan : welcome $nick $uhost :: $msg
                   
  return 1
}
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

As I indicated in my prior post, you can use the Eggdrop Tcl command 'isbotnick' to determine much more simply if it was the bot that triggered a join bind.

Code: Select all

bind JOIN - * pJoinProc

proc pJoinProc {nick uhost hand chan} {
  if {[isbotnick $nick]} {
    # code here if it was the bot that triggered the join bind
  } else {
    # code here if it was not the bot that triggered the join bind
  }
  return 0
}
I must have had nothing to do
User avatar
krystal
Voice
Posts: 3
Joined: Mon Oct 05, 2009 6:48 am
Location: Spain

Post by krystal »

ah, they couldn't be the same nick without being the same user/bot.. doh

thanks
Post Reply