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.

string lookup in file fails

Old posts that have not been replied to for several years.
Locked
E
Errtu

Post by Errtu »

I'm really new to tcl, and i try to do something more than basic. What this script is suppose to do, is when somebody does /msg bot chat-register, the script should look inside a textfile if their hostname is there. If it is, you'll get a msg "already registered". If it isn't the $nick and $uhost should be written. Even after a very friendly guy in #eggdrop@Undernet helped me with a few basic things, i still can't get it to work. The bot doesn't seem to look in the file and just adds the $nick $uhost to the file. Can someone tell me what i've done wrong here?

Code: Select all

set regfile "registered.txt"

bind msg - chat-register msg_regchat

proc msg_regchat {nick uhost hand args} { 
  global regfile
  set fs [open $regfile a+]
  while {![eof $fs]} { 
  set read [gets $fs]
  if {[string match $uhost $regfile]} { 
    putserv "NOTICE $nick :You have already registered."
  } else {
    putserv "NOTICE $nick :You have now registered for the chat."
    puts $fs "$nick $uhost"
  }
 }
 close $fs
}
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

First problem is you are opening the file in "a+" mode, which means "append plus read". Opening in append mode means you start at the end of the file. So that's why it's not finding the right entries.

The second problem is that you're doing your output within the loop. That means it's gonna do it for each line in the file, not just once over all.
E
Errtu

Post by Errtu »

so how do i change it? i can let it open in 'r' so it will read the entire file and should see the match then, but .. output outside the loop.. how do i do that?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

imagine you have a variable called "found"

you start off with found = 0, because it's not found

then you go through the file one line at a time

if the line matches the uhost, you set found to 1, and break from the loop

after the loop, you test the value of found

if it's 1, you send your error message, because they are already registered

if it's still 0, that means it wasn't found, and you reopen the file in 'a' mode and add the new entry
E
Errtu

Post by Errtu »

Okay, i've tried to do so .. but (obviously) i still make a mistake somewhere because now i get no reply back at all..

this is the modified part:

Code: Select all

set found 0

proc msg_regchat {nick uhost hand args} { 
  global regfile found
  set fs [open $regfile r]
  while {![eof $fs]} { 
  set read [gets $fs]
  if {[string match $nick $regfile]} { 
  set found 1
  } else {
  set found 0
  }
  close $fs
  }
  if {$found == "1"} {
    putserv "NOTICE $nick :You have already registered."
    } else {
    putserv "NOTICE $nick :You have now registered."
    set fs [open $regfile a+]
    puts $fs "$nick"
    close $fs   
   }
}
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

The 'found' variable should be initialized within your procedure, and should not be a global variable.

Look at your string match command.. you're comparing to the filename, not the line.

You shouldn't set found = 0 inside the loop.

You should break after you set found = 1 (look at the help page for 'break' to see what it does).

You should not close the file within the loop! hehe (you will only read the first line)

If you are getting *no reply* at all, that means the procedure either has an error (look on the dcc console and watch for an error) or it's not being called (do you have the 'bind' command?)
E
Errtu

Post by Errtu »

On 2001-10-17 07:06, stdragon wrote:
The 'found' variable should be initialized within your procedure, and should not be a global variable.
ok moved the set found to be within the proc
Look at your string match command.. you're comparing to the filename, not the line.
this was all the info i could get about string match. How can i compare the string to all the lines within the filename?
You shouldn't set found = 0 inside the loop.
removed
You should break after you set found = 1 (look at the help page for 'break' to see what it does).
ok
You should not close the file within the loop! hehe (you will only read the first line)
argh yes.. moved the close $fs one line down so it's after the loop
If you are getting *no reply* at all, that means the procedure either has an error (look on the dcc console and watch for an error) or it's not being called (do you have the 'bind' command?)
no errors on the console, i have the bind command and it's working.

Thanks btw for the help so far, i really appreciate it.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Code: Select all

set regfile "register.txt"
bind msg - chat-register msg_regchat
proc msg_regchat {nick uh hand arg} {
  global regfile
  set fp [open $regfile r+]
  set found 0
  while {![eof $fp]} {
    set _T [lindex [split [gets $fp]] 1]
    if {[string match "${nick}!${uh}" $_T]} {
      set found 1
    }
  }
  if {$found} {
    puthelp "NOTICE $nick :allready registered"
  } else {
    seek $fp end
    puts $fp "$nick [maskhost "${nick}!${uh}"]"
  }
  close $fp
}
Try this.
E
Errtu

Post by Errtu »

i got this error in console:

TCL Error [msg_regchat]: expected integer but got "end"
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

OOPS

change the line
seek $fp end

too
seek $fp 0 end
E
Errtu

Post by Errtu »

no go :sad: It seems like it still doesn't check if the $nick/$uhost is in the file because it simply appends ..
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

reverse the args to the string match function

_T is the maskhost you read in, so that should be the first arg to string match

if that doesn't work, then add some debugging statements (putlog) to see what is going wrong

i.e. where you set found add, putlog "setting found to 1"

right after you read the line, add, putlog "testing line $_T against $nick!$uhost"

etc
E
Errtu

Post by Errtu »

That didn't work (surprise). I added the putlog line. Output is now:

[09:22] testing line *!drow@*.upc-a.chello.nl against Errtu|TCL!~drow@b255089.upc-a.chello.nl

/me is getting frustrated. :sad:

[edit] i added a few bogus nicks/uhosts and every time each line in the textfile is compared against Errtu|TCL!~drow@b255089.upc-a.chello.nl

[edit2] just messing around with the register.txt file. Normally it adds nick/uhost like this:

Errtu|TCL *!drow@*.upc-a.chello.nl

but when i manually edit the file and add this line:

Errtu|TCL *drow@*.upc-a.chello.nl it suddenly works!! (rejoice!). Now, how can i let the script add the mask in that format?


<font size=-1>[ This Message was edited by: Errtu on 2001-10-18 03:27 ]</font>
E
Errtu

Post by Errtu »

yay, i finally got it working :smile:

when adding the nick/uhost like this:

Code: Select all

puts $fp "$nick *[string trimleft [maskhost $uh] "*!"]"


it works beautifully :smile: thanks to all who helped me with this.
Locked