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.

[No]greet

Help for those learning Tcl or writing their own scripts.
Post Reply
h
hakim
Voice
Posts: 19
Joined: Sun Apr 23, 2006 3:39 am

[No]greet

Post by hakim »

The script should send notice "greet" to the owner if his hostname is not in the host.db, send "no greet" if it's there, add the hostname to the db, when user msgs "!nogreet" to the bot and remove it from there if he msgs "!greet", but there is something wrong with the script :( Any help appreciated.
So here is the script:

Code: Select all

#setting channels which will be effected
set channel(one) "#channel"

#sends "greet/nogreet" to owner when he joins the channel
bind join n *!*@* greetuser

proc greetuser {nick uhost hand chan arg } {
   #only if in effected channel
   if {$chan==$channel(one)} {
   #creates host.db if it doesn't exist
   if {![file exists host.db]} {
    set fs [open host.db w]
    puts $fs ""
    close $fs
   }
 set dbfs [open host.db r]
 set tmpfs [open host.tmp w]
 set match 0
 #creates variable userhost, which will be the user host
 set userhost [getchanhost $nick $chan]
 global userhost
   #searches the file for userhost
   while {![eof $dbfs]} {
    if {![gets $dbfs line]} {
      puts $tmpfs "$line"
    } elseif {[lindex $line 0] == $userhost} {
      set match 1
    }
  }
   #if there is no entry, sends "greet"
   if {![match==1]} {
   puthelp "NOTICE Owner :greet"
   #otherwise nogreet
   } elseif {[match==1]} {
   puthelp "NOTICE Owner :nogreet"
   }
   }
      return 0
}

#adds userhost to host.db if user msgs "!nogreet" to bot
bind msg * !nogreet nogreet

proc nogreet { nick uhost hand text arg } {
  #looks if it's not there already
  while {![eof $dbfs]} {
    if {![gets $dbfs line]} {
      puts $tmpfs "$line"
    } elseif {[[lindex $line 0] == $userhost]} {
      }
      else {
      puts $tmpfs "$userhost"
    }
    }
  #writes to host.db
  close $dbfs
  close $tmpfs
  set fs [open host.db.tmp r]
  set hostsdb "[read $fs]"
  close $fs
  set fs [open host.db w]
  puts $fs "$hostsdb"
  close $fs
  return 0
  }
#removes hostname from db, if user msgs "!greet" to bot
bind msg * !greet greet

proc greet { nick uhost hand text arg } {
  set dbfs [open host.db r]
  set tmpfs [open host.db.tmp w]
  #finds and removes entry from db
  while {![eof $dbfs]} {
    if {![gets $dbfs line]} {
      puts $tmpfs "$line"
    } elseif {[lindex $line 0] == $userhost} {
      puts $tmpfs ""
      puthelp "NOTICE Owner :greeting again!"
    }
    }
  close $dbfs
  close $tmpfs
  set fs [open host.db.tmp r]
  set hostsdb "[read $fs]"
  close $fs
  set fs [open host.db w]
  puts $fs "$hostsdb"
  close $fs
  return 0
  }
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

hakim wrote:but there is something wrong with the script
Not very informative! Post the results of (via Command Console):

Code: Select all

.set errorInfo
Also post any other relevant information (errors in logs &c).
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
h
hakim
Voice
Posts: 19
Joined: Sun Apr 23, 2006 3:39 am

Post by hakim »

The script is very buggy :oops:
I think I corrected greetuser proc, but I still can't figure out what's wrong with nogreet proc :(
Corrected greet user (also changed userhost with host. wich is acquired automatically):

Code: Select all

set channel(one) "#channel"

bind join n|- *!*@* greetuser

proc greetuser { nick host hand chan } {
   global channel
   if {$chan==$channel(one)} {
   if {![file exists host.db]} {
    set fs [open host.db w]
    puts $fs ""
    close $fs
   }
 set dbfs [open host.db r]
 set tmpfs [open host.tmp w]
 global match
 set match 0
   while {![eof $dbfs]} {
    if {![gets $dbfs line]} {
      puts $tmpfs "$line"
    } elseif {[lindex $line 0] == $host} {
      set match 1
    }
  }
   if {!$match} {
   puthelp "NOTICE Owner :greet"
   } elseif {[match==1]} {
   puthelp "NOTICE Owner :nogreet"
   }
   }
  close $dbfs
  close $tmpfs
      return 0
}
Error message executing nogreet:

Code: Select all

 Tcl error [nogreet]: invalid command name ""
 invalid command name ""
     while executing
 "[lindex $line 0] == $host"
     (procedure "nogreet" line 9)
     invoked from within
 "nogreet $_msg1 $_msg2 $_msg3 $_msg4"
nogreet proc:

Code: Select all

bind msg * !nogreet nogreet

proc nogreet { nick host hand text } {
 set dbfs [open host.db r]
 set tmpfs [open host.tmp w]
  while {![eof $dbfs]} {
    if {![gets $dbfs line]} {
      puts $tmpfs "$line"
    } elseif {[[lindex $line 0] == $host]} {
      putserv "NOTICE Owner :Nogreet is on. Type !greet to greet again."
      } else {
      puts $tmpfs "$host"
     putserv "NOTICE Owner :turned on nogreet."
    }
    }
  close $dbfs
  close $tmpfs
  set fs [open host.db.tmp r]
  set hostsdb "[read $fs]"
  close $fs
  set fs [open host.db w]
  puts $fs "$hostsdb"
  close $fs
  return 0
  }
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Code: Select all

elseif {[[lindex $line 0] == $host]}
Look closely into the comparison, is it correct in your opinion? Are the extra brackets needed? It's usually done like this:

Code: Select all

if {$bla == $blo} {
h
hakim
Voice
Posts: 19
Joined: Sun Apr 23, 2006 3:39 am

Post by hakim »

Tahnks, Sir_Fz. Well, we live and learn :)
So, here is the completely working script:

Code: Select all

set channel(one) "#channel_name"

if {![file exists host.db]} {
    set fs [open host.db w]
    puts $fs ""
    close $fs
   }

bind join n|- *!*@* greetuser

proc greetuser { nick host hand chan } {
   global channel
   if {$chan==$channel(one)} {
 set dbfs [open host.db r]
 global match
 set match 0
   while {![eof $dbfs]} {
    gets $dbfs line
    if {[lindex $line 0] == $host} {
      set match 1
    }
  }
   if {!$match} {
   puthelp "NOTICE owners_nick :greet"
   } elseif {$match} {
   puthelp "NOTICE owners_nick :nogreet"
   }
   }
  close $dbfs
      return 0
}

bind msg * !nogreet nogreet

proc nogreet { nick host hand text } {
 set dbfs [open host.db r]
 set tmpfs [open host.db.tmp w]
  while {![eof $dbfs]} {
    if {![gets $dbfs line]} {
      puts $tmpfs "$line"
    } elseif {[lindex $line 0] == $host} {
      puthelp "NOTICE owners_nick :Nogreet is on. Type !greet to greet again."
      } else {
      puts $tmpfs "$host"
    }
    }
  close $dbfs
  close $tmpfs
  set fs [open host.db.tmp r]
  set hostsdb "[read $fs]"
  close $fs
  set fs [open host.db w]
  puts $fs "$hostsdb"
  close $fs
  return 0
  }

bind msg * !greet greet

proc greet { nick host hand text } {
  set dbfs [open host.db r]
  set tmpfs [open host.db.tmp w]

  while {![eof $dbfs]} {
    if {![gets $dbfs line]} {
      puts $tmpfs "$line"
    } elseif {[lindex $line 0] == $host} {
      puts $tmpfs ""
      puthelp "NOTICE Owners_nick :greeting again!"
    }
    }
  close $dbfs
  close $tmpfs
  set fs [open host.db.tmp r]
  set hostsdb "[read $fs]"
  close $fs
  set fs [open host.db w]
  puts $fs "$hostsdb"
  close $fs
  return 0
  }
Bind for greet_user proc can be changed to "bind join - *!*@* greetuser", "Owners_nick" to "$nick", "channel_name" to prefered channel and should work just fine for everyone ;)
Post Reply