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.

Reading from a Text file for a Rules Script

Old posts that have not been replied to for several years.
Locked
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Reading from a Text file for a Rules Script

Post by Weirdo »

This is my code

Code: Select all

et runchan "#weirdo"

bind pub - !rules2 pub:chanrule2
proc pub:chanrule2 { nick host hand chan text} {
	global runchan rulefile ruletext
	if {$chan != $runchan} {return 0} 
	if {![file exists text/rules.file]} {
		set rulefile [open text/rules.file r]
		set ruletext [read $rulefile 1000]
		close $rulefile 
	}
	putserv "Notice $nick :$ruletext"
}
What its supposed to do is to read the rules text file, and copy it, then deliver it to the person who activated the trigger. Problem is, it doesnt recognise ruletext as a variable, an so it dont work. Can anyone help me?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Yes, there are a few issues to point out, regarding the script.

Code: Select all

set runchan "#weirdo" 

bind pub - !rules2 pub:chanrule2 
proc pub:chanrule2 { nick host hand chan text} { 
   global runchan rulefile ruletext 
   if {$chan != $runchan} {return 0} 
   if {![file exists text/rules.file]} { 
      set rulefile [open text/rules.file r] 
      set ruletext [read $rulefile 1000] 
      close $rulefile 
   } 
   putserv "Notice $nick :$ruletext" 
}
First, the "putserv" command is triggered, even if the file does not exist.

Second, you have set the "ruletext" variable global. There is no need to do this, unless you have a need to access the variable outside of the script.

Third, you use the "read" command to read the file. This will read from the current position int he file (you can be located at any point other than the start) up to the end of output (in the case of a file, the end of it). This includes any newline characters (ie, the start of the next line in the file). When outputing this to a IRC server, it will display only the first line, because IRC server interpret the newline as the end of the current command, then treats the next line, as a whole new command (which causes an error).

You need to split the output into chunks, in needed.

And lastly, the main cuase of the problem

Code: Select all

if {![file exists text/rules.file]} {
This states, if the file does not exist, read the file (contradiction of statments).

Simply remove the !.

You should then move the putserv within the IF block, so it is only tirggered if the file exists (AKA, only tigger if data is available).
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

How do you get it to read from the Start of the File to the End of the file?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

If you havn't do any other seek, or gets commands, then it will automaticaly so this.

When you open a file, the seek pointer is set to the start of the file. If you use read straight away, then you will read from begining to end.
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

[10:28:13] <Weirdo> !rules2
[10:28:15] -Minako- General Member Ruleset for #Cometanime - Amendment 1.3 - 8/10/2002
On production of the trigger, i got this response from the bot. 1 line out of the 15 in the file. It does this even with the 1000 byte thingy removed from the read command. So, how do i split it up, so it will read each line, and send them in the notice?
t
tainted
Master
Posts: 239
Joined: Sun May 12, 2002 8:00 pm
Location: chicago
Contact:

Post by tainted »

try adding something similiar to this:

Code: Select all

        set blah1 [open "/path/to/rules.txt" r]
                while {![eof $blah1]} {
                        catch {set blah2 [gets $blah1]}
                        putserv etc etc etc
Make sure you also include the following somewhere after you are done with the file.

Code: Select all

catch {close $blah1}
Hope that sends you in the right direction.
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

I get wrong number of args for that

According to the bot... should be "While test command"

Have no idea what its going on about :P

Code: Select all

set runchan "#weirdo"

bind pub - !rules2 pub:chanrule2
proc pub:chanrule2 { nick host hand chan text} {
	global runchan rulefile 
	if {$chan != $runchan} {return 0} 
	if {[file exists text/rules.file]} {
		set rulefile [open text/rules.file r]
			while {![eof $rulefile] {
			catch {set ruletext [gets $rulefile]}
			putserv "Notice $nick :$ruletext"
			}
		catch {close $rulefile}	
	}
	}
}
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

Code: Select all

while {![eof $rulefile] {
should be

Code: Select all

while {![eof $rulefile]} {
u just forgot a }
Elen sila lúmenn' omentielvo
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

thats why it was giving me the close brace crap and crashing the bot before, AAAAAH :)

And works perfectly :)

Will this still work if more than two people do it at a time?
t
tainted
Master
Posts: 239
Joined: Sun May 12, 2002 8:00 pm
Location: chicago
Contact:

Post by tainted »

It might, if not then you would need to check a var right when its triggered, and if that var matches (IE the script is not curently processing a request) set it to the 'in progress' value. If the script is 'in progress' you could either A) queue it (a bit trickier to code) or B) Just reject it. After the close brace for the while statement you can set the var back to its origional (ready to reply to rules requests) value. Although, none of this may be nessesary unless your rule list is extremely long. The whole process is completed rather quickly, and the notices are sent to the bot's queue while they are being sent out I believe. I would provide a code example, but it's 5:30am. :o

-Sorry about that missing close brace. Third edit from massive typos.. :roll:
W
Weirdo
Master
Posts: 265
Joined: Sat Apr 27, 2002 8:00 pm
Location: Manchester, England

Post by Weirdo »

Absolutely no problem at all. You are doing a great job helping me here :)
Locked