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 

check from .txt file?

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


Joined: 19 Mar 2011
Posts: 19

PostPosted: Mon Apr 23, 2012 9:25 am    Post subject: check from .txt file? Reply with quote

Code:

bind pub - !check check
proc check {nick uhost hand chan text} {
  set t [open scripts/filename.txt r]
  set line [gets $t]
  if {[string match -nocase "*$text*" $line]} {
       putquick "privmsg $chan $text is in the file!"
       close $t
    } else {
    putquick "privmsg $chan $text isn't in the file!"
       close $t
  }
}

Isn't workin', how can we check a text if it is in the file? Like that,

<ZAL> !check Is it a problem?
<Egg> Is it a problem? < is in the file !
<ZAL> !check Is THAT a problem?
<Egg> Is THAT a problem? < is not in the file !

Thx.
Back to top
View user's profile Send private message
Madalin
Master


Joined: 24 Jun 2005
Posts: 310
Location: Constanta, Romania

PostPosted: Tue Jan 29, 2013 3:04 pm    Post subject: Reply with quote

Try this

Code:

bind pub - !check check

proc check {nick uhost hand chan arg} {
   global found

   unset -nocomplain found 0

   set what [join [lrange $arg 0 end]]

   set fd [open filename r]
   set cont [read $fd]
   close $fd

   foreach line [split $cont "\n"] {
      if {[string match -nocase "*$what*" $line]} {
         putserv "PRIVMSG $chan :'$what' is in the file!"

         set found 1
      }
   }

   if {![info exists found]} {
      putserv "PRIVMSG $chan :Text is not found.."
   }
}

_________________
https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
SpiKe^^
Owner


Joined: 12 May 2006
Posts: 792
Location: Tennessee, USA

PostPosted: Tue Jan 29, 2013 4:55 pm    Post subject: Reply with quote

There's no good reason to make the local process variable 'found' into a global variable.
It's never reused and forces us to unbind it to run the proc again.

Also 'arg' is not a list and shouldn't be handled like this... [join [lrange $arg 0 end]]

I might suggest something more like...

Code:

bind pub - !check check

proc check {nick uhost hand chan arg} {

   set fd [open scripts/filename.txt r]
   set cont [read $fd]
   close $fd

   foreach line [split $cont "\n"] {
      if {[string match -nocase "*$arg*" $line]} {
         putserv "PRIVMSG $chan :'$arg' is in the file!"

         set found 1  ;  break
      }
   }

   if {![info exists found]} {
      putserv "PRIVMSG $chan :Text is not found.."
   }
}
 

_________________
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
Back to top
View user's profile Send private message Visit poster's website
Madalin
Master


Joined: 24 Jun 2005
Posts: 310
Location: Constanta, Romania

PostPosted: Tue Jan 29, 2013 4:59 pm    Post subject: Reply with quote

Well i make the code as i know Smile if i encouter problems in the future with codes i write then i modify the code.. but any modifications/improvment of my code is welcomed Smile im always learning.. i also never used 'break'
_________________
https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3741
Location: Mint Factory

PostPosted: Wed Jan 30, 2013 7:19 am    Post subject: Reply with quote

Never trust user input, especially when it's not user restricted. How about this?
Code:

bind pub - !check check

proc check {nick uhost hand chan text} {
   set what [join [lrange $text 0 end]]
   set fo [open "scripts/filename.txt" r]
   set data [split [read $fo] "\n"]
   close $fo
   if {[lsearch -glob $data $what]} {
      puthelp "PRIVMSG $chan :'$what' is in the file!"
   } else {
      puthelp "PRIVMSG $chan :Text is not found.."
   }
}

PS: supports wildcards.
_________________
Once the game is over, the king and the pawn go back in the same box.
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