egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

flood check

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
tmyoungjr
Voice


Joined: 24 Aug 2007
Posts: 14

PostPosted: Wed Jan 07, 2009 11:22 am    Post subject: flood check Reply with quote

I have a real simple script that i found here in the forums :

Code:

bind pub - !fact pub_fact

proc pub_fact {n u h c t} {
  set fl [open fact.txt]
  set data [read $fl]
  close $fl
  set lines [split $data \n]
  set randline [lindex $lines [rand [llength $lines]]]
  putserv "privmsg $c $randline"
}


i'll modify it a bit for my needs. what i'm looking for tho is flood control. i want to limit the # of times this script will return anything (something like say 1 request every minute or some such). i've found many scripts with flood control built in, but they all seem excessively complicated for my needs. any assistance would be great.
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Wed Jan 07, 2009 2:53 pm    Post subject: Reply with quote

bind pub - !fact pub_fact

proc pub_fact {n u h c t} {
global varTimer
if {![info exists varTimer]} {
set varTimer 1
utimer 60 [unset varTimer]
set fl [open fact.txt]
set data [read $fl]
close $fl
set lines [split $data \n]
set randline [lindex $lines [rand [llength $lines]]]
putserv "privmsg $c :$randline"
}
return 0
}
Back to top
View user's profile Send private message
tmyoungjr
Voice


Joined: 24 Aug 2007
Posts: 14

PostPosted: Wed Jan 28, 2009 5:28 pm    Post subject: Reply with quote

thank you - this works wonderfully
Back to top
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Fri Jan 30, 2009 9:23 pm    Post subject: Reply with quote

I know you said the above worked but recent coding I have done on something unrelated has caused me to rethink the solution I provided, as a generic means of preventing flood. I found the solution below to be more reliable.

Code:

bind pub - !fact pub_fact

proc pub_fact {n u h c t} {
  unbind pub - !fact pub_fact
  set fl [open fact.txt]
  set data [read $fl]
  close $fl
  set lines [split $data \n]
  set randline [lindex $lines [rand [llength $lines]]]
  putserv "privmsg $c :$randline"
  utimer 60 [list bind pub - !fact pub_fact]
  return 0
}
Back to top
View user's profile Send private message
TCL_no_TK
Owner


Joined: 25 Aug 2006
Posts: 509
Location: England, Yorkshire

PostPosted: Mon Feb 02, 2009 7:25 am    Post subject: Reply with quote

Code:
  set fl [open fact.txt]
  set data [read $fl]

Replace with
Code:
  set fs [file size "fact.txt"]
  set fl [open fact.txt]
  set data [read $fl $fs]
Wink
_________________
TCL the misunderstood
Back to top
View user's profile Send private message Send e-mail
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Mon Feb 02, 2009 12:14 pm    Post subject: Reply with quote

TCL_no_TK:
What would the purpose of such a modification be?

Also, the extended syntax of read specifies the number of characters to read, while file size returns the number of bytes. Which certain charsets, such as UTF-8, one character may consist of several bytes. You will get away with this, as read, for normal files, reads as much as "numChars" charaters, or up until EOF, which ever comes first. Should the channelId however have been a serial port, the read command would block until the specific amount of characters have been recieved (of course, file size would not be useful on a serial port in the first place).

The point being, there is no good reason for specifying the number of characters to read, should you desire to read the whole file.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
TCL_no_TK
Owner


Joined: 25 Aug 2006
Posts: 509
Location: England, Yorkshire

PostPosted: Tue Feb 03, 2009 8:58 am    Post subject: Reply with quote

Performance Exclamation
Arrow
Quote:
... there is one trick that might be necessary. Determine the size of the file first, then 'read' that many bytes. This allows the channel code to optimize buffer handling (preallocation in the correct size). I don't know anymore who posted this first. But you only need this for Tcl 8.0. This is something for the Tcl Performance page as well.
from How do I read and write files in Tcl
_________________
TCL the misunderstood
Back to top
View user's profile Send private message Send e-mail
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue Feb 03, 2009 12:38 pm    Post subject: Reply with quote

That performance-issue would be restricted to Tcl 8.0, and would still be flawed with the byte vs char issue...
In any case, I fail to see how that is related to the topic.

@tmyoungjr:
I believe user posted a very flexible throttling-mechanism that could easily be adopted to your script.
Check this post: http://forum.egghelp.org/viewtopic.php?p=75097#75097

arfer's approach is interresting, yet may cause the script to stop responding should any I/O-errors occur during the reading. Would probably be a good idea to move the utimer to the top of the proc.
Also worth mentioning, is that this will keep resetting the bind-counter every time it triggers.

A third option would be to simply use timestamps, removing the need for any timers or cleanup. Example posted below:

Code:
proc throttle {id time} {
 if {[info exists ::throttle($id)] && $::throttle($id) > [clock seconds]} {
  return 0
 }
 set ::throttle($id) [expr [clock seconds] + $time]
 return 1
}

proc myproc {} {
 if {[throttle "theid" 60]} {
  #we're ok, do the good stuff here
 } {
  #still throttle'd, do nothing
  return 0
 }
}

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber